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);
100 utils::StrongTypedefOps Ops,
102 ValueBuilder
operator[](
const utils::StrongTypedef<Tag, std::string, Ops>& key);
168 class EmplaceEnabler {};
172 ValueBuilder(EmplaceEnabler,
const YAML::Node& value,
const formats::
yaml::
Path& path,
const std::string& key);
174 ValueBuilder(EmplaceEnabler,
const YAML::Node& value,
const formats::
yaml::
Path& path, size_t index);
178 static ValueBuilder MakeNonRoot(
const YAML::Node& val,
const formats::
yaml::
Path& path,
const std::string& key);
179 static ValueBuilder MakeNonRoot(
const YAML::Node& val,
const formats::
yaml::
Path& path, size_t index);
181 void Copy(
const ValueBuilder& from);
182 void Move(ValueBuilder&& from);
183 void NodeDataAssign(
const formats::
yaml::Value& from);
185 template <
typename T>
186 static Value DoSerialize(
const T& t);
188 formats::
yaml::Value value_;
193template <
typename Tag, utils::StrongTypedefOps Ops,
typename Enable>
194ValueBuilder ValueBuilder::
operator[](
const utils::StrongTypedef<Tag, std::string, Ops>& key) {
195 return (*
this)[key.GetUnderlying()];
199Value ValueBuilder::DoSerialize(
const T& t) {
201 formats::
common::impl::kHasSerialize<Value, T>,
202 "There is no `Serialize(const T&, formats::serialize::To<yaml::Value>)` "
203 "in namespace of `T` or `formats::serialize`. "
205 "Probably you forgot to include the "
206 "<userver/formats/serialize/common_containers.hpp> or you "
207 "have not provided a `Serialize` function overload."
210 return Serialize(t, formats::serialize::To<Value>());
214std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) <=
sizeof(
long long), Value>
215Serialize(T value, formats::serialize::To<Value>) {
216 using Type = std::conditional_t<std::is_signed<T>::value,
long long,
unsigned long long>;
217 return yaml::ValueBuilder(
static_cast<Type>(value)).ExtractValue();