forked from shiqiyue/dataloaden
/
generator.go
165 lines (132 loc) · 3.29 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
package generator
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"unicode"
"github.com/iancoleman/strcase"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
)
type templateData struct {
Package string
Name string
KeyType *goType
ValType *goType
}
type goType struct {
Modifiers string
ImportPath string
ImportName string
Name string
}
func (t *goType) String() string {
if t.ImportName != "" {
return t.Modifiers + t.ImportName + "." + t.Name
}
return t.Modifiers + t.Name
}
func (t *goType) IsPtr() bool {
return strings.HasPrefix(t.Modifiers, "*")
}
func (t *goType) IsSlice() bool {
return strings.HasPrefix(t.Modifiers, "[]")
}
var partsRe = regexp.MustCompile(`^([\[\]\*]*)(.*?)(\.\w*)?$`)
func parseType(str string) (*goType, error) {
parts := partsRe.FindStringSubmatch(str)
if len(parts) != 4 {
return nil, fmt.Errorf("type must be in the form []*github.com/import/path.Name")
}
t := &goType{
Modifiers: parts[1],
ImportPath: parts[2],
Name: strings.TrimPrefix(parts[3], "."),
}
if t.Name == "" {
t.Name = t.ImportPath
t.ImportPath = ""
}
if t.ImportPath != "" {
p, err := packages.Load(&packages.Config{Mode: packages.NeedName}, t.ImportPath)
if err != nil {
return nil, err
}
if len(p) != 1 {
return nil, fmt.Errorf("not found")
}
t.ImportName = p[0].Name
}
return t, nil
}
func Generate(name string, keyType string, valueType string, wd string) error {
data, err := getData(name, keyType, valueType, wd)
if err != nil {
return err
}
filename := strcase.ToSnake(data.Name) + ".go"
if err := writeTemplate(filepath.Join(wd, filename), data); err != nil {
return err
}
return nil
}
func getData(name string, keyType string, valueType string, wd string) (templateData, error) {
var data templateData
genPkg := getPackage(wd)
if genPkg == nil {
return templateData{}, fmt.Errorf("unable to find package info for " + wd)
}
var err error
data.Name = name
data.Package = genPkg.Name
data.KeyType, err = parseType(keyType)
if err != nil {
return templateData{}, fmt.Errorf("key type: %s", err.Error())
}
data.ValType, err = parseType(valueType)
if err != nil {
return templateData{}, fmt.Errorf("key type: %s", err.Error())
}
// if we are inside the same package as the type we don't need an import and can refer directly to the type
if genPkg.PkgPath == data.ValType.ImportPath {
data.ValType.ImportName = ""
data.ValType.ImportPath = ""
}
if genPkg.PkgPath == data.KeyType.ImportPath {
data.KeyType.ImportName = ""
data.KeyType.ImportPath = ""
}
return data, nil
}
func getPackage(dir string) *packages.Package {
p, _ := packages.Load(&packages.Config{
Dir: dir,
}, ".")
if len(p) != 1 {
return nil
}
return p[0]
}
func writeTemplate(filepath string, data templateData) error {
var buf bytes.Buffer
if err := tpl.Execute(&buf, data); err != nil {
return errors.Wrap(err, "generating code")
}
src, err := imports.Process(filepath, buf.Bytes(), nil)
if err != nil {
return errors.Wrap(err, "unable to gofmt")
}
if err := ioutil.WriteFile(filepath, src, 0644); err != nil {
return errors.Wrap(err, "writing output")
}
return nil
}
func lcFirst(s string) string {
r := []rune(s)
r[0] = unicode.ToLower(r[0])
return string(r)
}