diff options
| author | v01d <[email protected]> | 2022-02-10 16:49:40 +0300 | 
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:49:40 +0300 | 
| commit | fa8b0420162dd36d4f569fdc3f63da0bef8bb8c7 (patch) | |
| tree | ecec7ec14d9261b76724eeb69ab158c45a970dc4 /library/cpp/protobuf | |
| parent | ac559bd4bb6b11b17dce999cf58c05e08f6087a2 (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf')
| -rw-r--r-- | library/cpp/protobuf/json/config.h | 8 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/json2proto.cpp | 196 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/json2proto.h | 6 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.cpp | 742 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.h | 80 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/ut/fields.incl | 4 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/ut/json2proto_ut.cpp | 152 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/ut/proto2json_ut.cpp | 142 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/ut/test.proto | 66 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/util.cpp | 102 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/util.h | 20 | ||||
| -rw-r--r-- | library/cpp/protobuf/json/ya.make | 4 | 
12 files changed, 761 insertions, 761 deletions
| diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index dc84fb4d5db..9b39c3bd5b5 100644 --- a/library/cpp/protobuf/json/config.h +++ b/library/cpp/protobuf/json/config.h @@ -50,8 +50,8 @@ namespace NProtobufJson {              FieldNameLowerCase,              FieldNameUpperCase,              FieldNameCamelCase, -            FieldNameSnakeCase,     // ABC -> a_b_c,    UserID -> user_i_d -            FieldNameSnakeCaseDense // ABC -> abc,      UserID -> user_id +            FieldNameSnakeCase,     // ABC -> a_b_c,    UserID -> user_i_d  +            FieldNameSnakeCaseDense // ABC -> abc,      UserID -> user_id           };          FldNameMode FieldNameMode = FieldNameOriginalCase; @@ -79,7 +79,7 @@ namespace NProtobufJson {              StringifyLongNumbersForDouble,          };          EStringifyLongNumbersMode StringifyLongNumbers = StringifyLongNumbersNever; - +           /// Custom field names generator.          TNameGenerator NameGenerator = {}; @@ -139,7 +139,7 @@ namespace NProtobufJson {              MapAsObject = value;              return *this;          } - +           TSelf& SetStringifyLongNumbers(EStringifyLongNumbersMode stringify) {              StringifyLongNumbers = stringify;              return *this; diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp index 640c10f5a51..fa80d6c3975 100644 --- a/library/cpp/protobuf/json/json2proto.cpp +++ b/library/cpp/protobuf/json/json2proto.cpp @@ -1,5 +1,5 @@  #include "json2proto.h" -#include "util.h" +#include "util.h"   #include <library/cpp/json/json_value.h> @@ -65,12 +65,12 @@ static TString GetFieldName(const google::protobuf::FieldDescriptor& field,                  name[0] = AsciiToLower(name[0]);              }              break; -        case NProtobufJson::TJson2ProtoConfig::FieldNameSnakeCase: -            NProtobufJson::ToSnakeCase(&name); -            break; -        case NProtobufJson::TJson2ProtoConfig::FieldNameSnakeCaseDense: -            NProtobufJson::ToSnakeCaseDense(&name); -            break; +        case NProtobufJson::TJson2ProtoConfig::FieldNameSnakeCase:  +            NProtobufJson::ToSnakeCase(&name);  +            break;  +        case NProtobufJson::TJson2ProtoConfig::FieldNameSnakeCaseDense:  +            NProtobufJson::ToSnakeCaseDense(&name);  +            break;           default:              Y_VERIFY_DEBUG(false, "Unknown FieldNameMode.");      } @@ -171,27 +171,27 @@ static void  Json2SingleField(const NJson::TJsonValue& json,                   google::protobuf::Message& proto,                   const google::protobuf::FieldDescriptor& field, -                 const NProtobufJson::TJson2ProtoConfig& config, +                 const NProtobufJson::TJson2ProtoConfig& config,                    bool isMapValue = false) {      using namespace google::protobuf;      const Reflection* reflection = proto.GetReflection();      Y_ASSERT(!!reflection); -    TString name; -    if (!isMapValue) { -        name = GetFieldName(field, config); +    TString name;  +    if (!isMapValue) {  +        name = GetFieldName(field, config);           if (!json.Has(name) || json[name].GetType() == NJson::JSON_UNDEFINED || json[name].GetType() == NJson::JSON_NULL) {              if (field.is_required() && !field.has_default_value() && !reflection->HasField(proto, &field) && config.CheckRequiredFields) { -                ythrow yexception() << "JSON has no field for required field " -                                    << name << "."; -            } - -            return; +                ythrow yexception() << "JSON has no field for required field "  +                                    << name << ".";  +            }  +  +            return;           }      } -    const NJson::TJsonValue& fieldJson = name ? json[name] : json; +    const NJson::TJsonValue& fieldJson = name ? json[name] : json;       switch (field.cpp_type()) {          JSON_TO_FIELD(CPPTYPE_INT32, field.name(), fieldJson, IsInteger, SetInt32, GetInteger); @@ -227,48 +227,48 @@ Json2SingleField(const NJson::TJsonValue& json,  }  static void -SetKey(NProtoBuf::Message& proto, -       const NProtoBuf::FieldDescriptor& field, -       const TString& key) { -    using namespace google::protobuf; -    using namespace NProtobufJson; - -    const Reflection* reflection = proto.GetReflection(); -    TString result; -    switch (field.cpp_type()) { -        case FieldDescriptor::CPPTYPE_INT32: -            reflection->SetInt32(&proto, &field, FromString<int32>(key)); -            break; -        case FieldDescriptor::CPPTYPE_INT64: -            reflection->SetInt64(&proto, &field, FromString<int64>(key)); -            break; -        case FieldDescriptor::CPPTYPE_UINT32: -            reflection->SetUInt32(&proto, &field, FromString<uint32>(key)); -            break; -        case FieldDescriptor::CPPTYPE_UINT64: -            reflection->SetUInt64(&proto, &field, FromString<uint64>(key)); -            break; -        case FieldDescriptor::CPPTYPE_BOOL: -            reflection->SetBool(&proto, &field, FromString<bool>(key)); -            break; -        case FieldDescriptor::CPPTYPE_STRING: -            reflection->SetString(&proto, &field, key); -            break; -        default: -            ythrow yexception() << "Unsupported key type."; -    } -} - -static void -Json2RepeatedFieldValue(const NJson::TJsonValue& jsonValue, -                        google::protobuf::Message& proto, -                        const google::protobuf::FieldDescriptor& field, -                        const NProtobufJson::TJson2ProtoConfig& config, -                        const google::protobuf::Reflection* reflection, +SetKey(NProtoBuf::Message& proto,  +       const NProtoBuf::FieldDescriptor& field,  +       const TString& key) {  +    using namespace google::protobuf;  +    using namespace NProtobufJson;  +  +    const Reflection* reflection = proto.GetReflection();  +    TString result;  +    switch (field.cpp_type()) {  +        case FieldDescriptor::CPPTYPE_INT32:  +            reflection->SetInt32(&proto, &field, FromString<int32>(key));  +            break;  +        case FieldDescriptor::CPPTYPE_INT64:  +            reflection->SetInt64(&proto, &field, FromString<int64>(key));  +            break;  +        case FieldDescriptor::CPPTYPE_UINT32:  +            reflection->SetUInt32(&proto, &field, FromString<uint32>(key));  +            break;  +        case FieldDescriptor::CPPTYPE_UINT64:  +            reflection->SetUInt64(&proto, &field, FromString<uint64>(key));  +            break;  +        case FieldDescriptor::CPPTYPE_BOOL:  +            reflection->SetBool(&proto, &field, FromString<bool>(key));  +            break;  +        case FieldDescriptor::CPPTYPE_STRING:  +            reflection->SetString(&proto, &field, key);  +            break;  +        default:  +            ythrow yexception() << "Unsupported key type.";  +    }  +}  +  +static void  +Json2RepeatedFieldValue(const NJson::TJsonValue& jsonValue,  +                        google::protobuf::Message& proto,  +                        const google::protobuf::FieldDescriptor& field,  +                        const NProtobufJson::TJson2ProtoConfig& config,  +                        const google::protobuf::Reflection* reflection,                           const TMaybe<TString>& key = {}) { -    using namespace google::protobuf; - -    switch (field.cpp_type()) { +    using namespace google::protobuf;  +  +    switch (field.cpp_type()) {           JSON_TO_FIELD(CPPTYPE_INT32, field.name(), jsonValue, IsInteger, AddInt32, GetInteger);          JSON_TO_FIELD(CPPTYPE_INT64, field.name(), jsonValue, IsInteger, AddInt64, GetInteger);          JSON_TO_FIELD(CPPTYPE_UINT32, field.name(), jsonValue, IsInteger, AddUInt32, GetInteger); @@ -276,42 +276,42 @@ Json2RepeatedFieldValue(const NJson::TJsonValue& jsonValue,          JSON_TO_FIELD(CPPTYPE_DOUBLE, field.name(), jsonValue, IsDouble, AddDouble, GetDouble);          JSON_TO_FIELD(CPPTYPE_FLOAT, field.name(), jsonValue, IsDouble, AddFloat, GetDouble);          JSON_TO_FIELD(CPPTYPE_BOOL, field.name(), jsonValue, IsBoolean, AddBool, GetBoolean); - -        case FieldDescriptor::CPPTYPE_STRING: { -            JsonString2Field(jsonValue, proto, field, config); -            break; -        } - -        case FieldDescriptor::CPPTYPE_ENUM: { +  +        case FieldDescriptor::CPPTYPE_STRING: {  +            JsonString2Field(jsonValue, proto, field, config);  +            break;  +        }  +  +        case FieldDescriptor::CPPTYPE_ENUM: {               JsonEnum2Field(jsonValue, proto, field, config); -            break; -        } - -        case FieldDescriptor::CPPTYPE_MESSAGE: { -            Message* innerProto = reflection->AddMessage(&proto, &field); -            Y_ASSERT(!!innerProto); +            break;  +        }  +  +        case FieldDescriptor::CPPTYPE_MESSAGE: {  +            Message* innerProto = reflection->AddMessage(&proto, &field);  +            Y_ASSERT(!!innerProto);               if (key.Defined()) { -                const FieldDescriptor* keyField = innerProto->GetDescriptor()->FindFieldByName("key"); +                const FieldDescriptor* keyField = innerProto->GetDescriptor()->FindFieldByName("key");                   Y_ENSURE(keyField, "Map entry key field not found: " << field.name());                  SetKey(*innerProto, *keyField, *key); - -                const FieldDescriptor* valueField = innerProto->GetDescriptor()->FindFieldByName("value"); +  +                const FieldDescriptor* valueField = innerProto->GetDescriptor()->FindFieldByName("value");                   Y_ENSURE(valueField, "Map entry value field not found."); -                Json2SingleField(jsonValue, *innerProto, *valueField, config, /*isMapValue=*/true); -            } else { +                Json2SingleField(jsonValue, *innerProto, *valueField, config, /*isMapValue=*/true);  +            } else {                   NProtobufJson::MergeJson2Proto(jsonValue, *innerProto, config); -            } - -            break; -        } - -        default: -            ythrow yexception() << "Unknown protobuf field type: " -                                << static_cast<int>(field.cpp_type()) << "."; -    } -} - -static void +            }  +  +            break;  +        }  +  +        default:  +            ythrow yexception() << "Unknown protobuf field type: "  +                                << static_cast<int>(field.cpp_type()) << ".";  +    }  +}  +  +static void   Json2RepeatedField(const NJson::TJsonValue& json,                     google::protobuf::Message& proto,                     const google::protobuf::FieldDescriptor& field, @@ -326,15 +326,15 @@ Json2RepeatedField(const NJson::TJsonValue& json,      if (fieldJson.GetType() == NJson::JSON_UNDEFINED || fieldJson.GetType() == NJson::JSON_NULL)          return; -    bool isMap = fieldJson.GetType() == NJson::JSON_MAP; +    bool isMap = fieldJson.GetType() == NJson::JSON_MAP;       if (isMap) {          if (!config.MapAsObject) {              ythrow yexception() << "Map as object representation is not allowed, field: " << field.name();          } else if (!field.is_map() && !fieldJson.GetMap().empty()) {              ythrow yexception() << "Field " << field.name() << " is not a map.";          } -    } - +    }  +       if (fieldJson.GetType() != NJson::JSON_ARRAY && !config.MapAsObject && !config.VectorizeScalars && !config.ValueVectorizer) {          ythrow yexception() << "JSON field doesn't represent an array for "                              << name @@ -345,14 +345,14 @@ Json2RepeatedField(const NJson::TJsonValue& json,      const Reflection* reflection = proto.GetReflection();      Y_ASSERT(!!reflection); -    if (isMap) { -        const THashMap<TString, NJson::TJsonValue> jsonMap = fieldJson.GetMap(); -        for (const auto& x : jsonMap) { -            const TString& key = x.first; -            const NJson::TJsonValue& jsonValue = x.second; -            Json2RepeatedFieldValue(jsonValue, proto, field, config, reflection, key); +    if (isMap) {  +        const THashMap<TString, NJson::TJsonValue> jsonMap = fieldJson.GetMap();  +        for (const auto& x : jsonMap) {  +            const TString& key = x.first;  +            const NJson::TJsonValue& jsonValue = x.second;  +            Json2RepeatedFieldValue(jsonValue, proto, field, config, reflection, key);           } -    } else { +    } else {           if (config.ReplaceRepeatedFields) {              reflection->ClearField(&proto, &field);          } diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h index 4c33498dfa3..0fa972c89a3 100644 --- a/library/cpp/protobuf/json/json2proto.h +++ b/library/cpp/protobuf/json/json2proto.h @@ -26,8 +26,8 @@ namespace NProtobufJson {              FieldNameLowerCase,              FieldNameUpperCase,              FieldNameCamelCase, -            FieldNameSnakeCase,     // ABC -> a_b_c,    UserID -> user_i_d -            FieldNameSnakeCaseDense // ABC -> abc,      UserID -> user_id +            FieldNameSnakeCase,     // ABC -> a_b_c,    UserID -> user_i_d  +            FieldNameSnakeCaseDense // ABC -> abc,      UserID -> user_id           };          enum EnumValueMode { @@ -123,7 +123,7 @@ namespace NProtobufJson {          /// Consider map to be an object, otherwise consider it to be an array of key/value objects          bool MapAsObject = false; - +           /// Throw exception if there is no required fields in json object.          bool CheckRequiredFields = true; diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 6123eab0f25..e165853f0a4 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -1,195 +1,195 @@  #include "proto2json_printer.h"  #include "config.h" -#include "util.h" +#include "util.h"   #include <util/generic/yexception.h>  #include <util/string/ascii.h> -#include <util/string/cast.h> +#include <util/string/cast.h>   namespace NProtobufJson { -    using namespace NProtoBuf; +    using namespace NProtoBuf;  -    class TJsonKeyBuilder { -    public: -        TJsonKeyBuilder(const FieldDescriptor& field, const TProto2JsonConfig& config, TString& tmpBuf) -            : NewKeyStr(tmpBuf) -        { +    class TJsonKeyBuilder {  +    public:  +        TJsonKeyBuilder(const FieldDescriptor& field, const TProto2JsonConfig& config, TString& tmpBuf)  +            : NewKeyStr(tmpBuf)  +        {               if (config.NameGenerator) {                  NewKeyStr = config.NameGenerator(field);                  NewKeyBuf = NewKeyStr;                  return;              } -            if (config.UseJsonName) { -                Y_ASSERT(!field.json_name().empty()); +            if (config.UseJsonName) {  +                Y_ASSERT(!field.json_name().empty());                   NewKeyStr = field.json_name();                  if (!field.has_json_name() && !NewKeyStr.empty()) {                      // FIXME: https://st.yandex-team.ru/CONTRIB-139                      NewKeyStr[0] = AsciiToLower(NewKeyStr[0]);                  }                  NewKeyBuf = NewKeyStr; -                return; -            } - -            switch (config.FieldNameMode) { -                case TProto2JsonConfig::FieldNameOriginalCase: { -                    NewKeyBuf = field.name(); -                    break; -                } - -                case TProto2JsonConfig::FieldNameLowerCase: { -                    NewKeyStr = field.name(); -                    NewKeyStr.to_lower(); -                    NewKeyBuf = NewKeyStr; -                    break; -                } - -                case TProto2JsonConfig::FieldNameUpperCase: { -                    NewKeyStr = field.name(); -                    NewKeyStr.to_upper(); -                    NewKeyBuf = NewKeyStr; -                    break; -                } - -                case TProto2JsonConfig::FieldNameCamelCase: { -                    NewKeyStr = field.name(); +                return;  +            }  + +            switch (config.FieldNameMode) {  +                case TProto2JsonConfig::FieldNameOriginalCase: {  +                    NewKeyBuf = field.name();  +                    break;  +                }  + +                case TProto2JsonConfig::FieldNameLowerCase: {  +                    NewKeyStr = field.name();  +                    NewKeyStr.to_lower();  +                    NewKeyBuf = NewKeyStr;  +                    break;  +                }  + +                case TProto2JsonConfig::FieldNameUpperCase: {  +                    NewKeyStr = field.name();  +                    NewKeyStr.to_upper();  +                    NewKeyBuf = NewKeyStr;  +                    break;  +                }  + +                case TProto2JsonConfig::FieldNameCamelCase: {  +                    NewKeyStr = field.name();                       if (!NewKeyStr.empty()) { -                        NewKeyStr[0] = AsciiToLower(NewKeyStr[0]); -                    } -                    NewKeyBuf = NewKeyStr; -                    break; -                } - -                case TProto2JsonConfig::FieldNameSnakeCase: { -                    NewKeyStr = field.name(); -                    ToSnakeCase(&NewKeyStr); -                    NewKeyBuf = NewKeyStr; -                    break; -                } - -                case TProto2JsonConfig::FieldNameSnakeCaseDense: { -                    NewKeyStr = field.name(); -                    ToSnakeCaseDense(&NewKeyStr); -                    NewKeyBuf = NewKeyStr; -                    break; -                } - -                default: -                    Y_VERIFY_DEBUG(false, "Unknown FieldNameMode."); +                        NewKeyStr[0] = AsciiToLower(NewKeyStr[0]);  +                    }  +                    NewKeyBuf = NewKeyStr;  +                    break;  +                }  + +                case TProto2JsonConfig::FieldNameSnakeCase: {  +                    NewKeyStr = field.name();  +                    ToSnakeCase(&NewKeyStr);  +                    NewKeyBuf = NewKeyStr;  +                    break;  +                }  +  +                case TProto2JsonConfig::FieldNameSnakeCaseDense: {  +                    NewKeyStr = field.name();  +                    ToSnakeCaseDense(&NewKeyStr);  +                    NewKeyBuf = NewKeyStr;  +                    break;  +                }  +  +                default:  +                    Y_VERIFY_DEBUG(false, "Unknown FieldNameMode.");               }          } -        const TStringBuf& GetKey() const { -            return NewKeyBuf; +        const TStringBuf& GetKey() const {  +            return NewKeyBuf;           } - -    private: -        TStringBuf NewKeyBuf; -        TString& NewKeyStr; -    }; - -    TProto2JsonPrinter::TProto2JsonPrinter(const TProto2JsonConfig& cfg) -        : Config(cfg) -    { -    } - -    TProto2JsonPrinter::~TProto2JsonPrinter() { -    } - -    TStringBuf TProto2JsonPrinter::MakeKey(const FieldDescriptor& field) { -        return TJsonKeyBuilder(field, GetConfig(), TmpBuf).GetKey(); -    } - -    template <bool InMapContext, typename T> -    std::enable_if_t<InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& json, const TStringBuf& key, const T& value) { -        json.WriteKey(key).Write(value); +  +    private:  +        TStringBuf NewKeyBuf;  +        TString& NewKeyStr;  +    };  +  +    TProto2JsonPrinter::TProto2JsonPrinter(const TProto2JsonConfig& cfg)  +        : Config(cfg)  +    {       } -    template <bool InMapContext, typename T> -    std::enable_if_t<!InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& array, const TStringBuf& key, const T& value) { -        Y_ASSERT(!key); -        array.Write(value); +    TProto2JsonPrinter::~TProto2JsonPrinter() {       } -    template <bool InMapContext> -    void TProto2JsonPrinter::PrintStringValue(const FieldDescriptor& field, -                                              const TStringBuf& key, const TString& value, -                                              IJsonOutput& json) { -        if (!GetConfig().StringTransforms.empty()) { -            TString tmpBuf = value; -            for (const TStringTransformPtr& stringTransform : GetConfig().StringTransforms) { -                Y_ASSERT(stringTransform); -                if (stringTransform) { -                    if (field.type() == FieldDescriptor::TYPE_BYTES) -                        stringTransform->TransformBytes(tmpBuf); -                    else -                        stringTransform->Transform(tmpBuf); -                } -            } -            WriteWithMaybeEmptyKey<InMapContext>(json, key, tmpBuf); -        } else { -            WriteWithMaybeEmptyKey<InMapContext>(json, key, value); -        } -    } - -    template <bool InMapContext> -    void TProto2JsonPrinter::PrintEnumValue(const TStringBuf& key, -                                            const EnumValueDescriptor* value, -                                            IJsonOutput& json) { +    TStringBuf TProto2JsonPrinter::MakeKey(const FieldDescriptor& field) {  +        return TJsonKeyBuilder(field, GetConfig(), TmpBuf).GetKey();  +    }  + +    template <bool InMapContext, typename T>  +    std::enable_if_t<InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& json, const TStringBuf& key, const T& value) {  +        json.WriteKey(key).Write(value);  +    }  + +    template <bool InMapContext, typename T>  +    std::enable_if_t<!InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& array, const TStringBuf& key, const T& value) {  +        Y_ASSERT(!key);  +        array.Write(value);  +    }  + +    template <bool InMapContext>  +    void TProto2JsonPrinter::PrintStringValue(const FieldDescriptor& field,  +                                              const TStringBuf& key, const TString& value,  +                                              IJsonOutput& json) {  +        if (!GetConfig().StringTransforms.empty()) {  +            TString tmpBuf = value;  +            for (const TStringTransformPtr& stringTransform : GetConfig().StringTransforms) {  +                Y_ASSERT(stringTransform);  +                if (stringTransform) {  +                    if (field.type() == FieldDescriptor::TYPE_BYTES)  +                        stringTransform->TransformBytes(tmpBuf);  +                    else  +                        stringTransform->Transform(tmpBuf);  +                }  +            }  +            WriteWithMaybeEmptyKey<InMapContext>(json, key, tmpBuf);  +        } else {  +            WriteWithMaybeEmptyKey<InMapContext>(json, key, value);  +        }  +    }  + +    template <bool InMapContext>  +    void TProto2JsonPrinter::PrintEnumValue(const TStringBuf& key,  +                                            const EnumValueDescriptor* value,  +                                            IJsonOutput& json) {           if (Config.EnumValueGenerator) {              WriteWithMaybeEmptyKey<InMapContext>(json, key, Config.EnumValueGenerator(*value));              return;          } -        switch (GetConfig().EnumMode) { -            case TProto2JsonConfig::EnumNumber: { -                WriteWithMaybeEmptyKey<InMapContext>(json, key, value->number()); -                break; -            } +        switch (GetConfig().EnumMode) {  +            case TProto2JsonConfig::EnumNumber: {  +                WriteWithMaybeEmptyKey<InMapContext>(json, key, value->number());  +                break;  +            }  -            case TProto2JsonConfig::EnumName: { -                WriteWithMaybeEmptyKey<InMapContext>(json, key, value->name()); -                break; -            } +            case TProto2JsonConfig::EnumName: {  +                WriteWithMaybeEmptyKey<InMapContext>(json, key, value->name());  +                break;  +            }  -            case TProto2JsonConfig::EnumFullName: { -                WriteWithMaybeEmptyKey<InMapContext>(json, key, value->full_name()); -                break; +            case TProto2JsonConfig::EnumFullName: {  +                WriteWithMaybeEmptyKey<InMapContext>(json, key, value->full_name());  +                break;               } -            case TProto2JsonConfig::EnumNameLowerCase: { -                TString newName = value->name(); -                newName.to_lower(); -                WriteWithMaybeEmptyKey<InMapContext>(json, key, newName); -                break; -            } - -            case TProto2JsonConfig::EnumFullNameLowerCase: { -                TString newName = value->full_name(); -                newName.to_lower(); -                WriteWithMaybeEmptyKey<InMapContext>(json, key, newName); -                break; -            } - -            default: -                Y_VERIFY_DEBUG(false, "Unknown EnumMode."); -        } +            case TProto2JsonConfig::EnumNameLowerCase: {  +                TString newName = value->name();  +                newName.to_lower();  +                WriteWithMaybeEmptyKey<InMapContext>(json, key, newName);  +                break;  +            }  + +            case TProto2JsonConfig::EnumFullNameLowerCase: {  +                TString newName = value->full_name();  +                newName.to_lower();  +                WriteWithMaybeEmptyKey<InMapContext>(json, key, newName);  +                break;  +            }  + +            default:  +                Y_VERIFY_DEBUG(false, "Unknown EnumMode.");  +        }       } -    void TProto2JsonPrinter::PrintSingleField(const Message& proto, -                                              const FieldDescriptor& field, -                                              IJsonOutput& json, -                                              TStringBuf key) { -        Y_VERIFY(!field.is_repeated(), "field is repeated."); +    void TProto2JsonPrinter::PrintSingleField(const Message& proto,  +                                              const FieldDescriptor& field,  +                                              IJsonOutput& json,  +                                              TStringBuf key) {  +        Y_VERIFY(!field.is_repeated(), "field is repeated.");  -        if (!key) { -            key = MakeKey(field); -        } +        if (!key) {  +            key = MakeKey(field);  +        }  -#define FIELD_TO_JSON(EProtoCppType, ProtoGet)                         \ -    case FieldDescriptor::EProtoCppType: {                             \ -        json.WriteKey(key).Write(reflection->ProtoGet(proto, &field)); \ -        break;                                                         \ +#define FIELD_TO_JSON(EProtoCppType, ProtoGet)                         \  +    case FieldDescriptor::EProtoCppType: {                             \  +        json.WriteKey(key).Write(reflection->ProtoGet(proto, &field)); \  +        break;                                                         \       }  #define INT_FIELD_TO_JSON(EProtoCppType, ProtoGet)              \ @@ -203,7 +203,7 @@ namespace NProtobufJson {          break;                                                  \      } -        const Reflection* reflection = proto.GetReflection(); +        const Reflection* reflection = proto.GetReflection();           bool shouldPrintField = reflection->HasField(proto, &field);          if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) { @@ -217,251 +217,251 @@ namespace NProtobufJson {          shouldPrintField = shouldPrintField || GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyDefault;          if (shouldPrintField) { -            switch (field.cpp_type()) { +            switch (field.cpp_type()) {                   INT_FIELD_TO_JSON(CPPTYPE_INT32, GetInt32);                  INT_FIELD_TO_JSON(CPPTYPE_INT64, GetInt64);                  INT_FIELD_TO_JSON(CPPTYPE_UINT32, GetUInt32);                  INT_FIELD_TO_JSON(CPPTYPE_UINT64, GetUInt64); -                FIELD_TO_JSON(CPPTYPE_DOUBLE, GetDouble); -                FIELD_TO_JSON(CPPTYPE_FLOAT, GetFloat); -                FIELD_TO_JSON(CPPTYPE_BOOL, GetBool); - -                case FieldDescriptor::CPPTYPE_MESSAGE: { -                    json.WriteKey(key); -                    Print(reflection->GetMessage(proto, &field), json); -                    break; -                } - -                case FieldDescriptor::CPPTYPE_ENUM: { -                    PrintEnumValue<true>(key, reflection->GetEnum(proto, &field), json); -                    break; -                } - -                case FieldDescriptor::CPPTYPE_STRING: { -                    TString scratch; -                    const TString& value = reflection->GetStringReference(proto, &field, &scratch); -                    PrintStringValue<true>(field, key, value, json); -                    break; -                } - -                default: -                    ythrow yexception() << "Unknown protobuf field type: " -                                        << static_cast<int>(field.cpp_type()) << "."; -            } -        } else { -            switch (GetConfig().MissingSingleKeyMode) { -                case TProto2JsonConfig::MissingKeyNull: { -                    json.WriteKey(key).WriteNull(); -                    break; -                } - -                case TProto2JsonConfig::MissingKeySkip: +                FIELD_TO_JSON(CPPTYPE_DOUBLE, GetDouble);  +                FIELD_TO_JSON(CPPTYPE_FLOAT, GetFloat);  +                FIELD_TO_JSON(CPPTYPE_BOOL, GetBool);  + +                case FieldDescriptor::CPPTYPE_MESSAGE: {  +                    json.WriteKey(key);  +                    Print(reflection->GetMessage(proto, &field), json);  +                    break;  +                }  + +                case FieldDescriptor::CPPTYPE_ENUM: {  +                    PrintEnumValue<true>(key, reflection->GetEnum(proto, &field), json);  +                    break;  +                }  + +                case FieldDescriptor::CPPTYPE_STRING: {  +                    TString scratch;  +                    const TString& value = reflection->GetStringReference(proto, &field, &scratch);  +                    PrintStringValue<true>(field, key, value, json);  +                    break;  +                }  + +                default:  +                    ythrow yexception() << "Unknown protobuf field type: "  +                                        << static_cast<int>(field.cpp_type()) << ".";  +            }  +        } else {  +            switch (GetConfig().MissingSingleKeyMode) {  +                case TProto2JsonConfig::MissingKeyNull: {  +                    json.WriteKey(key).WriteNull();  +                    break;  +                }  + +                case TProto2JsonConfig::MissingKeySkip:                   case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired: -                default: -                    break; -            } +                default:  +                    break;  +            }           } -#undef FIELD_TO_JSON -    } - -    void TProto2JsonPrinter::PrintRepeatedField(const Message& proto, -                                                const FieldDescriptor& field, -                                                IJsonOutput& json, -                                                TStringBuf key) { -        Y_VERIFY(field.is_repeated(), "field isn't repeated."); - -        const bool isMap = field.is_map() && GetConfig().MapAsObject; -        if (!key) { -            key = MakeKey(field); +#undef FIELD_TO_JSON  +    }  + +    void TProto2JsonPrinter::PrintRepeatedField(const Message& proto,  +                                                const FieldDescriptor& field,  +                                                IJsonOutput& json,  +                                                TStringBuf key) {  +        Y_VERIFY(field.is_repeated(), "field isn't repeated.");  + +        const bool isMap = field.is_map() && GetConfig().MapAsObject;  +        if (!key) {  +            key = MakeKey(field);           } -#define REPEATED_FIELD_TO_JSON(EProtoCppType, ProtoGet)                                \ -    case FieldDescriptor::EProtoCppType: {                                             \ +#define REPEATED_FIELD_TO_JSON(EProtoCppType, ProtoGet)                                \  +    case FieldDescriptor::EProtoCppType: {                                             \           for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) \ -            json.Write(reflection->ProtoGet(proto, &field, i));                        \ -        break;                                                                         \ +            json.Write(reflection->ProtoGet(proto, &field, i));                        \  +        break;                                                                         \       } -        const Reflection* reflection = proto.GetReflection(); - -        if (reflection->FieldSize(proto, &field) > 0) { -            json.WriteKey(key); -            if (isMap) { -                json.BeginObject(); -            } else { -                json.BeginList(); +        const Reflection* reflection = proto.GetReflection();  + +        if (reflection->FieldSize(proto, &field) > 0) {  +            json.WriteKey(key);  +            if (isMap) {  +                json.BeginObject();  +            } else {  +                json.BeginList();  +            }  + +            switch (field.cpp_type()) {  +                REPEATED_FIELD_TO_JSON(CPPTYPE_INT32, GetRepeatedInt32);  +                REPEATED_FIELD_TO_JSON(CPPTYPE_INT64, GetRepeatedInt64);  +                REPEATED_FIELD_TO_JSON(CPPTYPE_UINT32, GetRepeatedUInt32);  +                REPEATED_FIELD_TO_JSON(CPPTYPE_UINT64, GetRepeatedUInt64);  +                REPEATED_FIELD_TO_JSON(CPPTYPE_DOUBLE, GetRepeatedDouble);  +                REPEATED_FIELD_TO_JSON(CPPTYPE_FLOAT, GetRepeatedFloat);  +                REPEATED_FIELD_TO_JSON(CPPTYPE_BOOL, GetRepeatedBool);  + +                case FieldDescriptor::CPPTYPE_MESSAGE: {  +                    if (isMap) {  +                        for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {  +                            PrintKeyValue(reflection->GetRepeatedMessage(proto, &field, i), json);  +                        }  +                    } else {  +                        for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {  +                            Print(reflection->GetRepeatedMessage(proto, &field, i), json);  +                        }  +                    }  +                    break;  +                }  +  +                case FieldDescriptor::CPPTYPE_ENUM: {  +                    for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i)  +                        PrintEnumValue<false>(TStringBuf(), reflection->GetRepeatedEnum(proto, &field, i), json);  +                    break;  +                }  +  +                case FieldDescriptor::CPPTYPE_STRING: {  +                    TString scratch;  +                    for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {  +                        const TString& value =  +                            reflection->GetRepeatedStringReference(proto, &field, i, &scratch);  +                        PrintStringValue<false>(field, TStringBuf(), value, json);  +                    }  +                    break;  +                }  +  +                default:  +                    ythrow yexception() << "Unknown protobuf field type: "  +                                        << static_cast<int>(field.cpp_type()) << ".";               } -            switch (field.cpp_type()) { -                REPEATED_FIELD_TO_JSON(CPPTYPE_INT32, GetRepeatedInt32); -                REPEATED_FIELD_TO_JSON(CPPTYPE_INT64, GetRepeatedInt64); -                REPEATED_FIELD_TO_JSON(CPPTYPE_UINT32, GetRepeatedUInt32); -                REPEATED_FIELD_TO_JSON(CPPTYPE_UINT64, GetRepeatedUInt64); -                REPEATED_FIELD_TO_JSON(CPPTYPE_DOUBLE, GetRepeatedDouble); -                REPEATED_FIELD_TO_JSON(CPPTYPE_FLOAT, GetRepeatedFloat); -                REPEATED_FIELD_TO_JSON(CPPTYPE_BOOL, GetRepeatedBool); - -                case FieldDescriptor::CPPTYPE_MESSAGE: { -                    if (isMap) { -                        for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { -                            PrintKeyValue(reflection->GetRepeatedMessage(proto, &field, i), json); -                        } -                    } else { -                        for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { -                            Print(reflection->GetRepeatedMessage(proto, &field, i), json); -                        } -                    } -                    break; -                } - -                case FieldDescriptor::CPPTYPE_ENUM: { -                    for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) -                        PrintEnumValue<false>(TStringBuf(), reflection->GetRepeatedEnum(proto, &field, i), json); -                    break; -                } - -                case FieldDescriptor::CPPTYPE_STRING: { -                    TString scratch; -                    for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { -                        const TString& value = -                            reflection->GetRepeatedStringReference(proto, &field, i, &scratch); -                        PrintStringValue<false>(field, TStringBuf(), value, json); -                    } -                    break; -                } - -                default: -                    ythrow yexception() << "Unknown protobuf field type: " -                                        << static_cast<int>(field.cpp_type()) << "."; -            } - -            if (isMap) { -                json.EndObject(); -            } else { -                json.EndList(); -            } -        } else { -            switch (GetConfig().MissingRepeatedKeyMode) { -                case TProto2JsonConfig::MissingKeyNull: { -                    json.WriteKey(key).WriteNull(); -                    break; -                } - -                case TProto2JsonConfig::MissingKeyDefault: { -                    json.WriteKey(key); -                    if (isMap) { -                        json.BeginObject().EndObject(); -                    } else { -                        json.BeginList().EndList(); -                    } -                    break; -                } - -                case TProto2JsonConfig::MissingKeySkip: +            if (isMap) {  +                json.EndObject();  +            } else {  +                json.EndList();  +            }  +        } else {  +            switch (GetConfig().MissingRepeatedKeyMode) {  +                case TProto2JsonConfig::MissingKeyNull: {  +                    json.WriteKey(key).WriteNull();  +                    break;  +                }  + +                case TProto2JsonConfig::MissingKeyDefault: {  +                    json.WriteKey(key);  +                    if (isMap) {  +                        json.BeginObject().EndObject();  +                    } else {  +                        json.BeginList().EndList();  +                    }  +                    break;  +                }  +  +                case TProto2JsonConfig::MissingKeySkip:                   case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired: -                default: -                    break; +                default:  +                    break;               }          } -#undef REPEATED_FIELD_TO_JSON -    } - -    void TProto2JsonPrinter::PrintKeyValue(const NProtoBuf::Message& proto, -                                           IJsonOutput& json) { -        const FieldDescriptor* keyField = proto.GetDescriptor()->FindFieldByName("key"); -        Y_VERIFY(keyField, "Map entry key field not found."); -        TString key = MakeKey(proto, *keyField); -        const FieldDescriptor* valueField = proto.GetDescriptor()->FindFieldByName("value"); -        Y_VERIFY(valueField, "Map entry value field not found."); -        PrintField(proto, *valueField, json, key); -    } - -    TString TProto2JsonPrinter::MakeKey(const NProtoBuf::Message& proto, -                                        const NProtoBuf::FieldDescriptor& field) { -        const Reflection* reflection = proto.GetReflection(); -        TString result; -        switch (field.cpp_type()) { -            case FieldDescriptor::CPPTYPE_INT32: -                result = ToString(reflection->GetInt32(proto, &field)); -                break; -            case FieldDescriptor::CPPTYPE_INT64: -                result = ToString(reflection->GetInt64(proto, &field)); -                break; -            case FieldDescriptor::CPPTYPE_UINT32: -                result = ToString(reflection->GetUInt32(proto, &field)); -                break; -            case FieldDescriptor::CPPTYPE_UINT64: -                result = ToString(reflection->GetUInt64(proto, &field)); -                break; -            case FieldDescriptor::CPPTYPE_DOUBLE: -                result = ToString(reflection->GetDouble(proto, &field)); -                break; -            case FieldDescriptor::CPPTYPE_FLOAT: -                result = ToString(reflection->GetFloat(proto, &field)); -                break; -            case FieldDescriptor::CPPTYPE_BOOL: -                result = ToString(reflection->GetBool(proto, &field)); -                break; -            case FieldDescriptor::CPPTYPE_ENUM: { -                const EnumValueDescriptor* value = reflection->GetEnum(proto, &field); -                switch (GetConfig().EnumMode) { -                    case TProto2JsonConfig::EnumNumber: -                        result = ToString(value->number()); -                        break; -                    case TProto2JsonConfig::EnumName: -                        result = value->name(); -                        break; -                    case TProto2JsonConfig::EnumFullName: -                        result = value->full_name(); -                        break; -                    case TProto2JsonConfig::EnumNameLowerCase: -                        result = value->name(); -                        result.to_lower(); -                        break; -                    case TProto2JsonConfig::EnumFullNameLowerCase: -                        result = value->full_name(); -                        result.to_lower(); -                        break; -                    default: -                        ythrow yexception() << "Unsupported enum mode."; -                } +#undef REPEATED_FIELD_TO_JSON  +    }  + +    void TProto2JsonPrinter::PrintKeyValue(const NProtoBuf::Message& proto,  +                                           IJsonOutput& json) {  +        const FieldDescriptor* keyField = proto.GetDescriptor()->FindFieldByName("key");  +        Y_VERIFY(keyField, "Map entry key field not found.");  +        TString key = MakeKey(proto, *keyField);  +        const FieldDescriptor* valueField = proto.GetDescriptor()->FindFieldByName("value");  +        Y_VERIFY(valueField, "Map entry value field not found.");  +        PrintField(proto, *valueField, json, key);  +    }  + +    TString TProto2JsonPrinter::MakeKey(const NProtoBuf::Message& proto,  +                                        const NProtoBuf::FieldDescriptor& field) {  +        const Reflection* reflection = proto.GetReflection();  +        TString result;  +        switch (field.cpp_type()) {  +            case FieldDescriptor::CPPTYPE_INT32:  +                result = ToString(reflection->GetInt32(proto, &field));  +                break;  +            case FieldDescriptor::CPPTYPE_INT64:  +                result = ToString(reflection->GetInt64(proto, &field));  +                break;  +            case FieldDescriptor::CPPTYPE_UINT32:  +                result = ToString(reflection->GetUInt32(proto, &field));  +                break;  +            case FieldDescriptor::CPPTYPE_UINT64:  +                result = ToString(reflection->GetUInt64(proto, &field));  +                break;  +            case FieldDescriptor::CPPTYPE_DOUBLE:  +                result = ToString(reflection->GetDouble(proto, &field));  +                break;  +            case FieldDescriptor::CPPTYPE_FLOAT:  +                result = ToString(reflection->GetFloat(proto, &field));  +                break;  +            case FieldDescriptor::CPPTYPE_BOOL:  +                result = ToString(reflection->GetBool(proto, &field));  +                break;  +            case FieldDescriptor::CPPTYPE_ENUM: {  +                const EnumValueDescriptor* value = reflection->GetEnum(proto, &field);  +                switch (GetConfig().EnumMode) {  +                    case TProto2JsonConfig::EnumNumber:  +                        result = ToString(value->number());  +                        break;  +                    case TProto2JsonConfig::EnumName:  +                        result = value->name();  +                        break;  +                    case TProto2JsonConfig::EnumFullName:  +                        result = value->full_name();  +                        break;  +                    case TProto2JsonConfig::EnumNameLowerCase:  +                        result = value->name();  +                        result.to_lower();  +                        break;  +                    case TProto2JsonConfig::EnumFullNameLowerCase:  +                        result = value->full_name();  +                        result.to_lower();  +                        break;  +                    default:  +                        ythrow yexception() << "Unsupported enum mode.";  +                }                   break; -            } -            case FieldDescriptor::CPPTYPE_STRING: -                result = reflection->GetString(proto, &field); -                break; -            default: -                ythrow yexception() << "Unsupported key type."; +            }  +            case FieldDescriptor::CPPTYPE_STRING:  +                result = reflection->GetString(proto, &field);  +                break;  +            default:  +                ythrow yexception() << "Unsupported key type.";           } -        return result; +        return result;       } -    void TProto2JsonPrinter::PrintField(const Message& proto, -                                        const FieldDescriptor& field, -                                        IJsonOutput& json, -                                        const TStringBuf key) { +    void TProto2JsonPrinter::PrintField(const Message& proto,  +                                        const FieldDescriptor& field,  +                                        IJsonOutput& json,  +                                        const TStringBuf key) {  -        if (field.is_repeated()) -            PrintRepeatedField(proto, field, json, key); -        else -            PrintSingleField(proto, field, json, key); -    } +        if (field.is_repeated())  +            PrintRepeatedField(proto, field, json, key);  +        else  +            PrintSingleField(proto, field, json, key);  +    }  -    void TProto2JsonPrinter::Print(const Message& proto, IJsonOutput& json, bool closeMap) { -        const Descriptor* descriptor = proto.GetDescriptor(); -        Y_ASSERT(descriptor); +    void TProto2JsonPrinter::Print(const Message& proto, IJsonOutput& json, bool closeMap) {  +        const Descriptor* descriptor = proto.GetDescriptor();  +        Y_ASSERT(descriptor);  -        json.BeginObject(); +        json.BeginObject();           // Iterate over all non-extension fields -        for (int f = 0, endF = descriptor->field_count(); f < endF; ++f) { -            const FieldDescriptor* field = descriptor->field(f); -            Y_ASSERT(field); -            PrintField(proto, *field, json); -        } +        for (int f = 0, endF = descriptor->field_count(); f < endF; ++f) {  +            const FieldDescriptor* field = descriptor->field(f);  +            Y_ASSERT(field);  +            PrintField(proto, *field, json);  +        }           // Check extensions via ListFields          std::vector<const FieldDescriptor*> fields; @@ -482,9 +482,9 @@ namespace NProtobufJson {              }          } -        if (closeMap) { -            json.EndObject(); -        } +        if (closeMap) {  +            json.EndObject();  +        }       }      template <class T, class U> diff --git a/library/cpp/protobuf/json/proto2json_printer.h b/library/cpp/protobuf/json/proto2json_printer.h index 9dc5aa86c62..5d1796d4901 100644 --- a/library/cpp/protobuf/json/proto2json_printer.h +++ b/library/cpp/protobuf/json/proto2json_printer.h @@ -10,59 +10,59 @@  #include <util/generic/string.h>  namespace NProtobufJson { -    struct TProto2JsonConfig; +    struct TProto2JsonConfig;  -    class TProto2JsonPrinter { -    public: -        TProto2JsonPrinter(const TProto2JsonConfig& config); -        virtual ~TProto2JsonPrinter(); +    class TProto2JsonPrinter {  +    public:  +        TProto2JsonPrinter(const TProto2JsonConfig& config);  +        virtual ~TProto2JsonPrinter();  -        virtual void Print(const NProtoBuf::Message& proto, IJsonOutput& json, bool closeMap = true); +        virtual void Print(const NProtoBuf::Message& proto, IJsonOutput& json, bool closeMap = true);  -        virtual const TProto2JsonConfig& GetConfig() const { -            return Config; -        } +        virtual const TProto2JsonConfig& GetConfig() const {  +            return Config;  +        }  -    protected: -        virtual TStringBuf MakeKey(const NProtoBuf::FieldDescriptor& field); +    protected:  +        virtual TStringBuf MakeKey(const NProtoBuf::FieldDescriptor& field);  -        virtual void PrintField(const NProtoBuf::Message& proto, -                                const NProtoBuf::FieldDescriptor& field, -                                IJsonOutput& json, -                                TStringBuf key = {}); +        virtual void PrintField(const NProtoBuf::Message& proto,  +                                const NProtoBuf::FieldDescriptor& field,  +                                IJsonOutput& json,  +                                TStringBuf key = {});  -        void PrintRepeatedField(const NProtoBuf::Message& proto, -                                const NProtoBuf::FieldDescriptor& field, -                                IJsonOutput& json, -                                TStringBuf key = {}); +        void PrintRepeatedField(const NProtoBuf::Message& proto,  +                                const NProtoBuf::FieldDescriptor& field,  +                                IJsonOutput& json,  +                                TStringBuf key = {});  -        void PrintSingleField(const NProtoBuf::Message& proto, -                              const NProtoBuf::FieldDescriptor& field, -                              IJsonOutput& json, -                              TStringBuf key = {}); +        void PrintSingleField(const NProtoBuf::Message& proto,  +                              const NProtoBuf::FieldDescriptor& field,  +                              IJsonOutput& json,  +                              TStringBuf key = {});  -        void PrintKeyValue(const NProtoBuf::Message& proto, -                           IJsonOutput& json); +        void PrintKeyValue(const NProtoBuf::Message& proto,  +                           IJsonOutput& json);  -        TString MakeKey(const NProtoBuf::Message& proto, -                        const NProtoBuf::FieldDescriptor& field); +        TString MakeKey(const NProtoBuf::Message& proto,  +                        const NProtoBuf::FieldDescriptor& field);  -        template <bool InMapContext> -        void PrintEnumValue(const TStringBuf& key, -                            const NProtoBuf::EnumValueDescriptor* value, -                            IJsonOutput& json); +        template <bool InMapContext>  +        void PrintEnumValue(const TStringBuf& key,  +                            const NProtoBuf::EnumValueDescriptor* value,  +                            IJsonOutput& json);  -        template <bool InMapContext> -        void PrintStringValue(const NProtoBuf::FieldDescriptor& field, -                              const TStringBuf& key, const TString& value, -                              IJsonOutput& json); +        template <bool InMapContext>  +        void PrintStringValue(const NProtoBuf::FieldDescriptor& field,  +                              const TStringBuf& key, const TString& value,  +                              IJsonOutput& json);           template <class T>          bool NeedStringifyNumber(T value) const; -    protected: -        const TProto2JsonConfig& Config; -        TString TmpBuf; -    }; +    protected:  +        const TProto2JsonConfig& Config;  +        TString TmpBuf;  +    };  -} +}  diff --git a/library/cpp/protobuf/json/ut/fields.incl b/library/cpp/protobuf/json/ut/fields.incl index 4b229858369..210645ce815 100644 --- a/library/cpp/protobuf/json/ut/fields.incl +++ b/library/cpp/protobuf/json/ut/fields.incl @@ -19,5 +19,5 @@ 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") -DEFINE_FIELD(UserID, "some_id")
\ No newline at end of file +DEFINE_FIELD(ABC, "abc")  +DEFINE_FIELD(UserID, "some_id") 
\ No newline at end of file diff --git a/library/cpp/protobuf/json/ut/json2proto_ut.cpp b/library/cpp/protobuf/json/ut/json2proto_ut.cpp index 0dfe57bc7a2..564ff12ed6e 100644 --- a/library/cpp/protobuf/json/ut/json2proto_ut.cpp +++ b/library/cpp/protobuf/json/ut/json2proto_ut.cpp @@ -23,7 +23,7 @@  using namespace NProtobufJson;  using namespace NProtobufJsonTest; -namespace google { +namespace google {       namespace protobuf {          namespace internal {              void MapTestForceDeterministic() { @@ -31,16 +31,16 @@ namespace google {              }          }      }     // namespace protobuf -} - +}  +   namespace { -    class TInit { -    public: -        TInit() { -            ::google::protobuf::internal::MapTestForceDeterministic(); -        } -    } Init; - +    class TInit {  +    public:  +        TInit() {  +            ::google::protobuf::internal::MapTestForceDeterministic();  +        }  +    } Init;  +       template <typename T>      TString ConvertToString(T value) {          return ToString(value); @@ -55,19 +55,19 @@ namespace {          NJsonWriter::TBuf buf(NJsonWriter::HEM_UNSAFE);          return buf.WriteJsonValue(&json).Str();      } - +       void TestComplexMapAsObject(std::function<void(TComplexMapType&)>&& init, const TString& json, const TJson2ProtoConfig& config = TJson2ProtoConfig().SetMapAsObject(true)) { -        TComplexMapType modelProto; - -        init(modelProto); - -        TString modelStr(json); - -        TComplexMapType proto; -        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TComplexMapType>(modelStr, config)); - -        UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); -    } +        TComplexMapType modelProto;  +  +        init(modelProto);  +  +        TString modelStr(json);  +  +        TComplexMapType proto;  +        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TComplexMapType>(modelStr, config));  +  +        UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto);  +    }   }  Y_UNIT_TEST_SUITE(TJson2ProtoTest) { @@ -398,38 +398,38 @@ Y_UNIT_TEST(TestFieldNameMode) {          UNIT_ASSERT(proto.GetOneTwoString() == "value");      } -    // snake_case -    { -        TString modelStr(R"_({"string":"value"})_"); - -        TFlatOptional proto; -        TJson2ProtoConfig config; -        config.FieldNameMode = TJson2ProtoConfig::FieldNameSnakeCase; - -        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); -        UNIT_ASSERT(proto.GetString() == "value"); -    } -    { -        TString modelStr(R"_({"one_string":"value"})_"); - -        TFlatOptional proto; -        TJson2ProtoConfig config; -        config.FieldNameMode = TJson2ProtoConfig::FieldNameSnakeCase; - -        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); -        UNIT_ASSERT(proto.GetOneString() == "value"); -    } -    { -        TString modelStr(R"_({"one_two_string":"value"})_"); - -        TFlatOptional proto; -        TJson2ProtoConfig config; -        config.FieldNameMode = TJson2ProtoConfig::FieldNameSnakeCase; - -        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); -        UNIT_ASSERT(proto.GetOneTwoString() == "value"); -    } - +    // snake_case  +    {  +        TString modelStr(R"_({"string":"value"})_");  +  +        TFlatOptional proto;  +        TJson2ProtoConfig config;  +        config.FieldNameMode = TJson2ProtoConfig::FieldNameSnakeCase;  +  +        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config));  +        UNIT_ASSERT(proto.GetString() == "value");  +    }  +    {  +        TString modelStr(R"_({"one_string":"value"})_");  +  +        TFlatOptional proto;  +        TJson2ProtoConfig config;  +        config.FieldNameMode = TJson2ProtoConfig::FieldNameSnakeCase;  +  +        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config));  +        UNIT_ASSERT(proto.GetOneString() == "value");  +    }  +    {  +        TString modelStr(R"_({"one_two_string":"value"})_");  +  +        TFlatOptional proto;  +        TJson2ProtoConfig config;  +        config.FieldNameMode = TJson2ProtoConfig::FieldNameSnakeCase;  +  +        UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config));  +        UNIT_ASSERT(proto.GetOneTwoString() == "value");  +    }  +       // Original case, repeated      {          TString modelStr(R"_({"I32":[1,2]})_"); @@ -743,22 +743,22 @@ Y_UNIT_TEST(TestValueVectorizer) {  Y_UNIT_TEST(TestMapAsObject) {      TMapType modelProto; - +       auto& items = *modelProto.MutableItems();      items["key1"] = "value1";      items["key2"] = "value2";      items["key3"] = "value3"; - +       TString modelStr(R"_({"Items":{"key1":"value1","key2":"value2","key3":"value3"}})_"); - +       TJson2ProtoConfig config;      config.MapAsObject = true;      TMapType proto;      UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TMapType>(modelStr, config)); - +       UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto);  } // TestMapAsObject - +   Y_UNIT_TEST(TestComplexMapAsObject_I32) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -769,7 +769,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_I32) {          },          R"_({"I32":{"1":1,"-2":-2,"3":3}})_");  } // TestComplexMapAsObject_I32 - +   Y_UNIT_TEST(TestComplexMapAsObject_I64) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -780,7 +780,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_I64) {          },          R"_({"I64":{"2147483649":2147483649,"-2147483650":-2147483650,"2147483651":2147483651}})_");  } // TestComplexMapAsObject_I64 - +   Y_UNIT_TEST(TestComplexMapAsObject_UI32) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -791,7 +791,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_UI32) {          },          R"_({"UI32":{"1073741825":1073741825,"1073741826":1073741826,"1073741827":1073741827}})_");  } // TestComplexMapAsObject_UI32 - +   Y_UNIT_TEST(TestComplexMapAsObject_UI64) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -802,7 +802,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_UI64) {          },          R"_({"UI64":{"9223372036854775809":9223372036854775809,"9223372036854775810":9223372036854775810,"9223372036854775811":9223372036854775811}})_");  } // TestComplexMapAsObject_UI64 - +   Y_UNIT_TEST(TestComplexMapAsObject_SI32) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -813,7 +813,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_SI32) {          },          R"_({"SI32":{"1":1,"-2":-2,"3":3}})_");  } // TestComplexMapAsObject_SI32 - +   Y_UNIT_TEST(TestComplexMapAsObject_SI64) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -824,7 +824,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_SI64) {          },          R"_({"SI64":{"2147483649":2147483649,"-2147483650":-2147483650,"2147483651":2147483651}})_");  } // TestComplexMapAsObject_SI64 - +   Y_UNIT_TEST(TestComplexMapAsObject_FI32) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -835,7 +835,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_FI32) {          },          R"_({"FI32":{"1073741825":1073741825,"1073741826":1073741826,"1073741827":1073741827}})_");  } // TestComplexMapAsObject_FI32 - +   Y_UNIT_TEST(TestComplexMapAsObject_FI64) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -846,7 +846,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_FI64) {          },          R"_({"FI64":{"9223372036854775809":9223372036854775809,"9223372036854775810":9223372036854775810,"9223372036854775811":9223372036854775811}})_");  } // TestComplexMapAsObject_FI64 - +   Y_UNIT_TEST(TestComplexMapAsObject_SFI32) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -857,7 +857,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_SFI32) {          },          R"_({"SFI32":{"1":1,"-2":-2,"3":3}})_");  } // TestComplexMapAsObject_SFI32 - +   Y_UNIT_TEST(TestComplexMapAsObject_SFI64) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -868,7 +868,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_SFI64) {          },          R"_({"SFI64":{"2147483649":2147483649,"-2147483650":-2147483650,"2147483651":2147483651}})_");  } // TestComplexMapAsObject_SFI64 - +   Y_UNIT_TEST(TestComplexMapAsObject_Bool) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -878,7 +878,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_Bool) {          },          R"_({"Bool":{"true":true,"false":false}})_");  } // TestComplexMapAsObject_Bool - +   Y_UNIT_TEST(TestComplexMapAsObject_String) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -890,7 +890,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_String) {          },          R"_({"String":{"key1":"value1","key2":"value2","key3":"value3","":"value4"}})_");  } // TestComplexMapAsObject_String - +   Y_UNIT_TEST(TestComplexMapAsObject_Enum) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -901,7 +901,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_Enum) {          },          R"_({"Enum":{"key1":1,"key2":2,"key3":3}})_");  } // TestComplexMapAsObject_Enum - +   Y_UNIT_TEST(TestComplexMapAsObject_EnumString) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -953,7 +953,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_Float) {          },          R"_({"Float":{"key1":0.1,"key2":0.2,"key3":0.3}})_");  } // TestComplexMapAsObject_Float - +   Y_UNIT_TEST(TestComplexMapAsObject_Double) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -964,7 +964,7 @@ Y_UNIT_TEST(TestComplexMapAsObject_Double) {          },          R"_({"Double":{"key1":0.1,"key2":0.2,"key3":0.3}})_");  } // TestComplexMapAsObject_Double - +   Y_UNIT_TEST(TestComplexMapAsObject_Nested) {      TestComplexMapAsObject(          [](TComplexMapType& proto) { @@ -978,10 +978,10 @@ Y_UNIT_TEST(TestComplexMapAsObject_Nested) {          },          R"_({"Nested":{"key1":{"String":{"key":"value"}},"key2":{"String":{"key":"value"}},"key3":{"String":{"key":"value"}}}})_");  } // TestComplexMapAsObject_Nested - +   Y_UNIT_TEST(TestMapAsObjectConfigNotSet) {      TString modelStr(R"_({"Items":{"key":"value"}})_"); - +       TJson2ProtoConfig config;      UNIT_ASSERT_EXCEPTION_CONTAINS(          Json2Proto<TMapType>(modelStr, config), yexception, diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f5..7cfdd3b14fc 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -756,72 +756,72 @@ Y_UNIT_TEST(TestFieldNameMode) {          UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);      } -    // snake_case -    { -        TString modelStr(R"_({"string":"value"})_"); - -        TFlatOptional proto; -        proto.SetString("value"); -        TStringStream jsonStr; -        TProto2JsonConfig config; -        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase; - -        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); -        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); -    } -    { -        TString modelStr(R"_({"one_string":"value"})_"); - -        TFlatOptional proto; -        proto.SetOneString("value"); -        TStringStream jsonStr; -        TProto2JsonConfig config; -        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase; - -        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); -        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); -    } -    { -        TString modelStr(R"_({"one_two_string":"value"})_"); - -        TFlatOptional proto; -        proto.SetOneTwoString("value"); -        TStringStream jsonStr; -        TProto2JsonConfig config; -        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase; - -        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); -        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); -    } -    { -        TString modelStr(R"_({"a_b_c":"value","user_i_d":"value"})_"); - -        TFlatOptional proto; -        proto.SetABC("value"); -        proto.SetUserID("value"); -        TStringStream jsonStr; -        TProto2JsonConfig config; -        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase; - -        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); -        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); -    } - -    // snake_case_dense -    { -        TString modelStr(R"_({"abc":"value","user_id":"value"})_"); - -        TFlatOptional proto; -        proto.SetABC("value"); -        proto.SetUserID("value"); -        TStringStream jsonStr; -        TProto2JsonConfig config; -        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCaseDense; - -        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); -        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); -    } - +    // snake_case  +    {  +        TString modelStr(R"_({"string":"value"})_");  +  +        TFlatOptional proto;  +        proto.SetString("value");  +        TStringStream jsonStr;  +        TProto2JsonConfig config;  +        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;  +  +        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));  +        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);  +    }  +    {  +        TString modelStr(R"_({"one_string":"value"})_");  +  +        TFlatOptional proto;  +        proto.SetOneString("value");  +        TStringStream jsonStr;  +        TProto2JsonConfig config;  +        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;  +  +        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));  +        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);  +    }  +    {  +        TString modelStr(R"_({"one_two_string":"value"})_");  +  +        TFlatOptional proto;  +        proto.SetOneTwoString("value");  +        TStringStream jsonStr;  +        TProto2JsonConfig config;  +        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;  +  +        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));  +        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);  +    }  +    {  +        TString modelStr(R"_({"a_b_c":"value","user_i_d":"value"})_");  +  +        TFlatOptional proto;  +        proto.SetABC("value");  +        proto.SetUserID("value");  +        TStringStream jsonStr;  +        TProto2JsonConfig config;  +        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;  +  +        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));  +        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);  +    }  +  +    // snake_case_dense  +    {  +        TString modelStr(R"_({"abc":"value","user_id":"value"})_");  +  +        TFlatOptional proto;  +        proto.SetABC("value");  +        proto.SetUserID("value");  +        TStringStream jsonStr;  +        TProto2JsonConfig config;  +        config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCaseDense;  +  +        UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));  +        UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);  +    }  +       // Original case, repeated      {          TString modelStr(R"_({"I32":[1,2]})_"); @@ -933,22 +933,22 @@ Y_UNIT_TEST(TestMap) {  Y_UNIT_TEST(TestMapAsObject) {      TMapType proto; - +       auto& items = *proto.MutableItems();      items["key1"] = "value1";      items["key2"] = "value2";      items["key3"] = "value3"; - +       TString modelStr(R"_({"Items":{"key3":"value3","key2":"value2","key1":"value1"}})_"); - +       TStringStream jsonStr;      TProto2JsonConfig config;      config.MapAsObject = true;      UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); - +       UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);  } // TestMapAsObject - +   Y_UNIT_TEST(TestMapWTF) {      TMapType proto; diff --git a/library/cpp/protobuf/json/ut/test.proto b/library/cpp/protobuf/json/ut/test.proto index 0fa996fd413..0a04bdd5284 100644 --- a/library/cpp/protobuf/json/ut/test.proto +++ b/library/cpp/protobuf/json/ut/test.proto @@ -1,7 +1,7 @@  package NProtobufJsonTest;  enum EEnum { -    E_0 = 0; +    E_0 = 0;       E_1 = 1;      E_2 = 2;      E_3 = 3; @@ -31,8 +31,8 @@ message TFlatOptional {      optional string OneString = 17;      optional string OneTwoString = 18; -    optional string ABC = 19; -    optional string UserID = 20; +    optional string ABC = 19;  +    optional string UserID = 20;   };  message TFlatRequired { @@ -59,8 +59,8 @@ message TFlatRequired {      required string OneString = 17;      required string OneTwoString = 18; -    required string ABC = 19; -    required string UserID = 20; +    required string ABC = 19;  +    required string UserID = 20;   };  message TFlatRepeated { @@ -87,8 +87,8 @@ message TFlatRepeated {      repeated string OneString = 17;      repeated string OneTwoString = 18; -    repeated string ABC = 19; -    repeated string UserID = 20; +    repeated string ABC = 19;  +    repeated string UserID = 20;   };  message TFlatDefault { @@ -115,8 +115,8 @@ message TFlatDefault {      optional string OneString = 17 [default = "string"];      optional string OneTwoString = 18 [default = "string"]; -    optional string ABC = 19 [default = "abc"]; -    optional string UserID = 20 [default = "some_id"]; +    optional string ABC = 19 [default = "abc"];  +    optional string UserID = 20 [default = "some_id"];   };  message TCompositeOptional { @@ -147,30 +147,30 @@ message TEnumValueGeneratorType {      optional EEnum Enum = 1;  }; -message TComplexMapType { -    map<int32, int32> I32 = 1; -    map<int64, int64> I64 = 2; -    map<uint32, uint32> UI32 = 3; -    map<uint64, uint64> UI64 = 4; -    map<sint32, sint32> SI32 = 5; -    map<sint64, sint64> SI64 = 6; -    map<fixed32, fixed32> FI32 = 7; -    map<fixed64, fixed64> FI64 = 8; -    map<sfixed32, sfixed32> SFI32 = 9; -    map<sfixed64, sfixed64> SFI64 = 10; - -    map<bool, bool> Bool = 11; - -    map<string, string> String = 12; - -    map<string, EEnum> Enum = 13; - -    map<string, float> Float = 14; -    map<string, double> Double = 15; - -    map<string, TComplexMapType> Nested = 16; -}; - +message TComplexMapType {  +    map<int32, int32> I32 = 1;  +    map<int64, int64> I64 = 2;  +    map<uint32, uint32> UI32 = 3;  +    map<uint64, uint64> UI64 = 4;  +    map<sint32, sint32> SI32 = 5;  +    map<sint64, sint64> SI64 = 6;  +    map<fixed32, fixed32> FI32 = 7;  +    map<fixed64, fixed64> FI64 = 8;  +    map<sfixed32, sfixed32> SFI32 = 9;  +    map<sfixed64, sfixed64> SFI64 = 10;  +  +    map<bool, bool> Bool = 11;  +  +    map<string, string> String = 12;  +  +    map<string, EEnum> Enum = 13;  +  +    map<string, float> Float = 14;  +    map<string, double> Double = 15;  +  +    map<string, TComplexMapType> Nested = 16;  +};  +   message TWithJsonName {      optional int32 my_upper = 1 [json_name = "My-Upper"];      optional int32 My_lower = 2 [json_name = "my-lower"]; diff --git a/library/cpp/protobuf/json/util.cpp b/library/cpp/protobuf/json/util.cpp index 53a065eee23..0145d6c6749 100644 --- a/library/cpp/protobuf/json/util.cpp +++ b/library/cpp/protobuf/json/util.cpp @@ -1,54 +1,54 @@ -#include "util.h" - -#include <util/string/ascii.h> - -namespace { -    void ToSnakeCaseImpl(TString* const name, std::function<bool(const char)> requiresUnderscore) { -        bool requiresChanges = false; -        size_t size = name->size(); -        for (size_t i = 0; i < name->size(); i++) { -            if (IsAsciiUpper(name->at(i))) { -                requiresChanges = true; -                if (i > 0 && requiresUnderscore(name->at(i - 1))) { -                    size++; -                } -            } -        } - -        if (!requiresChanges) { -            return; -        } - -        if (size != name->size()) { -            TString result; -            result.reserve(size); -            for (size_t i = 0; i < name->size(); i++) { -                const char c = name->at(i); -                if (IsAsciiUpper(c)) { -                    if (i > 0 && requiresUnderscore(name->at(i - 1))) { -                        result += '_'; -                    } -                    result += AsciiToLower(c); -                } else { -                    result += c; -                } -            } -            *name = std::move(result); -        } else { -            name->to_lower(); -        } -    } -} - -namespace NProtobufJson { -    void ToSnakeCase(TString* const name) { -        ToSnakeCaseImpl(name, [](const char prev) { return prev != '_'; }); -    } - -    void ToSnakeCaseDense(TString* const name) { -        ToSnakeCaseImpl(name, [](const char prev) { return prev != '_' && !IsAsciiUpper(prev); }); -    } +#include "util.h"  +  +#include <util/string/ascii.h>  +  +namespace {  +    void ToSnakeCaseImpl(TString* const name, std::function<bool(const char)> requiresUnderscore) {  +        bool requiresChanges = false;  +        size_t size = name->size();  +        for (size_t i = 0; i < name->size(); i++) {  +            if (IsAsciiUpper(name->at(i))) {  +                requiresChanges = true;  +                if (i > 0 && requiresUnderscore(name->at(i - 1))) {  +                    size++;  +                }  +            }  +        }  +  +        if (!requiresChanges) {  +            return;  +        }  +  +        if (size != name->size()) {  +            TString result;  +            result.reserve(size);  +            for (size_t i = 0; i < name->size(); i++) {  +                const char c = name->at(i);  +                if (IsAsciiUpper(c)) {  +                    if (i > 0 && requiresUnderscore(name->at(i - 1))) {  +                        result += '_';  +                    }  +                    result += AsciiToLower(c);  +                } else {  +                    result += c;  +                }  +            }  +            *name = std::move(result);  +        } else {  +            name->to_lower();  +        }  +    }  +}  +namespace NProtobufJson {  +    void ToSnakeCase(TString* const name) {  +        ToSnakeCaseImpl(name, [](const char prev) { return prev != '_'; });  +    }  +  +    void ToSnakeCaseDense(TString* const name) {  +        ToSnakeCaseImpl(name, [](const char prev) { return prev != '_' && !IsAsciiUpper(prev); });  +    }  +       bool EqualsIgnoringCaseAndUnderscores(TStringBuf s1, TStringBuf s2) {          size_t i1 = 0, i2 = 0; @@ -73,4 +73,4 @@ namespace NProtobufJson {          return (i1 == s1.size() && i2 == s2.size());      } -} +}  diff --git a/library/cpp/protobuf/json/util.h b/library/cpp/protobuf/json/util.h index d93342d3f88..8dbdcedabdc 100644 --- a/library/cpp/protobuf/json/util.h +++ b/library/cpp/protobuf/json/util.h @@ -1,14 +1,14 @@ -#pragma once - -#include <util/generic/string.h> - -namespace NProtobufJson { -    void ToSnakeCase(TString* const name); - -    void ToSnakeCaseDense(TString* const name); - +#pragma once  +  +#include <util/generic/string.h>  +  +namespace NProtobufJson {  +    void ToSnakeCase(TString* const name);  + +    void ToSnakeCaseDense(TString* const name);  +       /**       * "FOO_BAR" ~ "foo_bar" ~ "fooBar"       */      bool EqualsIgnoringCaseAndUnderscores(TStringBuf s1, TStringBuf s2); -} +}  diff --git a/library/cpp/protobuf/json/ya.make b/library/cpp/protobuf/json/ya.make index 2f2c75cfdb2..76a73d3a7c1 100644 --- a/library/cpp/protobuf/json/ya.make +++ b/library/cpp/protobuf/json/ya.make @@ -11,8 +11,8 @@ SRCS(      proto2json.cpp      proto2json_printer.cpp      string_transform.cpp -    util.h -    util.cpp +    util.h  +    util.cpp   )  PEERDIR( | 
