-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (92 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bytes"
"flag"
"fmt"
"go/parser"
"go/token"
"go/printer"
"go/ast"
"strings"
"os"
"github.com/Sirupsen/logrus"
)
const (
ReplaceTag = "// @replace-tag "
)
func replaceTagsInFile(input string) {
fset := token.NewFileSet() // positions are relative to fset
// Parse the file containing protobuf definitions
f, err := parser.ParseFile(fset, input, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
return
}
replacedTags := false
for _, d := range f.Decls {
// Get General Declaration
gDecl, ok := d.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range gDecl.Specs {
// Get Type Declaration
tSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
// Get Struct Declaration
sDecl, ok := tSpec.Type.(*ast.StructType)
if !ok {
continue
}
for _, field := range sDecl.Fields.List {
if field.Doc == nil {
continue
}
for _, comment := range field.Doc.List {
if strings.Contains(comment.Text, ReplaceTag) {
// Found a field
commentTag := strings.TrimPrefix(comment.Text, ReplaceTag)
commentTagKey := strings.Split(commentTag, ":")[0]
fieldTagTokens := strings.Split(strings.Trim(field.Tag.Value, "`"), " ")
foundFlt := ""
for _, flt := range fieldTagTokens {
if strings.HasPrefix(flt, commentTagKey) {
// Found the tag
foundFlt = flt
}
}
if foundFlt == "" {
continue
}
fmt.Printf("Replacing tag (%v) -> (%v) \n", foundFlt, commentTag)
r := strings.NewReplacer(foundFlt, commentTag)
field.Tag.Value = r.Replace(field.Tag.Value)
replacedTags = true
}
}
}
}
}
if replacedTags {
var buf bytes.Buffer
printer.Fprint(&buf, fset, f)
f, err := os.Create(input)
if err != nil {
fmt.Println("Unable to write protobuf files: ", err)
return
}
f.WriteString(buf.String())
}
}
func main() {
var input string
flag.StringVar(&input, "input", "", "path to input protobuf file")
flag.Parse()
if len(input) == 0 {
logrus.Errorf("input file not provided")
return
}
replaceTagsInFile(input)
}