From 805da79319a4b84b5d2a8a0c6f2234ff37f55acd Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 1 Oct 2024 09:42:40 +0300 Subject: [PATCH] [#120] protogen: Marshal enum as string Be compatible with protojson. Signed-off-by: Evgenii Stratonikov --- acl/grpc/types_frostfs.pb.go | 35 +++++++++++++++++++---- ape/grpc/types_frostfs.pb.go | 7 ++++- netmap/grpc/types_frostfs.pb.go | 21 ++++++++++++-- object/grpc/service_frostfs.pb.go | 21 ++++++++++++-- object/grpc/types_frostfs.pb.go | 14 +++++++-- refs/grpc/types_frostfs.pb.go | 14 +++++++-- session/grpc/types_frostfs.pb.go | 14 +++++++-- util/proto/test/custom/test_frostfs.pb.go | 7 ++++- util/protogen/internalgengo/json.go | 8 +++++- 9 files changed, 121 insertions(+), 20 deletions(-) diff --git a/acl/grpc/types_frostfs.pb.go b/acl/grpc/types_frostfs.pb.go index 2b116bd..b055167 100644 --- a/acl/grpc/types_frostfs.pb.go +++ b/acl/grpc/types_frostfs.pb.go @@ -356,12 +356,22 @@ func (x *EACLRecord_Filter) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"headerType\":" out.RawString(prefix[1:]) - out.Int32(int32(x.HeaderType)) + v := int32(x.HeaderType) + if vv, ok := HeaderType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"matchType\":" out.RawString(prefix) - out.Int32(int32(x.MatchType)) + v := int32(x.MatchType) + if vv, ok := MatchType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"key\":" @@ -570,7 +580,12 @@ func (x *EACLRecord_Target) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"role\":" out.RawString(prefix[1:]) - out.Int32(int32(x.Role)) + v := int32(x.Role) + if vv, ok := Role_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"keys\":" @@ -812,12 +827,22 @@ func (x *EACLRecord) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"operation\":" out.RawString(prefix[1:]) - out.Int32(int32(x.Operation)) + v := int32(x.Operation) + if vv, ok := Operation_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"action\":" out.RawString(prefix) - out.Int32(int32(x.Action)) + v := int32(x.Action) + if vv, ok := Action_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"filters\":" diff --git a/ape/grpc/types_frostfs.pb.go b/ape/grpc/types_frostfs.pb.go index 216415b..e19d9f6 100644 --- a/ape/grpc/types_frostfs.pb.go +++ b/ape/grpc/types_frostfs.pb.go @@ -159,7 +159,12 @@ func (x *ChainTarget) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"type\":" out.RawString(prefix[1:]) - out.Int32(int32(x.Type)) + v := int32(x.Type) + if vv, ok := TargetType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"name\":" diff --git a/netmap/grpc/types_frostfs.pb.go b/netmap/grpc/types_frostfs.pb.go index e7597ea..43af103 100644 --- a/netmap/grpc/types_frostfs.pb.go +++ b/netmap/grpc/types_frostfs.pb.go @@ -288,7 +288,12 @@ func (x *Filter) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"op\":" out.RawString(prefix) - out.Int32(int32(x.Op)) + v := int32(x.Op) + if vv, ok := Operation_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"value\":" @@ -572,7 +577,12 @@ func (x *Selector) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"clause\":" out.RawString(prefix) - out.Int32(int32(x.Clause)) + v := int32(x.Clause) + if vv, ok := Clause_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"attribute\":" @@ -1627,7 +1637,12 @@ func (x *NodeInfo) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"state\":" out.RawString(prefix) - out.Int32(int32(x.State)) + v := int32(x.State) + if vv, ok := NodeInfo_State_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } out.RawByte('}') } diff --git a/object/grpc/service_frostfs.pb.go b/object/grpc/service_frostfs.pb.go index dea1b5b..188d032 100644 --- a/object/grpc/service_frostfs.pb.go +++ b/object/grpc/service_frostfs.pb.go @@ -4117,7 +4117,12 @@ func (x *SearchRequest_Body_Filter) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"matchType\":" out.RawString(prefix[1:]) - out.Int32(int32(x.MatchType)) + v := int32(x.MatchType) + if vv, ok := MatchType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"key\":" @@ -6246,7 +6251,12 @@ func (x *GetRangeHashRequest_Body) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"type\":" out.RawString(prefix) - out.Int32(int32(x.Type)) + v := int32(x.Type) + if vv, ok := grpc.ChecksumType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } out.RawByte('}') } @@ -6659,7 +6669,12 @@ func (x *GetRangeHashResponse_Body) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"type\":" out.RawString(prefix[1:]) - out.Int32(int32(x.Type)) + v := int32(x.Type) + if vv, ok := grpc.ChecksumType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"hashList\":" diff --git a/object/grpc/types_frostfs.pb.go b/object/grpc/types_frostfs.pb.go index 4233417..9fc2769 100644 --- a/object/grpc/types_frostfs.pb.go +++ b/object/grpc/types_frostfs.pb.go @@ -323,7 +323,12 @@ func (x *ShortHeader) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"objectType\":" out.RawString(prefix) - out.Int32(int32(x.ObjectType)) + v := int32(x.ObjectType) + if vv, ok := ObjectType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"payloadLength\":" @@ -1659,7 +1664,12 @@ func (x *Header) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"objectType\":" out.RawString(prefix) - out.Int32(int32(x.ObjectType)) + v := int32(x.ObjectType) + if vv, ok := ObjectType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"homomorphicHash\":" diff --git a/refs/grpc/types_frostfs.pb.go b/refs/grpc/types_frostfs.pb.go index 35febe9..5f35eba 100644 --- a/refs/grpc/types_frostfs.pb.go +++ b/refs/grpc/types_frostfs.pb.go @@ -933,7 +933,12 @@ func (x *Signature) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"scheme\":" out.RawString(prefix) - out.Int32(int32(x.Scheme)) + v := int32(x.Scheme) + if vv, ok := SignatureScheme_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } out.RawByte('}') } @@ -1270,7 +1275,12 @@ func (x *Checksum) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"type\":" out.RawString(prefix[1:]) - out.Int32(int32(x.Type)) + v := int32(x.Type) + if vv, ok := ChecksumType_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"sum\":" diff --git a/session/grpc/types_frostfs.pb.go b/session/grpc/types_frostfs.pb.go index 41ff5d9..c92ab28 100644 --- a/session/grpc/types_frostfs.pb.go +++ b/session/grpc/types_frostfs.pb.go @@ -362,7 +362,12 @@ func (x *ObjectSessionContext) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"verb\":" out.RawString(prefix[1:]) - out.Int32(int32(x.Verb)) + v := int32(x.Verb) + if vv, ok := ObjectSessionContext_Verb_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"target\":" @@ -600,7 +605,12 @@ func (x *ContainerSessionContext) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"verb\":" out.RawString(prefix[1:]) - out.Int32(int32(x.Verb)) + v := int32(x.Verb) + if vv, ok := ContainerSessionContext_Verb_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } { const prefix string = ",\"wildcard\":" diff --git a/util/proto/test/custom/test_frostfs.pb.go b/util/proto/test/custom/test_frostfs.pb.go index 97efa51..005d6d1 100644 --- a/util/proto/test/custom/test_frostfs.pb.go +++ b/util/proto/test/custom/test_frostfs.pb.go @@ -614,7 +614,12 @@ func (x *Primitives) MarshalEasyJSON(out *jwriter.Writer) { { const prefix string = ",\"fieldH\":" out.RawString(prefix) - out.Int32(int32(x.FieldH)) + v := int32(x.FieldH) + if vv, ok := Primitives_SomeEnum_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + } } switch xx := x.FieldM.(type) { case *Primitives_FieldMa: diff --git a/util/protogen/internalgengo/json.go b/util/protogen/internalgengo/json.go index f7cd284..95a06d5 100644 --- a/util/protogen/internalgengo/json.go +++ b/util/protogen/internalgengo/json.go @@ -195,7 +195,13 @@ func emitJSONFieldWrite(g *protogen.GeneratedFile, f *protogen.Field, name strin case protoreflect.BoolKind: template = "out.Bool(%s)" case protoreflect.EnumKind: - template = "out.Int32(int32(%s))" + enumType := fieldType(g, f).String() + template = `v := int32(%s) + if vv, ok := ` + enumType + `_name[v]; ok { + out.String(vv) + } else { + out.Int32(v) + }` case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: template = "out.Int32(%s)" case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: