-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.go
579 lines (501 loc) · 14.7 KB
/
merge.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package merge
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"log"
"os"
"reflect"
"strings"
"text/template"
"github.com/forta-network/go-merge-types/utils"
"gopkg.in/yaml.v3"
)
func Run(configPath string) (*MergeConfig, []byte, error) {
b, err := os.ReadFile(configPath)
if err != nil {
return nil, nil, err
}
var config MergeConfig
if err := yaml.Unmarshal(b, &config); err != nil {
return &config, nil, err
}
for _, source := range config.Sources {
// fix package source dirs relative to the config path
source.Package.SourceDir = utils.RelativePath(configPath, source.Package.SourceDir)
// update known tags
config.Output.KnownTags = append(config.Output.KnownTags, source.Tag)
}
// find default tag from first source if default tag was not specified
if len(config.Output.DefaultTag) == 0 {
for _, source := range config.Sources {
config.Output.DefaultTag = source.Tag
break
}
}
b, err = Generate(&config)
if err != nil {
return &config, nil, err
}
return &config, b, nil
}
func Generate(config *MergeConfig) ([]byte, error) {
var pkgs []*ast.Package
for _, source := range config.Sources {
pkgs = append(pkgs, LoadPackage(source.Package.SourceDir))
}
var impls []*SourceImplementation
for i, pkg := range pkgs {
impls = append(impls, FindImplementation(pkg, config.Sources[i].Type))
}
return mergeAndGenerate(config, impls)
}
func LoadPackage(pkgDir string) *ast.Package {
fset := token.NewFileSet()
foundPkgs, err := parser.ParseDir(fset, pkgDir, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
for _, p := range foundPkgs {
return p
}
log.Fatalf("no package found at: %s", pkgDir)
return nil
}
type SourceImplementation struct {
Package *ast.Package
Object *ast.Object
Constructor *ast.FuncDecl
Methods []*ast.FuncDecl
Types []*ast.TypeSpec
Imports []*ast.ImportSpec
}
func (sourceImpl *SourceImplementation) GetType(expr ast.Expr) (*ast.TypeSpec, bool) {
searchType := deref(typeString("", "", expr))
for _, typ := range sourceImpl.Types {
if typ.Name.Name == searchType {
return typ, true
}
}
return nil, false
}
func (sourceImpl *SourceImplementation) GetImport(input string) (string, bool) {
parts := strings.Split(input, ".")
if len(parts) == 2 {
input = strings.Trim(parts[0], "*")
}
for _, imp := range sourceImpl.Imports {
// find by name (alias)
if imp.Name != nil && imp.Name.Name == input {
return fmt.Sprintf("%s %s", imp.Name.Name, imp.Path.Value), true
}
// find from path
parts := strings.Split(strings.Trim(imp.Path.Value, `"`), "/")
altName := parts[len(parts)-1]
if altName == input {
return imp.Path.Value, true
}
}
return "", false
}
func FindImplementation(pkg *ast.Package, implName string) *SourceImplementation {
var impl SourceImplementation
impl.Package = pkg
constructorName := fmt.Sprintf("New%s", implName)
for _, file := range pkg.Files {
// collect all imports from all files
impl.Imports = append(impl.Imports, file.Imports...)
if file.Scope != nil {
var ok bool
obj, ok := file.Scope.Objects[implName]
if ok && impl.Object == nil {
impl.Object = obj
}
}
}
if impl.Object == nil {
log.Fatalf("implementation not found in %s", pkg.Name)
return nil
}
for _, file := range pkg.Files {
for _, decl := range file.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
// find the constructor
if funcDecl.Recv == nil && funcDecl.Name.Name == constructorName {
impl.Constructor = funcDecl
continue
}
// find the implemented methods
if funcDecl.Recv != nil && len(funcDecl.Recv.List) > 0 {
recvList := funcDecl.Recv.List
if recvList[0].Type.(*ast.StarExpr).X.(*ast.Ident).Name == implName {
impl.Methods = append(impl.Methods, funcDecl)
}
}
}
}
for _, object := range file.Scope.Objects {
if typeDecl, ok := object.Decl.(*ast.TypeSpec); ok {
impl.Types = append(impl.Types, typeDecl)
}
}
}
if impl.Constructor == nil {
log.Fatalf("constructor %s was not found for type %s in package %s", constructorName, implName, pkg.Name)
return nil
}
return &impl
}
func mergeAndGenerate(config *MergeConfig, sourceImpls []*SourceImplementation) ([]byte, error) {
// fix empty package aliases: find package name from ast and append source index i to the name.
for i, source := range config.Sources {
if len(source.Package.Alias) > 0 {
continue
}
sourceImpl := sourceImpls[i]
source.Package.Alias = fmt.Sprintf("%s_%d", sourceImpl.Package.Name, i+1)
}
// find output type init args
for i, sourceImpl := range sourceImpls {
params := sourceImpl.Constructor.Type.Params
if params == nil {
continue
}
pkgName := config.Sources[i].Package.Alias
for _, param := range params.List {
foundParam, ok := isNewParam(pkgName, i, param, config.Output.InitArgs)
foundParam.SourceIndex = i
if ok {
config.Output.InitArgs = append(config.Output.InitArgs, foundParam)
}
// add source type init args
config.Sources[i].InitArgs = append(config.Sources[i].InitArgs, foundParam)
}
}
// collect all variations of all methods and their input & output types under bucket methods
allMethods := make([]*Method, 0)
for i, sourceImpl := range sourceImpls {
pkgName := config.Sources[i].Package.Alias
for _, sourceMethod := range sourceImpl.Methods {
// create a method variation
var variation Variation
variation.SourceIndex = i
variation.Tag = config.Sources[i].Tag
methodName := sourceMethod.Name.Name
variation.Name = methodName
// find out variation method return type
var resultsList []*ast.Field
if sourceMethod.Type.Results != nil {
resultsList = sourceMethod.Type.Results.List
}
var ret *ast.Field
switch len(resultsList) {
case 0:
variation.NoReturn = true
case 1:
v := resultsList[0]
if types.ExprString(v.Type) == "error" {
variation.OnlyError = true
} else {
ret = v
}
case 2:
v1, v2 := resultsList[0], resultsList[1]
if types.ExprString(v2.Type) != "error" {
log.Printf("warning: expected %s.%s.%s to return (<any>, error) - ignoring\n", pkgName, sourceImpl.Object.Name, methodName)
continue
}
ret = v1
default:
log.Printf("warning: %s.%s.%s has unsupported return list - ignoring\n", pkgName, sourceImpl.Object.Name, methodName)
continue
}
// find the bucket (merged) method
// if doesn't exist, create
var method *Method
for _, m := range allMethods {
if m.Name == methodName {
method = m
break
}
}
if method == nil {
method = &Method{
Name: methodName,
ReturnType: ReturnType{
Name: methodName + "Output",
},
}
allMethods = append(allMethods, method)
}
// add this definition as a variation of the method
method.Variations = append(method.Variations, &variation)
// set args
for _, param := range sourceMethod.Type.Params.List {
field := convertField(pkgName, i, param)
field.SourceIndex = i
variation.Args = append(variation.Args, field)
}
if ret == nil {
continue
}
// anonymous struct
structType, ok := ret.Type.(*ast.StructType)
if ok {
variation.MergeReturnedStruct = true
for _, param := range structType.Fields.List {
variation.ReturnedFields = append(variation.ReturnedFields, &Field{
SourceIndex: i,
Name: param.Names[0].Name,
Type: typeString("", pkgName, param.Type),
})
}
continue
}
// local struct
if isLocalType(true, ret.Type) {
localType, ok := sourceImpl.GetType(ret.Type)
if !ok {
log.Fatalf("local type not found: %s", typeString(pkgName, "", ret.Type))
}
if structType, ok := localType.Type.(*ast.StructType); ok {
if !hasUnexportedField(structType.Fields.List) {
// local struct with exported fields
variation.MergeReturnedStruct = true
for _, param := range structType.Fields.List {
variation.ReturnedFields = append(variation.ReturnedFields, &Field{
SourceIndex: i,
Name: param.Names[0].Name,
Type: typeString("", pkgName, param.Type),
})
}
} else {
// local struct with unexported fields
variation.ReturnedFields = append(variation.ReturnedFields, &Field{
SourceIndex: i,
Name: pkgNameToMethodPrefix(pkgName) + "Result",
Type: typeString("", pkgName, ret.Type),
})
}
}
}
// local non-struct or imported
if len(variation.ReturnedFields) == 0 {
retType := typeString("", pkgName, ret.Type)
retNameSuffix := deref(retType)
if strings.Contains(retNameSuffix, ".") {
retNameSuffix = strings.Title(strings.Join(strings.Split(retNameSuffix, "."), ""))
}
variation.ReturnedFields = append(variation.ReturnedFields, &Field{
SourceIndex: i,
Name: "Value",
Type: retType,
})
}
}
}
// construct all bucket method inputs and outputs
for _, method := range allMethods {
for _, variation := range method.Variations {
// merge args
method.Args = mergeFields(variation.Args, method.Args)
// merge return fields
method.ReturnType.Fields = mergeFields(variation.ReturnedFields, method.ReturnType.Fields)
}
// decide on return value
switch len(method.ReturnType.Fields) {
case 0:
method.NoReturn = true
case 1:
method.SingleReturn = true
method.ReturnType.Name = method.ReturnType.Fields[0].Type
}
}
// merge all imports for inputs and outputs
for _, method := range allMethods {
for _, field := range method.Args {
impl := sourceImpls[field.SourceIndex]
imp, ok := impl.GetImport(field.Type)
if ok {
config.Output.Imports = mergeImport(imp, config.Output.Imports)
}
}
for _, field := range method.ReturnType.Fields {
impl := sourceImpls[field.SourceIndex]
imp, ok := impl.GetImport(field.Type)
if ok {
config.Output.Imports = mergeImport(imp, config.Output.Imports)
}
}
}
// merge init arg imports
for _, field := range config.Output.InitArgs {
impl := sourceImpls[field.SourceIndex]
imp, ok := impl.GetImport(field.Type)
if ok {
config.Output.Imports = mergeImport(imp, config.Output.Imports)
}
}
// set all methods in the config
config.Output.Methods = allMethods
// rewrite some names: constructor (init) args, method names, method inputs, method outputs
rewriter := config.Output.Rewrite
for _, initArg := range config.Output.InitArgs {
initArg.Name = rewriter.Rewrite(initArg.Name)
initArg.Type = rewriter.Rewrite(initArg.Type)
}
for _, method := range config.Output.Methods {
method.Name = rewriter.Rewrite(method.Name)
for _, arg := range method.Args {
arg.Name = rewriter.Rewrite(arg.Name)
arg.Type = rewriter.Rewrite(arg.Type)
}
method.ReturnType.Name = rewriter.Rewrite(method.ReturnType.Name)
for _, field := range method.ReturnType.Fields {
field.Name = rewriter.Rewrite(field.Name)
field.Type = rewriter.Rewrite(field.Type)
}
}
// finally, execute the config on the template and return
buffer := new(bytes.Buffer)
tmpl := template.Must(template.New("").Parse(codeTemplate))
if err := tmpl.Execute(buffer, config); err != nil {
return nil, err
}
return []byte(strings.TrimSpace(string(buffer.Bytes()))), nil
}
var altParamIndex int = 0
func getAltSuffix() string {
altParamIndex++
return fmt.Sprintf("Alt%d", altParamIndex)
}
func isNewParam(pkgName string, sourceIndex int, param *ast.Field, knownParams []*Field) (*Field, bool) {
foundParam := convertField(pkgName, sourceIndex, param)
for _, knownParam := range knownParams {
if foundParam.Name == knownParam.Name && foundParam.Type == knownParam.Type {
return foundParam, false
}
}
// if there is a known param that is of a different type, use alt name but include
for _, knownParam := range knownParams {
if foundParam.Name == knownParam.Name && foundParam.Type != knownParam.Type {
foundParam.Name += getAltSuffix()
return foundParam, true
}
}
return foundParam, true
}
func convertField(pkgName string, sourceIndex int, astField *ast.Field) *Field {
var field Field
field.Name = astField.Names[0].Name
field.SourceIndex = sourceIndex
typ := typeString("", pkgName, astField.Type)
if len(typ) == 0 {
panic(fmt.Sprintf("unhandled field type %s", reflect.TypeOf(astField.Type)))
}
field.Type = typ
return &field
}
func typeString(name string, pkgName string, expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
ident := t.String()
// resolve exported local types in the package
if (name == "" || name == "*") && strings.ToUpper(string(ident[0])) == string(ident[0]) && len(pkgName) > 0 {
return name + pkgName + "." + t.Name
}
return name + t.Name
case *ast.StarExpr:
return typeString("*", pkgName, t.X)
case *ast.SelectorExpr:
return name + typeString("", pkgName, t.X) + "." + t.Sel.Name
case *ast.ArrayType:
ret := name + "["
if t.Len != nil {
ret += types.ExprString(t.Len)
}
return ret + "]" + typeString("", pkgName, t.Elt)
case *ast.ChanType:
chanStr := "chan"
switch t.Dir {
case ast.SEND:
chanStr += "<-"
case ast.RECV:
chanStr = "<-" + chanStr
}
return name + chanStr + " " + typeString("", pkgName, t.Value)
case *ast.MapType:
return fmt.Sprintf("map[%s]%s", typeString("", pkgName, t.Key), typeString("", pkgName, t.Value))
default:
return name + types.ExprString(expr)
}
}
func deref(name string) string {
if name[0] == '*' {
return name[1:]
}
return name
}
func isLocalType(is bool, expr ast.Expr) bool {
switch t := expr.(type) {
case *ast.Ident:
// if first char is upper case
if strings.ToUpper(string(t.Name[0])) == string(t.Name[0]) {
return true && is
}
return false
case *ast.StarExpr:
return isLocalType(true && is, t.X)
case *ast.SelectorExpr:
return false
default:
return false
}
}
func mergeFields(from, to []*Field) []*Field {
for _, fromField := range from {
var exists bool
for _, toField := range to {
if fromField.Name == toField.Name && fromField.Type == toField.Type {
exists = true
break
}
if fromField.Name == toField.Name && fromField.Type != toField.Type {
fromField.Name += getAltSuffix()
break
}
}
if !exists {
to = append(to, fromField)
}
}
return to
}
func mergeImport(imp string, imports []string) []string {
for _, oldImp := range imports {
if imp == oldImp {
return imports
}
}
imports = append(imports, imp)
return imports
}
func hasUnexportedField(fields []*ast.Field) bool {
for _, field := range fields {
firstLetter := string(field.Names[0].Name[0])
if strings.ToLower(firstLetter) == string(firstLetter) {
return true
}
}
return false
}
func pkgNameToMethodPrefix(pkgName string) string {
parts := strings.Split(pkgName, "_")
for i, part := range parts {
parts[i] = strings.Title(part)
}
return strings.Join(parts, "")
}