This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from datacontract/datacontract-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
842 lines (720 loc) · 22.2 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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
package datacontract
import (
"fmt"
"gopkg.in/yaml.v3"
"io"
"log"
"os"
)
// internal model
type InternalModelSpecification struct {
Type string
Models []InternalModel
}
type InternalModel struct {
Name string
Type *string
Description *string
Fields []InternalField
}
type InternalField struct {
Name string
Type *string
Description *string
Required bool
Unique bool
AdditionalConstraints []InternalFieldConstraint
Fields []InternalField
}
type InternalFieldConstraint struct {
Type string
Expression string
}
// model difference
type ModelDifference struct {
Type ModelDifferenceType
Level ModelDifferenceLevel
Severity ModelDifferenceSeverity
ModelName *string
FieldName *string
Description string
}
const InternalModelSpecificationType = "data-contract-specification"
type ModelDifferenceType int
const (
ModelDifferenceTypeModelRemoved ModelDifferenceType = iota
ModelDifferenceTypeFieldRemoved
ModelDifferenceTypeFieldTypeChanged
ModelDifferenceTypeFieldRequirementRemoved
ModelDifferenceTypeFieldUniquenessRemoved
ModelDifferenceTypeFieldAdditionalConstraintAdded
ModelDifferenceTypeFieldAdditionalConstraintRemoved
ModelDifferenceTypeDatasetSchemaTypeChanged
ModelDifferenceTypeModelAdded
ModelDifferenceTypeModelTypeChanged
ModelDifferenceTypeFieldAdded
ModelDifferenceTypeFieldRequirementAdded
ModelDifferenceTypeFieldUniquenessAdded
ModelDifferenceTypeFieldDescriptionChanged
)
func (d ModelDifferenceType) String() string {
switch d {
// breaking
case ModelDifferenceTypeModelRemoved:
return "model-removed"
case ModelDifferenceTypeFieldRemoved:
return "field-removed"
case ModelDifferenceTypeFieldTypeChanged:
return "field-type-changed"
case ModelDifferenceTypeFieldRequirementRemoved:
return "field-requirement-removed"
case ModelDifferenceTypeFieldUniquenessRemoved:
return "field-uniqueness-removed"
case ModelDifferenceTypeFieldAdditionalConstraintRemoved:
return "field-constraint-removed"
case ModelDifferenceTypeFieldAdditionalConstraintAdded:
return "field-constraint-added"
// info
case ModelDifferenceTypeDatasetSchemaTypeChanged:
return "dataset-schema-type-changed"
case ModelDifferenceTypeModelAdded:
return "model-added"
case ModelDifferenceTypeModelTypeChanged:
return "model-type-changed"
case ModelDifferenceTypeFieldAdded:
return "field-added"
case ModelDifferenceTypeFieldRequirementAdded:
return "field-requirement-added"
case ModelDifferenceTypeFieldUniquenessAdded:
return "field-uniqueness-added"
case ModelDifferenceTypeFieldDescriptionChanged:
return "field-description-changed"
}
return ""
}
type ModelDifferenceLevel int
const (
ModelDifferenceLevelDataset ModelDifferenceLevel = iota
ModelDifferenceLevelModel
ModelDifferenceLevelField
)
func (d ModelDifferenceLevel) String() string {
switch d {
case ModelDifferenceLevelDataset:
return "dataset"
case ModelDifferenceLevelModel:
return "model"
case ModelDifferenceLevelField:
return "field"
}
return ""
}
type ModelDifferenceSeverity int
const (
ModelDifferenceSeverityInfo ModelDifferenceSeverity = iota
ModelDifferenceSeverityBreaking
)
func (d ModelDifferenceSeverity) String() string {
switch d {
case ModelDifferenceSeverityInfo:
return "info"
case ModelDifferenceSeverityBreaking:
return "breaking"
}
return ""
}
type datasetComparison = func(old, new InternalModelSpecification) []ModelDifference
func CompareModelSpecifications(old, new InternalModelSpecification) (result []ModelDifference) {
comparisons := []datasetComparison{
// breaking
modelRemoved,
fieldRemoved,
fieldTypeChanged,
fieldRequirementRemoved,
fieldUniquenessRemoved,
fieldConstraintAdded,
fieldConstraintRemoved,
// info
datasetSchemaTypeChanged,
modelAdded,
modelTypeChanged,
fieldAdded,
fieldRequirementAdded,
fieldUniquenessAdded,
fieldDescriptionChanged,
}
for _, comparison := range comparisons {
result = append(result, comparison(old, new)...)
}
return result
}
func modelRemoved(old, new InternalModelSpecification) (result []ModelDifference) {
for _, oldModel := range old.Models {
if oldModel.findEquivalent(new.Models) == nil {
result = append(result, ModelDifference{
Type: ModelDifferenceTypeModelRemoved,
Level: ModelDifferenceLevelModel,
Severity: ModelDifferenceSeverityBreaking,
ModelName: &oldModel.Name,
Description: fmt.Sprintf("model '%v' was removed", oldModel.Name),
})
}
}
return result
}
func fieldRemoved(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExistsNot = func(
modelName, fieldName string,
field InternalField,
) (result []ModelDifference) {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldRemoved,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityBreaking,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field '%v.%v' was removed", modelName, fieldName),
})
}
return append(result, compareFields(old, new, nil, &difference)...)
}
func fieldTypeChanged(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
if !EqualStringPointers(oldField.Type, newField.Type) {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldTypeChanged,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityBreaking,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf(
"type of field '%v.%v' was changed from '%v' to '%v'",
modelName,
fieldName,
StringPointerString(oldField.Type),
StringPointerString(newField.Type),
),
})
} else {
return result
}
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func fieldRequirementRemoved(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
if oldField.Required && !newField.Required {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldRequirementRemoved,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityBreaking,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field requirement of '%v.%v' was removed", modelName, fieldName),
})
} else {
return result
}
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func fieldUniquenessRemoved(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
if oldField.Unique && !newField.Unique {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldUniquenessRemoved,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityBreaking,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field uniqueness of '%v.%v' was removed", modelName, fieldName),
})
} else {
return result
}
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func fieldConstraintAdded(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
for _, constraint := range newField.AdditionalConstraints {
if !constraint.isIn(oldField.AdditionalConstraints) {
result = append(result, ModelDifference{
Type: ModelDifferenceTypeFieldAdditionalConstraintAdded,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityBreaking,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field constraint (%v: %v) of '%v.%v' was added",
constraint.Type, constraint.Expression, modelName, fieldName),
})
}
}
return result
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func fieldConstraintRemoved(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
for _, constraint := range oldField.AdditionalConstraints {
if !constraint.isIn(newField.AdditionalConstraints) {
result = append(result, ModelDifference{
Type: ModelDifferenceTypeFieldAdditionalConstraintRemoved,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityBreaking,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field constraint (%v: %v) of '%v.%v' was removed",
constraint.Type, constraint.Expression, modelName, fieldName),
})
}
}
return result
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func datasetSchemaTypeChanged(old, new InternalModelSpecification) (result []ModelDifference) {
if old.Type != new.Type {
result = append(result, ModelDifference{
Type: ModelDifferenceTypeDatasetSchemaTypeChanged,
Level: ModelDifferenceLevelDataset,
Severity: ModelDifferenceSeverityInfo,
Description: fmt.Sprintf("schema type changed from '%v' to '%v'", old.Type, new.Type),
})
}
return result
}
func modelAdded(old, new InternalModelSpecification) (result []ModelDifference) {
for _, newModel := range new.Models {
if newModel.findEquivalent(old.Models) == nil {
result = append(result, ModelDifference{
Type: ModelDifferenceTypeModelAdded,
Level: ModelDifferenceLevelModel,
Severity: ModelDifferenceSeverityInfo,
ModelName: &newModel.Name,
Description: fmt.Sprintf("model '%v' was added", newModel.Name),
})
}
}
return result
}
func modelTypeChanged(old, new InternalModelSpecification) (result []ModelDifference) {
for _, oldModel := range old.Models {
if newModel := oldModel.findEquivalent(new.Models); newModel != nil && !EqualStringPointers(oldModel.Type, newModel.Type) {
result = append(result, ModelDifference{
Type: ModelDifferenceTypeModelTypeChanged,
Level: ModelDifferenceLevelModel,
Severity: ModelDifferenceSeverityInfo,
ModelName: &oldModel.Name,
Description: fmt.Sprintf("type of model '%v' was changed from '%v' to '%v'", oldModel.Name, StringPointerString(oldModel.Type), StringPointerString(newModel.Type)),
})
}
}
return result
}
func fieldAdded(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExistsNot = func(
modelName, fieldName string,
field InternalField,
) (result []ModelDifference) {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldAdded,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityInfo,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field '%v.%v' was added", modelName, fieldName),
})
}
return append(result, compareFields(new, old, nil, &difference)...)
}
func fieldRequirementAdded(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
if !oldField.Required && newField.Required {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldRequirementAdded,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityInfo,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field requirement of '%v.%v' was added", modelName, fieldName),
})
} else {
return result
}
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func fieldUniquenessAdded(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
if !oldField.Unique && newField.Unique {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldUniquenessAdded,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityInfo,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf("field uniqueness of '%v.%v' was added", modelName, fieldName),
})
} else {
return result
}
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func fieldDescriptionChanged(old, new InternalModelSpecification) (result []ModelDifference) {
var difference fieldEquivalentExists = func(
modelName, fieldName string,
oldField, newField InternalField,
) (result []ModelDifference) {
if !EqualStringPointers(oldField.Description, newField.Description) {
return append(result, ModelDifference{
Type: ModelDifferenceTypeFieldDescriptionChanged,
Level: ModelDifferenceLevelField,
Severity: ModelDifferenceSeverityInfo,
ModelName: &modelName,
FieldName: &fieldName,
Description: fmt.Sprintf(
"description of field '%v.%v' has changed from '%v' to '%v'",
modelName,
fieldName,
StringPointerString(oldField.Description),
StringPointerString(newField.Description),
),
})
} else {
return result
}
}
return append(result, compareFields(old, new, &difference, nil)...)
}
func (constraint InternalFieldConstraint) isIn(list []InternalFieldConstraint) bool {
for _, oldConstraint := range list {
if constraint.Type == oldConstraint.Type && constraint.Expression == oldConstraint.Expression {
return true
}
}
return false
}
type fieldEquivalentExists func(
modelName, prefixedFieldName string,
left, right InternalField,
) []ModelDifference
type fieldEquivalentExistsNot func(
modelName string,
prefixedFieldName string,
field InternalField,
) []ModelDifference
// traverse through fields and their subfields
// apply corresponding methods if equivalent field in left dataset exists in right dataset or not
func compareFields(
left, right InternalModelSpecification,
whenEquivalentExists *fieldEquivalentExists,
whenEquivalentExistsNot *fieldEquivalentExistsNot,
) (result []ModelDifference) {
for _, leftModel := range left.Models {
if rightModel := leftModel.findEquivalent(right.Models); rightModel != nil {
result = append(
result,
compareFieldsRecursive(
leftModel.Fields,
rightModel.Fields,
leftModel.Name,
nil,
whenEquivalentExists,
whenEquivalentExistsNot,
)...,
)
}
}
return result
}
func compareFieldsRecursive(
leftFields, rightFields []InternalField,
modelName string,
fieldPrefix *string,
whenEquivalentExists *fieldEquivalentExists,
whenEquivalentExistsNot *fieldEquivalentExistsNot,
) (result []ModelDifference) {
for _, leftField := range leftFields {
fieldName := leftField.prefixedName(fieldPrefix)
if rightField := leftField.findEquivalent(rightFields); rightField == nil {
if whenEquivalentExistsNot != nil {
result = append(result, (*whenEquivalentExistsNot)(modelName, fieldName, leftField)...)
}
} else {
if whenEquivalentExists != nil {
result = append(result, (*whenEquivalentExists)(modelName, fieldName, leftField, *rightField)...)
}
result = append(result,
compareFieldsRecursive(
leftField.Fields,
rightField.Fields,
modelName,
&fieldName,
whenEquivalentExists,
whenEquivalentExistsNot,
)...)
}
}
return result
}
func (field InternalField) prefixedName(prefix *string) string {
var fieldName string
if prefix == nil {
fieldName = field.Name
} else {
fieldName = fmt.Sprintf("%v.%v", *prefix, field.Name)
}
return fieldName
}
func (model InternalModel) findEquivalent(otherModels []InternalModel) (result *InternalModel) {
for _, newModel := range otherModels {
if model.Name == newModel.Name {
result = &newModel
break
}
}
return result
}
func (field InternalField) findEquivalent(otherFields []InternalField) (result *InternalField) {
for _, newField := range otherFields {
if field.Name == newField.Name {
result = &newField
break
}
}
return result
}
func GetModelsFromSpecification(contract DataContract, pathToModels []string) (*InternalModelSpecification, error) {
sm, err := GetValue(contract, pathToModels)
if err != nil {
// ignore if no contract was found
return nil, nil
}
specModels, err := enforceMap(sm)
if err != nil {
return nil, err
}
// inline all references inside the models section
InlineReferences(&specModels, contract)
if err != nil {
// only warn on error to be compatible to embedded schema notation, this should fail, when schema support is dropped
log.Printf("Can not resolve specModels for path %v: %v", pathToModels, err)
return nil, nil
}
modelsMap, err := enforceMap(specModels)
if err != nil {
return nil, err
}
return internalModelSpecification(modelsMap)
}
func internalModelSpecification(modelsMap map[string]any) (*InternalModelSpecification, error) {
var internalModels []InternalModel
for modelName, specModel := range modelsMap {
specModelMap, err := enforceMap(specModel)
if err != nil {
return nil, err
}
model, err := internalModel(specModelMap, modelName)
if err != nil {
return nil, err
}
internalModels = append(internalModels, *model)
}
return &InternalModelSpecification{Type: InternalModelSpecificationType, Models: internalModels}, nil
}
func internalModel(specModel map[string]any, modelName string) (*InternalModel, error) {
modelType, err := fieldAsString(specModel, "type")
if err != nil {
return nil, err
}
modelDescription, err := fieldAsString(specModel, "description")
if err != nil {
return nil, err
}
internalFields, err := internalFields(specModel)
if err != nil {
return nil, err
}
return &InternalModel{
Name: modelName,
Type: modelType,
Description: modelDescription,
Fields: internalFields,
}, nil
}
func internalFields(specModelMap map[string]any) ([]InternalField, error) {
var internalFields []InternalField
fields := specModelMap["fields"]
if fields != nil {
fieldsMap, err := enforceMap(fields)
if err != nil {
return nil, err
}
for fieldName, specField := range fieldsMap {
fieldMap, err := enforceMap(specField)
if err != nil {
return nil, err
}
internalField, err := internalField(fieldMap, fieldName)
if err != nil {
return nil, err
}
internalFields = append(internalFields, *internalField)
}
}
return internalFields, nil
}
func internalField(fieldMap map[string]any, fieldName string) (*InternalField, error) {
fieldType, err := fieldAsString(fieldMap, "type")
if err != nil {
return nil, err
}
fieldDescription, err := fieldAsString(fieldMap, "description")
if err != nil {
return nil, err
}
return &InternalField{
Name: fieldName,
Type: fieldType,
Description: fieldDescription,
}, nil
}
func enforceMap(field any) (map[string]any, error) {
if anyMap, isMap := field.(map[string]any); !isMap {
return nil, fmt.Errorf("field is not a map")
} else {
return anyMap, nil
}
}
func fieldAsString(anyMap map[string]any, fieldName string) (*string, error) {
field := anyMap[fieldName]
if field == nil {
return nil, nil
}
if result, ok := field.(string); !ok {
return nil, fmt.Errorf("field %v is not a map", fieldName)
} else {
return &result, nil
}
}
func InsertModel(dataContractLocation string, model []byte, modelFormat string, pathToModels []string) error {
contract, err := GetDataContract(dataContractLocation)
if err != nil {
return err
}
var models map[string]any
if modelFormat == InternalModelSpecificationType {
models, err = parseModels(model)
if err != nil {
return err
}
} else {
models = ParseSchema(modelFormat, model).asMapForDataContract()
}
SetValue(contract, pathToModels, models)
result, err := ToYaml(contract)
if err != nil {
return err
}
err = os.WriteFile(dataContractLocation, result, os.ModePerm)
if err != nil {
return err
}
return nil
}
func parseModels(model []byte) (map[string]any, error) {
modelsFromInput := map[string]any{}
yaml.Unmarshal(model, modelsFromInput)
// convert to spec model and back to map, to clean / validate the input
specification, err := internalModelSpecification(modelsFromInput)
if err != nil {
return nil, err
}
return specification.asMapForDataContract(), nil
}
func (specification InternalModelSpecification) asMapForDataContract() map[string]any {
models := map[string]any{}
for _, model := range specification.Models {
models[model.Name] = model.asMapForDataContract()
}
return models
}
func (model InternalModel) asMapForDataContract() map[string]any {
modelAsMAp := map[string]any{}
if model.Type != nil {
modelAsMAp["type"] = *model.Type
} else {
modelAsMAp["type"] = "table"
}
if model.Description != nil {
modelAsMAp["description"] = *model.Description
}
fields := map[string]any{}
for _, field := range model.Fields {
fields[field.Name] = field.asMapForDataContract()
}
modelAsMAp["fields"] = fields
return modelAsMAp
}
func (field InternalField) asMapForDataContract() map[string]any {
fieldAsMap := map[string]any{}
if field.Type != nil {
fieldAsMap["type"] = *field.Type
}
if field.Description != nil {
fieldAsMap["description"] = *field.Description
}
return fieldAsMap
}
func PrintModel(dataContractLocation string, modelType string, pathToModels []string, target io.Writer) error {
contract, err := GetDataContract(dataContractLocation)
if err != nil {
return err
}
var output []byte
if modelType == InternalModelSpecificationType {
models, err := GetValue(contract, pathToModels)
if err != nil {
return err
}
println(modelType)
output, err = yaml.Marshal(models.(map[string]any))
if err != nil {
return err
}
} else {
specification, err := GetModelsFromSpecification(contract, pathToModels)
if err != nil {
return err
}
output, err = CreateSchema(modelType, *specification)
if err != nil {
return err
}
}
Log(target, string(output))
return nil
}