/
generate.go
87 lines (81 loc) · 1.94 KB
/
generate.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
package main
import (
"strings"
"text/template"
)
func generateEqualAndDiff(opt generateEqualAndDiffOptions) error {
funcMaps := template.FuncMap{
"HasPrefix": strings.HasPrefix,
"TrimPrefix": strings.TrimPrefix,
"Title": toTitle,
"CamelCase": toCamelCase,
"LowerCase": toLowerCase,
"JSON": toJSON,
}
tmpl, err := template.New("generate.tmpl").Funcs(funcMaps).Parse(tmplEqualAndDiff)
if err != nil {
return err
}
data := map[string]interface{}{
"Mode": opt.Mode,
"Name": opt.Name,
"Type": opt.Type,
"Fields": opt.Fields,
"NeedsOptions": opt.NeedsOptions,
"NeedsOptionsIndex": opt.NeedsOptionsIndex,
"IsBasicType": opt.IsBasicType,
"IsComplex": opt.IsComplex,
"IsComparable": opt.IsComparable,
"IsPointer": opt.IsPointer,
}
functions := map[string]interface{}{
"Functions": []interface{}{
map[string]interface{}{
"Name": "Equal",
"Data": data,
},
map[string]interface{}{
"Name": "Diff",
"Data": data,
},
},
}
err = tmpl.Execute(opt.File, functions)
if err != nil {
return err
}
return generateCompareTests(opt)
}
func generateCompareTests(opt generateEqualAndDiffOptions) error {
if opt.Mode == "array" {
return nil
}
funcMaps := template.FuncMap{
"HasPrefix": strings.HasPrefix,
"Title": toTitle,
"CamelCase": toCamelCase,
}
tmpl, err := template.New("generate.tmpl").Funcs(funcMaps).Parse(tmplCompareTest)
if err != nil {
return err
}
hasIndex := false
for _, file := range opt.Fields {
if file.Name == "Index" && file.Type == "*int64" {
hasIndex = true
break
}
}
err = tmpl.Execute(opt.FileTest, map[string]interface{}{
"TestType": []string{"Equal", "Diff"},
"Name": opt.Name,
"HasIndex": hasIndex,
"Fields": opt.Fields,
"FieldCount": len(opt.Fields),
"PackageName": opt.PackageName,
})
if err != nil {
return err
}
return nil
}