aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf
diff options
context:
space:
mode:
authorart-snake <art-snake@yandex-team.ru>2022-02-10 16:50:35 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:50:35 +0300
commitdc5517df41fe6319ff249956fe5650e4bbc3e660 (patch)
tree5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/protobuf
parent1700010e2088971894d12a7a16d6004866f986fd (diff)
downloadydb-dc5517df41fe6319ff249956fe5650e4bbc3e660.tar.gz
Restoring authorship annotation for <art-snake@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/protobuf')
-rw-r--r--library/cpp/protobuf/json/config.h24
-rw-r--r--library/cpp/protobuf/json/json2proto.cpp18
-rw-r--r--library/cpp/protobuf/json/json2proto.h24
-rw-r--r--library/cpp/protobuf/json/proto2json_printer.cpp28
-rw-r--r--library/cpp/protobuf/json/ut/json2proto_ut.cpp136
-rw-r--r--library/cpp/protobuf/json/ut/proto2json_ut.cpp114
-rw-r--r--library/cpp/protobuf/json/ut/test.proto32
7 files changed, 188 insertions, 188 deletions
diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h
index ae06b3d8ec..dc84fb4d5d 100644
--- a/library/cpp/protobuf/json/config.h
+++ b/library/cpp/protobuf/json/config.h
@@ -15,20 +15,20 @@ namespace NProtobufJson {
bool FormatOutput = false;
enum MissingKeyMode {
- // Skip missing keys
+ // Skip missing keys
MissingKeySkip = 0,
- // Fill missing keys with json null value.
+ // Fill missing keys with json null value.
MissingKeyNull,
- // Use default value in any case.
- // If default value is not explicitly defined, use default type value:
- // i.e. 0 for integers, "" for strings
- // For repeated keys, means []
- MissingKeyDefault,
- // Use default value if it is explicitly specified for optional fields.
- // Skip if no explicitly defined default value for optional fields.
- // Throw exception if required field is empty.
- // For repeated keys, same as MissingKeySkip
- MissingKeyExplicitDefaultThrowRequired
+ // Use default value in any case.
+ // If default value is not explicitly defined, use default type value:
+ // i.e. 0 for integers, "" for strings
+ // For repeated keys, means []
+ MissingKeyDefault,
+ // Use default value if it is explicitly specified for optional fields.
+ // Skip if no explicitly defined default value for optional fields.
+ // Throw exception if required field is empty.
+ // For repeated keys, same as MissingKeySkip
+ MissingKeyExplicitDefaultThrowRequired
};
MissingKeyMode MissingSingleKeyMode = MissingKeySkip;
MissingKeyMode MissingRepeatedKeyMode = MissingKeySkip;
diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp
index f19204ac35..640c10f5a5 100644
--- a/library/cpp/protobuf/json/json2proto.cpp
+++ b/library/cpp/protobuf/json/json2proto.cpp
@@ -19,10 +19,10 @@
} \
if (!json.JsonCheckType()) { \
if (config.CastFromString && json.IsString()) { \
- if (config.DoNotCastEmptyStrings && json.GetString().empty()) { \
- /* Empty string is same as "no value" for scalar types.*/ \
- break; \
- } \
+ if (config.DoNotCastEmptyStrings && json.GetString().empty()) { \
+ /* Empty string is same as "no value" for scalar types.*/ \
+ break; \
+ } \
reflection->ProtoSet(&proto, &field, FromString(json.GetString())); \
break; \
} \
@@ -335,7 +335,7 @@ Json2RepeatedField(const NJson::TJsonValue& json,
}
}
- if (fieldJson.GetType() != NJson::JSON_ARRAY && !config.MapAsObject && !config.VectorizeScalars && !config.ValueVectorizer) {
+ if (fieldJson.GetType() != NJson::JSON_ARRAY && !config.MapAsObject && !config.VectorizeScalars && !config.ValueVectorizer) {
ythrow yexception() << "JSON field doesn't represent an array for "
<< name
<< "(actual type is "
@@ -361,10 +361,10 @@ Json2RepeatedField(const NJson::TJsonValue& json,
for (const NJson::TJsonValue& jsonValue : jsonArray) {
Json2RepeatedFieldValue(jsonValue, proto, field, config, reflection);
}
- } else if (config.ValueVectorizer) {
- for (const NJson::TJsonValue& jsonValue : config.ValueVectorizer(fieldJson)) {
- Json2RepeatedFieldValue(jsonValue, proto, field, config, reflection);
- }
+ } else if (config.ValueVectorizer) {
+ for (const NJson::TJsonValue& jsonValue : config.ValueVectorizer(fieldJson)) {
+ Json2RepeatedFieldValue(jsonValue, proto, field, config, reflection);
+ }
} else if (config.VectorizeScalars) {
Json2RepeatedFieldValue(fieldJson, proto, field, config, reflection);
}
diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h
index 458e0c0909..4c33498dfa 100644
--- a/library/cpp/protobuf/json/json2proto.h
+++ b/library/cpp/protobuf/json/json2proto.h
@@ -19,7 +19,7 @@ namespace google {
namespace NProtobufJson {
struct TJson2ProtoConfig {
using TSelf = TJson2ProtoConfig;
- using TValueVectorizer = std::function<NJson::TJsonValue::TArray(const NJson::TJsonValue& jsonValue)>;
+ using TValueVectorizer = std::function<NJson::TJsonValue::TArray(const NJson::TJsonValue& jsonValue)>;
enum FldNameMode {
FieldNameOriginalCase = 0, // default
@@ -58,11 +58,11 @@ namespace NProtobufJson {
return *this;
}
- TSelf& SetDoNotCastEmptyStrings(bool cast) {
- DoNotCastEmptyStrings = cast;
- return *this;
- }
-
+ TSelf& SetDoNotCastEmptyStrings(bool cast) {
+ DoNotCastEmptyStrings = cast;
+ return *this;
+ }
+
TSelf& SetCastRobust(bool cast) {
CastRobust = cast;
return *this;
@@ -115,9 +115,9 @@ namespace NProtobufJson {
/// Cast string json values to protobuf field type
bool CastFromString = false;
- /// Skip empty strings, instead casting from string into scalar types.
- /// I.e. empty string like default value for scalar types.
- bool DoNotCastEmptyStrings = false;
+ /// Skip empty strings, instead casting from string into scalar types.
+ /// I.e. empty string like default value for scalar types.
+ bool DoNotCastEmptyStrings = false;
/// Cast all json values to protobuf field types
bool CastRobust = false;
@@ -138,9 +138,9 @@ namespace NProtobufJson {
/// Append scalars to repeated fields
bool VectorizeScalars = false;
-
- /// Custom spliter non array value to repeated fields.
- TValueVectorizer ValueVectorizer;
+
+ /// Custom spliter non array value to repeated fields.
+ TValueVectorizer ValueVectorizer;
/// Allow js-style comments (both // and /**/)
bool AllowComments = false;
diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp
index 69a0aa25f6..6123eab0f2 100644
--- a/library/cpp/protobuf/json/proto2json_printer.cpp
+++ b/library/cpp/protobuf/json/proto2json_printer.cpp
@@ -205,18 +205,18 @@ namespace NProtobufJson {
const Reflection* reflection = proto.GetReflection();
- bool shouldPrintField = reflection->HasField(proto, &field);
- if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) {
- if (field.has_default_value()) {
- shouldPrintField = true;
- } else if (field.is_required()) {
- ythrow yexception() << "Empty required protobuf field: "
- << field.full_name() << ".";
- }
- }
- shouldPrintField = shouldPrintField || GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyDefault;
-
- if (shouldPrintField) {
+ bool shouldPrintField = reflection->HasField(proto, &field);
+ if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) {
+ if (field.has_default_value()) {
+ shouldPrintField = true;
+ } else if (field.is_required()) {
+ ythrow yexception() << "Empty required protobuf field: "
+ << field.full_name() << ".";
+ }
+ }
+ shouldPrintField = shouldPrintField || GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyDefault;
+
+ if (shouldPrintField) {
switch (field.cpp_type()) {
INT_FIELD_TO_JSON(CPPTYPE_INT32, GetInt32);
INT_FIELD_TO_JSON(CPPTYPE_INT64, GetInt64);
@@ -256,7 +256,7 @@ namespace NProtobufJson {
}
case TProto2JsonConfig::MissingKeySkip:
- case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
+ case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
default:
break;
}
@@ -358,7 +358,7 @@ namespace NProtobufJson {
}
case TProto2JsonConfig::MissingKeySkip:
- case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
+ case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
default:
break;
}
diff --git a/library/cpp/protobuf/json/ut/json2proto_ut.cpp b/library/cpp/protobuf/json/ut/json2proto_ut.cpp
index 081072971c..0dfe57bc7a 100644
--- a/library/cpp/protobuf/json/ut/json2proto_ut.cpp
+++ b/library/cpp/protobuf/json/ut/json2proto_ut.cpp
@@ -690,57 +690,57 @@ Y_UNIT_TEST(TestVectorizeScalars) {
#undef DEFINE_FIELD
}
-Y_UNIT_TEST(TestValueVectorizer) {
- {
- // No ValueVectorizer
- NJson::TJsonValue json;
- json["RepeatedString"] = "123";
- TJson2ProtoConfig config;
- TSingleRepeatedString expected;
- UNIT_ASSERT_EXCEPTION(Json2Proto(json, expected, config), yexception);
- }
- {
- // ValueVectorizer replace original value by array
- NJson::TJsonValue json;
- json["RepeatedString"] = "123";
- TJson2ProtoConfig config;
-
- TSingleRepeatedString expected;
- expected.AddRepeatedString("4");
- expected.AddRepeatedString("5");
- expected.AddRepeatedString("6");
-
- config.ValueVectorizer = [](const NJson::TJsonValue& val) -> NJson::TJsonValue::TArray {
- Y_UNUSED(val);
- return {NJson::TJsonValue("4"), NJson::TJsonValue("5"), NJson::TJsonValue("6")};
- };
- TSingleRepeatedString actual;
- Json2Proto(json, actual, config);
- UNIT_ASSERT_PROTOS_EQUAL(expected, actual);
- }
- {
- // ValueVectorizer replace original value by array and cast
- NJson::TJsonValue json;
- json["RepeatedInt"] = 123;
- TJson2ProtoConfig config;
-
- TSingleRepeatedInt expected;
- expected.AddRepeatedInt(4);
- expected.AddRepeatedInt(5);
- expected.AddRepeatedInt(6);
-
- config.ValueVectorizer = [](const NJson::TJsonValue& val) -> NJson::TJsonValue::TArray {
- Y_UNUSED(val);
- return {NJson::TJsonValue("4"), NJson::TJsonValue(5), NJson::TJsonValue("6")};
- };
- config.CastFromString = true;
-
- TSingleRepeatedInt actual;
- Json2Proto(json, actual, config);
- UNIT_ASSERT_PROTOS_EQUAL(expected, actual);
- }
-}
-
+Y_UNIT_TEST(TestValueVectorizer) {
+ {
+ // No ValueVectorizer
+ NJson::TJsonValue json;
+ json["RepeatedString"] = "123";
+ TJson2ProtoConfig config;
+ TSingleRepeatedString expected;
+ UNIT_ASSERT_EXCEPTION(Json2Proto(json, expected, config), yexception);
+ }
+ {
+ // ValueVectorizer replace original value by array
+ NJson::TJsonValue json;
+ json["RepeatedString"] = "123";
+ TJson2ProtoConfig config;
+
+ TSingleRepeatedString expected;
+ expected.AddRepeatedString("4");
+ expected.AddRepeatedString("5");
+ expected.AddRepeatedString("6");
+
+ config.ValueVectorizer = [](const NJson::TJsonValue& val) -> NJson::TJsonValue::TArray {
+ Y_UNUSED(val);
+ return {NJson::TJsonValue("4"), NJson::TJsonValue("5"), NJson::TJsonValue("6")};
+ };
+ TSingleRepeatedString actual;
+ Json2Proto(json, actual, config);
+ UNIT_ASSERT_PROTOS_EQUAL(expected, actual);
+ }
+ {
+ // ValueVectorizer replace original value by array and cast
+ NJson::TJsonValue json;
+ json["RepeatedInt"] = 123;
+ TJson2ProtoConfig config;
+
+ TSingleRepeatedInt expected;
+ expected.AddRepeatedInt(4);
+ expected.AddRepeatedInt(5);
+ expected.AddRepeatedInt(6);
+
+ config.ValueVectorizer = [](const NJson::TJsonValue& val) -> NJson::TJsonValue::TArray {
+ Y_UNUSED(val);
+ return {NJson::TJsonValue("4"), NJson::TJsonValue(5), NJson::TJsonValue("6")};
+ };
+ config.CastFromString = true;
+
+ TSingleRepeatedInt actual;
+ Json2Proto(json, actual, config);
+ UNIT_ASSERT_PROTOS_EQUAL(expected, actual);
+ }
+}
+
Y_UNIT_TEST(TestMapAsObject) {
TMapType modelProto;
@@ -1103,23 +1103,23 @@ Y_UNIT_TEST(TestMergeRepeatedAppend) {
UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto);
} // TestMergeRepeatedAppend
-Y_UNIT_TEST(TestEmptyStringForCastFromString) {
- NJson::TJsonValue json;
- json["I32"] = "";
- json["Bool"] = "";
- json["OneString"] = "";
-
- TJson2ProtoConfig config;
- config.SetCastFromString(true);
- config.SetDoNotCastEmptyStrings(true);
- TFlatOptional proto;
- UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto, config));
- UNIT_ASSERT(!proto.HasBool());
- UNIT_ASSERT(!proto.HasI32());
- UNIT_ASSERT(proto.HasOneString());
- UNIT_ASSERT_EQUAL("", proto.GetOneString());
-} // TestEmptyStringForCastFromString
-
+Y_UNIT_TEST(TestEmptyStringForCastFromString) {
+ NJson::TJsonValue json;
+ json["I32"] = "";
+ json["Bool"] = "";
+ json["OneString"] = "";
+
+ TJson2ProtoConfig config;
+ config.SetCastFromString(true);
+ config.SetDoNotCastEmptyStrings(true);
+ TFlatOptional proto;
+ UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto, config));
+ UNIT_ASSERT(!proto.HasBool());
+ UNIT_ASSERT(!proto.HasI32());
+ UNIT_ASSERT(proto.HasOneString());
+ UNIT_ASSERT_EQUAL("", proto.GetOneString());
+} // TestEmptyStringForCastFromString
+
Y_UNIT_TEST(TestAllowComments) {
constexpr TStringBuf json = R"(
{
diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp
index 6ae7960bb1..07e52d7f2f 100644
--- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp
+++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp
@@ -465,52 +465,52 @@ Y_UNIT_TEST(TestMissingSingleKeyConfig) {
UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
}
- {
- // Test MissingKeyExplicitDefaultThrowRequired for non explicit default values.
- TFlatOptional proto;
- NJson::TJsonValue modelJson(NJson::JSON_MAP);
- NJson::TJsonValue json;
- TProto2JsonConfig config;
- config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
-
- UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
- UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
- }
- {
- // Test MissingKeyExplicitDefaultThrowRequired for explicit default values.
- NJson::TJsonValue modelJson;
- modelJson["String"] = "value";
-
- TSingleDefaultString proto;
- NJson::TJsonValue json;
- TProto2JsonConfig config;
- config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
- UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
- UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
- }
- {
- // Test MissingKeyExplicitDefaultThrowRequired for empty required values.
- TFlatRequired proto;
- NJson::TJsonValue json;
- TProto2JsonConfig config;
- config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
- UNIT_ASSERT_EXCEPTION_CONTAINS(Proto2Json(proto, json, config), yexception, "Empty required protobuf field");
- }
- {
- // Test MissingKeyExplicitDefaultThrowRequired for required value.
- TSingleRequiredString proto;
- NJson::TJsonValue json;
- TProto2JsonConfig config;
- config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
-
- UNIT_ASSERT_EXCEPTION_CONTAINS(Proto2Json(proto, json, config), yexception, "Empty required protobuf field");
-
- NJson::TJsonValue modelJson;
- modelJson["String"] = "value";
- proto.SetString("value");
- UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
- UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
- }
+ {
+ // Test MissingKeyExplicitDefaultThrowRequired for non explicit default values.
+ TFlatOptional proto;
+ NJson::TJsonValue modelJson(NJson::JSON_MAP);
+ NJson::TJsonValue json;
+ TProto2JsonConfig config;
+ config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
+
+ UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
+ UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
+ }
+ {
+ // Test MissingKeyExplicitDefaultThrowRequired for explicit default values.
+ NJson::TJsonValue modelJson;
+ modelJson["String"] = "value";
+
+ TSingleDefaultString proto;
+ NJson::TJsonValue json;
+ TProto2JsonConfig config;
+ config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
+ UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
+ UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
+ }
+ {
+ // Test MissingKeyExplicitDefaultThrowRequired for empty required values.
+ TFlatRequired proto;
+ NJson::TJsonValue json;
+ TProto2JsonConfig config;
+ config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
+ UNIT_ASSERT_EXCEPTION_CONTAINS(Proto2Json(proto, json, config), yexception, "Empty required protobuf field");
+ }
+ {
+ // Test MissingKeyExplicitDefaultThrowRequired for required value.
+ TSingleRequiredString proto;
+ NJson::TJsonValue json;
+ TProto2JsonConfig config;
+ config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
+
+ UNIT_ASSERT_EXCEPTION_CONTAINS(Proto2Json(proto, json, config), yexception, "Empty required protobuf field");
+
+ NJson::TJsonValue modelJson;
+ modelJson["String"] = "value";
+ proto.SetString("value");
+ UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
+ UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
+ }
} // TestMissingSingleKeyConfig
Y_UNIT_TEST(TestMissingRepeatedKeyNoConfig) {
@@ -551,17 +551,17 @@ Y_UNIT_TEST(TestMissingRepeatedKeyConfig) {
UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
}
- {
- TFlatRepeated proto;
- NJson::TJsonValue modelJson(NJson::JSON_MAP);
- NJson::TJsonValue json;
- TProto2JsonConfig config;
- config.MissingRepeatedKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
-
- // SHould be same as MissingKeySkip
- UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
- UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
- }
+ {
+ TFlatRepeated proto;
+ NJson::TJsonValue modelJson(NJson::JSON_MAP);
+ NJson::TJsonValue json;
+ TProto2JsonConfig config;
+ config.MissingRepeatedKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
+
+ // SHould be same as MissingKeySkip
+ UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
+ UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
+ }
} // TestMissingRepeatedKeyConfig
Y_UNIT_TEST(TestEscaping) {
diff --git a/library/cpp/protobuf/json/ut/test.proto b/library/cpp/protobuf/json/ut/test.proto
index 8cf7bf8a5d..0fa996fd41 100644
--- a/library/cpp/protobuf/json/ut/test.proto
+++ b/library/cpp/protobuf/json/ut/test.proto
@@ -177,22 +177,22 @@ message TWithJsonName {
optional int32 Def_upper = 3; // json_name = "DefUpper"
optional int32 def_lower = 4; // json_name = "defLower"
}
-
-message TSingleRequiredString {
- required string String = 1;
-}
-
-message TSingleDefaultString {
- optional string String = 1 [default = "value"];
-}
-
-message TSingleRepeatedString {
- repeated string RepeatedString = 1;
-}
-
-message TSingleRepeatedInt {
- repeated int32 RepeatedInt = 1;
-}
+
+message TSingleRequiredString {
+ required string String = 1;
+}
+
+message TSingleDefaultString {
+ optional string String = 1 [default = "value"];
+}
+
+message TSingleRepeatedString {
+ repeated string RepeatedString = 1;
+}
+
+message TSingleRepeatedInt {
+ repeated int32 RepeatedInt = 1;
+}
message TExtensionField {
extensions 100 to 199;