From aa68585b53040cca8c1367f9915e8627a8cb86c5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 26 Aug 2024 14:33:18 +0300 Subject: [PATCH] protogen: Unify oneof getters with default protoc plugin Signed-off-by: Evgenii Stratonikov --- util/protogen/internalgengo/file.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/util/protogen/internalgengo/file.go b/util/protogen/internalgengo/file.go index cd54fa0..ff1c830 100644 --- a/util/protogen/internalgengo/file.go +++ b/util/protogen/internalgengo/file.go @@ -126,23 +126,18 @@ func emitOneofGettersSetters(g *protogen.GeneratedFile, msg *protogen.Message, f // For some reason protoc generates different code for oneof message/non-message fields: // 1. For message we have 2 level struct wrapping and setters use inner type. // 2. For other types we also have 2 level wrapping, but setters use outer type. + ft := fieldType(g, ff) + + g.P("func (x *", msg.GoIdent.GoName, ") Get", ff.GoName, "() ", ft, " {") + g.P("if xx, ok := x.Get", ff.Oneof.GoName, "().(*", ff.GoIdent, "); ok { return xx.", ff.GoName, " }") + g.P("return ", fieldDefaultValue(ff)) + g.P("}") + if ff.Desc.Kind() == protoreflect.MessageKind { - ft := fieldType(g, ff) - - g.P("func (x *", msg.GoIdent.GoName, ") Get", ff.GoName, "() ", ft, " {") - g.P("if xx, ok := x.Get", ff.Oneof.GoName, "().(*", ff.GoIdent, "); ok { return xx.", ff.GoName, " }") - g.P("return nil") - g.P("}") - g.P("func (x *", msg.GoIdent.GoName, ") Set", ff.GoName, "(v ", ft, ") {") g.P("x.", ff.Oneof.GoName, " = &", ff.GoIdent, "{", ff.GoName, ": v}") g.P("}") } else { - g.P("func (x *", msg.GoIdent.GoName, ") Get", ff.GoName, "() *", ff.GoIdent, " {") - g.P("if xx, ok := x.Get", ff.Oneof.GoName, "().(*", ff.GoIdent, "); ok { return xx }") - g.P("return nil") - g.P("}") - g.P("func (x *", msg.GoIdent.GoName, ") Set", ff.GoName, "(v *", ff.GoIdent, ") {") g.P("x.", ff.Oneof.GoName, " = v") g.P("}")