This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialiser.go
356 lines (322 loc) · 8.91 KB
/
initialiser.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* Date: 9/02/14
* Time: 7:06 PM
*/
package opal
import (
"bitbucket.org/pkg/inflect"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"strconv"
"strings"
"text/template"
"time"
)
// Some rules
// 1: Primary keys if not one of the four base types will
// be an embedded type where only one param will be generated.
// 2: Compound keys which are expanded will be of the four base
// base types and their primitives will be expanded into the
// parameters.
type ModelTemplate struct {
Version string
Date string
Time string
Types map[string]*TemplateType
}
type TemplateType struct {
Domain
Version string
Date string
Time string
Receiver string
Model string
DAOName string
Path string
Package string
ImportName string
Table string
Keys []KeyField
Columns []TemplateField
}
type KeyField struct {
Name string
TypeName string
Index int
Tag string
Kind string
Primitive string
}
type TemplateField struct {
Name string
Index int
Tag string
Kind string
Primitive string
}
// INIT will scan each supplied Model/Domain object
// and generate the relevant Boilerplate code with which you can run
// TODO doco
func INIT(pBaseModel BaseModel) {
gatherTemplateData(pBaseModel.Models())
}
// The system will run the code generation template
// based on the received skeletal Models and generate the code
func gatherTemplateData(pModels []Domain) {
plate := ModelTemplate{}
// Header information
now := time.Now()
//plate.Version = version.String()
plate.Date = now.Format("2 January 2006")
plate.Time = now.Format("Monday 3:04 AM")
// Gather Table information:
plate.Types = make(map[string]*TemplateType, len(pModels))
// Check the first field
// if it's an anonymous Entity extract its metadata
// if not panic - invalid Domain Model
// TODO does this have to be first
for _, domain := range pModels {
model := reflect.TypeOf(domain).Elem()
temp := TemplateType{}
//var useDefaultKey bool
temp.Domain = domain
temp.Version = plate.Version
temp.Date = plate.Date
temp.Time = plate.Time
temp.Model = model.Name()
temp.DAOName = inflect.Camelize(inflect.Tableize(temp.Model))
temp.Receiver = strings.ToLower(temp.Model)
temp.Path = model.PkgPath()
temp.ImportName = importName(model)
s := strings.Split(temp.Path, "/")
temp.Package = s[len(s)-1]
var keys map[string]bool = make(map[string]bool)
var i int
if model.Field(i).Type.Implements(reflect.TypeOf((*Entity)(nil)).Elem()) && model.Field(i).Anonymous {
opalTags := ExtractOpalTags(model.Field(i).Tag)
// Derive table name or get specific
if opalTags.Get("Name") == "" {
temp.Table = fmt.Sprintf("Name: %q", inflect.Tableize(temp.Model))
} else {
temp.Table = string(opalTags)
}
if opalTags.Get("Key") == "" {
//useDefaultKey = true
keys["Id"] = true
} else {
// TODO support compound keys
key, _ := strconv.Unquote(opalTags.Get("Key"))
keys[key] = true
}
i++
} else {
// TODO error
log.Fatalf("Opal.RunTemplate: Model does not anonymously embed type which implements the BaseModel interface at field position 0.")
}
// Check for the Key field
// if it's an anonymous BaseModel extract its metadata
// if not panic - invalid Entity
opal := reflect.TypeOf((*Opal)(nil)).Elem()
field := model.Field(i)
typ := field.Type
if typ.Name() == "Key" && field.Anonymous {
opalTags := ExtractOpalTags(field.Tag)
// TODO support compound keys // TODO check field type
if _, ok := model.FieldByName(string(opalTags)); ok {
keys[string(opalTags)] = true
} else {
panic("Opal.runTemplate: Key metatag error")
}
i++
}
// Check each field if its an OPAL extract its metadata after we have the entity data
for i < model.NumField() {
field = model.Field(i)
typ = field.Type
if field.Type.Implements(opal) {
opalTags := ExtractOpalTags(field.Tag)
if opalTags == "" {
opalTags = Tag(fmt.Sprintf("Name: %q", field.Name))
}
// Infer column name or get explicit
if opalTags.Get("Name") == "" {
opalTags = Tag(fmt.Sprintf("Name: %q, %s", field.Name, opalTags))
}
if keys[field.Name] {
if typ.Name() == "AutoIncrement" {
opalTags += ", AutoIncrement: true"
}
kind := reflect.Kind(reflect.New(typ).MethodByName("Kind").Call(nil)[0].Uint())
key := KeyField{field.Name, typ.Name(), i, string(opalTags), getKind(typ.Name()), kind.String()}
temp.Keys = append(temp.Keys, key)
} else {
kind := reflect.Kind(reflect.New(typ).MethodByName("Kind").Call(nil)[0].Uint())
s := kind.String()
if s == "slice" {
s = "[]byte"
}
// TODO handle primitive types properly
temp.Columns = append(temp.Columns, TemplateField{field.Name, i, string(opalTags), getKind(typ.Name()), s})
}
}
i++
}
plate.Types[model.Name()] = &temp
}
// Gather relationship information
for _, domain := range pModels {
var i int
t := reflect.TypeOf(domain).Elem()
// assoc := reflect.TypeOf((*Association)(nil)).Elem()
dom := reflect.TypeOf((*Domain)(nil)).Elem()
// Treat as a HasOne relationship
for i < t.NumField() {
if t.Field(i).Type.Implements(dom) {
// TODO gather key information for each model first
//
}
i++
}
}
runTemplate(plate)
}
func runTemplate(pModels ModelTemplate) {
// Create a template, add the function map, and parse the text.
b, err := ioutil.ReadFile("src/github.com/twinj/opal/entity.template")
if err != nil {
log.Fatalf("Opal.runTemplate: reading file: %s", err)
}
tmpl, err := template.New("Model setup").Parse(string(b))
if err != nil {
log.Fatalf("Opal.runTemplate: parsing: %s", err)
}
// Run the template to verify the output.
for _, li := range pModels.Types {
buf := bytes.Buffer{}
err = tmpl.Execute(&buf, li)
if err != nil {
log.Fatalf("Opal.runTemplate: execution: %s", err)
}
s := fmt.Sprintf("src/%s/%s_%s_init.go", li.Path, li.Package, li.Receiver)
err = ioutil.WriteFile(s, buf.Bytes(), os.ModeTemporary)
if err != nil {
log.Fatalf("Opal.runTemplate: writing file %s: ", s, err)
} // TODO parse multiple templates from one domain package into 1 file
}
}
// A StructTag is the tag string in a struct field.
//
// By convention, tag strings are a concatenation of
// optionally space-separated key:"value" pairs.
// Each key is a non-empty string consisting of non-control
// characters other than space (U+0020 ' '), quote (U+0022 '"'),
// and colon (U+003A ':'). Each value is quoted using U+0022 '"'
// characters and Go string literal syntax.
type Tag string
func ExtractOpalTags(pStructTag reflect.StructTag) Tag {
tag := string(pStructTag)
if strings.Count(tag, "|") == 2 {
tag = strings.Split(tag, "|")[1]
return Tag(tag)
}
return Tag("")
}
// Get returns the value associated with key in the tag string.
// If there is no such key in the tag, Get returns the empty string.
// If the tag does not have the conventional format, the value
// returned by Get is unspecified.
func (tag Tag) Get(key string) string {
for tag != "" {
var quoted = true
// skip leading space
i := 0
for i < len(tag) && tag[i] == ' ' {
i++
}
tag = tag[i:]
if tag == "" {
break
}
// scan to colon.
// a space or a quote is a syntax error
i = 0
for i < len(tag) && tag[i] != ' ' && tag[i] != ':' && tag[i] != '"' {
i++
}
// check if the next rune is either a space or quote
if i+1 >= len(tag) || tag[i] != ':' || !(tag[i+1] == '"' || tag[i+1] == ' ') {
break
}
name := string(tag[:i])
tag = tag[i+1:]
i = 0
// if a space is included skip it - only supports one
if i < len(tag) && tag[i] == ' ' {
i++
}
// Skips quote but what if not quoted value
if i < len(tag) && tag[i] != '"' {
quoted = false
} else {
i++
}
// Scan quoted string to find value if value is not quoted check for
// comma
for i < len(tag) && !((quoted && tag[i] == '"') || (!quoted && tag[i] == ',')) {
if tag[i] == '\\' {
i++
}
i++
}
if quoted {
i++
}
if i >= len(tag) {
i = len(tag) - 1
}
value := string(tag[:i+1])
tag = tag[i+1:]
if key == name {
value = strings.Trim(value, " ")
value = strings.Trim(value, ",")
/*if ! strings.Contains(value, `"`) {
return value
}
value, _ = strconv.Unquote(value)*/
return value
}
}
return ""
}
// Helper to retrieve Type name and package name with which we
// use to name a Model within the domain
func importName(pType reflect.Type) string {
return fmt.Sprintf("%v", pType)
}
func getKind(pName string) string {
switch pName {
case "String":
return "reflect.String"
case "Int64":
return "reflect.Int64"
case "AutoIncrement":
return "reflect.Int64"
case "Float64":
return "reflect.Float64"
case "Bool":
return "reflect.Bool"
case "Key":
return "PrimaryKey"
case "Time":
return "OpalTime"
case "Slice":
return "reflect.Slice"
default:
log.Panicln("Opal.Kind: non supported type:", pName)
}
return "paniced"
}