-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
275 lines (230 loc) · 6.82 KB
/
generator.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package setter
import (
"fmt"
"go/ast"
"log"
"reflect"
"strings"
"github.com/lawrsp/pigo/generator/builder"
"github.com/lawrsp/pigo/generator"
"github.com/lawrsp/pigo/generator/parser"
"github.com/lawrsp/pigo/generator/tagutil"
)
type CustomAssign struct {
Source parser.Type
Target parser.Type
Assign parser.Type
Check string
}
type AssignConfig struct {
Source string
Target string
Assign string
Check string
}
type Config struct {
Type string
Receiver string
Target string
Name string
Withmap bool
WithOldMap bool
CheckDiff bool
Output string
MapTag string
Imports map[string]string
Assigns []*AssignConfig
}
type Generator struct {
generator.Generator
TagName string
Type parser.Type
Receiver parser.Type
FuncType parser.Type
Withmap bool
WithOldMap bool
CheckDiff bool
Reverse bool
Assigns []*CustomAssign
MapTag string
updateV *builder.Variable
oldV *builder.Variable
}
func NewGenerator() *Generator {
return &Generator{}
}
func (g *Generator) PrepareAssigns(configAssigns []*AssignConfig) {
assigns := []*CustomAssign{}
for _, v := range configAssigns {
cp := &CustomAssign{}
cp.Source = g.ReduceTypeSrc(v.Source)
if cp.Source == nil {
log.Fatalf("type %s not reduced", v.Source)
}
cp.Target = g.ReduceTypeSrc(v.Target)
if cp.Target == nil {
log.Fatalf("type %s not reduced", v.Target)
}
cp.Assign = g.ReduceTypeSrc(v.Assign)
if cp.Assign == nil {
log.Fatalf("type %s not reduced", v.Assign)
}
cp.Check = v.Check
assigns = append(assigns, cp)
}
g.Assigns = assigns
}
func (g *Generator) PrepareTask(config *Config) {
g.Type = parser.TypeWithPointer(g.File.ReduceTypeSrc(config.Type))
funcName := config.Name
var fnt *parser.FuncType
if config.Receiver != "" {
g.Receiver = parser.TypeWithPointer(g.File.ReduceTypeSrc(config.Receiver))
if funcName == "" {
funcName = fmt.Sprintf("Set%s", config.Type)
}
fnt = &parser.FuncType{
Receiver: parser.NewField(g.Receiver, "r", ""),
Params: []*parser.Field{
parser.NewField(g.Type, "info", ""),
},
Results: []*parser.Field{},
}
g.Reverse = false
} else if config.Target != "" {
log.Printf("target: %s", config.Target)
tRecv := g.File.ReduceTypeSrc(config.Target)
if tRecv == nil {
log.Fatalf("cannot reduce type %s", config.Target)
}
g.Receiver = parser.TypeWithPointer(tRecv)
if funcName == "" {
funcName = fmt.Sprintf("Set%s", config.Target)
}
fnt = &parser.FuncType{
Receiver: parser.NewField(g.Type, "t", ""),
Params: []*parser.Field{
parser.NewField(g.Receiver, "target", ""),
},
Results: []*parser.Field{},
}
g.Reverse = true
}
if config.Withmap {
mapType := parser.MapType(parser.NewBasicType("string"), parser.NewBasicType("interface"))
fnt.Results = append(fnt.Results, parser.NewField(mapType, "updated", ""))
}
if config.WithOldMap {
mapType := parser.MapType(parser.NewBasicType("string"), parser.NewBasicType("interface"))
fnt.Results = append(fnt.Results, parser.NewField(mapType, "old", ""))
}
g.MapTag = config.MapTag
g.FuncType = parser.TypeWithFile(parser.TypeWithName(fnt, funcName), g.File)
g.Withmap = config.Withmap
g.WithOldMap = config.WithOldMap
g.CheckDiff = config.CheckDiff
}
func (g *Generator) GetCallFunc(field *builder.Field) parser.Type {
tags := field.Field.Tag
if len(tags) == 0 {
return nil
}
stag := reflect.StructTag(tags).Get(g.TagName)
if len(stag) == 0 {
return nil
}
stags := strings.Split(stag, ",")
if len(stags) < 1 {
return nil
}
callFunc := stags[1]
expr := parser.TypeExprInFile(g.Receiver, g.File)
t := g.File.ReduceType(builder.DotExpr(expr, ast.NewIdent(callFunc)))
// log.Printf("finded call func: %s", t)
return t
}
func (g *Generator) GetFieldMapName(field *builder.Field) string {
// fmt.Println("=== g.MapTag:", g.MapTag, field.Field.Tag)
if g.MapTag != "" {
tags := field.Field.Tag
return tagutil.GetTagValue(reflect.StructTag(tags), g.MapTag)
}
return field.Field.Name()
}
func (g *Generator) AssignerFunc(field *builder.Field) func(builder.Builder, *builder.Variable, ast.Expr) {
fieldName := g.GetFieldMapName(field)
return func(inBuilder builder.Builder, v *builder.Variable, value ast.Expr) {
if g.oldV != nil {
collector := builder.NewVariable(v.Type).WriteOnly().WithExpr(g.oldV.StringKeyItemExpr(fieldName))
builder.AddVariableAssign(inBuilder, collector, v.Ident())
}
builder.AddVariableAssign(inBuilder, v, value)
if g.updateV != nil {
collector := builder.NewVariable(v.Type).WriteOnly().WithExpr(g.updateV.StringKeyItemExpr(fieldName))
builder.AddVariableAssign(inBuilder, collector, v.Ident())
}
}
}
func (g *Generator) Run() {
outer := builder.NewFile(nil, g.File)
var fb *builder.FuncBuilder
if g.Reverse {
fb = builder.NewFunction(outer, nil, g.FuncType, nil)
} else {
fb = builder.NewFunction(outer, nil, g.FuncType, nil)
}
tpaths := []*parser.TPath{}
for _, assign := range g.Assigns {
if _, ok := assign.Assign.Underlying().(*parser.FuncType); ok {
tpaths = append(tpaths, parser.NewTPath(assign.Source, assign.Target).WithFunction(assign.Assign))
} else {
tpaths = append(tpaths, parser.NewTPath(assign.Source, assign.Target).WithTypeConversion(assign.Assign))
}
}
structSetter := builder.NewStructSetter(fb, g.TagName, nil, tpaths)
if g.CheckDiff {
structSetter = structSetter.WithCheckDiff()
}
srcV := builder.GetVariable(fb, g.Type, builder.READ_MODE, builder.Scope_Function)
var checkV *builder.Variable
if g.Reverse {
checkV = builder.GetVariable(fb, g.Receiver, builder.READ_MODE, builder.Scope_Function)
} else {
checkV = srcV
}
builder.AddCheckReturn(fb, checkV.CheckNilExpr(true), nil)
if g.WithOldMap {
mapType := parser.MapType(parser.NewBasicType("string"), parser.NewBasicType("interface"))
oldV := builder.NewVariable(mapType).WithName("old").ReadOnly()
g.oldV = builder.AddVariableAssign(fb, oldV, parser.TypeInitValue(mapType, g.File))
}
if g.Withmap {
mapType := parser.MapType(parser.NewBasicType("string"), parser.NewBasicType("interface"))
updateV := builder.NewVariable(mapType).WithName("updated").ReadOnly()
g.updateV = builder.AddVariableAssign(fb, updateV, parser.TypeInitValue(mapType, g.File))
}
if g.Withmap || g.WithOldMap {
structSetter = structSetter.WithAssigner(g.AssignerFunc)
}
skip := 0
if g.Receiver.EqualTo(g.Type) {
skip = 1
}
dstV := builder.GetVariableWithSkip(fb, g.Receiver, builder.READ_MODE, builder.Scope_Function, skip)
if ok := structSetter.TryAssign(srcV, dstV); ok {
fb.Block().Add(structSetter)
}
if fb.HasResults() {
builder.AddSuccessReturn(fb)
}
outer.Add(fb)
}
func (g *Generator) Generate(config *Config) error {
g.Prepare(".", nil, config.Output)
g.PrepareImports(config.Imports)
g.PrepareAssigns(config.Assigns)
g.PrepareTask(config)
g.Run()
g.Output(config.Output)
return nil
}