Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
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
}

// 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) == 0 {
return 0
}
i, err := strconv.Atoi(tags[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it may be better to use proteus:",101" notation - it will be easier to extend later.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your suggestion, as you wish.

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