Skip to content

Commit 482a696

Browse files
committed
add support for proteus:"(protobuf field id)" in golang tag
1 parent 9175115 commit 482a696

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

protobuf/transform.go

+6
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ func (t *Transformer) transformField(pkg *Package, msg *Message, field *scanner.
256256
Repeated: repeated,
257257
}
258258

259+
// If this field has set pos in tag,
260+
// use customize protobuf positin.
261+
if field.Pos != 0 {
262+
f.Pos = field.Pos
263+
}
264+
259265
// []byte is the only repeated type that maps to
260266
// a non-repeated type in protobuf, so we handle
261267
// it a bit differently.

scanner/package.go

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ type Field struct {
302302
Docs
303303
Name string
304304
Type Type
305+
Pos int
305306
}
306307

307308
// Func is either a function or a method. Receiver will be nil in functions,

scanner/scanner.go

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"sort"
10+
"strconv"
1011
"strings"
1112
"sync"
1213

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

395+
func getProtoID(f *types.Var, tags []string) int {
396+
if len(tags) == 0 {
397+
return 0
398+
}
399+
i, err := strconv.Atoi(tags[0])
400+
if err != nil {
401+
return 0
402+
}
403+
return i
404+
}
405+
393406
func objectsInScope(scope *types.Scope) (objs []types.Object) {
394407
for _, n := range scope.Names() {
395408
obj := scope.Lookup(n)

0 commit comments

Comments
 (0)