Repository navigation
Expand file tree
/
Copy pathparse_definition.go
More file actions
657 lines (609 loc) · 17.4 KB
/
Copy pathparse_definition.go
File metadata and controls
657 lines (609 loc) · 17.4 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package definitionc
import (
"go/ast"
"go/parser"
"go/token"
"strconv"
"strings"
"webtyp.com/definitioncheck"
"webtyp.com/fmt"
"webtyp.com/model"
)
func (g *Generator) parseDefinitionsInFile(path string) ([]StructInfo, error) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
var infos []StructInfo
for _, decl := range node.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.VAR {
continue
}
for _, spec := range genDecl.Specs {
valueSpec, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
for i, name := range valueSpec.Names {
if !strings.HasSuffix(name.Name, "Model") {
// Plan §4.2: Falla ruidoso if it doesn't end in Model but looks like a Definition?
// For now, only process if it ends in Model as per §4.2.
continue
}
if i >= len(valueSpec.Values) {
continue
}
info, err := g.parseDefinition(name.Name, valueSpec.Values[i], node)
if err != nil {
return nil, err // Falla ruidoso
}
if info != nil {
info.SourceFile = path
info.PackageName = node.Name.Name
infos = append(infos, *info)
}
}
}
}
return infos, nil
}
func (g *Generator) parseDefinition(varName string, expr ast.Expr, file *ast.File) (*StructInfo, error) {
cl, ok := expr.(*ast.CompositeLit)
if !ok {
return nil, nil
}
// Check if type is model.Definition
typeStr := exprToString(cl.Type)
if !strings.Contains(typeStr, "Definition") {
return nil, nil
}
structName := strings.TrimSuffix(varName, "Model")
if structName == "" {
return nil, fmt.Errf("Variable name %s must have a prefix before 'Model'", varName)
}
info := &StructInfo{
Name: structName,
ModelNameDeclared: true,
}
for _, elt := range cl.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
key := exprToString(kv.Key)
switch key {
case "Name":
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.STRING {
val, _ := strconv.Unquote(lit.Value)
info.ModelName = val
}
case "ListsOnlyAfterSearch":
if id, ok := kv.Value.(*ast.Ident); ok && id.Name == "true" {
info.ListsOnlyAfterSearch = true
}
case "Wire":
if id, ok := kv.Value.(*ast.Ident); ok && id.Name == "true" {
info.Wire = true
}
case "Allow":
allow, err := parseAllow(varName, "Allow", kv.Value)
if err != nil {
return nil, err
}
info.Allow = allow
case "MFA":
mfa, err := parseAllow(varName, "MFA", kv.Value)
if err != nil {
return nil, err
}
info.MFA = mfa
case "Rules":
rules, err := parseRules(varName, kv.Value)
if err != nil {
return nil, err
}
info.Rules = rules
case "Fields":
fields, err := g.parseFields(kv.Value)
if err != nil {
return nil, err
}
info.Fields = fields
case "ExternalCalls":
calls, err := parseExternalCalls(varName, kv.Value)
if err != nil {
return nil, err
}
info.ExternalCalls = calls
}
}
if err := validateItem(varName, info.Fields); err != nil {
return nil, err
}
if info.ModelName == "" {
info.ModelName = ToSnakeCase(info.Name)
}
// A struct with no DB field is not a table.
hasDB := false
for _, f := range info.Fields {
if f.HasDB {
hasDB = true
}
}
info.NoDB = !hasDB
if info.NoDB && info.Allow != 0 {
if info.Allow != uint8(model.Read) {
return nil, fmt.Errf("%s: only model.Read can be allowed on a model without a key field; Create/Update/Delete need a primary key declared with DB (a table, or a resource served by the module's own crud.Store)", varName)
}
}
def := model.Definition{Name: info.ModelName, Wire: info.Wire, ListsOnlyAfterSearch: info.ListsOnlyAfterSearch, Allow: model.Action(info.Allow), Rules: info.Rules.modelRules(), MFA: model.Action(info.MFA), ExternalCalls: info.ExternalCalls}
if err := definitioncheck.CheckRules(def); err != nil {
return nil, fmt.Errf("%s: %s", varName, err.Error())
}
if err := def.ValidateExternalCalls(); err != nil {
return nil, fmt.Errf("%s: %s", varName, err.Error())
}
if info.NoDB && !info.Wire && info.Allow == uint8(model.Read) && info.Rules.Read != uint8(model.AddMyBusinessRule) {
return nil, fmt.Errf("%s: a model without a table has no default read; declare Rules: model.Rules{Read: model.AddMyBusinessRule} and write its rule", varName)
}
if (!info.NoDB || info.Allow == uint8(model.Read)) && !info.Wire {
for _, f := range info.Fields {
if strings.EqualFold(f.Name, "allow") || strings.EqualFold(f.ColumnName, "allow") {
return nil, fmt.Errf("%s: a field named allow collides with the generated Allow() method; rename the column", varName)
}
}
}
return info, nil
}
// parseAllow reads a set of actions (model.Create | model.Read | …) written in field of varName.
func parseAllow(varName, field string, expr ast.Expr) (uint8, error) {
switch t := expr.(type) {
case *ast.SelectorExpr:
switch t.Sel.Name {
case "Create":
return uint8(model.Create), nil
case "Read":
return uint8(model.Read), nil
case "Update":
return uint8(model.Update), nil
case "Delete":
return uint8(model.Delete), nil
}
return 0, fmt.Errf("field %s of %s: write it as model.Create | model.Read | … (constants only)", field, varName)
case *ast.BinaryExpr:
if t.Op != token.OR {
return 0, fmt.Errf("field %s of %s: write it as model.Create | model.Read | … (constants only)", field, varName)
}
left, err := parseAllow(varName, field, t.X)
if err != nil {
return 0, err
}
right, err := parseAllow(varName, field, t.Y)
if err != nil {
return 0, err
}
return left | right, nil
default:
return 0, fmt.Errf("field %s of %s: write it as model.Create | model.Read | … (constants only)", field, varName)
}
}
const errRulesShape = "field Rules of %s: write it as model.Rules{Create: model.AddMyBusinessRule, Read: model.HaveNoBusinessRule, …} (constants only)"
// parseRules reads the Rules literal of varName: one RuleMode per action, written as a constant.
func parseRules(varName string, expr ast.Expr) (RulesInfo, error) {
cl, ok := expr.(*ast.CompositeLit)
if !ok {
return RulesInfo{}, fmt.Errf(errRulesShape, varName)
}
var r RulesInfo
for _, elt := range cl.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
return RulesInfo{}, fmt.Errf(errRulesShape, varName)
}
mode, err := parseRuleMode(varName, kv.Value)
if err != nil {
return RulesInfo{}, err
}
switch key := exprToString(kv.Key); key {
case "Create":
r.Create = mode
case "Read":
r.Read = mode
case "Update":
r.Update = mode
case "Delete":
r.Delete = mode
default:
return RulesInfo{}, fmt.Errf("field Rules of %s: %s is not an action; use Create, Read, Update or Delete", varName, key)
}
}
return r, nil
}
func parseRuleMode(varName string, expr ast.Expr) (uint8, error) {
switch exprToString(expr) {
case "model.AddMyBusinessRule":
return uint8(model.AddMyBusinessRule), nil
case "model.HaveNoBusinessRule":
return uint8(model.HaveNoBusinessRule), nil
}
return 0, fmt.Errf("field Rules of %s: write model.AddMyBusinessRule or model.HaveNoBusinessRule (constants only), got %s", varName, exprToString(expr))
}
func (g *Generator) parseFields(expr ast.Expr) ([]FieldInfo, error) {
cl, ok := expr.(*ast.CompositeLit)
if !ok {
return nil, nil
}
var fields []FieldInfo
for _, elt := range cl.Elts {
fieldCl, ok := elt.(*ast.CompositeLit)
if !ok {
continue
}
fi, err := g.parseField(fieldCl)
if err != nil {
return nil, err
}
fields = append(fields, fi)
}
return fields, nil
}
func (g *Generator) parseField(cl *ast.CompositeLit) (FieldInfo, error) {
var fi FieldInfo
for _, elt := range cl.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
key := exprToString(kv.Key)
switch key {
case "Name":
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.STRING {
fi.ColumnName, _ = strconv.Unquote(lit.Value)
fi.Name = fmt.Convert(fi.ColumnName).CamelUp().String()
}
case "Type":
fi.KindConstructor = exprToString(kv.Value)
if call, ok := kv.Value.(*ast.CallExpr); ok {
for _, arg := range call.Args {
if ident, ok := arg.(*ast.Ident); ok {
fi.KindArgIdents = append(fi.KindArgIdents, ident.Name)
}
}
}
case "NotNull":
fi.NotNull = exprToBool(kv.Value)
case "OmitEmpty":
fi.OmitEmpty = exprToBool(kv.Value)
case "Exclude":
fi.Exclude = exprToBool(kv.Value)
case "Widget":
return FieldInfo{}, fmt.Err(fmt.Sprintf("field %s: Field.Widget was removed (Kind unification): declare the kind in Type — e.g. Type: input.Email()", fi.ColumnName))
case "DB":
g.parseDBField(kv.Value, &fi)
case "Item":
slot, ok := itemSlotOf(exprToString(kv.Value))
if !ok {
return FieldInfo{}, fmt.Err(fmt.Sprintf(errUnknownItemSlot, fi.ColumnName, exprToString(kv.Value)))
}
fi.Item = slot
case "Row":
return FieldInfo{}, fmt.Err(fmt.Sprintf("field %s: Field.Row was replaced by Field.Item (model.ItemLabel, model.ItemDescription, model.ItemBadge)", fi.ColumnName))
case "Options":
fi.Options = parseRef(kv.Value)
case "Ref":
fi.Ref = parseRef(kv.Value)
case "Permitted":
g.parsePermitted(kv.Value, &fi)
}
}
if fi.KindConstructor == "" {
return FieldInfo{}, fmt.Err(fmt.Sprintf("field %s: kind required", fi.ColumnName))
}
// Composition refs come from the constructor argument, not Ref:
_, ctorName := parseConstructor(fi.KindConstructor)
if ctorName == "Struct" || ctorName == "StructSlice" {
// Extract arg from model.Struct(&AddressModel) / model.StructSlice(&AddressModel)
arg := ""
if call, ok := kvToExpr(cl, "Type").(*ast.CallExpr); ok && len(call.Args) > 0 {
arg = exprToString(call.Args[0])
}
if arg == "" || arg == "nil" {
return FieldInfo{}, fmt.Err(fmt.Sprintf("field %s: %s requires a non-nil Definition argument", fi.ColumnName, fi.KindConstructor))
}
if fi.Ref != "" {
return FieldInfo{}, fmt.Err(fmt.Sprintf("field %s: contradiction: Ref: cannot be used with composition kind %s (it already takes the definition as an argument)", fi.ColumnName, fi.KindConstructor))
}
fi.Ref = strings.TrimPrefix(arg, "&")
if ctorName == "StructSlice" {
fi.Type = model.FieldStructSlice
} else {
fi.Type = model.FieldStruct
}
fi.GoType = FieldTypeToGoType(fi.Type, fi.Ref)
}
// Non-composition fields: fi.Type stays 0 here and is populated by
// stage-2 resolution (resolveStorage: builtin table or dependency probe).
// GoType for those fields is computed there once fi.Type is known.
return fi, nil
}
func (g *Generator) parseDBField(expr ast.Expr, fi *FieldInfo) {
fi.HasDB = true
cl, ok := expr.(*ast.CompositeLit)
if !ok {
if unary, ok := expr.(*ast.UnaryExpr); ok && unary.Op == token.AND {
if cl2, ok := unary.X.(*ast.CompositeLit); ok {
cl = cl2
}
}
}
if cl == nil {
return
}
for _, elt := range cl.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
key := exprToString(kv.Key)
switch key {
case "PK":
fi.PK = exprToBool(kv.Value)
case "Unique":
fi.Unique = exprToBool(kv.Value)
case "Revision":
fi.Revision = exprToBool(kv.Value)
case "AutoInc":
fi.AutoInc = exprToBool(kv.Value)
case "RefColumn":
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.STRING {
fi.RefColumn, _ = strconv.Unquote(lit.Value)
}
case "OnDelete":
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.STRING {
fi.OnDelete, _ = strconv.Unquote(lit.Value)
}
}
}
}
func (g *Generator) parsePermitted(expr ast.Expr, fi *FieldInfo) {
cl, ok := expr.(*ast.CompositeLit)
if !ok {
if unary, ok := expr.(*ast.UnaryExpr); ok && unary.Op == token.AND {
if cl2, ok := unary.X.(*ast.CompositeLit); ok {
cl = cl2
}
}
}
if cl == nil {
return
}
for _, elt := range cl.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
key := exprToString(kv.Key)
switch key {
case "Letters":
fi.Letters = exprToBool(kv.Value)
case "Tilde":
fi.Tilde = exprToBool(kv.Value)
case "Numbers":
fi.Numbers = exprToBool(kv.Value)
case "Spaces":
fi.Spaces = exprToBool(kv.Value)
case "Minimum":
fi.Minimum = exprToInt(kv.Value)
case "Maximum":
fi.Maximum = exprToInt(kv.Value)
}
}
}
const errUnknownItemSlot = "field %s: unknown Item slot %s (use model.ItemLabel, model.ItemDescription or model.ItemBadge)"
// itemSlotOf maps the selector written in a Definition (model.ItemLabel, or ItemLabel
// with a dot-import) to its slot.
func itemSlotOf(expr string) (model.ItemSlot, bool) {
name := expr
if i := strings.LastIndex(expr, "."); i >= 0 {
name = expr[i+1:]
}
switch name {
case "ItemLabel":
return model.ItemLabel, true
case "ItemDescription":
return model.ItemDescription, true
case "ItemBadge":
return model.ItemBadge, true
case "ItemNone":
return model.ItemNone, true
}
return model.ItemNone, false
}
func parseRef(expr ast.Expr) string {
s := exprToString(expr)
s = strings.TrimPrefix(s, "&")
return s
}
func exprToString(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.SelectorExpr:
return exprToString(t.X) + "." + t.Sel.Name
case *ast.BasicLit:
return t.Value
case *ast.UnaryExpr:
return t.Op.String() + exprToString(t.X)
case *ast.CallExpr:
s := exprToString(t.Fun) + "("
for i, arg := range t.Args {
if i > 0 {
s += ", "
}
s += exprToString(arg)
}
s += ")"
return s
case *ast.CompositeLit:
s := ""
if t.Type != nil {
s += exprToString(t.Type)
}
s += "{"
for i, elt := range t.Elts {
if i > 0 {
s += ", "
}
s += exprToString(elt)
}
s += "}"
return s
case *ast.KeyValueExpr:
return exprToString(t.Key) + ": " + exprToString(t.Value)
}
return ""
}
func exprToBool(expr ast.Expr) bool {
if ident, ok := expr.(*ast.Ident); ok {
return ident.Name == "true"
}
return false
}
func exprToInt(expr ast.Expr) int {
if lit, ok := expr.(*ast.BasicLit); ok && lit.Kind == token.INT {
val, _ := strconv.Atoi(lit.Value)
return val
}
return 0
}
func ToSnakeCase(s string) string {
return fmt.Convert(s).SnakeLow().String()
}
func kvToExpr(cl *ast.CompositeLit, key string) ast.Expr {
for _, elt := range cl.Elts {
if kv, ok := elt.(*ast.KeyValueExpr); ok {
if exprToString(kv.Key) == key {
return kv.Value
}
}
}
return nil
}
func FieldTypeToGoType(ft model.FieldType, ref string) string {
switch ft {
case model.FieldText, model.FieldRaw:
return "string"
case model.FieldInt:
return "int64"
case model.FieldFloat:
return "float64"
case model.FieldBool:
return "bool"
case model.FieldBlob:
return "[]byte"
case model.FieldIntSlice:
return "[]int"
case model.FieldStruct:
if ref != "" {
return resolveStructType(ref)
}
return "struct{}"
case model.FieldStructSlice:
if ref != "" {
return "[]" + resolveStructType(ref)
}
return "[]struct{}"
}
return "string"
}
func resolveStructType(ref string) string {
// ref is like "UserModel" or "pkg.UserModel"
parts := strings.Split(ref, ".")
last := parts[len(parts)-1]
if strings.HasSuffix(last, "Model") {
parts[len(parts)-1] = strings.TrimSuffix(last, "Model")
}
return strings.Join(parts, ".")
}
// validateItem hands model.ValidateItem the facts it reads (name, item slot, PK,
// and whether Ref/Options are set) and names the Definition in the error.
func validateItem(varName string, fields []FieldInfo) error {
mf := make([]model.Field, len(fields))
for i, f := range fields {
var db *model.FieldDB
if f.PK {
db = &model.FieldDB{PK: true}
}
mf[i] = model.Field{Name: f.ColumnName, Item: f.Item, DB: db}
if f.Ref != "" && f.Type != model.FieldStruct && f.Type != model.FieldStructSlice {
mf[i].Ref = &model.Definition{Name: f.Ref}
}
if f.Options != "" {
mf[i].Options = &model.Definition{Name: f.Options}
}
}
if err := model.ValidateItem(mf); err != nil {
return fmt.Err(varName + ": " + err.Error())
}
return nil
}
func parseExternalCalls(varName string, expr ast.Expr) ([]model.ExternalCallDefinition, error) {
cl, ok := expr.(*ast.CompositeLit)
if !ok {
return nil, fmt.Errf("%s: ExternalCalls must be a slice literal []model.ExternalCallDefinition{...}", varName)
}
var out []model.ExternalCallDefinition
for _, elt := range cl.Elts {
itemCl, ok := elt.(*ast.CompositeLit)
if !ok {
continue
}
var def model.ExternalCallDefinition
for _, itemElt := range itemCl.Elts {
kv, ok := itemElt.(*ast.KeyValueExpr)
if !ok {
continue
}
key := exprToString(kv.Key)
switch key {
case "Name":
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.STRING {
val, _ := strconv.Unquote(lit.Value)
def.Name = val
}
case "Mode":
modeName := ""
if sel, ok := kv.Value.(*ast.SelectorExpr); ok {
modeName = sel.Sel.Name
} else if id, ok := kv.Value.(*ast.Ident); ok {
modeName = id.Name
}
switch modeName {
case "WaitForReply":
def.Mode = model.WaitForReply
case "RetryInBackground":
def.Mode = model.RetryInBackground
case "WaitForConfirmation":
def.Mode = model.WaitForConfirmation
}
case "MaxWaitSeconds":
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.INT {
v, _ := strconv.ParseInt(lit.Value, 10, 64)
def.MaxWaitSeconds = v
}
case "MaxAttempts":
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.INT {
v, _ := strconv.ParseInt(lit.Value, 10, 64)
def.MaxAttempts = v
}
}
}
if err := def.Validate(); err != nil {
return nil, fmt.Errf("%s: %s", varName, err.Error())
}
out = append(out, def)
}
return out, nil
}