3#include <ydb-cpp-sdk/client/result/result.h>
4#include <ydb-cpp-sdk/client/value/value.h>
13#include <boost/pfr/core.hpp>
14#include <boost/pfr/core_name.hpp>
16#include <userver/utils/assert.hpp>
17#include <userver/utils/constexpr_indices.hpp>
18#include <userver/utils/enumerate.hpp>
19#include <userver/utils/trivial_map.hpp>
21#include <userver/ydb/exceptions.hpp>
22#include <userver/ydb/impl/cast.hpp>
23#include <userver/ydb/io/traits.hpp>
25USERVER_NAMESPACE_BEGIN
31struct NotStruct
final {};
34constexpr decltype(T::kYdbMemberNames) DetectStructMemberNames()
noexcept {
35 return T::kYdbMemberNames;
38template <
typename T,
typename... Args>
39constexpr NotStruct DetectStructMemberNames(Args&&...)
noexcept {
46struct CustomMemberName
final {
47 std::string_view cpp_name;
48 std::string_view ydb_name;
54template <std::size_t N>
55struct StructMemberNames
final {
56 CustomMemberName custom_names[N];
60StructMemberNames() -> StructMemberNames<0>;
63template <std::size_t N>
64StructMemberNames(CustomMemberName (&&)[N]) -> StructMemberNames<N>;
88constexpr auto kStructMemberNames = impl::DetectStructMemberNames<T>();
92template <
typename T, std::size_t N>
93constexpr auto WithDeducedNames(
const StructMemberNames<N>& given_names) {
94 auto names = boost::pfr::names_as_array<T>();
95 for (
const CustomMemberName& entry : given_names.custom_names) {
96 std::size_t count = 0;
97 for (
auto& name : names) {
98 if (name == entry.cpp_name) {
99 name = entry.ydb_name;
106 "In a StructMemberNames, each cpp_name must match the C++ name of "
107 "exactly 1 member of struct T"
114template <
typename T, std::size_t... Indices>
115constexpr auto MakeTupleOfOptionals(std::index_sequence<Indices...>) {
116 return std::tuple<std::optional<boost::pfr::tuple_element_t<Indices, T>>...>();
119template <
typename T,
typename Tuple, std::size_t... Indices>
120constexpr auto MakeFromTupleOfOptionals(Tuple&& tuple, std::index_sequence<Indices...>) {
121 return T{*std::get<Indices>(std::forward<Tuple>(tuple))...};
127struct ValueTraits<T, std::enable_if_t<!std::is_same_v<
decltype(kStructMemberNames<T>),
const impl::NotStruct>>> {
128 static_assert(std::is_aggregate_v<T>);
129 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
130 static constexpr auto kFieldNamesSet = utils::MakeTrivialSet<kFieldNames>();
131 static constexpr auto kFieldsCount = kFieldNames.size();
133 static T Parse(NYdb::TValueParser& parser,
const ParseContext& context) {
134 auto parsed_fields = impl::MakeTupleOfOptionals<T>(std::make_index_sequence<kFieldsCount>{});
137 while (parser.TryNextMember()) {
138 const auto& field_name = parser.GetMemberName();
139 const auto index = kFieldNamesSet.GetIndex(field_name);
140 if (!index.has_value()) {
141 throw ColumnParseError(
144 "Unexpected field name '{}' for '{}' struct type, "
145 "expected one of: {}",
147 compiler::GetTypeName<T>(),
148 fmt::join(kFieldNames,
", ")
152 utils::WithConstexprIndex<kFieldsCount>(*index, [&](
auto index_c) {
153 auto& field = std::get<
decltype(index_c)::value>(parsed_fields);
154 using FieldType =
typename std::decay_t<
decltype(field)>::value_type;
155 field.emplace(ydb::Parse<FieldType>(parser, context));
159 parser.CloseStruct();
161 std::string_view missing_field;
162 utils::ForEachIndex<kFieldsCount>([&](
auto index_c) {
163 if (!std::get<
decltype(index_c)::value>(parsed_fields).has_value()) {
164 missing_field = kFieldNames[index_c];
167 if (!missing_field.empty()) {
168 throw ColumnParseError(
170 fmt::format(
"Missing field '{}' for '{}' struct type", missing_field, compiler::GetTypeName<T>())
174 return impl::MakeFromTupleOfOptionals<T>(std::move(parsed_fields), std::make_index_sequence<kFieldsCount>{});
177 template <
typename Builder>
178 static void Write(NYdb::TValueBuilderBase<Builder>& builder,
const T& value) {
179 builder.BeginStruct();
180 boost::pfr::for_each_field(value, [&](
const auto& field, std::size_t i) {
181 builder.AddMember(impl::ToString(kFieldNames[i]));
182 ydb::Write(builder, field);
187 static NYdb::TType MakeType() {
188 NYdb::TTypeBuilder builder;
189 builder.BeginStruct();
190 utils::ForEachIndex<kFieldsCount>([&](
auto index_c) {
192 impl::ToString(kFieldNames[index_c]),
193 ValueTraits<boost::pfr::tuple_element_t<
decltype(index_c)::value, T>>::MakeType()
197 return builder.Build();
204struct StructRowParser
final {
205 static_assert(std::is_aggregate_v<T>);
206 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
207 static constexpr auto kFieldsCount = kFieldNames.size();
209 static std::unique_ptr<std::size_t[]> MakeCppToYdbFieldMapping(NYdb::TResultSetParser& parser) {
210 auto result = std::make_unique<std::size_t[]>(kFieldsCount);
211 for (
const auto [pos, field_name] :
utils::enumerate(kFieldNames)) {
212 const auto column_index = parser.ColumnIndex(impl::ToString(field_name));
213 if (column_index == -1) {
215 fmt::format(
"Missing column '{}' for '{}' struct type", field_name,
compiler::GetTypeName<T>())
218 result[pos] =
static_cast<std::size_t>(column_index);
221 const auto columns_count = parser.ColumnsCount();
222 UASSERT(columns_count >= kFieldsCount);
223 if (columns_count != kFieldsCount) {
225 "Unexpected extra columns while parsing row to '{}' struct type",
compiler::GetTypeName<T>()
232 static T ParseRow(NYdb::TResultSetParser& parser,
const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping) {
233 return ParseRowImpl(parser, cpp_to_ydb_mapping, std::make_index_sequence<kFieldsCount>{});
236 template <std::size_t... Indices>
238 ParseRowImpl(NYdb::TResultSetParser& parser,
const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping, std::index_sequence<Indices...>) {
240 Parse<boost::pfr::tuple_element_t<Indices, T>>(
241 parser.ColumnParser(cpp_to_ydb_mapping[Indices]), ParseContext{kFieldNames[Indices]}