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(
const char* str);
66 ValueBuilder(
const std::string& str);
68 ValueBuilder(
unsigned int t);
70 ValueBuilder(
unsigned long t);
71 ValueBuilder(
long long t);
72 ValueBuilder(
unsigned long long t);
73 ValueBuilder(
float t);
74 ValueBuilder(
double t);
86 ValueBuilder
operator[](
const std::string& key);
97 const utils::StrongTypedef<Tag, std::string, Ops>& key);
163 class EmplaceEnabler {};
167 ValueBuilder(EmplaceEnabler,
const YAML::Node& value,
168 const formats::
yaml::
Path& path,
const std::string& key);
170 ValueBuilder(EmplaceEnabler,
const YAML::Node& value,
171 const formats::
yaml::
Path& path, size_t index);
175 static ValueBuilder MakeNonRoot(
const YAML::Node& val,
177 const std::string& key);
178 static ValueBuilder MakeNonRoot(
const YAML::Node& val,
182 void Copy(
const ValueBuilder& from);
183 void Move(ValueBuilder&& from);
184 void NodeDataAssign(
const formats::
yaml::Value& from);
186 template <
typename T>
187 static Value DoSerialize(
const T& t);
189 formats::
yaml::Value value_;
196 const utils::StrongTypedef<Tag, std::string, Ops>& key) {
197 return (*
this)[key.GetUnderlying()];
201Value ValueBuilder::DoSerialize(
const T& t) {
203 formats::
common::impl::kHasSerialize<Value, T>,
204 "There is no `Serialize(const T&, formats::serialize::To<yaml::Value>)` "
205 "in namespace of `T` or `formats::serizalize`. "
207 "Probably you forgot to include the "
208 "<userver/formats/serialize/common_containers.hpp> or you "
209 "have not provided a `Serialize` function overload.");
211 return Serialize(t, formats::serialize::To<Value>());
215std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) <=
sizeof(
long long),
217Serialize(T value, formats::serialize::To<Value>) {
218 using Type = std::conditional_t<std::is_signed<T>::value,
long long,
220 return yaml::ValueBuilder(
static_cast<Type>(value)).ExtractValue();