@@ -147,8 +147,10 @@ inline void AddJsonObjectToProtoAsMap(
147
147
const google::protobuf::Reflection* reflection,
148
148
grpc::protobuf::Message* message,
149
149
const JSON& jsonObject,
150
+ ui32 depth,
150
151
std::function<const MAP (const JSON&)> extractMap,
151
- std::function<const TString(const JSON&)> valueToString
152
+ std::function<const TString(const JSON&)> valueToString,
153
+ std::function<void(const JSON&, grpc::protobuf::Message*, ui32)> jsonObjectToMessage
152
154
) {
153
155
const auto & protoMap = reflection->GetMutableRepeatedFieldRef <google::protobuf::Message>(message, fieldDescriptor);
154
156
for (const auto & [key, value] : extractMap (jsonObject)) {
@@ -160,46 +162,64 @@ inline void AddJsonObjectToProtoAsMap(
160
162
stringStringEntry
161
163
->GetReflection ()
162
164
->SetString (stringStringEntry.get (), fieldDescriptor->message_type ()->field (0 ), key);
163
- stringStringEntry
164
- ->GetReflection ()
165
- ->SetString (stringStringEntry.get (), fieldDescriptor->message_type ()->field (1 ), valueToString (value));
165
+
166
+ if (fieldDescriptor->message_type ()->field (1 )->cpp_type () == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
167
+ auto *msg = stringStringEntry->GetReflection ()->MutableMessage (stringStringEntry.get (), fieldDescriptor->message_type ()->field (1 ));
168
+ jsonObjectToMessage (value, msg, depth);
169
+ } else if (fieldDescriptor->message_type ()->field (1 )->cpp_type () == google::protobuf::FieldDescriptor::CPPTYPE_STRING) {
170
+ stringStringEntry
171
+ ->GetReflection ()
172
+ ->SetString (stringStringEntry.get (), fieldDescriptor->message_type ()->field (1 ), valueToString (value));
173
+ } else {
174
+ throw NKikimr::NSQS::TSQSException (NKikimr::NSQS::NErrors::INVALID_PARAMETER_VALUE)
175
+ << " Only String and Object can be converted to protobuf map" ;
176
+ }
166
177
protoMap.Add (*stringStringEntry);
167
178
}
168
179
}
169
180
181
+ void JsonToProto (const NJson::TJsonValue& jsonValue, NProtoBuf::Message* message, ui32 depth = 0 );
182
+
170
183
inline void AddJsonObjectToProtoAsMap (
171
184
const google::protobuf::FieldDescriptor* fieldDescriptor,
172
185
const google::protobuf::Reflection* reflection,
173
186
grpc::protobuf::Message* message,
174
- const NJson::TJsonValue& jsonObject
187
+ const NJson::TJsonValue& jsonObject,
188
+ ui32 depth
175
189
) {
176
190
AddJsonObjectToProtoAsMap<NJson::TJsonValue, NJson::TJsonValue::TMapType>(
177
191
fieldDescriptor,
178
192
reflection,
179
193
message,
180
194
jsonObject,
195
+ depth,
181
196
[](auto & json) { return json.GetMap (); },
182
- [](auto & value) -> const TString { return value.GetString (); }
197
+ [](auto & value) -> const TString { return value.GetString (); },
198
+ [](auto & json, auto message, auto depth) { JsonToProto (json, message, depth); }
183
199
);
184
200
}
201
+ void NlohmannJsonToProto (const nlohmann::json& jsonValue, NProtoBuf::Message* message, ui32 depth = 0 );
185
202
186
203
inline void AddJsonObjectToProtoAsMap (
187
204
const google::protobuf::FieldDescriptor* fieldDescriptor,
188
205
const google::protobuf::Reflection* reflection,
189
206
grpc::protobuf::Message* message,
190
- const nlohmann::basic_json<>& jsonObject
207
+ const nlohmann::basic_json<>& jsonObject,
208
+ ui32 depth
191
209
) {
192
210
AddJsonObjectToProtoAsMap<nlohmann::basic_json<>, std::map<TString, nlohmann::basic_json<>>>(
193
211
fieldDescriptor,
194
212
reflection,
195
213
message,
196
214
jsonObject,
215
+ depth,
197
216
[](auto & json) { return json.template get <std::map<TString, nlohmann::basic_json<>>>(); },
198
- [](auto & value) -> const TString { return value.template get <TString>(); }
217
+ [](auto & value) -> const TString { return value.template get <TString>(); },
218
+ [](auto & json, auto message, auto depth) { NlohmannJsonToProto (json, message, depth); }
199
219
);
200
220
}
201
221
202
- inline void JsonToProto (const NJson::TJsonValue& jsonValue, NProtoBuf::Message* message, ui32 depth = 0 ) {
222
+ inline void JsonToProto (const NJson::TJsonValue& jsonValue, NProtoBuf::Message* message, ui32 depth) {
203
223
Y_ENSURE (depth < 101 , " Json depth is > 100" );
204
224
Y_ENSURE_EX (
205
225
!jsonValue.IsNull (),
@@ -348,7 +368,7 @@ inline void JsonToProto(const NJson::TJsonValue& jsonValue, NProtoBuf::Message*
348
368
break ;
349
369
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
350
370
if (fieldDescriptor->is_map ()) {
351
- AddJsonObjectToProtoAsMap (fieldDescriptor, reflection, message, value);
371
+ AddJsonObjectToProtoAsMap (fieldDescriptor, reflection, message, value, depth );
352
372
} else {
353
373
auto *msg = reflection->MutableMessage (message, fieldDescriptor);
354
374
JsonToProto (value, msg, depth + 1 );
@@ -366,7 +386,7 @@ inline void JsonToProto(const NJson::TJsonValue& jsonValue, NProtoBuf::Message*
366
386
}
367
387
}
368
388
369
- inline void NlohmannJsonToProto (const nlohmann::json& jsonValue, NProtoBuf::Message* message, ui32 depth = 0 ) {
389
+ inline void NlohmannJsonToProto (const nlohmann::json& jsonValue, NProtoBuf::Message* message, ui32 depth) {
370
390
Y_ENSURE (depth < 101 , " Json depth is > 100" );
371
391
Y_ENSURE_EX (
372
392
!jsonValue.is_null (),
@@ -518,7 +538,7 @@ inline void NlohmannJsonToProto(const nlohmann::json& jsonValue, NProtoBuf::Mess
518
538
break ;
519
539
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
520
540
if (fieldDescriptor->is_map ()) {
521
- AddJsonObjectToProtoAsMap (fieldDescriptor, reflection, message, value);
541
+ AddJsonObjectToProtoAsMap (fieldDescriptor, reflection, message, value, depth );
522
542
} else {
523
543
auto *msg = reflection->MutableMessage (message, fieldDescriptor);
524
544
NlohmannJsonToProto (value, msg, depth + 1 );
0 commit comments