-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
166 lines (151 loc) · 3.65 KB
/
module.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"strings"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
"github.com/syzoj/protoc-gen-gotype/gotype"
)
type module struct {
*pgs.ModuleBase
ctx pgsgo.Context
}
func newModule() pgs.Module {
return &module{ModuleBase: &pgs.ModuleBase{}}
}
func (m *module) InitContext(c pgs.BuildContext) {
m.ModuleBase.InitContext(c)
m.ctx = pgsgo.InitContext(c.Parameters())
}
func (m *module) Name() string {
return "gotype"
}
func (m *module) Execute(targets map[string]pgs.File, packages map[string]pgs.Package) []pgs.Artifact {
for _, f := range targets {
v := makeVisitor(m)
if err := pgs.Walk(v, f); err != nil {
panic(err)
}
f2 := m.BuildContext.JoinPath(f.InputPath().SetExt(".pb.go").Base())
fs := token.NewFileSet()
fn, err := parser.ParseFile(fs, f2, nil, parser.ParseComments)
if err != nil {
panic(err)
}
v2 := &goVisitor{nodes: v.nodes}
ast.Walk(v2, fn)
var b strings.Builder
err = printer.Fprint(&b, fs, fn)
if err != nil {
panic(err)
}
m.OverwriteGeneratorFile(f2, b.String())
}
return m.Artifacts()
}
type visitor struct {
pgs.Visitor
pgs.DebuggerCommon
nodes map[string]map[string]string
}
func makeVisitor(d pgs.DebuggerCommon) *visitor {
return &visitor{
Visitor: pgs.NilVisitor(),
DebuggerCommon: d,
nodes: make(map[string]map[string]string),
}
}
func (v *visitor) VisitPackage(pgs.Package) (pgs.Visitor, error) { return v, nil }
func (v *visitor) VisitFile(pgs.File) (pgs.Visitor, error) { return v, nil }
func (v *visitor) VisitMessage(pgs.Message) (pgs.Visitor, error) { return v, nil }
func (v *visitor) VisitField(f pgs.Field) (pgs.Visitor, error) {
var gtype string
ok, err := f.Extension(gotype.E_Gotype, >ype)
if err != nil {
return nil, err
}
if ok {
var msgName string
if f.InOneOf() {
msgName = f.Message().Name().UpperCamelCase().String() + "_" + f.Name().UpperCamelCase().String()
} else {
msgName = f.Message().Name().UpperCamelCase().String()
}
m := v.nodes[msgName]
if m == nil {
m = make(map[string]string)
}
m[f.Name().UpperCamelCase().String()] = gtype
v.nodes[msgName] = m
}
return v, nil
}
type goVisitor struct {
nodes map[string]map[string]string
}
func (v *goVisitor) Visit(n ast.Node) ast.Visitor {
if t, ok := n.(*ast.TypeSpec); ok {
if _, ok := t.Type.(*ast.StructType); ok {
return &fieldVisitor{fields: v.nodes[t.Name.String()]}
}
}
if f, ok := n.(*ast.FuncDecl); ok {
var typeName string
if f.Recv != nil {
if starExpr, ok := f.Recv.List[0].Type.(*ast.StarExpr); ok {
if n, ok := starExpr.X.(*ast.Ident); ok {
typeName = n.Name
}
}
}
if typeName == "" {
return nil
}
fields := v.nodes[typeName]
if fields == nil {
return nil
}
var fieldName string
if f.Name != nil {
name := f.Name.Name
if strings.HasPrefix(name, "Get") {
fieldName = f.Name.Name[3:]
}
}
if fieldName == "" {
return nil
}
newType, ok := fields[fieldName]
if !ok {
return nil
}
if f.Type.Results == nil || len(f.Type.Results.List) != 1 {
panic(fmt.Sprintf("Message %s method %s does not have exactly one result"))
}
res := f.Type.Results.List[0]
res2 := new(ast.Field)
*res2 = *res
res2.Type = ast.NewIdent(newType)
f.Type.Results.List[0] = res2
return nil
}
return v
}
type fieldVisitor struct {
fields map[string]string
}
func (v *fieldVisitor) Visit(n ast.Node) ast.Visitor {
if f, ok := n.(*ast.Field); ok {
newName, ok := v.fields[f.Names[0].String()]
if !ok {
return nil
}
f.Type = &ast.StarExpr{X: ast.NewIdent(newName)}
return nil
}
return v
}