-
Notifications
You must be signed in to change notification settings - Fork 6
/
generator.go
126 lines (114 loc) · 2.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
package main
import (
"bytes"
"fmt"
"strings"
"text/template"
"github.com/iancoleman/strcase"
"golang.org/x/tools/imports"
)
var templ = `// Code generated by github.com/Bin-Huang/newc; DO NOT EDIT.
package {{.PkgName}}
import (
{{ range .Imports -}}
{{ if .Name -}}
{{.Name}} {{.Path}}
{{ else -}}
{{.Path}}
{{ end -}}
{{ end -}}
)
{{ range .Constructors }}
// {{.Name}} Create a new {{.Struct}}
func {{.Name}}({{.Params}}) {{if not .ValueFlag}}*{{end -}} {{.Struct}} {
{{ if .InitFlag -}}
s := {{if not .ValueFlag}}&{{end -}} {{.Struct}} {
{{.Fields}}
}
s.init()
return s
{{ else -}}
return {{if not .ValueFlag}}&{{end -}} {{.Struct}} {
{{.Fields}}
}
{{ end -}}
}
{{ end }}
`
// GenerateCode generate constructors code
func GenerateCode(pkgName string, importInfos []ImportInfo, structInfos []StructInfo) (string, error) {
// remove duplicate imports
importInfos = filterUniqueImports(importInfos)
// generate code with template
t, err := template.New("").Parse(templ)
if err != nil {
return "", err
}
data := o{
"PkgName": pkgName,
"Imports": importInfos,
"Constructor": []o{},
}
constructors := []o{}
for _, structInfo := range structInfos {
params := []string{}
fields := []string{}
for _, field := range structInfo.Fields {
if field.Skipped {
continue
}
params = append(params, fmt.Sprintf("%v %v", toLowerCamel(field.Name), field.Type))
fields = append(fields, fmt.Sprintf("%v: %v,", field.Name, toLowerCamel(field.Name)))
}
constructors = append(constructors, o{
"Name": "New" + strcase.ToCamel(structInfo.StructName),
"Struct": structInfo.StructName,
"InitFlag": structInfo.InitFlag,
"ValueFlag": structInfo.ValueFlag,
"Params": strings.Join(params, ", "),
"Fields": strings.Join(fields, "\n"),
})
}
data["Constructors"] = constructors
var buffer bytes.Buffer
err = t.Execute(&buffer, data)
if err != nil {
return "", err
}
// format code
buf, err := formatCode(buffer.Bytes())
if err != nil {
return "", err
}
return string(buf), nil
}
func formatCode(source []byte) ([]byte, error) {
output, err := imports.Process("", source, &imports.Options{
AllErrors: true,
Comments: true,
TabIndent: true,
TabWidth: 8,
Fragment: true,
})
if err != nil {
return output, err
}
if bytes.Equal(source, output) {
return output, nil
}
return formatCode(output)
}
// filterUniqueImports remove duplicate imports, return unqiue imports
func filterUniqueImports(imports []ImportInfo) []ImportInfo {
hash := map[string]ImportInfo{}
for _, importInfo := range imports {
key := fmt.Sprintf("%v|%v", importInfo.Name, importInfo.Path)
hash[key] = importInfo
}
ret := []ImportInfo{}
for _, importInfo := range hash {
ret = append(ret, importInfo)
}
return ret
}
type o map[string]interface{}