Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for proteus:",(protobuf field id)" in golang tag #109

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: go

go:
- 1.7
- 1.8
- 1.10.x
- 1.11.x
- tip

matrix:
Expand Down
5 changes: 5 additions & 0 deletions example/categories/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ package categories
type CategoryOptions struct {
ShowPrices bool
CanBuy bool
// The next field was deleted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The next field was deleted.
// The next field was added initially (with a number 3) and was deprecated later.

// Field3 int

// 4th field, use `proteus:",4"` forward compatible
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 4th field, use `proteus:",4"` forward compatible
// Field4 uses `proteus:",4"` for forward compatibility, see Field3 comment above.

Field4 int `proteus:",4"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also update the generated files in the example folder - this field will affect some of those files.

}
6 changes: 6 additions & 0 deletions protobuf/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ func (t *Transformer) transformField(pkg *Package, msg *Message, field *scanner.
Repeated: repeated,
}

// If this field has set pos in tag,
// use customize protobuf positin.
if field.Pos != 0 {
f.Pos = field.Pos
}

// []byte is the only repeated type that maps to
// a non-repeated type in protobuf, so we handle
// it a bit differently.
Expand Down
24 changes: 24 additions & 0 deletions protobuf/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (s *TransformerSuite) TestTransformField() {
name string
typ scanner.Type
expected *Field
pos int
}{
{
"Foo",
Expand All @@ -279,6 +280,7 @@ func (s *TransformerSuite) TestTransformField() {
Type: NewBasic("int64"),
Options: Options{"(gogoproto.casttype)": NewStringValue("int")},
},
0,
},
{
"Bar",
Expand All @@ -288,6 +290,7 @@ func (s *TransformerSuite) TestTransformField() {
Type: NewBasic("bytes"),
Options: Options{},
},
0,
},
{
"BazBar",
Expand All @@ -298,6 +301,7 @@ func (s *TransformerSuite) TestTransformField() {
Repeated: true,
Options: Options{"(gogoproto.casttype)": NewStringValue("int")},
},
0,
},
{
"CustomID",
Expand All @@ -310,6 +314,7 @@ func (s *TransformerSuite) TestTransformField() {
"(gogoproto.casttype)": NewStringValue("int"),
},
},
0,
},
{
"NullableType",
Expand All @@ -319,6 +324,7 @@ func (s *TransformerSuite) TestTransformField() {
Type: NewNamed("my.pckg", "hello"),
Options: Options{},
},
0,
},
{
"NonNullableType",
Expand All @@ -330,11 +336,13 @@ func (s *TransformerSuite) TestTransformField() {
"(gogoproto.nullable)": NewLiteralValue("false"),
},
},
0,
},
{
"Invalid",
scanner.NewBasic("complex64"),
nil,
0,
},
{
"MyEnum",
Expand All @@ -344,6 +352,7 @@ func (s *TransformerSuite) TestTransformField() {
Type: NewNamed("my.pckg", "MyEnum"),
Options: Options{},
},
0,
},
{
"MyAlias",
Expand All @@ -361,6 +370,7 @@ func (s *TransformerSuite) TestTransformField() {
"(gogoproto.casttype)": NewStringValue("my/pckg.MyAlias"),
},
},
0,
},
{
"MyRepeatedAlias",
Expand All @@ -376,6 +386,18 @@ func (s *TransformerSuite) TestTransformField() {
),
Options: Options{},
},
0,
},
{
"ProtoID",
scanner.NewBasic("int64"),
&Field{
Name: "proto_id",
Type: NewBasic("int64"),
Options: Options{"(gogoproto.customname)": NewStringValue("ProtoID")},
Pos: 101,
},
101,
},
}

Expand All @@ -387,13 +409,15 @@ func (s *TransformerSuite) TestTransformField() {
f := s.t.transformField(&Package{}, &Message{}, &scanner.Field{
Name: c.name,
Type: c.typ,
Pos: c.pos,
}, 0)
if c.expected == nil {
s.Nil(f, c.name)
} else {
s.Equal(c.expected.Name, f.Name, fmt.Sprintf("Name in %s", c.name))
s.assertType(c.expected.Type, f.Type, c.name)
s.Equal(c.expected.Options, f.Options, fmt.Sprintf("Options in %s", c.name))
s.Equal(c.expected.Pos, f.Pos, fmt.Sprintf("Proto id in %d", c.pos))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions scanner/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ type Field struct {
Docs
Name string
Type Type
Pos int
dennwc marked this conversation as resolved.
Show resolved Hide resolved
}

// Func is either a function or a method. Receiver will be nil in functions,
Expand Down
13 changes: 13 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -294,6 +295,7 @@ func scanStruct(s *Struct, elem *types.Struct) *Struct {
f := &Field{
Name: v.Name(),
Type: scanType(v.Type()),
Pos: getProtoID(v, tags),
}
if f.Type == nil {
continue
Expand Down Expand Up @@ -390,6 +392,17 @@ func isIgnoredField(f *types.Var, tags []string) bool {
return !f.Exported() || (len(tags) > 0 && tags[0] == "-")
}

func getProtoID(f *types.Var, tags []string) int {
if len(tags) < 2 {
return 0
}
i, err := strconv.Atoi(tags[1])
if err != nil {
return 0
}
return i
}

func objectsInScope(scope *types.Scope) (objs []types.Object) {
for _, n := range scope.Names() {
obj := scope.Lookup(n)
Expand Down
30 changes: 30 additions & 0 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,36 @@ func TestScanStruct(t *testing.T) {
},
},
},
{
"struct with proto field id tag",
types.NewStruct(
[]*types.Var{
mkField("Foo", types.Typ[types.Int], false),
},
[]string{`proteus:",101"`},
),
&Struct{
Fields: []*Field{
{Name: "Foo", Type: NewBasic("int"), Pos: 101},
},
},
},
{
"invalid struct with proto field id tag",
types.NewStruct(
[]*types.Var{
mkField("Foo", types.Typ[types.Int], false),
mkField("Bar", types.Typ[types.Int], false),
},
[]string{`proteus:","`, `proteus:",lol"`},
),
&Struct{
Fields: []*Field{
{Name: "Foo", Type: NewBasic("int"), Pos: 0},
{Name: "Bar", Type: NewBasic("int"), Pos: 0},
},
},
},
{
"struct with unsupported type",
types.NewStruct(
Expand Down