6#include <ydb-cpp-sdk/client/result/result.h>
7#include <ydb-cpp-sdk/client/value/value.h>
16#include <fmt/ranges.h>
17#include <boost/pfr/core.hpp>
18#include <boost/pfr/core_name.hpp>
27#include <userver/ydb/impl/cast.hpp>
31USERVER_NAMESPACE_BEGIN
37struct NotStruct final {};
40constexpr decltype(T::kYdbMemberNames) DetectStructMemberNames()
noexcept {
41 return T::kYdbMemberNames;
44template <
typename T,
typename... Args>
45constexpr NotStruct DetectStructMemberNames(Args&&...) noexcept {
51class EmptyRange final {
53 constexpr T* begin() const noexcept {
return nullptr; }
54 constexpr T* end() const noexcept {
return nullptr; }
61 std::string_view cpp_name;
62 std::string_view ydb_name;
68template <std::
size_t N>
76 impl::EmptyRange<CustomMemberName> custom_names;
81StructMemberNames() -> StructMemberNames<0>;
84template <std::
size_t N>
85StructMemberNames(CustomMemberName (&&)[N]) -> StructMemberNames<N>;
113template <
typename T, std::
size_t N>
115 auto names = boost::pfr::names_as_array<T>();
117 std::size_t count = 0;
118 for (
auto& name : names) {
119 if (name == entry.cpp_name) {
120 name = entry.ydb_name;
127 "In a StructMemberNames, each cpp_name must match the C++ name of "
128 "exactly 1 member of struct T"
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>>...>();
140template <
typename T,
typename Tuple, std::size_t... Indices>
141constexpr auto MakeFromTupleOfOptionals(Tuple&& tuple, std::index_sequence<Indices...>) {
146concept IsStruct = !std::is_same_v<
decltype(kStructMemberNames<T>),
const impl::NotStruct>;
151requires impl::IsStruct<T>
153 static_assert(std::is_aggregate_v<T>);
154 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
155 static constexpr auto kFieldNamesSet = utils::MakeTrivialSet<kFieldNames>();
156 static constexpr auto kFieldsCount = kFieldNames.size();
158 static T
Parse(NYdb::TValueParser& parser,
const ParseContext& context) {
159 auto parsed_fields = impl::MakeTupleOfOptionals<T>(std::make_index_sequence<kFieldsCount>{});
162 while (parser.TryNextMember()) {
163 const auto& field_name = parser.GetMemberName();
164 const auto index = kFieldNamesSet.GetIndex(field_name);
165 if (!index.has_value()) {
166 throw ColumnParseError(
169 "Unexpected field name '{}' for '{}' struct type, "
170 "expected one of: {}",
173 fmt::join(kFieldNames,
", ")
178 auto& field = std::get<decltype(index_c)::value>(parsed_fields);
179 using FieldType =
typename std::decay_t<
decltype(field)>::value_type;
180 field.emplace(ydb::Parse<FieldType>(parser, context));
184 parser.CloseStruct();
186 std::string_view missing_field;
188 if (!std::get<
decltype(index_c)::value>(parsed_fields).has_value()) {
189 missing_field = kFieldNames[index_c];
192 if (!missing_field.empty()) {
193 throw ColumnParseError(
199 return impl::MakeFromTupleOfOptionals<T>(std::move(parsed_fields), std::make_index_sequence<kFieldsCount>{});
202 template <
typename Builder>
203 static void Write(NYdb::TValueBuilderBase<Builder>& builder,
const T& value) {
204 builder.BeginStruct();
205 boost::pfr::for_each_field(value, [&](
const auto& field, std::size_t i) {
206 builder.AddMember(impl::ToString(kFieldNames[i]));
213 NYdb::TTypeBuilder builder;
214 builder.BeginStruct();
217 impl::ToString(kFieldNames[index_c]),
218 ValueTraits<boost::pfr::tuple_element_t<
decltype(index_c)::value, T>>
::MakeType()
222 return builder.Build();
227requires impl::IsStruct<T>
228struct ValueTraits<std::optional<T>> : impl::GenericOptionalValueTraits<T> {};
233struct StructRowParser final {
234 static_assert(std::is_aggregate_v<T>);
235 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
236 static constexpr auto kFieldsCount = kFieldNames.size();
238 static std::unique_ptr<std::size_t[]> MakeCppToYdbFieldMapping(NYdb::TResultSetParser& parser) {
239 auto result = std::make_unique<std::size_t[]>(kFieldsCount);
241 const auto column_index = parser.ColumnIndex(impl::ToString(field_name));
242 if (column_index == -1) {
247 result[pos] =
static_cast<std::size_t
>(column_index);
250 const auto columns_count = parser.ColumnsCount();
251 UASSERT(columns_count >= kFieldsCount);
252 if (columns_count != kFieldsCount) {
254 "Unexpected extra columns while parsing row to '{}' struct type",
262 static T ParseRow(NYdb::TResultSetParser& parser,
const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping) {
263 return ParseRowImpl(parser, cpp_to_ydb_mapping, std::make_index_sequence<kFieldsCount>{});
266 template <std::size_t... Indices>
267 static T ParseRowImpl(NYdb::TResultSetParser& parser,
const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping, std::index_sequence<Indices...>) {
269 Parse<boost::pfr::tuple_element_t<Indices, T>>(
270 parser.ColumnParser(cpp_to_ydb_mapping[Indices]),
271 ParseContext{.column_name = kFieldNames[Indices]}