userver: userver/dynamic_config/storage_mock.hpp Source File
Loading...
Searching...
No Matches
storage_mock.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <initializer_list>
7#include <memory>
8#include <vector>
9
15
16USERVER_NAMESPACE_BEGIN
17
18namespace dynamic_config {
19namespace impl {
20
21template <typename T>
22concept IsJson = std::is_same_v<T, formats::json::Value>;
23
24} // namespace impl
25
27class KeyValue final {
28public:
34 template <typename VariableType, typename Value = VariableType>
35 KeyValue(const Key<VariableType>& key, Value&& value)
36 : id_(impl::ConfigIdGetter::Get(key)),
37 value_(Convert<VariableType>(std::forward<Value>(value)))
38 {}
39
41 impl::ConfigId GetId() const { return id_; }
42
44 std::any GetValue() const { return value_; }
45
46private:
47 template <typename VariableType, typename Value>
48 static VariableType Convert(Value&& value) {
49 if constexpr (std::is_same_v<std::decay_t<Value>, formats::json::Value>) {
50 return value.template As<VariableType>();
51 } else {
52 return static_cast<VariableType>(std::forward<Value>(value));
53 }
54 }
55
56 impl::ConfigId id_;
57 std::any value_;
58};
59
66class StorageMock final {
67public:
70
73 StorageMock(std::initializer_list<KeyValue> config_variables);
74
76 explicit StorageMock(const std::vector<KeyValue>& config_variables);
77
81 StorageMock(const DocsMap& defaults, const std::vector<KeyValue>& overrides);
82
83 StorageMock(StorageMock&&) noexcept;
84 StorageMock& operator=(StorageMock&&) noexcept;
85 ~StorageMock();
86
88 void Extend(const std::vector<KeyValue>& overrides);
89
90 Source GetSource() const&;
91 Snapshot GetSnapshot() const&;
92
93 // Store the StorageMock in a variable before using
94 Snapshot GetSource() && = delete;
95 Snapshot GetSnapshot() && = delete;
96
97private:
98 std::unique_ptr<impl::StorageData> storage_;
99};
100
101} // namespace dynamic_config
102
103USERVER_NAMESPACE_END