userver: userver/dynamic_config/snapshot.hpp Source File
Loading...
Searching...
No Matches
snapshot.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/dynamic_config/snapshot.hpp
4/// @brief @copybrief dynamic_config::Snapshot
5
6#include <cstdint>
7#include <string>
8#include <type_traits>
9
10#include <userver/dynamic_config/impl/snapshot.hpp>
11#include <userver/dynamic_config/impl/to_json.hpp>
12#include <userver/formats/json_fwd.hpp>
13#include <userver/utils/fast_pimpl.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace dynamic_config {
18
19/// A strong typedef for usage in dynamic_config::Key constructors.
20struct DefaultAsJsonString final {
21 constexpr explicit DefaultAsJsonString(std::string_view json_string);
22
23 std::string_view json_string;
24};
25
26/// A config name-value pair for usage in dynamic_config::Key constructors.
27struct ConfigDefault final {
28 template <typename T>
29 ConfigDefault(std::string_view name, const T& value);
30
31 ConfigDefault(std::string_view name, DefaultAsJsonString default_json);
32
33 std::string_view name;
34 std::string default_json;
35};
36
37/// A tag type for usage in dynamic_config::Key constructors.
38struct ConstantConfig final {
39 constexpr explicit ConstantConfig() = default;
40};
41
42/// @brief A config key is a unique identifier for a config variable
43/// @snippet core/src/components/logging_configurator.cpp key
44template <typename Variable>
45class Key final {
46public:
47 /// The type of the parsed config variable.
48 using VariableType = Variable;
49
50 using JsonParser = Variable (*)(const formats::json::Value&);
51 using DocsMapParser = Variable (*)(const DocsMap&);
52
53 /// @brief The constructor for a trivial `VariableType`, e.g. `bool`, integer,
54 /// `double`, `string`. The default is passed by value.
55 ///
56 /// Usage example:
57 /// @snippet server/handlers/http_server_settings.cpp bool config sample
58 Key(std::string_view name, const VariableType& default_value);
59
60 /// @brief The constructor for a non-trivial `VariableType`. The default is
61 /// passed as a JSON string.
62 ///
63 /// Uses formats::json::Value `Parse` customization point function to parse
64 /// `VariableType`.
65 ///
66 /// Usage example:
67 /// @snippet components/logging_configurator.cpp key
68 Key(std::string_view name, DefaultAsJsonString default_json);
69
70 /// @brief The constructor that provides a special parser from JSON.
71 /// @warning Prefer the constructors above whenever possible.
72 /// @details Can be used when generic `Parse` is not applicable. Sometimes
73 /// used to add validation, e.g. minimum, maximum, string pattern, etc.
74 Key(std::string_view name, JsonParser parser, DefaultAsJsonString default_json);
75
76 /// @brief The constructor that parses multiple JSON config items
77 /// into a single C++ object.
78 /// @warning Prefer to use a separate `Key` per JSON config item and use the
79 /// constructors above whenever possible.
80 ///
81 /// Usage example:
82 /// @snippet clients/http/component.cpp docs map config sample
83 template <std::size_t N>
84 Key(DocsMapParser parser, const ConfigDefault (&default_json_map)[N]);
85
86 /// Creates a config that always has the same value.
87 Key(ConstantConfig, VariableType value);
88
89 /// @cond
90 Key(impl::InternalTag, std::string_view name);
91
92 Key(impl::InternalTag, DocsMapParser parser);
93 /// @endcond
94
95 Key(const Key&) noexcept = delete;
96 Key& operator=(const Key&) noexcept = delete;
97
98 /// @returns the name of the config variable, as passed at the construction.
99 std::string_view GetName() const noexcept;
100
101 /// Parses the config. Useful only in some very niche scenarios. The config
102 /// value should be typically be retrieved from dynamic_config::Snapshot,
103 /// which is obtained from components::DynamicConfig in production or from
104 /// dynamic_config::StorageMock in unit tests.
105 VariableType Parse(const DocsMap& docs_map) const;
106
107private:
108 friend struct impl::ConfigIdGetter;
109
110 const impl::ConfigId id_;
111};
112
113// clang-format off
114
115/// @brief The shared snapshot of
116/// @ref scripts/docs/en/userver/dynamic_config.md "dynamic configs". Cheap to
117/// copy, even cheaper to move. Thread safe, not updated with new dynamic
118/// config values in background (it's a snapshot!).
119///
120/// When a config update comes in via new `DocsMap`, configs of all
121/// the registered types are constructed and stored in `Config`. After that
122/// the `DocsMap` is dropped.
123///
124/// Config types are automatically registered if they are used
125/// somewhere in the program.
126///
127/// ## Usage example:
128/// @snippet components/component_sample_test.cpp Sample user component runtime config source
129
130// clang-format on
131class Snapshot final {
132public:
133 Snapshot(const Snapshot&);
134 Snapshot& operator=(const Snapshot&);
135
136 Snapshot(Snapshot&&) noexcept;
137 Snapshot& operator=(Snapshot&&) noexcept;
138
139 ~Snapshot();
140
141 /// Used to access individual configs in the type-safe config map
142 template <typename VariableType>
143 const VariableType& operator[](const Key<VariableType>& key) const&;
144
145 /// Used to access individual configs in the type-safe config map
146 template <typename VariableType>
147 const VariableType& operator[](const Key<VariableType>&) &&;
148
149 /// @cond
150 // No longer supported, use `config[key]` instead
151 template <typename T>
152 const T& Get() const&;
153
154 // No longer supported, use `config[key]` instead
155 template <typename T>
156 const T& Get() &&;
157 /// @endcond
158
159private:
160 // for the constructor
161 friend class Source;
162 friend class impl::StorageData;
163
164 explicit Snapshot(const impl::StorageData& storage);
165
166 const impl::SnapshotData& GetData() const;
167
168 struct Impl;
169 utils::FastPimpl<Impl, 16, 8> impl_;
170};
171
172// ========================== Implementation follows ==========================
173
174constexpr DefaultAsJsonString::DefaultAsJsonString(std::string_view json_string) : json_string(json_string) {}
175
176template <typename T>
177ConfigDefault::ConfigDefault(std::string_view name, const T& value)
178 : name(name), default_json(impl::ToJsonString(value)) {}
179
180template <typename Variable>
181Key<Variable>::Key(std::string_view name, const VariableType& default_value)
182 : id_(impl::Register(
183 std::string{name},
184 [name = std::string{name}](const auto& docs_map) -> std::any {
185 return impl::DocsMapGet(docs_map, name).template As<VariableType>();
186 },
188 )) {}
189
190template <typename Variable>
191Key<Variable>::Key(std::string_view name, DefaultAsJsonString default_json)
192 : id_(impl::Register(
193 std::string{name},
194 [name = std::string{name}](const auto& docs_map) -> std::any {
195 return impl::DocsMapGet(docs_map, name).template As<VariableType>();
196 },
198 )) {}
199
200template <typename Variable>
201Key<Variable>::Key(std::string_view name, JsonParser parser, DefaultAsJsonString default_json)
202 : id_(impl::Register(
203 std::string{name},
204 [name = std::string{name}, parser](const auto& docs_map) -> std::any {
206 },
208 )) {}
209
210template <typename Variable>
211template <std::size_t N>
212Key<Variable>::Key(DocsMapParser parser, const ConfigDefault (&default_json_map)[N])
213 : id_(impl::Register(
214 std::string{},
215 [parser](const DocsMap& docs_map) -> std::any { return parser(docs_map); },
216 impl::MultipleToDocsMapString(default_json_map, N)
217 )) {}
218
219template <typename Variable>
220Key<Variable>::Key(ConstantConfig /*tag*/, VariableType value)
221 : id_(impl::Register(
222 std::string{},
223 [value = std::move(value)](const DocsMap& /*unused*/) { return value; },
224 "{}"
225 )) {}
226
227template <typename Variable>
228Key<Variable>::Key(impl::InternalTag, std::string_view name)
229 : id_(impl::Register(
230 std::string{name},
231 [name = std::string{name}](const auto& docs_map) -> std::any {
232 return impl::DocsMapGet(docs_map, name).template As<VariableType>();
233 },
234 "{}"
235 )) {}
236
237template <typename Variable>
238Key<Variable>::Key(impl::InternalTag, DocsMapParser parser)
239 : id_(impl::Register(
240 std::string{},
241 [parser](const DocsMap& docs_map) -> std::any { return parser(docs_map); },
242 "{}"
243 )) {}
244
245template <typename VariableType>
246std::string_view Key<VariableType>::GetName() const noexcept {
247 return impl::GetName(id_);
248}
249
250template <typename VariableType>
251VariableType Key<VariableType>::Parse(const DocsMap& docs_map) const {
252 return std::any_cast<VariableType>(impl::MakeConfig(id_, docs_map));
253}
254
255template <typename VariableType>
256const VariableType& Snapshot::operator[](const Key<VariableType>& key) const& {
257 return GetData().Get<VariableType>(impl::ConfigIdGetter::Get(key));
258}
259
260template <typename VariableType>
261const VariableType& Snapshot::operator[](const Key<VariableType>&) && {
262 static_assert(!sizeof(VariableType), "keep the Snapshot before using, please");
263}
264
265template <typename T>
266const T& Snapshot::Get() const& {
267 return (*this)[T::kDeprecatedKey];
268}
269
270template <typename T>
271const T& Snapshot::Get() && {
272 static_assert(!sizeof(T), "keep the Snapshot before using, please");
273}
274
275} // namespace dynamic_config
276
277USERVER_NAMESPACE_END