diff options
author | familom <familom@yandex-team.ru> | 2022-02-10 16:49:49 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:49 +0300 |
commit | f281aaf77179d27d6208b873e95ae6cd45765a63 (patch) | |
tree | b4229c6ece98c855bd9821ef0b656042c29a8953 /library/cpp/protobuf | |
parent | 53d07fb9e28d179add32cd299c9341bf8a231a31 (diff) | |
download | ydb-f281aaf77179d27d6208b873e95ae6cd45765a63.tar.gz |
Restoring authorship annotation for <familom@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf')
-rw-r--r-- | library/cpp/protobuf/json/README | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.cpp | 142 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.h | 12 | ||||
-rw-r--r-- | library/cpp/protobuf/json/proto2json.cpp | 30 | ||||
-rw-r--r-- | library/cpp/protobuf/json/proto2json.h | 34 | ||||
-rw-r--r-- | library/cpp/protobuf/json/string_transform.cpp | 14 | ||||
-rw-r--r-- | library/cpp/protobuf/json/string_transform.h | 36 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/fields.incl | 38 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/json.h | 50 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/json2proto_ut.cpp | 84 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto.h | 32 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto2json_ut.cpp | 204 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/repeated_fields.incl | 34 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/test.proto | 176 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/ya.make | 16 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ya.make | 26 |
16 files changed, 465 insertions, 465 deletions
diff --git a/library/cpp/protobuf/json/README b/library/cpp/protobuf/json/README index a0d1092ee2..b3a79ad7e6 100644 --- a/library/cpp/protobuf/json/README +++ b/library/cpp/protobuf/json/README @@ -1 +1 @@ -Protobuf to/from JSON converter. +Protobuf to/from JSON converter. diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp index 640c10f5a5..f2bb8d61ee 100644 --- a/library/cpp/protobuf/json/json2proto.cpp +++ b/library/cpp/protobuf/json/json2proto.cpp @@ -1,16 +1,16 @@ -#include "json2proto.h" +#include "json2proto.h" #include "util.h" - + #include <library/cpp/json/json_value.h> - + #include <google/protobuf/message.h> #include <google/protobuf/descriptor.h> - + #include <util/generic/hash.h> #include <util/generic/maybe.h> #include <util/string/ascii.h> -#include <util/string/cast.h> - +#include <util/string/cast.h> + #define JSON_TO_FIELD(EProtoCppType, name, json, JsonCheckType, ProtoSet, JsonGet) \ case FieldDescriptor::EProtoCppType: { \ if (config.CastRobust) { \ @@ -32,8 +32,8 @@ } \ reflection->ProtoSet(&proto, &field, json.JsonGet()); \ break; \ - } - + } + static TString GetFieldName(const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config) { if (config.NameGenerator) { @@ -71,13 +71,13 @@ static TString GetFieldName(const google::protobuf::FieldDescriptor& field, case NProtobufJson::TJson2ProtoConfig::FieldNameSnakeCaseDense: NProtobufJson::ToSnakeCaseDense(&name); break; - default: + default: Y_VERIFY_DEBUG(false, "Unknown FieldNameMode."); } return name; } -static void +static void JsonString2Field(const NJson::TJsonValue& json, google::protobuf::Message& proto, const google::protobuf::FieldDescriptor& field, @@ -123,28 +123,28 @@ FindEnumValue(const NProtoBuf::EnumDescriptor* enumField, } static void -JsonEnum2Field(const NJson::TJsonValue& json, - google::protobuf::Message& proto, +JsonEnum2Field(const NJson::TJsonValue& json, + google::protobuf::Message& proto, const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config) { - using namespace google::protobuf; - - const Reflection* reflection = proto.GetReflection(); + using namespace google::protobuf; + + const Reflection* reflection = proto.GetReflection(); Y_ASSERT(!!reflection); - - const EnumDescriptor* enumField = field.enum_type(); + + const EnumDescriptor* enumField = field.enum_type(); Y_ASSERT(!!enumField); - - /// @todo configure name/numerical value + + /// @todo configure name/numerical value const EnumValueDescriptor* enumFieldValue = nullptr; - - if (json.IsInteger()) { + + if (json.IsInteger()) { const auto value = json.GetInteger(); enumFieldValue = enumField->FindValueByNumber(value); if (!enumFieldValue) { ythrow yexception() << "Invalid integer value of JSON enum field: " << value << "."; } - } else if (json.IsString()) { + } else if (json.IsString()) { const auto& value = json.GetString(); if (config.EnumValueMode == NProtobufJson::TJson2ProtoConfig::EnumCaseInsensetive) { enumFieldValue = FindEnumValue(enumField, value, AsciiEqualsIgnoreCase); @@ -156,25 +156,25 @@ JsonEnum2Field(const NJson::TJsonValue& json, if (!enumFieldValue) { ythrow yexception() << "Invalid string value of JSON enum field: " << TStringBuf(value).Head(100) << "."; } - } else { - ythrow yexception() << "Invalid type of JSON enum field: not an integer/string."; - } - + } else { + ythrow yexception() << "Invalid type of JSON enum field: not an integer/string."; + } + if (field.is_repeated()) { reflection->AddEnum(&proto, &field, enumFieldValue); - } else { + } else { reflection->SetEnum(&proto, &field, enumFieldValue); - } -} - -static void -Json2SingleField(const NJson::TJsonValue& json, - google::protobuf::Message& proto, + } +} + +static void +Json2SingleField(const NJson::TJsonValue& json, + google::protobuf::Message& proto, const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config, bool isMapValue = false) { - using namespace google::protobuf; - + using namespace google::protobuf; + const Reflection* reflection = proto.GetReflection(); Y_ASSERT(!!reflection); @@ -188,12 +188,12 @@ Json2SingleField(const NJson::TJsonValue& json, } return; - } - } - + } + } + const NJson::TJsonValue& fieldJson = name ? json[name] : json; - - switch (field.cpp_type()) { + + switch (field.cpp_type()) { JSON_TO_FIELD(CPPTYPE_INT32, field.name(), fieldJson, IsInteger, SetInt32, GetInteger); JSON_TO_FIELD(CPPTYPE_INT64, field.name(), fieldJson, IsInteger, SetInt64, GetInteger); JSON_TO_FIELD(CPPTYPE_UINT32, field.name(), fieldJson, IsInteger, SetUInt32, GetInteger); @@ -201,7 +201,7 @@ Json2SingleField(const NJson::TJsonValue& json, JSON_TO_FIELD(CPPTYPE_DOUBLE, field.name(), fieldJson, IsDouble, SetDouble, GetDouble); JSON_TO_FIELD(CPPTYPE_FLOAT, field.name(), fieldJson, IsDouble, SetFloat, GetDouble); JSON_TO_FIELD(CPPTYPE_BOOL, field.name(), fieldJson, IsBoolean, SetBool, GetBoolean); - + case FieldDescriptor::CPPTYPE_STRING: { JsonString2Field(fieldJson, proto, field, config); break; @@ -211,22 +211,22 @@ Json2SingleField(const NJson::TJsonValue& json, JsonEnum2Field(fieldJson, proto, field, config); break; } - + case FieldDescriptor::CPPTYPE_MESSAGE: { Message* innerProto = reflection->MutableMessage(&proto, &field); Y_ASSERT(!!innerProto); NProtobufJson::MergeJson2Proto(fieldJson, *innerProto, config); - + break; } - + default: ythrow yexception() << "Unknown protobuf field type: " << static_cast<int>(field.cpp_type()) << "."; - } -} - -static void + } +} + +static void SetKey(NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, const TString& key) { @@ -312,20 +312,20 @@ Json2RepeatedFieldValue(const NJson::TJsonValue& jsonValue, } static void -Json2RepeatedField(const NJson::TJsonValue& json, - google::protobuf::Message& proto, +Json2RepeatedField(const NJson::TJsonValue& json, + google::protobuf::Message& proto, const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config) { - using namespace google::protobuf; - + using namespace google::protobuf; + TString name = GetFieldName(field, config); if (!json.Has(name)) - return; - + return; + const NJson::TJsonValue& fieldJson = json[name]; if (fieldJson.GetType() == NJson::JSON_UNDEFINED || fieldJson.GetType() == NJson::JSON_NULL) - return; - + return; + bool isMap = fieldJson.GetType() == NJson::JSON_MAP; if (isMap) { if (!config.MapAsObject) { @@ -336,15 +336,15 @@ Json2RepeatedField(const NJson::TJsonValue& json, } if (fieldJson.GetType() != NJson::JSON_ARRAY && !config.MapAsObject && !config.VectorizeScalars && !config.ValueVectorizer) { - ythrow yexception() << "JSON field doesn't represent an array for " + ythrow yexception() << "JSON field doesn't represent an array for " << name - << "(actual type is " - << static_cast<int>(fieldJson.GetType()) << ")."; - } - - const Reflection* reflection = proto.GetReflection(); + << "(actual type is " + << static_cast<int>(fieldJson.GetType()) << ")."; + } + + const Reflection* reflection = proto.GetReflection(); Y_ASSERT(!!reflection); - + if (isMap) { const THashMap<TString, NJson::TJsonValue> jsonMap = fieldJson.GetMap(); for (const auto& x : jsonMap) { @@ -367,16 +367,16 @@ Json2RepeatedField(const NJson::TJsonValue& json, } } else if (config.VectorizeScalars) { Json2RepeatedFieldValue(fieldJson, proto, field, config, reflection); - } - } -} - -namespace NProtobufJson { + } + } +} + +namespace NProtobufJson { void MergeJson2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { if (json.IsNull()) { return; } - + Y_ENSURE(json.IsMap(), "expected json map"); const google::protobuf::Descriptor* descriptor = proto.GetDescriptor(); @@ -385,7 +385,7 @@ namespace NProtobufJson { for (int f = 0, endF = descriptor->field_count(); f < endF; ++f) { const google::protobuf::FieldDescriptor* field = descriptor->field(f); Y_ASSERT(!!field); - + if (field->is_repeated()) { Json2RepeatedField(json, proto, *field, config); } else { @@ -404,7 +404,7 @@ namespace NProtobufJson { } } } - + void MergeJson2Proto(const TStringBuf& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { NJson::TJsonReaderConfig jsonCfg; jsonCfg.DontValidateUtf8 = true; diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h index 4c33498dfa..343502aab2 100644 --- a/library/cpp/protobuf/json/json2proto.h +++ b/library/cpp/protobuf/json/json2proto.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include "string_transform.h" #include "name_generator.h" @@ -9,18 +9,18 @@ #include <util/stream/input.h> #include <util/stream/str.h> #include <util/stream/mem.h> - + namespace google { namespace protobuf { class Message; } } -namespace NProtobufJson { +namespace NProtobufJson { struct TJson2ProtoConfig { using TSelf = TJson2ProtoConfig; using TValueVectorizer = std::function<NJson::TJsonValue::TArray(const NJson::TJsonValue& jsonValue)>; - + enum FldNameMode { FieldNameOriginalCase = 0, // default FieldNameLowerCase, @@ -167,7 +167,7 @@ namespace NProtobufJson { /// @throw yexception void Json2Proto(const TStringBuf& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config = TJson2ProtoConfig()); - + /// @throw yexception inline void Json2Proto(const TString& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config = TJson2ProtoConfig()) { diff --git a/library/cpp/protobuf/json/proto2json.cpp b/library/cpp/protobuf/json/proto2json.cpp index 3d76a91686..662172caaa 100644 --- a/library/cpp/protobuf/json/proto2json.cpp +++ b/library/cpp/protobuf/json/proto2json.cpp @@ -1,25 +1,25 @@ -#include "proto2json.h" - +#include "proto2json.h" + #include "json_output_create.h" #include "proto2json_printer.h" #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_value.h> #include <library/cpp/json/json_writer.h> - -#include <util/generic/ptr.h> -#include <util/generic/strbuf.h> -#include <util/stream/output.h> -#include <util/stream/str.h> -#include <util/system/yassert.h> - + +#include <util/generic/ptr.h> +#include <util/generic/strbuf.h> +#include <util/stream/output.h> +#include <util/stream/str.h> +#include <util/system/yassert.h> + namespace NProtobufJson { void Proto2Json(const NProtoBuf::Message& proto, IJsonOutput& jsonOutput, const TProto2JsonConfig& config, bool closeMap) { TProto2JsonPrinter printer(config); printer.Print(proto, jsonOutput, closeMap); } - + void Proto2Json(const NProtoBuf::Message& proto, NJson::TJsonValue& json, const TProto2JsonConfig& config) { Proto2Json(proto, *CreateJsonMapOutput(json), config); @@ -30,27 +30,27 @@ namespace NProtobufJson { Proto2Json(proto, *CreateJsonMapOutput(writer), config); writer.Flush(); } - + void Proto2Json(const NProtoBuf::Message& proto, IOutputStream& out, const TProto2JsonConfig& config) { Proto2Json(proto, *CreateJsonMapOutput(out, config), config); } - + void Proto2Json(const NProtoBuf::Message& proto, TStringStream& out, const TProto2JsonConfig& config) { Proto2Json(proto, *CreateJsonMapOutput(out, config), config); } - + void Proto2Json(const NProtoBuf::Message& proto, TString& str, const TProto2JsonConfig& config) { Proto2Json(proto, *CreateJsonMapOutput(str, config), config); } - + TString Proto2Json(const ::NProtoBuf::Message& proto, const TProto2JsonConfig& config) { TString res; Proto2Json(proto, res, config); return res; } - + } diff --git a/library/cpp/protobuf/json/proto2json.h b/library/cpp/protobuf/json/proto2json.h index 89a1781a40..7381297b56 100644 --- a/library/cpp/protobuf/json/proto2json.h +++ b/library/cpp/protobuf/json/proto2json.h @@ -1,38 +1,38 @@ -#pragma once - +#pragma once + #include "config.h" #include "json_output.h" - + #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/message.h> #include <util/generic/fwd.h> -#include <util/generic/vector.h> +#include <util/generic/vector.h> #include <util/generic/yexception.h> #include <util/stream/str.h> - + #include <functional> - -namespace NJson { + +namespace NJson { class TJsonValue; class TJsonWriter; } - + class IOutputStream; -class TStringStream; - -namespace NProtobufJson { +class TStringStream; + +namespace NProtobufJson { void Proto2Json(const NProtoBuf::Message& proto, IJsonOutput& jsonOutput, const TProto2JsonConfig& config = TProto2JsonConfig(), bool closeMap = true); - + void Proto2Json(const NProtoBuf::Message& proto, NJson::TJsonWriter& writer, const TProto2JsonConfig& config = TProto2JsonConfig()); - + /// @throw yexception void Proto2Json(const NProtoBuf::Message& proto, NJson::TJsonValue& json, const TProto2JsonConfig& config = TProto2JsonConfig()); - + /// @throw yexception void Proto2Json(const NProtoBuf::Message& proto, IOutputStream& out, const TProto2JsonConfig& config); @@ -41,7 +41,7 @@ namespace NProtobufJson { inline void Proto2Json(const T& proto, IOutputStream& out) { out << proto.AsJSON(); } - + // TStringStream deserves a special overload as its operator TString() would cause ambiguity /// @throw yexception void Proto2Json(const NProtoBuf::Message& proto, TStringStream& out, @@ -62,7 +62,7 @@ namespace NProtobufJson { TStringOutput out(str); out << proto.AsJSON(); } - + /// @throw yexception TString Proto2Json(const NProtoBuf::Message& proto, const TProto2JsonConfig& config); @@ -74,5 +74,5 @@ namespace NProtobufJson { Proto2Json(proto, result); return result; } - + } diff --git a/library/cpp/protobuf/json/string_transform.cpp b/library/cpp/protobuf/json/string_transform.cpp index 7c42daa677..0931f47f53 100644 --- a/library/cpp/protobuf/json/string_transform.cpp +++ b/library/cpp/protobuf/json/string_transform.cpp @@ -1,18 +1,18 @@ -#include "string_transform.h" - +#include "string_transform.h" + #include <google/protobuf/stubs/strutil.h> - + #include <library/cpp/string_utils/base64/base64.h> -namespace NProtobufJson { +namespace NProtobufJson { void TCEscapeTransform::Transform(TString& str) const { str = google::protobuf::CEscape(str); } - + void TSafeUtf8CEscapeTransform::Transform(TString& str) const { str = google::protobuf::strings::Utf8SafeCEscape(str); } - + void TDoubleEscapeTransform::Transform(TString& str) const { TString escaped = google::protobuf::CEscape(str); str = ""; @@ -22,7 +22,7 @@ namespace NProtobufJson { str += *it; } } - + void TDoubleUnescapeTransform::Transform(TString& str) const { str = google::protobuf::UnescapeCEscapeString(Unescape(str)); } diff --git a/library/cpp/protobuf/json/string_transform.h b/library/cpp/protobuf/json/string_transform.h index e4b296bc01..3676c0eae5 100644 --- a/library/cpp/protobuf/json/string_transform.h +++ b/library/cpp/protobuf/json/string_transform.h @@ -1,26 +1,26 @@ -#pragma once - +#pragma once + #include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h> -#include <util/generic/ptr.h> -#include <util/generic/refcount.h> - -namespace NProtobufJson { +#include <util/generic/ptr.h> +#include <util/generic/refcount.h> + +namespace NProtobufJson { class IStringTransform: public TSimpleRefCount<IStringTransform> { public: virtual ~IStringTransform() { } - + /// Some transforms have special meaning. /// For example, escape transforms cause generic JSON escaping to be turned off. enum Type { EscapeTransform = 0x1, }; - + virtual int GetType() const = 0; - + /// This method is called for each string field in proto virtual void Transform(TString& str) const = 0; - + /// This method is called for each bytes field in proto virtual void TransformBytes(TString& str) const { // Default behaviour is to apply string transform @@ -29,45 +29,45 @@ namespace NProtobufJson { }; using TStringTransformPtr = TIntrusivePtr<IStringTransform>; - + template <bool quote, bool tounicode> class TEscapeJTransform: public IStringTransform { public: int GetType() const override { return EscapeTransform; } - + void Transform(TString& str) const override { TString newStr; NEscJ::EscapeJ<quote, tounicode>(str, newStr); str = newStr; } }; - + class TCEscapeTransform: public IStringTransform { public: int GetType() const override { return EscapeTransform; } - + void Transform(TString& str) const override; }; - + class TSafeUtf8CEscapeTransform: public IStringTransform { public: int GetType() const override { return EscapeTransform; } - + void Transform(TString& str) const override; }; - + class TDoubleEscapeTransform: public IStringTransform { public: int GetType() const override { return EscapeTransform; } - + void Transform(TString& str) const override; }; diff --git a/library/cpp/protobuf/json/ut/fields.incl b/library/cpp/protobuf/json/ut/fields.incl index 4b22985836..de255a08da 100644 --- a/library/cpp/protobuf/json/ut/fields.incl +++ b/library/cpp/protobuf/json/ut/fields.incl @@ -1,22 +1,22 @@ -// Intentionally no #pragma once - -// (Field name == JSON key, Value) -DEFINE_FIELD(I32, Min<i32>()) -DEFINE_FIELD(I64, Min<i64>()) -DEFINE_FIELD(UI32, Max<ui32>()) -DEFINE_FIELD(UI64, Max<ui64>()) -DEFINE_FIELD(SI32, Min<i32>()) -DEFINE_FIELD(SI64, Min<i64>()) -DEFINE_FIELD(FI32, Max<ui32>()) -DEFINE_FIELD(FI64, Max<ui64>()) -DEFINE_FIELD(SFI32, Min<i32>()) -DEFINE_FIELD(SFI64, Min<i64>()) -DEFINE_FIELD(Bool, true) -DEFINE_FIELD(String, "Lorem ipsum") -DEFINE_FIELD(Bytes, "מחשב") -DEFINE_FIELD(Enum, E_1) -DEFINE_FIELD(Float, 1.123f) -DEFINE_FIELD(Double, 1.123456789012) +// Intentionally no #pragma once + +// (Field name == JSON key, Value) +DEFINE_FIELD(I32, Min<i32>()) +DEFINE_FIELD(I64, Min<i64>()) +DEFINE_FIELD(UI32, Max<ui32>()) +DEFINE_FIELD(UI64, Max<ui64>()) +DEFINE_FIELD(SI32, Min<i32>()) +DEFINE_FIELD(SI64, Min<i64>()) +DEFINE_FIELD(FI32, Max<ui32>()) +DEFINE_FIELD(FI64, Max<ui64>()) +DEFINE_FIELD(SFI32, Min<i32>()) +DEFINE_FIELD(SFI64, Min<i64>()) +DEFINE_FIELD(Bool, true) +DEFINE_FIELD(String, "Lorem ipsum") +DEFINE_FIELD(Bytes, "מחשב") +DEFINE_FIELD(Enum, E_1) +DEFINE_FIELD(Float, 1.123f) +DEFINE_FIELD(Double, 1.123456789012) DEFINE_FIELD(OneString, "Lorem ipsum dolor") DEFINE_FIELD(OneTwoString, "Lorem ipsum dolor sit") DEFINE_FIELD(ABC, "abc") diff --git a/library/cpp/protobuf/json/ut/json.h b/library/cpp/protobuf/json/ut/json.h index c1f108e6e4..4f942f347b 100644 --- a/library/cpp/protobuf/json/ut/json.h +++ b/library/cpp/protobuf/json/ut/json.h @@ -1,34 +1,34 @@ -#pragma once - +#pragma once + #include <library/cpp/protobuf/json/ut/test.pb.h> - + #include <library/cpp/json/json_value.h> - -#include <cstdarg> - -#include <util/generic/hash_set.h> + +#include <cstdarg> + +#include <util/generic/hash_set.h> #include <util/generic/string.h> - -#include <util/system/defaults.h> - -namespace NProtobufJsonTest { + +#include <util/system/defaults.h> + +namespace NProtobufJsonTest { inline NJson::TJsonValue CreateFlatJson(const THashSet<TString>& skippedKeys = THashSet<TString>()) { NJson::TJsonValue json; - + #define DEFINE_FIELD(name, value) \ if (skippedKeys.find(#name) == skippedKeys.end()) \ - json.InsertValue(#name, value); -#include "fields.incl" -#undef DEFINE_FIELD - + json.InsertValue(#name, value); +#include "fields.incl" +#undef DEFINE_FIELD + return json; } - + inline NJson::TJsonValue CreateRepeatedFlatJson(const THashSet<TString>& skippedKeys = THashSet<TString>()) { NJson::TJsonValue json; - + #define DEFINE_REPEATED_FIELD(name, type, ...) \ if (skippedKeys.find(#name) == skippedKeys.end()) { \ type values[] = {__VA_ARGS__}; \ @@ -37,22 +37,22 @@ namespace NProtobufJsonTest { array.AppendValue(values[i]); \ } \ json.InsertValue(#name, array); \ - } -#include "repeated_fields.incl" -#undef DEFINE_REPEATED_FIELD - + } +#include "repeated_fields.incl" +#undef DEFINE_REPEATED_FIELD + return json; } - + inline NJson::TJsonValue CreateCompositeJson(const THashSet<TString>& skippedKeys = THashSet<TString>()) { const NJson::TJsonValue& part = CreateFlatJson(skippedKeys); NJson::TJsonValue json; json.InsertValue("Part", part); - + return json; } - + #define UNIT_ASSERT_JSONS_EQUAL(lhs, rhs) \ if (lhs != rhs) { \ UNIT_ASSERT_STRINGS_EQUAL(lhs.GetStringRobust(), rhs.GetStringRobust()); \ diff --git a/library/cpp/protobuf/json/ut/json2proto_ut.cpp b/library/cpp/protobuf/json/ut/json2proto_ut.cpp index 0dfe57bc7a..4752d2dfa6 100644 --- a/library/cpp/protobuf/json/ut/json2proto_ut.cpp +++ b/library/cpp/protobuf/json/ut/json2proto_ut.cpp @@ -1,28 +1,28 @@ -#include "json.h" -#include "proto.h" +#include "json.h" +#include "proto.h" #include "proto2json.h" - + #include <library/cpp/protobuf/json/ut/test.pb.h> #include <library/cpp/json/json_value.h> #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_writer.h> - + #include <library/cpp/protobuf/json/json2proto.h> - + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/hash_set.h> + +#include <util/generic/hash_set.h> #include <util/generic/string.h> -#include <util/generic/ylimits.h> -#include <util/stream/str.h> +#include <util/generic/ylimits.h> +#include <util/stream/str.h> #include <util/string/cast.h> -#include <util/system/defaults.h> -#include <util/system/yassert.h> - -using namespace NProtobufJson; -using namespace NProtobufJsonTest; - +#include <util/system/defaults.h> +#include <util/system/yassert.h> + +using namespace NProtobufJson; +using namespace NProtobufJsonTest; + namespace google { namespace protobuf { namespace internal { @@ -79,7 +79,7 @@ Y_UNIT_TEST_SUITE(TJson2ProtoTest) { FillFlatProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -93,9 +93,9 @@ Y_UNIT_TEST_SUITE(TJson2ProtoTest) { UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestFlatOptional - + Y_UNIT_TEST(TestFlatRequired){ {const NJson::TJsonValue& json = CreateFlatJson(); TFlatRequired proto; @@ -104,7 +104,7 @@ TFlatRequired modelProto; FillFlatProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -115,9 +115,9 @@ UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); UNIT_ASSERT_EXCEPTION(Json2Proto(json, proto), yexception); \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestFlatRequired - + Y_UNIT_TEST(TestNameGenerator) { TJson2ProtoConfig cfg; cfg.SetNameGenerator([](const NProtoBuf::FieldDescriptor&) { return "42"; }); @@ -165,7 +165,7 @@ TFlatRepeated modelProto; FillRepeatedProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + // Try to skip each field #define DEFINE_REPEATED_FIELD(name, ...) \ { \ @@ -179,9 +179,9 @@ UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); \ } #include <library/cpp/protobuf/json/ut/repeated_fields.incl> -#undef DEFINE_REPEATED_FIELD +#undef DEFINE_REPEATED_FIELD } // TestFlatRepeated - + Y_UNIT_TEST(TestCompositeOptional){ {const NJson::TJsonValue& json = CreateCompositeJson(); TCompositeOptional proto; @@ -190,7 +190,7 @@ TCompositeOptional modelProto; FillCompositeProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -204,9 +204,9 @@ UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestCompositeOptional - + Y_UNIT_TEST(TestCompositeOptionalStringBuf){ {NJson::TJsonValue json = CreateCompositeJson(); json["Part"]["Double"] = 42.5; @@ -249,37 +249,37 @@ Y_UNIT_TEST(TestCompositeRequired) { FillCompositeProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + { NJson::TJsonValue json; TCompositeRequired proto; UNIT_ASSERT_EXCEPTION(Json2Proto(json, proto), yexception); } } // TestCompositeRequired - + Y_UNIT_TEST(TestCompositeRepeated) { { NJson::TJsonValue json; NJson::TJsonValue array; array.AppendValue(CreateFlatJson()); json.InsertValue("Part", array); - + TCompositeRepeated proto; Json2Proto(json, proto); - + TFlatOptional partModelProto; FillFlatProto(&partModelProto); TCompositeRepeated modelProto; modelProto.AddPart()->CopyFrom(partModelProto); - + UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + { // Array of messages with each field skipped TCompositeRepeated modelProto; NJson::TJsonValue array; - + #define DEFINE_REPEATED_FIELD(name, ...) \ { \ THashSet<TString> skippedField; \ @@ -290,18 +290,18 @@ Y_UNIT_TEST(TestCompositeRepeated) { array.AppendValue(CreateFlatJson(skippedField)); \ } #include <library/cpp/protobuf/json/ut/repeated_fields.incl> -#undef DEFINE_REPEATED_FIELD - +#undef DEFINE_REPEATED_FIELD + NJson::TJsonValue json; json.InsertValue("Part", array); - + TCompositeRepeated proto; Json2Proto(json, proto); - + UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } } // TestCompositeRepeated - + Y_UNIT_TEST(TestInvalidEnum) { { NJson::TJsonValue json; @@ -309,13 +309,13 @@ Y_UNIT_TEST(TestInvalidEnum) { TFlatOptional proto; UNIT_ASSERT_EXCEPTION(Json2Proto(json, proto), yexception); } - + { NJson::TJsonValue json; json.InsertValue("Enum", 100); TFlatOptional proto; UNIT_ASSERT_EXCEPTION(Json2Proto(json, proto), yexception); - } + } } Y_UNIT_TEST(TestFieldNameMode) { @@ -1144,4 +1144,4 @@ Y_UNIT_TEST(TestAllowComments) { UNIT_ASSERT_VALUES_EQUAL(proto.GetI64(), 3423); } // TestAllowComments -} // TJson2ProtoTest +} // TJson2ProtoTest diff --git a/library/cpp/protobuf/json/ut/proto.h b/library/cpp/protobuf/json/ut/proto.h index 8183bfc8e1..db27ed52c7 100644 --- a/library/cpp/protobuf/json/ut/proto.h +++ b/library/cpp/protobuf/json/ut/proto.h @@ -1,11 +1,11 @@ -#pragma once - -#include <util/generic/hash_set.h> +#pragma once + +#include <util/generic/hash_set.h> #include <util/generic/string.h> - -#include <util/system/defaults.h> - -namespace NProtobufJsonTest { + +#include <util/system/defaults.h> + +namespace NProtobufJsonTest { template <typename TProto> inline void FillFlatProto(TProto* proto, @@ -14,20 +14,20 @@ namespace NProtobufJsonTest { if (skippedFields.find(#name) == skippedFields.end()) \ proto->Set##name(value); #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } - + template <typename TRepeatedField, typename TValue> inline void AddValue(TRepeatedField* field, TValue value) { field->Add(value); } - + inline void AddValue(google::protobuf::RepeatedPtrField<TString>* field, const TString& value) { *(field->Add()) = value; } - + inline void FillRepeatedProto(TFlatRepeated* proto, const THashSet<TString>& skippedFields = THashSet<TString>()) { @@ -37,17 +37,17 @@ namespace NProtobufJsonTest { for (size_t i = 0, end = Y_ARRAY_SIZE(values); i < end; ++i) { \ AddValue(proto->Mutable##name(), values[i]); \ } \ - } + } #include <library/cpp/protobuf/json/ut/repeated_fields.incl> -#undef DEFINE_REPEATED_FIELD +#undef DEFINE_REPEATED_FIELD } - + template <typename TProto> inline void FillCompositeProto(TProto* proto, const THashSet<TString>& skippedFields = THashSet<TString>()) { FillFlatProto(proto->MutablePart(), skippedFields); } - + #define UNIT_ASSERT_PROTOS_EQUAL(lhs, rhs) \ do { \ if (lhs.SerializeAsString() != rhs.SerializeAsString()) { \ @@ -58,5 +58,5 @@ namespace NProtobufJsonTest { UNIT_ASSERT_STRINGS_EQUAL(lhs.SerializeAsString(), rhs.SerializeAsString()); \ } \ } while (false); - + } diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..0de60cbe8c 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -1,30 +1,30 @@ -#include "json.h" -#include "proto.h" - +#include "json.h" +#include "proto.h" + #include <library/cpp/protobuf/json/ut/test.pb.h> #include <library/cpp/json/json_value.h> #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_writer.h> - + #include <library/cpp/protobuf/json/proto2json.h> - + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/hash_set.h> + +#include <util/generic/hash_set.h> #include <util/generic/string.h> -#include <util/generic/ylimits.h> - -#include <util/stream/str.h> - -#include <util/system/defaults.h> -#include <util/system/yassert.h> - +#include <util/generic/ylimits.h> + +#include <util/stream/str.h> + +#include <util/system/defaults.h> +#include <util/system/yassert.h> + #include <limits> -using namespace NProtobufJson; -using namespace NProtobufJsonTest; - +using namespace NProtobufJson; +using namespace NProtobufJsonTest; + Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) { Y_UNIT_TEST(TestFlatDefault) { using namespace ::google::protobuf; @@ -109,7 +109,7 @@ Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) { UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TStringStream jsonStream; NJson::TJsonValue json; @@ -118,7 +118,7 @@ Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) { UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } // streamed } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -138,9 +138,9 @@ Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) { } \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestFlatOptional - + Y_UNIT_TEST(TestFlatRequired){ {TFlatRequired proto; FillFlatProto(&proto); @@ -150,7 +150,7 @@ const NJson::TJsonValue& modelJson = CreateFlatJson(); UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TStringStream jsonStream; NJson::TJsonValue json; @@ -159,7 +159,7 @@ const NJson::TJsonValue& modelJson = CreateFlatJson(); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } // streamed } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -179,20 +179,20 @@ const NJson::TJsonValue& modelJson = CreateFlatJson(); } \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestFlatRequired - + Y_UNIT_TEST(TestFlatRepeated) { { TFlatRepeated proto; FillRepeatedProto(&proto); const NJson::TJsonValue& modelJson = CreateRepeatedFlatJson(); - { + { NJson::TJsonValue json; UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); - } - + } + { TStringStream jsonStream; NJson::TJsonValue json; @@ -224,9 +224,9 @@ Y_UNIT_TEST(TestFlatRepeated) { } \ } #include <library/cpp/protobuf/json/ut/repeated_fields.incl> -#undef DEFINE_REPEATED_FIELD +#undef DEFINE_REPEATED_FIELD } // TestFlatRepeated - + Y_UNIT_TEST(TestCompositeOptional){ {TCompositeOptional proto; FillCompositeProto(&proto); @@ -236,7 +236,7 @@ const NJson::TJsonValue& modelJson = CreateCompositeJson(); UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TStringStream jsonStream; NJson::TJsonValue json; @@ -245,7 +245,7 @@ const NJson::TJsonValue& modelJson = CreateCompositeJson(); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } // streamed } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -265,9 +265,9 @@ const NJson::TJsonValue& modelJson = CreateCompositeJson(); } \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestCompositeOptional - + Y_UNIT_TEST(TestCompositeRequired){ {TCompositeRequired proto; FillCompositeProto(&proto); @@ -277,7 +277,7 @@ const NJson::TJsonValue& modelJson = CreateCompositeJson(); UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TStringStream jsonStream; NJson::TJsonValue json; @@ -286,7 +286,7 @@ const NJson::TJsonValue& modelJson = CreateCompositeJson(); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } // streamed } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -306,9 +306,9 @@ const NJson::TJsonValue& modelJson = CreateCompositeJson(); } \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestCompositeRequired - + Y_UNIT_TEST(TestCompositeRepeated) { { TFlatOptional partProto; @@ -320,13 +320,13 @@ Y_UNIT_TEST(TestCompositeRepeated) { NJson::TJsonValue modelArray; modelArray.AppendValue(CreateFlatJson()); modelJson.InsertValue("Part", modelArray); - { + { NJson::TJsonValue json; UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); - } - - { + } + + { TStringStream jsonStream; NJson::TJsonValue json; UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream)); @@ -334,7 +334,7 @@ Y_UNIT_TEST(TestCompositeRepeated) { UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } // streamed } - + { // Array of messages with each field skipped TCompositeRepeated proto; @@ -350,27 +350,27 @@ Y_UNIT_TEST(TestCompositeRepeated) { modelArray.AppendValue(CreateFlatJson(skippedField)); \ } #include <library/cpp/protobuf/json/ut/repeated_fields.incl> -#undef DEFINE_REPEATED_FIELD - +#undef DEFINE_REPEATED_FIELD + NJson::TJsonValue modelJson; modelJson.InsertValue("Part", modelArray); - - { - NJson::TJsonValue json; + + { + NJson::TJsonValue json; UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); - } - - { + } + + { TStringStream jsonStream; - NJson::TJsonValue json; + NJson::TJsonValue json; UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream)); UNIT_ASSERT(ReadJsonTree(&jsonStream, &json)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } // streamed } } // TestCompositeRepeated - + Y_UNIT_TEST(TestEnumConfig) { { TFlatOptional proto; @@ -380,11 +380,11 @@ Y_UNIT_TEST(TestEnumConfig) { NJson::TJsonValue json; TProto2JsonConfig config; config.EnumMode = TProto2JsonConfig::EnumNumber; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TFlatOptional proto; proto.SetEnum(E_1); @@ -393,11 +393,11 @@ Y_UNIT_TEST(TestEnumConfig) { NJson::TJsonValue json; TProto2JsonConfig config; config.EnumMode = TProto2JsonConfig::EnumName; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TFlatOptional proto; proto.SetEnum(E_1); @@ -406,11 +406,11 @@ Y_UNIT_TEST(TestEnumConfig) { NJson::TJsonValue json; TProto2JsonConfig config; config.EnumMode = TProto2JsonConfig::EnumFullName; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TFlatOptional proto; proto.SetEnum(E_1); @@ -419,11 +419,11 @@ Y_UNIT_TEST(TestEnumConfig) { NJson::TJsonValue json; TProto2JsonConfig config; config.EnumMode = TProto2JsonConfig::EnumNameLowerCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { TFlatOptional proto; proto.SetEnum(E_1); @@ -455,13 +455,13 @@ Y_UNIT_TEST(TestMissingSingleKeyConfig) { #define DEFINE_FIELD(name, value) \ modelJson.InsertValue(#name, NJson::TJsonValue(NJson::JSON_NULL)); #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD - +#undef DEFINE_FIELD + TFlatOptional proto; NJson::TJsonValue json; TProto2JsonConfig config; config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyNull; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } @@ -512,7 +512,7 @@ Y_UNIT_TEST(TestMissingSingleKeyConfig) { UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } } // TestMissingSingleKeyConfig - + Y_UNIT_TEST(TestMissingRepeatedKeyNoConfig) { { TFlatRepeated proto; @@ -531,23 +531,23 @@ Y_UNIT_TEST(TestMissingRepeatedKeyConfig) { NJson::TJsonValue json; TProto2JsonConfig config; config.MissingRepeatedKeyMode = TProto2JsonConfig::MissingKeySkip; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } - + { NJson::TJsonValue modelJson; #define DEFINE_FIELD(name, value) \ modelJson.InsertValue(#name, NJson::TJsonValue(NJson::JSON_NULL)); #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD - +#undef DEFINE_FIELD + TFlatRepeated proto; NJson::TJsonValue json; TProto2JsonConfig config; config.MissingRepeatedKeyMode = TProto2JsonConfig::MissingKeyNull; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } @@ -563,63 +563,63 @@ Y_UNIT_TEST(TestMissingRepeatedKeyConfig) { UNIT_ASSERT_JSONS_EQUAL(json, modelJson); } } // TestMissingRepeatedKeyConfig - + Y_UNIT_TEST(TestEscaping) { // No escape { TString modelStr(R"_({"String":"value\""})_"); - + TFlatOptional proto; proto.SetString(R"_(value")_"); TStringStream jsonStr; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // TEscapeJTransform { TString modelStr(R"_({"String":"value\""})_"); - + TFlatOptional proto; proto.SetString(R"_(value")_"); TProto2JsonConfig config; config.StringTransforms.push_back(new TEscapeJTransform<false, true>()); TStringStream jsonStr; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(modelStr, jsonStr.Str()); } - + // TCEscapeTransform { TString modelStr(R"_({"String":"value\""})_"); - + TFlatOptional proto; proto.SetString(R"_(value")_"); TProto2JsonConfig config; config.StringTransforms.push_back(new TCEscapeTransform()); TStringStream jsonStr; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // TSafeUtf8CEscapeTransform { TString modelStr(R"_({"String":"value\""})_"); - + TFlatOptional proto; proto.SetString(R"_(value")_"); TProto2JsonConfig config; config.StringTransforms.push_back(new TSafeUtf8CEscapeTransform()); TStringStream jsonStr; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } } // TestEscaping - + class TBytesTransform: public IStringTransform { public: int GetType() const override { @@ -666,58 +666,58 @@ Y_UNIT_TEST(TestFieldNameMode) { // Original case 1 { TString modelStr(R"_({"String":"value"})_"); - + TFlatOptional proto; proto.SetString("value"); TStringStream jsonStr; TProto2JsonConfig config; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // Original case 2 { TString modelStr(R"_({"String":"value"})_"); - + TFlatOptional proto; proto.SetString("value"); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameOriginalCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // Lowercase { TString modelStr(R"_({"string":"value"})_"); - + TFlatOptional proto; proto.SetString("value"); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameLowerCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // Uppercase { TString modelStr(R"_({"STRING":"value"})_"); - + TFlatOptional proto; proto.SetString("value"); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameUpperCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // Camelcase { TString modelStr(R"_({"string":"value"})_"); @@ -825,33 +825,33 @@ Y_UNIT_TEST(TestFieldNameMode) { // Original case, repeated { TString modelStr(R"_({"I32":[1,2]})_"); - + TFlatRepeated proto; proto.AddI32(1); proto.AddI32(2); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameOriginalCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // Lower case, repeated { TString modelStr(R"_({"i32":[1,2]})_"); - + TFlatRepeated proto; proto.AddI32(1); proto.AddI32(2); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameLowerCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // UseJsonName { // FIXME(CONTRIB-139): see the comment about UseJsonName in json2proto_ut.cpp: @@ -1019,4 +1019,4 @@ Y_UNIT_TEST(TestExtension) { UNIT_ASSERT_EQUAL(Proto2Json(proto, cfg), "{\"bar\":1}"); } // TestExtension -} // TProto2JsonTest +} // TProto2JsonTest diff --git a/library/cpp/protobuf/json/ut/repeated_fields.incl b/library/cpp/protobuf/json/ut/repeated_fields.incl index e9548917d8..53c1dc94a7 100644 --- a/library/cpp/protobuf/json/ut/repeated_fields.incl +++ b/library/cpp/protobuf/json/ut/repeated_fields.incl @@ -1,21 +1,21 @@ -// Intentionally no #pragma once - -// (Field name == JSON key, Type, Values...) -DEFINE_REPEATED_FIELD(I32, i32, Min<i32>(), -1, 0, 1, Max<i32>()) -DEFINE_REPEATED_FIELD(I64, i64, Min<i64>(), -1ll, 0ll, 1ll, Max<i64>()) -DEFINE_REPEATED_FIELD(UI32, ui32, 0ul, 1ul, Max<ui32>()) -DEFINE_REPEATED_FIELD(UI64, ui64, 0ull, 1ull, Max<ui64>()) -DEFINE_REPEATED_FIELD(SI32, i32, Min<i32>(), -1, 0, 1, Max<i32>()) -DEFINE_REPEATED_FIELD(SI64, i64, Min<i64>(), -1ll, 0ll, 1ll, Max<i64>()) -DEFINE_REPEATED_FIELD(FI32, ui32, 0, 1, Max<ui32>()) -DEFINE_REPEATED_FIELD(FI64, ui64, 0ull, 1ull, Max<ui64>()) -DEFINE_REPEATED_FIELD(SFI32, i32, Min<i32>(), -1, 0, 1, Max<i32>()) -DEFINE_REPEATED_FIELD(SFI64, i64, Min<i64>(), -1ll, 0ll, 1ll, Max<i64>()) -DEFINE_REPEATED_FIELD(Bool, bool, false, true) +// Intentionally no #pragma once + +// (Field name == JSON key, Type, Values...) +DEFINE_REPEATED_FIELD(I32, i32, Min<i32>(), -1, 0, 1, Max<i32>()) +DEFINE_REPEATED_FIELD(I64, i64, Min<i64>(), -1ll, 0ll, 1ll, Max<i64>()) +DEFINE_REPEATED_FIELD(UI32, ui32, 0ul, 1ul, Max<ui32>()) +DEFINE_REPEATED_FIELD(UI64, ui64, 0ull, 1ull, Max<ui64>()) +DEFINE_REPEATED_FIELD(SI32, i32, Min<i32>(), -1, 0, 1, Max<i32>()) +DEFINE_REPEATED_FIELD(SI64, i64, Min<i64>(), -1ll, 0ll, 1ll, Max<i64>()) +DEFINE_REPEATED_FIELD(FI32, ui32, 0, 1, Max<ui32>()) +DEFINE_REPEATED_FIELD(FI64, ui64, 0ull, 1ull, Max<ui64>()) +DEFINE_REPEATED_FIELD(SFI32, i32, Min<i32>(), -1, 0, 1, Max<i32>()) +DEFINE_REPEATED_FIELD(SFI64, i64, Min<i64>(), -1ll, 0ll, 1ll, Max<i64>()) +DEFINE_REPEATED_FIELD(Bool, bool, false, true) DEFINE_REPEATED_FIELD(String, TString, "", "Lorem ipsum", "123123") DEFINE_REPEATED_FIELD(Bytes, TString, "", "מחשב", "\x1") -DEFINE_REPEATED_FIELD(Enum, EEnum, E_1, E_2, E_3) -DEFINE_REPEATED_FIELD(Float, float, 0.0f, 1.0f, 1.123f) -DEFINE_REPEATED_FIELD(Double, double, 0.0, 1.0, 1.123456789012) +DEFINE_REPEATED_FIELD(Enum, EEnum, E_1, E_2, E_3) +DEFINE_REPEATED_FIELD(Float, float, 0.0f, 1.0f, 1.123f) +DEFINE_REPEATED_FIELD(Double, double, 0.0, 1.0, 1.123456789012) DEFINE_REPEATED_FIELD(OneString, TString, "", "Lorem ipsum dolor", "1231231") DEFINE_REPEATED_FIELD(OneTwoString, TString, "", "Lorem ipsum dolor sit", "12312312") diff --git a/library/cpp/protobuf/json/ut/test.proto b/library/cpp/protobuf/json/ut/test.proto index 0fa996fd41..fab7b6a5f2 100644 --- a/library/cpp/protobuf/json/ut/test.proto +++ b/library/cpp/protobuf/json/ut/test.proto @@ -1,96 +1,96 @@ -package NProtobufJsonTest; - -enum EEnum { +package NProtobufJsonTest; + +enum EEnum { E_0 = 0; - E_1 = 1; - E_2 = 2; - E_3 = 3; -}; - -message TFlatOptional { - optional int32 I32 = 1; - optional int64 I64 = 2; - optional uint32 UI32 = 3; - optional uint64 UI64 = 4; - optional sint32 SI32 = 5; - optional sint64 SI64 = 6; - optional fixed32 FI32 = 7; - optional fixed64 FI64 = 8; - optional sfixed32 SFI32 = 9; - optional sfixed64 SFI64 = 10; - - optional bool Bool = 11; - - optional string String = 12; - optional bytes Bytes = 13; - - optional EEnum Enum = 14; - - optional float Float = 15; - optional double Double = 16; + E_1 = 1; + E_2 = 2; + E_3 = 3; +}; + +message TFlatOptional { + optional int32 I32 = 1; + optional int64 I64 = 2; + optional uint32 UI32 = 3; + optional uint64 UI64 = 4; + optional sint32 SI32 = 5; + optional sint64 SI64 = 6; + optional fixed32 FI32 = 7; + optional fixed64 FI64 = 8; + optional sfixed32 SFI32 = 9; + optional sfixed64 SFI64 = 10; + + optional bool Bool = 11; + + optional string String = 12; + optional bytes Bytes = 13; + + optional EEnum Enum = 14; + + optional float Float = 15; + optional double Double = 16; optional string OneString = 17; optional string OneTwoString = 18; optional string ABC = 19; optional string UserID = 20; -}; - -message TFlatRequired { - required int32 I32 = 1; - required int64 I64 = 2; - required uint32 UI32 = 3; - required uint64 UI64 = 4; - required sint32 SI32 = 5; - required sint64 SI64 = 6; - required fixed32 FI32 = 7; - required fixed64 FI64 = 8; - required sfixed32 SFI32 = 9; - required sfixed64 SFI64 = 10; - - required bool Bool = 11; - - required string String = 12; - required bytes Bytes = 13; - - required EEnum Enum = 14; - - required float Float = 15; - required double Double = 16; +}; + +message TFlatRequired { + required int32 I32 = 1; + required int64 I64 = 2; + required uint32 UI32 = 3; + required uint64 UI64 = 4; + required sint32 SI32 = 5; + required sint64 SI64 = 6; + required fixed32 FI32 = 7; + required fixed64 FI64 = 8; + required sfixed32 SFI32 = 9; + required sfixed64 SFI64 = 10; + + required bool Bool = 11; + + required string String = 12; + required bytes Bytes = 13; + + required EEnum Enum = 14; + + required float Float = 15; + required double Double = 16; required string OneString = 17; required string OneTwoString = 18; required string ABC = 19; required string UserID = 20; -}; - -message TFlatRepeated { - repeated int32 I32 = 1; - repeated int64 I64 = 2; - repeated uint32 UI32 = 3; - repeated uint64 UI64 = 4; - repeated sint32 SI32 = 5; - repeated sint64 SI64 = 6; - repeated fixed32 FI32 = 7; - repeated fixed64 FI64 = 8; - repeated sfixed32 SFI32 = 9; - repeated sfixed64 SFI64 = 10; - - repeated bool Bool = 11; - - repeated string String = 12; - repeated bytes Bytes = 13; - - repeated EEnum Enum = 14; - - repeated float Float = 15; - repeated double Double = 16; +}; + +message TFlatRepeated { + repeated int32 I32 = 1; + repeated int64 I64 = 2; + repeated uint32 UI32 = 3; + repeated uint64 UI64 = 4; + repeated sint32 SI32 = 5; + repeated sint64 SI64 = 6; + repeated fixed32 FI32 = 7; + repeated fixed64 FI64 = 8; + repeated sfixed32 SFI32 = 9; + repeated sfixed64 SFI64 = 10; + + repeated bool Bool = 11; + + repeated string String = 12; + repeated bytes Bytes = 13; + + repeated EEnum Enum = 14; + + repeated float Float = 15; + repeated double Double = 16; repeated string OneString = 17; repeated string OneTwoString = 18; repeated string ABC = 19; repeated string UserID = 20; -}; - +}; + message TFlatDefault { optional int32 I32 = 1 [default = 132]; optional int64 I64 = 2 [default = 164]; @@ -119,17 +119,17 @@ message TFlatDefault { optional string UserID = 20 [default = "some_id"]; }; -message TCompositeOptional { - optional TFlatOptional Part = 1; -}; - -message TCompositeRequired { - required TFlatRequired Part = 1; -}; - -message TCompositeRepeated { - repeated TFlatOptional Part = 1; -}; +message TCompositeOptional { + optional TFlatOptional Part = 1; +}; + +message TCompositeRequired { + required TFlatRequired Part = 1; +}; + +message TCompositeRepeated { + repeated TFlatOptional Part = 1; +}; message TMapType { map<string, string> Items = 1; diff --git a/library/cpp/protobuf/json/ut/ya.make b/library/cpp/protobuf/json/ut/ya.make index b60a6d3c17..8f224e22fa 100644 --- a/library/cpp/protobuf/json/ut/ya.make +++ b/library/cpp/protobuf/json/ut/ya.make @@ -1,23 +1,23 @@ UNITTEST_FOR(library/cpp/protobuf/json) - + OWNER(avitella) -SRCS( +SRCS( filter_ut.cpp - json2proto_ut.cpp - proto2json_ut.cpp + json2proto_ut.cpp + proto2json_ut.cpp inline_ut.proto inline_ut.cpp string_transform_ut.cpp filter_ut.proto test.proto util_ut.cpp -) - +) + GENERATE_ENUM_SERIALIZATION(test.pb.h) PEERDIR( library/cpp/protobuf/json ) - -END() + +END() diff --git a/library/cpp/protobuf/json/ya.make b/library/cpp/protobuf/json/ya.make index 2f2c75cfdb..49e073c89b 100644 --- a/library/cpp/protobuf/json/ya.make +++ b/library/cpp/protobuf/json/ya.make @@ -1,25 +1,25 @@ -LIBRARY() - +LIBRARY() + OWNER(avitella) - -SRCS( - json2proto.cpp + +SRCS( + json2proto.cpp json_output_create.cpp json_value_output.cpp json_writer_output.cpp name_generator.cpp proto2json.cpp proto2json_printer.cpp - string_transform.cpp + string_transform.cpp util.h util.cpp -) - -PEERDIR( - contrib/libs/protobuf +) + +PEERDIR( + contrib/libs/protobuf library/cpp/json library/cpp/protobuf/util library/cpp/string_utils/relaxed_escaper -) - -END() +) + +END() |