-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
238 lines (199 loc) · 4.55 KB
/
main.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
package main
import (
"bufio"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"strings"
"text/template"
"unicode"
"sour.is/x/toolbox/log"
)
type model struct {
Types []structMap
}
type structMap struct {
Name string
ID string
Fields []structField
Table bool
View bool
ROnly bool
HasID bool
}
type structField struct {
Name string
Container string
Auto bool
ROnly bool
}
func init() {
if len(os.Args) < 3 {
fmt.Println("Usage: gogen CONFIG MODEL")
}
}
func main() {
fset := token.NewFileSet() // positions are relative to fset
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, os.Args[2], nil, 0)
if err != nil {
log.Fatal(err)
}
var m model
for _, d := range f.Decls {
var ok bool
var gen *ast.GenDecl
if gen, ok = d.(*ast.GenDecl); !ok {
continue
}
for _, spec := range gen.Specs {
var ts *ast.TypeSpec
if ts, ok = spec.(*ast.TypeSpec); !ok {
continue
}
smap := structMap{}
smap.Name = ts.Name.Name
log.Debugf("type %s\n", ts.Name.Name)
var st *ast.StructType
if st, ok = ts.Type.(*ast.StructType); !ok {
continue
}
smap.Table, smap.View, smap.Fields, smap.HasID, smap.ID = procStruct(st)
if !smap.Table && !smap.View {
// No Table or View == Do not generate.
continue
}
if !smap.Table && smap.View {
// View only == Only generate read items.
smap.ROnly = true
}
m.Types = append(m.Types, smap)
}
}
fh, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
basepath := filepath.Dir(os.Args[1])
err = os.Chdir(basepath)
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
fn := scanner.Text()
log.Print(fn)
tpl, err := template.New("tpl").Funcs(fnMap).ParseFiles(fn)
if err != nil {
log.Fatal(err)
}
w, err := os.Create(fn + ".go")
if err != nil {
log.Fatal(err)
}
fmt.Fprint(w, "// Code generated by sour.is/x/toolbox/cmd/modelgen, DO NOT EDIT.\n\n")
err = tpl.ExecuteTemplate(w, tpl.Templates()[0].ParseName, m)
if err != nil {
log.Error(err)
}
}
}
func procStruct(st *ast.StructType) (table, view bool, fields []structField, hasID bool, ID string) {
for _, field := range st.Fields.List {
for _, name := range field.Names {
if name.Name == "ID" {
ID = name.Name
hasID = true
}
tag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
auto := false
if db := tag.Get("db"); strings.Contains(db, ",AUTO") {
auto = true
}
readonly := false
if db := tag.Get("db"); strings.Contains(db, ",RO") {
readonly = true
}
if db := tag.Get("db"); strings.Contains(db, ",ID") {
hasID = true
ID = name.Name
}
if tab := tag.Get("table"); tab != "" {
table = true
}
if tab := tag.Get("view"); tab != "" {
view = true
}
if arr, ok := field.Type.(*ast.ArrayType); ok {
if _, ok = arr.Elt.(*ast.Ident); !ok {
log.Debug("non-ident array ", name, field)
continue
}
fields = append(
fields,
structField{
Name: name.Name,
Container: tag.Get("cont"),
Auto: auto,
ROnly: readonly,
})
continue
}
fields = append(
fields,
structField{
Name: name.Name,
Container: "",
Auto: auto,
ROnly: readonly,
})
}
}
return
}
var fnMap = template.FuncMap{
"snakeCase": SnakeCase,
"spineCase": SpineCase,
}
// SnakeCase converts the given string to snake case following the Golang format:
// acronyms are converted to lower-case and preceded by an underscore.
func SnakeCase(s string) string {
in := []rune(s)
isLower := func(idx int) bool {
return idx >= 0 && idx < len(in) && unicode.IsLower(in[idx])
}
out := make([]rune, 0, len(in)+len(in)/2)
for i, r := range in {
if unicode.IsUpper(r) {
r = unicode.ToLower(r)
if i > 0 && in[i-1] != '_' && (isLower(i-1) || isLower(i+1)) {
out = append(out, '_')
}
}
out = append(out, r)
}
return string(out)
}
// SpineCase converts the given string to snake case following the Golang format:
// acronyms are converted to lower-case and preceded by an underscore.
func SpineCase(s string) string {
in := []rune(s)
isLower := func(idx int) bool {
return idx >= 0 && idx < len(in) && unicode.IsLower(in[idx])
}
out := make([]rune, 0, len(in)+len(in)/2)
for i, r := range in {
if unicode.IsUpper(r) {
r = unicode.ToLower(r)
if i > 0 && in[i-1] != '-' && (isLower(i-1) || isLower(i+1)) {
out = append(out, '-')
}
}
out = append(out, r)
}
return string(out)
}