userver: userver/ydb/io/structs.hpp Source File
Loading...
Searching...
No Matches
structs.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ydb/io/structs.hpp
4/// @brief YDB struct serialization traits and customization
5
6#include <ydb-cpp-sdk/client/result/result.h>
7#include <ydb-cpp-sdk/client/value/value.h>
8
9#include <cstddef>
10#include <memory>
11#include <optional>
12#include <string_view>
13#include <tuple>
14#include <type_traits>
15
16#include <fmt/ranges.h>
17#include <boost/pfr/core.hpp>
18#include <boost/pfr/core_name.hpp>
19
20#include <userver/utils/assert.hpp>
21#include <userver/utils/constexpr_indices.hpp>
22#include <userver/utils/enumerate.hpp>
23#include <userver/utils/forward_like.hpp>
24#include <userver/utils/trivial_map.hpp>
25
26#include <userver/ydb/exceptions.hpp>
27#include <userver/ydb/impl/cast.hpp>
28#include <userver/ydb/io/generic_optional.hpp>
29#include <userver/ydb/io/traits.hpp>
30
31USERVER_NAMESPACE_BEGIN
32
33namespace ydb {
34
35namespace impl {
36
37struct NotStruct final {};
38
39template <typename T>
40constexpr decltype(T::kYdbMemberNames) DetectStructMemberNames() noexcept {
41 return T::kYdbMemberNames;
42}
43
44template <typename T, typename... Args>
45constexpr NotStruct DetectStructMemberNames(Args&&...) noexcept {
46 return NotStruct{};
47}
48
49// To avoid including heavy <array> header for std::array<T, 0>.
50template <typename T>
51class EmptyRange final {
52public:
53 constexpr T* begin() const noexcept { return nullptr; }
54 constexpr T* end() const noexcept { return nullptr; }
55};
56
57} // namespace impl
58
59/// @see ydb::CustomMemberNames
60struct CustomMemberName final {
61 std::string_view cpp_name;
62 std::string_view ydb_name;
63};
64
65/// @brief Specifies C++ to YDB struct member names mapping. It's enough to only
66/// specify the names that are different between C++ and YDB.
67/// @see ydb::kStructMemberNames
68template <std::size_t N>
69struct StructMemberNames final {
70 CustomMemberName custom_names[N];
71};
72
73/// @cond
74template <>
75struct StructMemberNames<0> final {
76 impl::EmptyRange<CustomMemberName> custom_names;
77};
78/// @endcond
79
80// clang-format off
81StructMemberNames() -> StructMemberNames<0>;
82// clang-format on
83
84template <std::size_t N>
85StructMemberNames(CustomMemberName (&&)[N]) -> StructMemberNames<N>;
86
87/// @brief Customization point for YDB serialization of structs.
88///
89/// In order to get serialization for a struct, you need to define
90/// `kYdbMemberNames` inside it:
91///
92/// @snippet ydb/small_table.hpp struct sample
93///
94/// Field names can be overridden:
95///
96/// @snippet ydb/tests/struct_io_test.cpp custom names
97///
98/// To enable YDB serialization for an external struct, specialize
99/// ydb::kStructMemberNames for it:
100///
101/// @snippet ydb/tests/struct_io_test.cpp external specialization
102///
103/// @warning The struct must not reside in an anonymous namespace, otherwise
104/// struct member names detection will break.
105///
106/// For extra fields on C++ side, parsing throws ydb::ParseError.
107/// For extra fields on YDB side, parsing throws ydb::ParseError.
108template <typename T>
110
111namespace impl {
112
113template <typename T, std::size_t N>
114constexpr auto WithDeducedNames(const StructMemberNames<N>& given_names) {
115 auto names = boost::pfr::names_as_array<T>();
116 for (const CustomMemberName& entry : given_names.custom_names) {
117 std::size_t count = 0;
118 for (auto& name : names) {
119 if (name == entry.cpp_name) {
120 name = entry.ydb_name;
121 ++count;
122 break;
123 }
124 }
125 if (count != 1) {
126 throw BaseError(
127 "In a StructMemberNames, each cpp_name must match the C++ name of "
128 "exactly 1 member of struct T"
129 );
130 }
131 }
132 return names;
133}
134
135template <typename T, std::size_t... Indices>
136constexpr auto MakeTupleOfOptionals(std::index_sequence<Indices...>) {
137 return std::tuple<std::optional<boost::pfr::tuple_element_t<Indices, T>>...>();
138}
139
140template <typename T, typename Tuple, std::size_t... Indices>
141constexpr auto MakeFromTupleOfOptionals(Tuple&& tuple, std::index_sequence<Indices...>) {
142 return T{*utils::ForwardLike<Tuple>(std::get<Indices>(tuple))...};
143}
144
145} // namespace impl
146
147template <typename T>
148struct ValueTraits<T, std::enable_if_t<!std::is_same_v<decltype(kStructMemberNames<T>), const impl::NotStruct>>> {
149 static_assert(std::is_aggregate_v<T>);
150 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
151 static constexpr auto kFieldNamesSet = utils::MakeTrivialSet<kFieldNames>();
152 static constexpr auto kFieldsCount = kFieldNames.size();
153
154 static T Parse(NYdb::TValueParser& parser, const ParseContext& context) {
155 auto parsed_fields = impl::MakeTupleOfOptionals<T>(std::make_index_sequence<kFieldsCount>{});
156 parser.OpenStruct();
157
158 while (parser.TryNextMember()) {
159 const auto& field_name = parser.GetMemberName();
160 const auto index = kFieldNamesSet.GetIndex(field_name);
161 if (!index.has_value()) {
162 throw ColumnParseError(
163 context.column_name,
164 fmt::format(
165 "Unexpected field name '{}' for '{}' struct type, "
166 "expected one of: {}",
167 field_name,
168 compiler::GetTypeName<T>(),
169 fmt::join(kFieldNames, ", ")
170 )
171 );
172 }
173 utils::WithConstexprIndex<kFieldsCount>(*index, [&](auto index_c) {
174 auto& field = std::get<decltype(index_c)::value>(parsed_fields);
175 using FieldType = typename std::decay_t<decltype(field)>::value_type;
176 field.emplace(ydb::Parse<FieldType>(parser, context));
177 });
178 }
179
180 parser.CloseStruct();
181
182 std::string_view missing_field;
183 utils::ForEachIndex<kFieldsCount>([&](auto index_c) {
184 if (!std::get<decltype(index_c)::value>(parsed_fields).has_value()) {
185 missing_field = kFieldNames[index_c];
186 }
187 });
188 if (!missing_field.empty()) {
189 throw ColumnParseError(
190 context.column_name,
191 fmt::format("Missing field '{}' for '{}' struct type", missing_field, compiler::GetTypeName<T>())
192 );
193 }
194
195 return impl::MakeFromTupleOfOptionals<T>(std::move(parsed_fields), std::make_index_sequence<kFieldsCount>{});
196 }
197
198 template <typename Builder>
199 static void Write(NYdb::TValueBuilderBase<Builder>& builder, const T& value) {
200 builder.BeginStruct();
201 boost::pfr::for_each_field(value, [&](const auto& field, std::size_t i) {
202 builder.AddMember(impl::ToString(kFieldNames[i]));
203 ydb::Write(builder, field);
204 });
205 builder.EndStruct();
206 }
207
208 static NYdb::TType MakeType() {
209 NYdb::TTypeBuilder builder;
210 builder.BeginStruct();
211 utils::ForEachIndex<kFieldsCount>([&](auto index_c) {
212 builder.AddMember(
213 impl::ToString(kFieldNames[index_c]),
214 ValueTraits<boost::pfr::tuple_element_t<decltype(index_c)::value, T>>::MakeType()
215 );
216 });
217 builder.EndStruct();
218 return builder.Build();
219 }
220};
221
222template <typename T>
223struct ValueTraits<
224 std::optional<T>,
225 std::enable_if_t<!std::is_same_v<decltype(kStructMemberNames<T>), const impl::NotStruct>>>
226 : impl::GenericOptionalValueTraits<T> {};
227
228namespace impl {
229
230template <typename T>
231struct StructRowParser final {
232 static_assert(std::is_aggregate_v<T>);
233 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
234 static constexpr auto kFieldsCount = kFieldNames.size();
235
236 static std::unique_ptr<std::size_t[]> MakeCppToYdbFieldMapping(NYdb::TResultSetParser& parser) {
237 auto result = std::make_unique<std::size_t[]>(kFieldsCount);
238 for (const auto [pos, field_name] : utils::enumerate(kFieldNames)) {
239 const auto column_index = parser.ColumnIndex(impl::ToString(field_name));
240 if (column_index == -1) {
241 throw ParseError(
242 fmt::format("Missing column '{}' for '{}' struct type", field_name, compiler::GetTypeName<T>())
243 );
244 }
245 result[pos] = static_cast<std::size_t>(column_index);
246 }
247
248 const auto columns_count = parser.ColumnsCount();
249 UASSERT(columns_count >= kFieldsCount);
250 if (columns_count != kFieldsCount) {
251 throw ParseError(fmt::format(
252 "Unexpected extra columns while parsing row to '{}' struct type",
253 compiler::GetTypeName<T>()
254 ));
255 }
256
257 return result;
258 }
259
260 static T ParseRow(NYdb::TResultSetParser& parser, const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping) {
261 return ParseRowImpl(parser, cpp_to_ydb_mapping, std::make_index_sequence<kFieldsCount>{});
262 }
263
264 template <std::size_t... Indices>
265 static T ParseRowImpl(NYdb::TResultSetParser& parser, const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping, std::index_sequence<Indices...>) {
266 return T{
267 Parse<boost::pfr::tuple_element_t<Indices, T>>(
268 parser.ColumnParser(cpp_to_ydb_mapping[Indices]),
269 ParseContext{.column_name = kFieldNames[Indices]}
270 )...,
271 };
272 }
273};
274
275} // namespace impl
276
277} // namespace ydb
278
279USERVER_NAMESPACE_END