-
Notifications
You must be signed in to change notification settings - Fork 0
/
enums.go
282 lines (238 loc) · 6.89 KB
/
enums.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
package enums
import (
"bytes"
"database/sql"
"database/sql/driver"
"fmt"
"go/ast"
"go/format"
"go/token"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"text/template"
"github.com/iancoleman/strcase"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"golang.org/x/tools/go/packages"
)
var enCaser = cases.Title(language.English)
// Data to pass to the template
type templateData struct {
PkgName string // Name of the package.
TypeName string // The Enum type in generated go code.
Values []string // Enum options
WritePKGDecl bool // Whether to write imports and package declaration
}
// Enumer interface embeds sql.Scanner interface, database/driver.Valuer.
// Has 2 methods ValidValues()[]string to return valid enums and DatabaseType()
// that returns the sql data type.
type Enumer interface {
sql.Scanner
driver.Valuer
ValidValues() []string
DatabaseType() string
}
func ImplementsEnumer(field reflect.StructField) bool {
fieldPtr := reflect.New(field.Type)
interfaceType := reflect.TypeOf((*Enumer)(nil)).Elem()
return fieldPtr.Type().AssignableTo(interfaceType)
}
type pkgInfo struct {
Name string
Path string
}
const mode packages.LoadMode = packages.NeedName |
packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo
// Map representing all packages processed.
// Each key is a unique package path in the file system.
//
// pkgInfoMap := [pkgInfo][fileName][enumType][]{"type1", "type2",...}
type pkgInfoMap map[pkgInfo]map[string]map[string][]string
func getPKGEnumMap(pkgName string) (pkgInfoMap, error) {
cfg := &packages.Config{
Mode: mode,
Tests: false,
}
pkgs, err := packages.Load(cfg, pkgName)
if err != nil {
packages.PrintErrors(pkgs)
return nil, err
}
mapInfo := make(pkgInfoMap)
for _, pkg := range pkgs {
for _, file := range pkg.Syntax {
absPath, err := filepath.Abs(pkg.Fset.File(file.Pos()).Name())
if err != nil {
return nil, err
}
for _, decl := range file.Decls {
// We only care about enumerated constants
if genDecl, ok := decl.(*ast.GenDecl); ok && genDecl.Tok == token.CONST {
for _, spec := range genDecl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok && valueSpec.Type != nil {
// Only care about identifiers
keySpec, ok := valueSpec.Type.(*ast.Ident)
if !ok {
continue
}
enumTypeName := keySpec.Name
for _, value := range valueSpec.Values {
if basicLit, ok := value.(*ast.BasicLit); ok && basicLit.Kind == token.STRING {
pkgKey := pkgInfo{Name: pkg.Name, Path: pkg.PkgPath}
if mapInfo[pkgKey] == nil {
mapInfo[pkgKey] = make(map[string]map[string][]string)
}
if mapInfo[pkgKey][absPath] == nil {
mapInfo[pkgKey][absPath] = make(map[string][]string)
}
constantValue := strings.Trim(basicLit.Value, `"`)
mapInfo[pkgKey][absPath][enumTypeName] = append(mapInfo[pkgKey][absPath][enumTypeName], constantValue)
}
}
}
}
}
}
}
}
return mapInfo, nil
}
// Surround each value with single quote and return a comma-seperated string
func QuoteEnums(values []string) string {
quoted := make([]string, len(values))
for i, v := range values {
quoted[i] = fmt.Sprintf("'%s'", v)
}
return strings.Join(quoted, ",")
}
// Passes data to the string template that is executed into w.
func parseTemplate(w io.Writer, data templateData) error {
tmpl, err := template.New("tmpl").Funcs(template.FuncMap{
"toCamelCase": func(s string) string {
return strcase.ToCamel(enCaser.String(s))
},
"ToSnakeCase": func(s string) string {
return strcase.ToSnake(enCaser.String(s))
},
}).Parse(templateString)
if err != nil {
return fmt.Errorf("error parsing template: %w", err)
}
if err := tmpl.Execute(w, data); err != nil {
return fmt.Errorf("error executing template: %w", err)
}
return nil
}
// For all enumerated constants, generate enums that satisfy the Enummer interface
// suffix is appended to each filename e.g Uses _enum by default.
// Returns sql string that you can use to create these ENUM types in postgres
func GenerateEnums(pkgNames []string, suffix ...string) (sql string, err error) {
if len(suffix) == 0 {
suffix = append(suffix, "_enum")
}
base := suffix[0] + ".go"
globalBuffer := new(bytes.Buffer)
for pkgIndex, pkgName := range pkgNames {
enums, err := getPKGEnumMap(pkgName)
if err != nil {
return "", fmt.Errorf("getPKGEnumMap(): %w", err)
}
// Create types for postgres
type enum_type struct {
name string
values []string
}
enumsTypes := []enum_type{}
for pkgPath, pkgData := range enums {
for filename, enumDict := range pkgData {
buffer := bytes.Buffer{}
count := 0
for key, values := range enumDict {
err := parseTemplate(&buffer, templateData{
PkgName: pkgPath.Name,
TypeName: key,
Values: values,
WritePKGDecl: count == 0 && pkgIndex == 0,
})
if err != nil {
return "", err
}
enumsTypes = append(enumsTypes, enum_type{
name: key,
values: values,
})
}
// increase count
count++
filebase := strings.Split(filepath.Base(filename), ".")[0]
absPath := filepath.Join(filepath.Dir(filename), filebase+base)
// Format source
b, err := format.Source(buffer.Bytes())
if err != nil {
return "", fmt.Errorf("error format source file: %w, source: %s", err, b)
}
// Write contents to the file.
if err := os.WriteFile(absPath, b, 0644); err != nil {
return "", fmt.Errorf("error writing buffer: %w", err)
}
}
}
// Write the enum types
buf := new(bytes.Buffer)
for _, item := range enumsTypes {
// convert to name to snake case.
fmt.Fprintf(buf, "CREATE TYPE %s AS ENUM(%s);\n", strcase.ToSnake(item.name), QuoteEnums(item.values))
}
globalBuffer.Write(buf.Bytes())
}
return globalBuffer.String(), nil
}
var templateString = `
{{$typeName := .TypeName}}{{$values := .Values}}
{{if .WritePKGDecl}}
// Code generated by "apigen"; DO NOT EDIT.
package {{.PkgName}}
import (
"database/sql/driver"
"fmt"
)
{{end}}
func (e {{.TypeName}}) IsValid() bool {
for _, val := range e.ValidValues() {
if val == string(e) {
return true
}
}
return false
}
func (e {{.TypeName}}) ValidValues() []string {
return []string{
{{range $val := $values -}}
"{{$val -}}",
{{end -}}
}
}
func (e *{{.TypeName}}) Scan(src interface{}) error {
source, ok := src.(string)
if !ok {
return fmt.Errorf("invalid value for %s: %s", "{{.TypeName}}", source)
}
*e = {{.TypeName}}(source)
return nil
}
func (e {{.TypeName}}) Value() (driver.Value, error) {
if !e.IsValid() {
return nil, fmt.Errorf("invalid value for %s", "{{.TypeName}}")
}
return string(e), nil
}
func (e {{.TypeName}}) DatabaseType() string {
return "{{.TypeName | ToSnakeCase }}"
}
func (e {{.TypeName}}) GormDataType() string {
return "{{.TypeName | ToSnakeCase }}"
}
`