forked from chop-dbhi/data-models-service
/
model.go
596 lines (459 loc) · 10.4 KB
/
model.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
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
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
type Attrs map[string]string
type TableFieldIndex map[string]map[string]Attrs
func (i TableFieldIndex) Add(t, f string, a Attrs) {
t = strings.ToLower(t)
f = strings.ToLower(f)
if _, ok := i[t]; !ok {
i[t] = make(map[string]Attrs)
}
i[t][f] = a
}
func (i TableFieldIndex) Get(t, f string) Attrs {
t = strings.ToLower(t)
f = strings.ToLower(f)
if _, ok := i[t]; !ok {
return nil
}
return i[t][f]
}
type Models []*Model
func (m Models) Len() int {
return len(m)
}
// TODO: handle version numbers correctly.
func (m Models) Less(i, j int) bool {
a := m[i]
b := m[j]
if a.Name < b.Name {
return true
} else if a.Name > b.Name {
return false
}
return a.Version < b.Version
}
func (m Models) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
type ModelIndex map[string]map[string]*Model
func (mi ModelIndex) Keys() []string {
keys := make([]string, len(mi))
i := 0
for k, _ := range mi {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}
func (mi ModelIndex) Add(m *Model) {
n := strings.ToLower(m.Name)
v := strings.ToLower(m.Version)
if _, ok := mi[n]; !ok {
mi[n] = make(map[string]*Model)
}
mi[n][v] = m
}
func (mi ModelIndex) Get(n, v string) *Model {
n = strings.ToLower(n)
v = strings.ToLower(v)
if ms, ok := mi[n]; ok {
return ms[v]
}
return nil
}
func (mi ModelIndex) Versions(n string) Models {
n = strings.ToLower(n)
if _, ok := mi[n]; !ok {
return nil
}
models := make(Models, 0)
for _, m := range mi[n] {
models = append(models, m)
}
sort.Sort(models)
return models
}
func (mi ModelIndex) List() Models {
models := make(Models, 0)
for _, mn := range mi {
for _, m := range mn {
models = append(models, m)
}
}
sort.Sort(models)
return models
}
type Tables []*Table
func (ts Tables) Len() int {
return len(ts)
}
func (ts Tables) Less(i, j int) bool {
a := ts[i]
b := ts[j]
return a.Name < b.Name
}
func (ts Tables) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}
type TableIndex map[string]*Table
func (ti TableIndex) Add(t *Table) {
ti[strings.ToLower(t.Name)] = t
}
func (ti TableIndex) Get(n string) *Table {
return ti[strings.ToLower(n)]
}
func (ti TableIndex) Names() []string {
names := make([]string, len(ti))
i := 0
for s, _ := range ti {
names[i] = s
i++
}
sort.Strings(names)
return names
}
func (ti TableIndex) List() Tables {
tables := make(Tables, len(ti))
i := 0
for _, t := range ti {
tables[i] = t
i++
}
sort.Sort(tables)
return tables
}
// MarshalJSON implements the json.Marshaler interface. The marshaled value
// is a sorted list of tables.
func (ti TableIndex) MarshalJSON() ([]byte, error) {
return json.Marshal(ti.List())
}
type Fields []*Field
func (fs Fields) Len() int {
return len(fs)
}
func (fs Fields) Less(i, j int) bool {
a := fs[i]
b := fs[j]
return a.Name < b.Name
}
func (fs Fields) Swap(i, j int) {
fs[i], fs[j] = fs[j], fs[i]
}
type FieldIndex map[string]*Field
func (fi FieldIndex) Add(f *Field) {
fi[strings.ToLower(f.Name)] = f
}
func (fi FieldIndex) Get(n string) *Field {
return fi[strings.ToLower(n)]
}
func (ti FieldIndex) Names() []string {
names := make([]string, len(ti))
i := 0
for s, _ := range ti {
names[i] = s
i++
}
sort.Strings(names)
return names
}
func (fi FieldIndex) List() Fields {
fields := make(Fields, len(fi))
i := 0
for _, f := range fi {
fields[i] = f
i++
}
sort.Sort(fields)
return fields
}
// MarshalJSON implements the json.Marshaler interface. The marshaled value
// is a sorted list of fields.
func (fi FieldIndex) MarshalJSON() ([]byte, error) {
return json.Marshal(fi.List())
}
type Model struct {
Label string
Name string
Version string
Description string
URL string
Tables TableIndex
schema *Schema
path string
}
func (m *Model) MarshalJSON() ([]byte, error) {
aux := map[string]interface{}{
"name": m.Name,
"version": m.Version,
"description": m.Description,
"url": m.URL,
"tables": m.Tables,
}
return json.Marshal(aux)
}
func (m *Model) String() string {
if m.Label != "" {
return m.Label
}
return fmt.Sprintf("%s/%s", m.Name, m.Version)
}
func (m *Model) Path() string {
return fmt.Sprintf("%s/%s", m.Name, m.Version)
}
func (m *Model) Slug() string {
return fmt.Sprintf("%s-%s", m.Name, m.Version)
}
type Table struct {
Name string
Label string
Description string
Fields FieldIndex
Model *Model
attrs Attrs
}
func (t *Table) MarshalJSON() ([]byte, error) {
aux := map[string]interface{}{
"model": t.Model.Name,
"version": t.Model.Version,
"name": t.Name,
"label": t.Label,
"description": t.Description,
"fields": t.Fields,
}
return json.Marshal(aux)
}
func (t *Table) String() string {
if t.Label != "" {
return t.Label
}
return t.Name
}
func (t *Table) Path() string {
return fmt.Sprintf("%s/%s", t.Model.Path(), t.Name)
}
func (t *Table) Slug() string {
return fmt.Sprintf("%s-%s", t.Model.Slug(), t.Name)
}
type Field struct {
Name string
Label string
Description string
Required bool
// Schema fields
Type string
Length string
Precision string
Scale string
Default string
Table *Table
Mappings []*Mapping
// The field this field was renamed from in the previous version.
RenamedFrom *Field
// The field this field was renamed to in the next version.
RenamedTo *Field
// The field this field references.
References *Reference
// Fields that reference this field.
InboundRefs []*Reference
attrs Attrs
}
func (f *Field) String() string {
if f.Label != "" {
return f.Label
}
return f.Name
}
func (f *Field) Path() string {
return fmt.Sprintf("%s/%s", f.Table.Path(), f.Name)
}
func (f *Field) Slug() string {
return fmt.Sprintf("%s-%s", f.Table.Slug(), f.Name)
}
func (f *Field) MarshalJSON() ([]byte, error) {
aux := map[string]interface{}{
"name": f.Name,
"label": f.Label,
"table": f.Table.Name,
"description": f.Description,
"required": f.Required,
"type": f.Type,
"length": json.Number(f.Length),
"precision": json.Number(f.Precision),
"scale": json.Number(f.Scale),
"default": f.Default,
}
return json.Marshal(aux)
}
// A mapping defined a correspondence between two fields. A mapping points
// points to the opposing field and a *comment* which describes the nuances
// of the relationship between the two fields.
type Mapping struct {
Field *Field
Comment string
}
// Reference declares that the source field is a reference to the target field.
type Reference struct {
Name string
Field *Field
attrs Attrs
}
func (r *Reference) String() string {
return r.Name
}
func (r *Reference) MarshalJSON() ([]byte, error) {
aux := map[string]string{
"name": r.Name,
"table": r.Field.Table.Name,
"field": r.Field.Name,
}
return json.Marshal(aux)
}
// Schema contains constraints and indexes for a model.
type Schema struct {
// Schematic components.
PrimaryKeys map[string]*PrimaryKey
ForeignKeys []*ForeignKey
NotNullables []*NotNullable
Uniques map[string]*Unique
Indexes map[string]*Index
}
func (s *Schema) MarshalJSON() ([]byte, error) {
pks := make([]*PrimaryKey, len(s.PrimaryKeys))
uniqs := make([]*Unique, len(s.Uniques))
indexes := make([]*Index, len(s.Indexes))
i := 0
for _, pk := range s.PrimaryKeys {
pks[i] = pk
i++
}
i = 0
for _, un := range s.Uniques {
uniqs[i] = un
i++
}
i = 0
for _, idx := range s.Indexes {
indexes[i] = idx
i++
}
aux := map[string]interface{}{
"indexes": indexes,
"constraints": map[string]interface{}{
"foreign_keys": s.ForeignKeys,
"primary_keys": pks,
"uniques": uniqs,
"not_null": s.NotNullables,
},
}
return json.Marshal(aux)
}
func (s *Schema) AddPrimaryKey(a Attrs) {
if s.PrimaryKeys == nil {
s.PrimaryKeys = make(map[string]*PrimaryKey)
}
n := a["name"]
if pk, ok := s.PrimaryKeys[n]; !ok {
s.PrimaryKeys[n] = &PrimaryKey{
Name: n,
Table: a["table"],
Fields: []string{a["field"]},
}
} else {
pk.Fields = append(pk.Fields, a["field"])
}
}
func (s *Schema) AddForeignKey(a Attrs) {
s.ForeignKeys = append(s.ForeignKeys, &ForeignKey{
Name: a["name"],
SourceTable: a["table"],
SourceField: a["field"],
TargetTable: a["ref_table"],
TargetField: a["ref_field"],
})
}
func (s *Schema) AddNotNullable(a Attrs) {
s.NotNullables = append(s.NotNullables, &NotNullable{
Table: a["table"],
Field: a["field"],
})
}
func (s *Schema) AddUnique(a Attrs) {
if s.Uniques == nil {
s.Uniques = make(map[string]*Unique)
}
n := a["name"]
if un, ok := s.Uniques[n]; !ok {
s.Uniques[n] = &Unique{
Name: n,
Table: a["table"],
Fields: []string{a["field"]},
}
} else {
un.Fields = append(un.Fields, a["field"])
}
}
func (s *Schema) AddIndex(a Attrs) {
if s.Indexes == nil {
s.Indexes = make(map[string]*Index)
}
n := a["name"]
if idx, ok := s.Indexes[n]; !ok {
var uniq bool
switch strings.ToLower(a["unique"]) {
case "yes", "y", "1":
uniq = true
}
s.Indexes[n] = &Index{
Name: n,
Order: a["order"],
Table: a["table"],
Unique: uniq,
Fields: []string{a["field"]},
}
} else {
idx.Fields = append(idx.Fields, a["field"])
}
}
// PrimaryKey is a constraint which declares the field values uniquely define
// a record in the respective table.
type PrimaryKey struct {
Name string `json:"name"`
Table string `json:"table"`
Fields []string `json:"fields"`
}
// Unique is a constraint which declares the field values be unique for
// a record in the respective table.
type Unique struct {
Name string `json:"name"`
Table string `json:"table"`
Fields []string `json:"fields"`
}
// ForeignKey is a constraint which declares the field values are constrained
// to values in the referenced fields.
type ForeignKey struct {
Name string `json:"name"`
SourceTable string `json:"source_table"`
SourceField string `json:"source_field"`
TargetTable string `json:"target_table"`
TargetField string `json:"target_field"`
}
// NotNullable is a constraint which declares the field cannot be a null.
type NotNullable struct {
Table string `json:"table"`
Field string `json:"field"`
}
// Index represents a schematic index for one or more fields.
type Index struct {
Name string `json:"name"`
Unique bool `json:"unique"`
Order string `json:"order"`
Table string `json:"table"`
Fields []string `json:"fields"`
}