userver: userver/dynamic_config/storage_mock.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
storage_mock.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/dynamic_config/storage_mock.hpp
4/// @brief @copybrief dynamic_config::StorageMock
5
6#include <initializer_list>
7#include <memory>
8#include <vector>
9
10#include <userver/dynamic_config/snapshot.hpp>
11#include <userver/dynamic_config/source.hpp>
12#include <userver/dynamic_config/value.hpp>
13#include <userver/formats/json/value.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace dynamic_config {
18namespace impl {
19
20template <typename T>
21using IsJson = std::enable_if_t<std::is_same_v<T, formats::json::Value>>;
22
23} // namespace impl
24
25/// A type-erased config key-value pair
26class KeyValue final {
27 public:
28 /// Uses the provided value directly. It can also be constructed in-place:
29 /// @code
30 /// {kMyConfig, {"foo", 42}}
31 /// @endcode
32 template <typename Key>
33 KeyValue(Key /*key*/, VariableOfKey<Key> value)
34 : id_(impl::kConfigId<Key>), value_(std::move(value)) {}
35
36 /// Parses the value from `formats::json::Value`
37 template <typename Key, typename Json, typename = impl::IsJson<Json>>
38 KeyValue(Key /*key*/, const Json& value)
39 : id_(impl::kConfigId<Key>),
40 value_(value.template As<VariableOfKey<Key>>()) {}
41
42 /// For internal use only
43 impl::ConfigId GetId() const { return id_; }
44
45 /// For internal use only
46 std::any GetValue() const { return value_; }
47
48 private:
49 impl::ConfigId id_;
50 std::any value_;
51};
52
53/// @brief Backing storage for `dynamic_config::Source` in tests and benchmarks
54/// @warning Make sure that `StorageMock` outlives all the acquired pointers!
55/// @snippet core/src/dynamic_config/config_test.cpp Sample StorageMock usage
56/// @snippet core/src/dynamic_config/config_test.cpp StorageMock from JSON
57/// @see dynamic_config::GetDefaultSource
58/// @see dynamic_config::MakeDefaultStorage
59class StorageMock final {
60 public:
61 /// Create an empty `StorageMock`
63
64 /// Only store `config_variables` in the `Config`.
65 /// Use as: `StorageMock{{kVariableKey1, value1}, {kVariableKey2, value2}}`
66 StorageMock(std::initializer_list<KeyValue> config_variables);
67
68 /// Only store `config_variables` in the `Config`
69 explicit StorageMock(const std::vector<KeyValue>& config_variables);
70
71 /// @brief Store `overrides` in the `Config`, then parse all the remaining
72 /// variables from `defaults`
73 /// @see dynamic_config::MakeDefaultStorage
74 StorageMock(const DocsMap& defaults, const std::vector<KeyValue>& overrides);
75
76 StorageMock(StorageMock&&) noexcept;
77 StorageMock& operator=(StorageMock&&) noexcept;
78 ~StorageMock();
79
80 /// Update some config variables
81 void Extend(const std::vector<KeyValue>& overrides);
82
83 Source GetSource() const&;
84 Snapshot GetSnapshot() const&;
85
86 // Store the StorageMock in a variable before using
87 Snapshot GetSource() && = delete;
88 Snapshot GetSnapshot() && = delete;
89
90 private:
91 std::unique_ptr<impl::StorageData> storage_;
92};
93
94} // namespace dynamic_config
95
96USERVER_NAMESPACE_END