Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
QueryParams.hpp
Go to the documentation of this file.
1#pragma once
2#include <expected>
3#include <format>
4#include <vector>
5#include <string>
6
9
10namespace Thoth::Http{
11 struct QueryParams {
12 using QueryKey = std::string;
13 using QueryKeyRef = std::string_view;
14
15 using QueryValue = std::string;
16 using QueryValueRef = std::string_view;
17 using QueryValues = std::vector<QueryValue>;
18
19 using QueryPair = std::pair<QueryKey, QueryValues>;
20 // using MapType = std::map<QueryKey, QueryValues, std::less<>>;
22
23 using IterType = decltype(MapType().begin());
24 using CIterType = decltype(MapType().cbegin());
25
26
28 explicit QueryParams(const MapType& initAs);
29
30 QueryParams(std::initializer_list<QueryPair> init);
31
32
34 static QueryParams Parse(std::string_view paramsStr);
36 static std::expected<QueryParams, RequestError> ParseDecodified(std::string_view str);
37
38
42 [[nodiscard]] bool Exists(QueryKeyRef key) const;
43
48 [[nodiscard]] bool ValExists(QueryKeyRef key, QueryValueRef val) const;
49
53 void Add(QueryKeyRef key, QueryValue val);
54
60
65
74 std::optional<QueryValues*> Get(QueryKeyRef key);
75
76
79 [[nodiscard]] CIterType begin() const;
80 [[nodiscard]] CIterType end() const;
81
82
84 void Clear();
85
87 [[nodiscard]] size_t Size() const;
88
90 [[nodiscard]] bool Empty() const;
91
95
97 bool operator==(const QueryParams& other) const;
98 private:
99 MapType _elements;
100
101 friend struct std::formatter<QueryParams>;
102 friend struct std::hash<QueryParams>;
103 };
104}
105
106
107#include <Thoth/Http/Request/QueryParams.tpp>
Definition Client.hpp:12
constexpr const_iterator cbegin() const
constexpr iterator begin()
Definition QueryParams.hpp:11
decltype(MapType().begin()) IterType
Definition QueryParams.hpp:23
std::string QueryValue
Definition QueryParams.hpp:15
CIterType begin() const
bool RemoveKey(QueryKeyRef key)
Remove a key and all values associated.
std::string_view QueryValueRef
Definition QueryParams.hpp:16
void Clear()
Clear all keys.
bool operator==(const QueryParams &other) const
std::pair< QueryKey, QueryValues > QueryPair
Definition QueryParams.hpp:19
QueryValues & operator[](QueryKeyRef key)
bool Exists(QueryKeyRef key) const
check if a key exists.
CIterType end() const
static QueryParams Parse(std::string_view paramsStr)
Parse the query as it is.
std::string QueryKey
Definition QueryParams.hpp:12
decltype(MapType().cbegin()) CIterType
Definition QueryParams.hpp:24
std::vector< QueryValue > QueryValues
Definition QueryParams.hpp:17
void Add(QueryKeyRef key, QueryValue val)
Add a value with the specified key.
static std::expected< QueryParams, RequestError > ParseDecodified(std::string_view str)
Tries to decode and then parse.
Dsa::LinearMap< QueryKey, QueryValues > MapType
Definition QueryParams.hpp:21
QueryParams(const MapType &initAs)
Create with an existing map.
QueryParams(std::initializer_list< QueryPair > init)
bool Remove(QueryKeyRef key, QueryValueRef val)
Remove a value with the specified key.
bool ValExists(QueryKeyRef key, QueryValueRef val) const
check if a key=val exists.
std::string_view QueryKeyRef
Definition QueryParams.hpp:13
bool SetIfNull(QueryKeyRef key, QueryValueRef value)
If key not exists, set it to value.
std::optional< QueryValues * > Get(QueryKeyRef key)
Get the reference of a key but don't create if it not exists.