Skip to content

Commit 7119aa1

Browse files
author
pananton
committed
feat userver/proto-structs: add support for Value/ListValue/Struct to codegen
Relates: <https://nda.ya.ru/t/XA9jYhs_7TE6Tr> commit_hash:1b788a008cb39140217a2a3b5983a4263275c6f6
1 parent 7355091 commit 7119aa1

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

libraries/proto-structs/codegen-tests/proto/simple/base/base.proto

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ option go_package = "a.yandex-team.ru/taxi/uservices/userver/libraries/proto-str
77
import "simple/subdirectory/subdirectory.proto";
88
import "simple/subdirectory/separate_enum.proto";
99

10-
import "google/protobuf/timestamp.proto";
10+
import "google/protobuf/any.proto";
1111
import "google/protobuf/duration.proto";
12+
import "google/protobuf/struct.proto";
13+
import "google/protobuf/timestamp.proto";
1214
import "google/type/date.proto";
1315
import "google/type/timeofday.proto";
14-
import "google/protobuf/any.proto";
1516

1617
message SimpleStruct {
1718
int32 some_integer = 1;
@@ -75,4 +76,7 @@ message WellKnownUsrv {
7576
google.type.Date f3 = 3; // proto_structs::Date
7677
google.type.TimeOfDay f4 = 4; // proto_structs::TimeOfDay
7778
google.protobuf.Any f5 = 5; // proto_structs::Any
79+
google.protobuf.Value f6 = 6; // formats::json::Value
80+
google.protobuf.ListValue f7 = 7; // formats::json::Array
81+
google.protobuf.Struct f8 = 8; // formats::json::Object
7882
}

libraries/proto-structs/codegen-tests/src/simple/simple_test.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#include <type_traits>
2+
#include <unordered_map>
3+
#include <vector>
24

35
#include <gtest/gtest.h>
46

7+
#include <userver/formats/json/value_builder.hpp>
8+
#include <userver/formats/parse/common_containers.hpp>
9+
#include <userver/formats/serialize/common_containers.hpp>
510
#include <userver/proto-structs/convert.hpp>
611
#include <userver/proto-structs/type_mapping.hpp>
712

813
#include <simple/base/base.pb.h>
914
#include <simple/base/base.structs.usrv.pb.hpp>
1015

11-
#include <google/protobuf/any.h>
12-
1316
namespace ss = simple::base::structs;
1417

1518
USERVER_NAMESPACE_BEGIN
@@ -124,6 +127,11 @@ TEST(Oneof, WellKnownTypes) {
124127
const std::chrono::hours hours{20};
125128
const std::chrono::minutes minutes{10};
126129
const std::string string_value{"swag"};
130+
const formats::json::Value json_bool = formats::json::ValueBuilder{true}.ExtractValue();
131+
const formats::json::Array json_array{formats::json::ValueBuilder{std::vector<double>{-1.5, 1.5}}.ExtractValue()};
132+
const formats::json::Object json_object{
133+
formats::json::ValueBuilder(std::unordered_map<std::string, std::string>{{"a", "1"}, {"b", "2"}}).ExtractValue()
134+
};
127135

128136
ss::WellKnownUsrv message;
129137

@@ -136,9 +144,12 @@ TEST(Oneof, WellKnownTypes) {
136144
proto_structs::traits::CompatibleMessageType<ss::ForAny> for_any;
137145
for_any.set_f1(string_value);
138146

139-
EXPECT_TRUE(pbuf_any.PackFrom(for_any));
147+
ASSERT_TRUE(pbuf_any.PackFrom(for_any));
140148

141149
message.f5 = proto_structs::Any{pbuf_any};
150+
message.f6 = json_bool;
151+
message.f7 = json_array;
152+
message.f8 = json_object;
142153

143154
const auto vanilla = proto_structs::StructToMessage(std::move(message));
144155

@@ -168,6 +179,10 @@ TEST(Oneof, WellKnownTypes) {
168179

169180
const auto parsed_any = parsed.f5.Unpack<ss::ForAny>();
170181
ASSERT_EQ(parsed_any.f1, string_value);
182+
183+
ASSERT_EQ(parsed.f6, json_bool);
184+
ASSERT_EQ(parsed.f7, json_array);
185+
ASSERT_EQ(parsed.f8, json_object);
171186
}
172187

173188
USERVER_NAMESPACE_END

scripts/proto_structs/models/type_ref_consts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
'google.type.Date': type_ref.UserverLibraryType(full_cpp_name_wo_userver='proto_structs::Date'),
4848
'google.type.TimeOfDay': type_ref.UserverLibraryType(full_cpp_name_wo_userver='proto_structs::TimeOfDay'),
4949
'google.protobuf.Any': type_ref.UserverLibraryType(full_cpp_name_wo_userver='proto_structs::Any'),
50+
'google.protobuf.Value': type_ref.UserverLibraryType(full_cpp_name_wo_userver='formats::json::Value'),
51+
'google.protobuf.ListValue': type_ref.UserverLibraryType(full_cpp_name_wo_userver='formats::json::Array'),
52+
'google.protobuf.Struct': type_ref.UserverLibraryType(full_cpp_name_wo_userver='formats::json::Object'),
5053
}
5154

5255

0 commit comments

Comments
 (0)