6#include <userver/formats/common/transfer_tag.hpp>
7#include <userver/formats/serialize/to.hpp>
8#include <userver/formats/yaml/value.hpp>
9#include <userver/utils/strong_typedef.hpp>
11USERVER_NAMESPACE_BEGIN
13namespace formats::
yaml {
36class ValueBuilder
final {
39 using native_iter = YAML::iterator;
40 using value_type = formats::
yaml::ValueBuilder;
41 using reference = formats::
yaml::ValueBuilder&;
42 using pointer = formats::
yaml::ValueBuilder*;
53 ValueBuilder(
const ValueBuilder& other);
55 ValueBuilder(ValueBuilder&& other);
56 ValueBuilder& operator=(
const ValueBuilder& other);
58 ValueBuilder& operator=(ValueBuilder&& other);
60 ValueBuilder(
const formats::
yaml::Value& other);
61 ValueBuilder(formats::
yaml::Value&& other);
65 ValueBuilder(std::nullptr_t) : ValueBuilder
() {}
67 ValueBuilder(
const char* str);
68 ValueBuilder(
char* str);
69 ValueBuilder(
const std::string& str);
70 ValueBuilder(std::string_view str);
72 ValueBuilder(
unsigned int t);
74 ValueBuilder(
unsigned long t);
75 ValueBuilder(
long long t);
76 ValueBuilder(
unsigned long long t);
77 ValueBuilder(
float t);
78 ValueBuilder(
double t);
91 ValueBuilder
operator[](
const std::string& key);
102 const utils::StrongTypedef<Tag, std::string, Ops>& key);
168 class EmplaceEnabler {};
172 ValueBuilder(EmplaceEnabler,
const YAML::Node& value,
173 const formats::
yaml::
Path& path,
const std::string& key);
175 ValueBuilder(EmplaceEnabler,
const YAML::Node& value,
176 const formats::
yaml::
Path& path, size_t index);
180 static ValueBuilder MakeNonRoot(
const YAML::Node& val,
182 const std::string& key);
183 static ValueBuilder MakeNonRoot(
const YAML::Node& val,
187 void Copy(
const ValueBuilder& from);
188 void Move(ValueBuilder&& from);
189 void NodeDataAssign(
const formats::
yaml::Value& from);
191 template <
typename T>
192 static Value DoSerialize(
const T& t);
194 formats::
yaml::Value value_;
201 const utils::StrongTypedef<Tag, std::string, Ops>& key) {
202 return (*
this)[key.GetUnderlying()];
206Value ValueBuilder::DoSerialize(
const T& t) {
208 formats::
common::impl::kHasSerialize<Value, T>,
209 "There is no `Serialize(const T&, formats::serialize::To<yaml::Value>)` "
210 "in namespace of `T` or `formats::serialize`. "
212 "Probably you forgot to include the "
213 "<userver/formats/serialize/common_containers.hpp> or you "
214 "have not provided a `Serialize` function overload.");
216 return Serialize(t, formats::serialize::To<Value>());
220std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) <=
sizeof(
long long),
222Serialize(T value, formats::serialize::To<Value>) {
223 using Type = std::conditional_t<std::is_signed<T>::value,
long long,
225 return yaml::ValueBuilder(
static_cast<Type>(value)).ExtractValue();