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/generic_optional.hpp>
24#include <userver/ydb/io/traits.hpp>
26USERVER_NAMESPACE_BEGIN
32struct NotStruct
final {};
35constexpr decltype(T::kYdbMemberNames) DetectStructMemberNames()
noexcept {
36 return T::kYdbMemberNames;
39template <
typename T,
typename... Args>
40constexpr NotStruct DetectStructMemberNames(Args&&...)
noexcept {
47struct CustomMemberName
final {
48 std::string_view cpp_name;
49 std::string_view ydb_name;
47struct CustomMemberName
final {
…};
55template <std::size_t N>
56struct StructMemberNames
final {
57 CustomMemberName custom_names[N];
56struct StructMemberNames
final {
…};
61StructMemberNames() -> StructMemberNames<0>;
64template <std::size_t N>
65StructMemberNames(CustomMemberName (&&)[N]) -> StructMemberNames<N>;
89inline constexpr auto kStructMemberNames = impl::DetectStructMemberNames<T>();
93template <
typename T, std::size_t N>
94constexpr auto WithDeducedNames(
const StructMemberNames<N>& given_names) {
95 auto names = boost::pfr::names_as_array<T>();
96 for (
const CustomMemberName& entry : given_names.custom_names) {
97 std::size_t count = 0;
98 for (
auto& name : names) {
99 if (name == entry.cpp_name) {
100 name = entry.ydb_name;
107 "In a StructMemberNames, each cpp_name must match the C++ name of "
108 "exactly 1 member of struct T"
115template <
typename T, std::size_t... Indices>
116constexpr auto MakeTupleOfOptionals(std::index_sequence<Indices...>) {
117 return std::tuple<std::optional<boost::pfr::tuple_element_t<Indices, T>>...>();
120template <
typename T,
typename Tuple, std::size_t... Indices>
121constexpr auto MakeFromTupleOfOptionals(Tuple&& tuple, std::index_sequence<Indices...>) {
122 return T{*std::get<Indices>(std::forward<Tuple>(tuple))...};
128struct ValueTraits<T, std::enable_if_t<!std::is_same_v<
decltype(kStructMemberNames<T>),
const impl::NotStruct>>> {
129 static_assert(std::is_aggregate_v<T>);
130 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
131 static constexpr auto kFieldNamesSet =
utils::MakeTrivialSet<kFieldNames>();
132 static constexpr auto kFieldsCount = kFieldNames.size();
134 static T Parse(NYdb::TValueParser& parser,
const ParseContext& context) {
135 auto parsed_fields = impl::MakeTupleOfOptionals<T>(std::make_index_sequence<kFieldsCount>{});
138 while (parser.TryNextMember()) {
139 const auto& field_name = parser.GetMemberName();
140 const auto index = kFieldNamesSet.GetIndex(field_name);
141 if (!index.has_value()) {
145 "Unexpected field name '{}' for '{}' struct type, "
146 "expected one of: {}",
149 fmt::join(kFieldNames,
", ")
153 utils::WithConstexprIndex<kFieldsCount>(*index, [&](
auto index_c) {
154 auto& field = std::get<
decltype(index_c)::value>(parsed_fields);
155 using FieldType =
typename std::decay_t<
decltype(field)>::value_type;
156 field.emplace(ydb::Parse<FieldType>(parser, context));
160 parser.CloseStruct();
162 std::string_view missing_field;
163 utils::ForEachIndex<kFieldsCount>([&](
auto index_c) {
164 if (!std::get<
decltype(index_c)::value>(parsed_fields).has_value()) {
165 missing_field = kFieldNames[index_c];
168 if (!missing_field.empty()) {
171 fmt::format(
"Missing field '{}' for '{}' struct type", missing_field,
compiler::GetTypeName<T>())
175 return impl::MakeFromTupleOfOptionals<T>(std::move(parsed_fields), std::make_index_sequence<kFieldsCount>{});
178 template <
typename Builder>
179 static void Write(NYdb::TValueBuilderBase<Builder>& builder,
const T& value) {
180 builder.BeginStruct();
181 boost::pfr::for_each_field(value, [&](
const auto& field, std::size_t i) {
182 builder.AddMember(impl::ToString(kFieldNames[i]));
183 ydb::Write(builder, field);
188 static NYdb::TType MakeType() {
189 NYdb::TTypeBuilder builder;
190 builder.BeginStruct();
191 utils::ForEachIndex<kFieldsCount>([&](
auto index_c) {
193 impl::ToString(kFieldNames[index_c]),
194 ValueTraits<boost::pfr::tuple_element_t<
decltype(index_c)::value, T>>::MakeType()
198 return builder.Build();
205 std::enable_if_t<!std::is_same_v<
decltype(kStructMemberNames<T>),
const impl::NotStruct>>>
206 : impl::GenericOptionalValueTraits<T> {};
211struct StructRowParser
final {
212 static_assert(std::is_aggregate_v<T>);
213 static constexpr auto kFieldNames = impl::WithDeducedNames<T>(kStructMemberNames<T>);
214 static constexpr auto kFieldsCount = kFieldNames.size();
216 static std::unique_ptr<std::size_t[]> MakeCppToYdbFieldMapping(NYdb::TResultSetParser& parser) {
217 auto result = std::make_unique<std::size_t[]>(kFieldsCount);
218 for (
const auto [pos, field_name] :
utils::enumerate(kFieldNames)) {
219 const auto column_index = parser.ColumnIndex(impl::ToString(field_name));
220 if (column_index == -1) {
222 fmt::format(
"Missing column '{}' for '{}' struct type", field_name,
compiler::GetTypeName<T>())
225 result[pos] =
static_cast<std::size_t>(column_index);
228 const auto columns_count = parser.ColumnsCount();
229 UASSERT(columns_count >= kFieldsCount);
230 if (columns_count != kFieldsCount) {
232 "Unexpected extra columns while parsing row to '{}' struct type",
compiler::GetTypeName<T>()
239 static T ParseRow(NYdb::TResultSetParser& parser,
const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping) {
240 return ParseRowImpl(parser, cpp_to_ydb_mapping, std::make_index_sequence<kFieldsCount>{});
243 template <std::size_t... Indices>
245 ParseRowImpl(NYdb::TResultSetParser& parser,
const std::unique_ptr<std::size_t[]>& cpp_to_ydb_mapping, std::index_sequence<Indices...>) {
247 Parse<boost::pfr::tuple_element_t<Indices, T>>(
248 parser.ColumnParser(cpp_to_ydb_mapping[Indices]), ParseContext{kFieldNames[Indices]}