forked from TrueCloudLab/frostfs-api-go
[#120] protogen: Omit empty fields from JSON output
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
805da79319
commit
eeb754c327
19 changed files with 20 additions and 11 deletions
BIN
accounting/grpc/service_frostfs.pb.go
generated
BIN
accounting/grpc/service_frostfs.pb.go
generated
Binary file not shown.
BIN
accounting/grpc/types_frostfs.pb.go
generated
BIN
accounting/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
acl/grpc/types_frostfs.pb.go
generated
BIN
acl/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
ape/grpc/types_frostfs.pb.go
generated
BIN
ape/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
apemanager/grpc/service_frostfs.pb.go
generated
BIN
apemanager/grpc/service_frostfs.pb.go
generated
Binary file not shown.
BIN
container/grpc/service_frostfs.pb.go
generated
BIN
container/grpc/service_frostfs.pb.go
generated
Binary file not shown.
BIN
container/grpc/types_frostfs.pb.go
generated
BIN
container/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
lock/grpc/types_frostfs.pb.go
generated
BIN
lock/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
netmap/grpc/service_frostfs.pb.go
generated
BIN
netmap/grpc/service_frostfs.pb.go
generated
Binary file not shown.
BIN
netmap/grpc/types_frostfs.pb.go
generated
BIN
netmap/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
object/grpc/service_frostfs.pb.go
generated
BIN
object/grpc/service_frostfs.pb.go
generated
Binary file not shown.
BIN
object/grpc/types_frostfs.pb.go
generated
BIN
object/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
refs/grpc/types_frostfs.pb.go
generated
BIN
refs/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
session/grpc/service_frostfs.pb.go
generated
BIN
session/grpc/service_frostfs.pb.go
generated
Binary file not shown.
BIN
session/grpc/types_frostfs.pb.go
generated
BIN
session/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
status/grpc/types_frostfs.pb.go
generated
BIN
status/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
tombstone/grpc/types_frostfs.pb.go
generated
BIN
tombstone/grpc/types_frostfs.pb.go
generated
Binary file not shown.
BIN
util/proto/test/custom/test_frostfs.pb.go
generated
BIN
util/proto/test/custom/test_frostfs.pb.go
generated
Binary file not shown.
|
@ -147,8 +147,11 @@ func emitJSONMarshal(g *protogen.GeneratedFile, msg *protogen.Message) {
|
|||
g.P("func (x *", msg.GoIdent.GoName, ") MarshalEasyJSON(out *", jwriterPackage.Ident("Writer"), ") {")
|
||||
g.P(`if x == nil { out.RawString("null"); return }`)
|
||||
|
||||
if len(msg.Fields) != 0 {
|
||||
g.P("first := true")
|
||||
}
|
||||
g.P("out.RawByte('{')")
|
||||
for i, f := range msg.Fields {
|
||||
for _, f := range msg.Fields {
|
||||
if f.Oneof != nil {
|
||||
if f.Oneof.Fields[0] != f {
|
||||
continue
|
||||
|
@ -157,29 +160,35 @@ func emitJSONMarshal(g *protogen.GeneratedFile, msg *protogen.Message) {
|
|||
g.P("switch xx := x.", f.Oneof.GoName, ".(type) {")
|
||||
for _, ff := range f.Oneof.Fields {
|
||||
g.P("case *", ff.GoIdent, ":")
|
||||
emitJSONFieldWrite(g, ff, "xx", i == 0)
|
||||
emitJSONFieldWrite(g, ff, "xx")
|
||||
}
|
||||
g.P("}")
|
||||
continue
|
||||
}
|
||||
emitJSONFieldWrite(g, f, "x", i == 0)
|
||||
emitJSONFieldWrite(g, f, "x")
|
||||
}
|
||||
g.P("out.RawByte('}')")
|
||||
g.P("}")
|
||||
}
|
||||
|
||||
func emitJSONFieldWrite(g *protogen.GeneratedFile, f *protogen.Field, name string, first bool) {
|
||||
func emitJSONFieldWrite(g *protogen.GeneratedFile, f *protogen.Field, name string) {
|
||||
g.P("{")
|
||||
defer g.P("}")
|
||||
|
||||
g.P("const prefix string = ", `",\"`, fieldJSONName(f), `\":"`)
|
||||
if first {
|
||||
g.P("out.RawString(prefix[1:])")
|
||||
} else {
|
||||
g.P("out.RawString(prefix)")
|
||||
}
|
||||
|
||||
selector := name + "." + f.GoName
|
||||
|
||||
isNotDefault := notNil
|
||||
if f.Desc.IsList() {
|
||||
isNotDefault = notEmpty
|
||||
} else if f.Desc.Kind() != protoreflect.MessageKind {
|
||||
_, isNotDefault = easyprotoKindInfo(f.Desc.Kind())
|
||||
}
|
||||
g.P("if ", isNotDefault(selector), "{")
|
||||
defer g.P("}")
|
||||
|
||||
g.P("if !first { out.RawByte(','); } else { first = false; }")
|
||||
g.P("const prefix string = ", `"\"`, fieldJSONName(f), `\":"`)
|
||||
g.P("out.RawString(prefix)")
|
||||
if f.Desc.IsList() {
|
||||
selector += "[i]"
|
||||
g.P("out.RawByte('[')")
|
||||
|
|
Loading…
Reference in a new issue