[#120] protogen: Marshal enum as string

Be compatible with protojson.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-10-01 09:42:40 +03:00
parent f812b1ae5b
commit 805da79319
9 changed files with 121 additions and 20 deletions

View file

@ -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\":"

View file

@ -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\":"

View file

@ -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('}')
}

View file

@ -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\":"

View file

@ -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\":"

View file

@ -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\":"

View file

@ -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\":"

View file

@ -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:

View file

@ -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: