Small fixes for protogen plugin #107

Merged
fyrchik merged 8 commits from fyrchik/frostfs-api-go:protogen-config into master 2024-09-04 19:51:17 +00:00
11 changed files with 22 additions and 25 deletions

View file

@ -79,6 +79,7 @@ $(BIN)/protogen:
protoc-test: protoc $(BIN)/protogen
@$(PROTOC_DIR)/bin/protoc \
--go_out=. --go_opt=paths=source_relative \
--plugin=protoc-gen-go-frostfs=$(abspath $(BIN)/protogen) \
--go-frostfs_opt=Mutil/proto/test/test.proto=git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test/custom \
--go-frostfs_opt=module=git.frostfs.info/TrueCloudLab/frostfs-api-go/v2 \

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -49,6 +49,8 @@ func TestStableMarshalSingle(t *testing.T) {
{name: "fixed32", input: &generated.Primitives{FieldK: nonZero[uint32]()}},
{name: "enum, positive", input: &generated.Primitives{FieldH: generated.Primitives_POSITIVE}},
{name: "enum, negative", input: &generated.Primitives{FieldH: generated.Primitives_NEGATIVE}},
{name: "oneof, first", input: &generated.Primitives{FieldM: &generated.Primitives_FieldMa{FieldMa: []byte{4, 2}}}},
{name: "oneof, second", input: &generated.Primitives{FieldM: &generated.Primitives_FieldMe{FieldMe: nonZero[uint32]()}}},
}
for _, tc := range marshalCases {
t.Run(tc.name, func(t *testing.T) {
@ -97,6 +99,9 @@ func primitivesEqual(t *testing.T, a *generated.Primitives, b *test.Primitives)
require.Equal(t, a.FieldJ, b.FieldJ)
require.Equal(t, a.FieldK, b.FieldK)
require.EqualValues(t, a.FieldH, b.FieldH)
require.Equal(t, a.GetFieldMa(), b.GetFieldMa())
require.Equal(t, a.GetFieldMe(), b.GetFieldMe())
require.Equal(t, a.GetFieldAux().GetInnerField(), b.GetFieldAux().GetInnerField())
}
func repPrimitivesEqual(t *testing.T, a *generated.RepPrimitives, b *test.RepPrimitives) {

Binary file not shown.

Binary file not shown.

View file

@ -22,6 +22,14 @@ message Primitives {
NEGATIVE = -1;
}
SomeEnum field_h = 300;
message Aux { uint32 inner_field = 1; }
oneof field_m {
bytes field_ma = 401;
uint32 field_me = 402;
Aux field_aux = 403;
}
}
message RepPrimitives {

View file

@ -126,29 +126,24 @@ 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("}")
ft := fieldType(g, ff)
emitGetterSetter(g, ff.GoIdent.GoName, ff.GoName, ft.String(), "nil")
emitGetterSetter(g, ff.GoIdent.GoName, ff.GoName, ft.String(), fieldDefaultValue(ff))
}
}

View file

@ -159,8 +159,6 @@ func emitJSONMarshal(g *protogen.GeneratedFile, msg *protogen.Message) {
g.P("case *", ff.GoIdent, ":")
emitJSONFieldWrite(g, ff, "xx", i == 0)
}
g.P("default:")
g.P(`out.RawString("null")`)
g.P("}")
continue
}

View file

@ -2,8 +2,6 @@ package main
import (
"flag"
"fmt"
"os"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/protogen/internalgengo"
"google.golang.org/protobuf/compiler/protogen"
@ -12,18 +10,10 @@ import (
func main() {
var flags flag.FlagSet
genFuzz := flags.Bool("fuzz", false, "generate fuzz tests")
yamlConfigPath := flags.String("config", "", "path to the configuration")
protogen.Options{
ParamFunc: flags.Set,
}.Run(func(gen *protogen.Plugin) error {
if *yamlConfigPath != "" {
s, e := os.Getwd()
fmt.Fprintln(os.Stderr, s, e)
data, err := os.ReadFile(*yamlConfigPath)
fmt.Fprintln(os.Stderr, string(data), err)
}
for _, f := range gen.Files {
if f.Generate {
internalgengo.GenerateFile(gen, f)