Small fixes for protogen plugin #107
11 changed files with 22 additions and 25 deletions
1
Makefile
1
Makefile
|
@ -79,6 +79,7 @@ $(BIN)/protogen:
|
||||||
|
|
||||||
protoc-test: protoc $(BIN)/protogen
|
protoc-test: protoc $(BIN)/protogen
|
||||||
@$(PROTOC_DIR)/bin/protoc \
|
@$(PROTOC_DIR)/bin/protoc \
|
||||||
|
--go_out=. --go_opt=paths=source_relative \
|
||||||
--plugin=protoc-gen-go-frostfs=$(abspath $(BIN)/protogen) \
|
--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=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 \
|
--go-frostfs_opt=module=git.frostfs.info/TrueCloudLab/frostfs-api-go/v2 \
|
||||||
|
|
BIN
ape/grpc/types_frostfs.pb.go
generated
BIN
ape/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
session/grpc/types_frostfs.pb.go
generated
BIN
session/grpc/types_frostfs.pb.go
generated
Binary file not shown.
|
@ -49,6 +49,8 @@ func TestStableMarshalSingle(t *testing.T) {
|
||||||
{name: "fixed32", input: &generated.Primitives{FieldK: nonZero[uint32]()}},
|
{name: "fixed32", input: &generated.Primitives{FieldK: nonZero[uint32]()}},
|
||||||
{name: "enum, positive", input: &generated.Primitives{FieldH: generated.Primitives_POSITIVE}},
|
{name: "enum, positive", input: &generated.Primitives{FieldH: generated.Primitives_POSITIVE}},
|
||||||
{name: "enum, negative", input: &generated.Primitives{FieldH: generated.Primitives_NEGATIVE}},
|
{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 {
|
for _, tc := range marshalCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
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.FieldJ, b.FieldJ)
|
||||||
require.Equal(t, a.FieldK, b.FieldK)
|
require.Equal(t, a.FieldK, b.FieldK)
|
||||||
require.EqualValues(t, a.FieldH, b.FieldH)
|
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) {
|
func repPrimitivesEqual(t *testing.T, a *generated.RepPrimitives, b *test.RepPrimitives) {
|
||||||
|
|
BIN
util/proto/test/custom/test_frostfs.pb.go
generated
BIN
util/proto/test/custom/test_frostfs.pb.go
generated
Binary file not shown.
BIN
util/proto/test/test.pb.go
generated
BIN
util/proto/test/test.pb.go
generated
Binary file not shown.
|
@ -22,6 +22,14 @@ message Primitives {
|
||||||
NEGATIVE = -1;
|
NEGATIVE = -1;
|
||||||
}
|
}
|
||||||
SomeEnum field_h = 300;
|
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 {
|
message RepPrimitives {
|
||||||
|
|
|
@ -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:
|
// 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.
|
// 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.
|
// 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 {
|
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("func (x *", msg.GoIdent.GoName, ") Set", ff.GoName, "(v ", ft, ") {")
|
||||||
g.P("x.", ff.Oneof.GoName, " = &", ff.GoIdent, "{", ff.GoName, ": v}")
|
g.P("x.", ff.Oneof.GoName, " = &", ff.GoIdent, "{", ff.GoName, ": v}")
|
||||||
g.P("}")
|
g.P("}")
|
||||||
} else {
|
} 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("func (x *", msg.GoIdent.GoName, ") Set", ff.GoName, "(v *", ff.GoIdent, ") {")
|
||||||
g.P("x.", ff.Oneof.GoName, " = v")
|
g.P("x.", ff.Oneof.GoName, " = v")
|
||||||
g.P("}")
|
g.P("}")
|
||||||
|
|
||||||
ft := fieldType(g, ff)
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -159,8 +159,6 @@ func emitJSONMarshal(g *protogen.GeneratedFile, msg *protogen.Message) {
|
||||||
g.P("case *", ff.GoIdent, ":")
|
g.P("case *", ff.GoIdent, ":")
|
||||||
emitJSONFieldWrite(g, ff, "xx", i == 0)
|
emitJSONFieldWrite(g, ff, "xx", i == 0)
|
||||||
}
|
}
|
||||||
g.P("default:")
|
|
||||||
g.P(`out.RawString("null")`)
|
|
||||||
g.P("}")
|
g.P("}")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/protogen/internalgengo"
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/protogen/internalgengo"
|
||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
|
@ -12,18 +10,10 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
var flags flag.FlagSet
|
var flags flag.FlagSet
|
||||||
genFuzz := flags.Bool("fuzz", false, "generate fuzz tests")
|
genFuzz := flags.Bool("fuzz", false, "generate fuzz tests")
|
||||||
yamlConfigPath := flags.String("config", "", "path to the configuration")
|
|
||||||
|
|
||||||
protogen.Options{
|
protogen.Options{
|
||||||
ParamFunc: flags.Set,
|
ParamFunc: flags.Set,
|
||||||
}.Run(func(gen *protogen.Plugin) error {
|
}.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 {
|
for _, f := range gen.Files {
|
||||||
if f.Generate {
|
if f.Generate {
|
||||||
internalgengo.GenerateFile(gen, f)
|
internalgengo.GenerateFile(gen, f)
|
||||||
|
|
Loading…
Reference in a new issue