-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscalars.go
1371 lines (1221 loc) · 37 KB
/
scalars.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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package raml
import (
"fmt"
"math/big"
"regexp"
"time"
"gopkg.in/yaml.v3"
"github.com/acronis/go-stacktrace"
)
const (
RFC2616 = "Mon, 02 Jan 2006 15:04:05 GMT"
// NOTE: time.DateTime uses "2006-01-02 15:04:05" format which is different from date-time defined in RAML spec.
DateTime = "2006-01-02T15:04:05"
)
type EnumFacets struct {
Enum Nodes
}
func (r *RAML) MakeEnum(v *yaml.Node, location string) (Nodes, error) {
if v.Kind != yaml.SequenceNode {
return nil, StacktraceNew("enum must be sequence node", location, WithNodePosition(v))
}
enums := make(Nodes, len(v.Content))
for i, v := range v.Content {
n, err := r.makeRootNode(v, location)
if err != nil {
return nil, StacktraceNewWrapped("make node enum", err, location, WithNodePosition(v))
}
enums[i] = n
}
return enums, nil
}
func isCompatibleEnum(source Nodes, target Nodes) bool {
// Target enum must be a subset of source enum
for _, v := range target {
found := false
for _, e := range source {
if v.Value == e.Value {
found = true
break
}
}
if !found {
return false
}
}
return true
}
type FormatFacets struct {
Format *string
}
type IntegerFacets struct {
Minimum *big.Int
Maximum *big.Int
MultipleOf *float64
}
type scalarShape struct{}
func (scalarShape) IsScalar() bool {
return true
}
type IntegerShape struct {
scalarShape
*BaseShape
EnumFacets
FormatFacets
IntegerFacets
}
func (s *IntegerShape) Base() *BaseShape {
return s.BaseShape
}
func (s *IntegerShape) cloneShallow(base *BaseShape) Shape {
return s.clone(base, nil)
}
func (s *IntegerShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
c := *s
c.BaseShape = base
return &c
}
func (s *IntegerShape) alias(source Shape) (Shape, error) {
ss, ok := source.(*IntegerShape)
if !ok {
return nil, StacktraceNew("cannot make alias from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
s.Minimum = ss.Minimum
s.Maximum = ss.Maximum
s.MultipleOf = ss.MultipleOf
s.Format = ss.Format
return s, nil
}
func (s *IntegerShape) validate(v interface{}, _ string) error {
var val big.Int
switch v := v.(type) {
case int:
val.SetInt64(int64(v))
case uint:
val.SetUint64(uint64(v))
// json unmarshals numbers as float64
case float64:
val.SetInt64(int64(v))
default:
return fmt.Errorf("invalid type, got %T, expected int, uint or float64", v)
}
if s.Minimum != nil && val.Cmp(s.Minimum) < 0 {
return fmt.Errorf("value must be greater than %s", s.Minimum.String())
}
if s.Maximum != nil && val.Cmp(s.Maximum) > 0 {
return fmt.Errorf("value must be less than %s", s.Maximum.String())
}
// TODO: Implement multipleOf validation
// TODO: Implement format validation
if s.Enum != nil {
// TODO: Probably enum values should be stored as big.Int to simplify validation
var num any
if val.IsInt64() {
num = int(val.Int64())
} else if val.IsUint64() {
num = uint(val.Uint64())
}
found := false
for _, e := range s.Enum {
if e.Value == num {
found = true
break
}
}
if !found {
return fmt.Errorf("value must be one of (%s)", s.Enum.String())
}
}
return nil
}
func (s *IntegerShape) inherit(source Shape) (Shape, error) {
ss, ok := source.(*IntegerShape)
if !ok {
return nil, StacktraceNew("cannot inherit from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
if s.Minimum == nil {
s.Minimum = ss.Minimum
} else if ss.Minimum != nil && s.Minimum.Cmp(ss.Minimum) < 0 {
return nil, StacktraceNew("minimum constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.Minimum),
stacktrace.WithInfo("target", *s.Minimum))
}
if s.Maximum == nil {
s.Maximum = ss.Maximum
} else if ss.Maximum != nil && s.Maximum.Cmp(ss.Maximum) > 0 {
return nil, StacktraceNew("maximum constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.Maximum),
stacktrace.WithInfo("target", *s.Maximum))
}
// TODO: multipleOf validation
if s.MultipleOf == nil {
// TODO: Disallow multipleOf 0 to avoid division by zero during validation
s.MultipleOf = ss.MultipleOf
}
if s.Enum == nil {
s.Enum = ss.Enum
} else if ss.Enum != nil && !isCompatibleEnum(ss.Enum, s.Enum) {
return nil, StacktraceNew("enum constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", ss.Enum.String()),
stacktrace.WithInfo("target", s.Enum.String()))
}
if s.Format == nil {
s.Format = ss.Format
} else if ss.Format != nil && SetOfIntegerFormats[*s.Format] != SetOfIntegerFormats[*ss.Format] {
return nil, StacktraceNew("format constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.Format),
stacktrace.WithInfo("target", *s.Format))
}
return s, nil
}
func (s *IntegerShape) check() error {
if s.Minimum != nil && s.Maximum != nil && s.Minimum.Cmp(s.Maximum) > 0 {
return StacktraceNew("minimum must be less than or equal to maximum", s.Location,
stacktrace.WithPosition(&s.Position))
}
if s.Enum != nil {
for _, e := range s.Enum {
switch e.Value.(type) {
case int, uint:
default:
return StacktraceNew("enum value must be int or uint", s.Location,
stacktrace.WithPosition(&e.Position))
}
}
}
// invalid format, found by copilot =)
if s.Format != nil {
if _, ok := SetOfIntegerFormats[*s.Format]; !ok {
return StacktraceNew("invalid format", s.Location, stacktrace.WithPosition(&s.Position))
}
}
return nil
}
func (s *IntegerShape) unmarshalYAMLNode(node, valueNode *yaml.Node) error {
switch node.Value {
case FacetMinimum:
if valueNode.Tag != TagInt {
return StacktraceNew("minimum must be integer", s.Location, WithNodePosition(valueNode))
}
num, ok := big.NewInt(0).SetString(valueNode.Value, 10)
if !ok {
return StacktraceNew("invalid minimum value", s.Location, WithNodePosition(valueNode))
}
s.Minimum = num
case FacetMaximum:
if valueNode.Tag != TagInt {
return StacktraceNew("maximum must be integer", s.Location, WithNodePosition(valueNode))
}
num, ok := big.NewInt(0).SetString(valueNode.Value, 10)
if !ok {
return StacktraceNew("invalid maximum value", s.Location, WithNodePosition(valueNode))
}
s.Maximum = num
case FacetMultipleOf:
if err := valueNode.Decode(&s.MultipleOf); err != nil {
return StacktraceNewWrapped("decode multipleOf", err, s.Location, WithNodePosition(valueNode))
}
case FacetFormat:
if _, ok := SetOfIntegerFormats[valueNode.Value]; !ok {
return StacktraceNew("invalid format", s.Location, WithNodePosition(valueNode),
stacktrace.WithInfo("allowed_formats", SetOfIntegerFormats))
}
if err := valueNode.Decode(&s.Format); err != nil {
return StacktraceNewWrapped("decode format", err, s.Location, WithNodePosition(valueNode))
}
case FacetEnum:
enums, err := s.raml.MakeEnum(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make enum", err, s.Location, WithNodePosition(valueNode))
}
s.Enum = enums
default:
n, err := s.raml.makeRootNode(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make node", err, s.Location, WithNodePosition(valueNode))
}
s.CustomShapeFacets.Set(node.Value, n)
}
return nil
}
func (s *IntegerShape) unmarshalYAMLNodes(v []*yaml.Node) error {
if len(v)%2 != 0 {
return StacktraceNew("odd number of nodes", s.Location)
}
for i := 0; i != len(v); i += 2 {
node := v[i]
valueNode := v[i+1]
if err := s.unmarshalYAMLNode(node, valueNode); err != nil {
return fmt.Errorf("unmarshal %v: %v: %w", node, valueNode, err)
}
}
return nil
}
type NumberFacets struct {
// Minimum and maximum are unset since there's no theoretical minimum and maximum for numbers by default
Minimum *float64
Maximum *float64
MultipleOf *float64
}
type NumberShape struct {
scalarShape
*BaseShape
EnumFacets
FormatFacets
NumberFacets
}
func (s *NumberShape) Base() *BaseShape {
return s.BaseShape
}
func (s *NumberShape) cloneShallow(base *BaseShape) Shape {
return s.clone(base, nil)
}
func (s *NumberShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
c := *s
c.BaseShape = base
return &c
}
func (s *NumberShape) alias(source Shape) (Shape, error) {
ss, ok := source.(*NumberShape)
if !ok {
return nil, StacktraceNew("cannot make alias from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
s.Minimum = ss.Minimum
s.Maximum = ss.Maximum
s.MultipleOf = ss.MultipleOf
s.Format = ss.Format
return s, nil
}
func (s *NumberShape) validate(v interface{}, _ string) error {
var val float64
switch v := v.(type) {
// go-yaml unmarshals integers as int
case int:
val = float64(v)
case uint:
val = float64(v)
case float64:
val = v
default:
return fmt.Errorf("invalid type, got %T, expected int, uint, float64", v)
}
if s.Minimum != nil && val < *s.Minimum {
return fmt.Errorf("value must be greater than %f", *s.Minimum)
}
if s.Maximum != nil && val > *s.Maximum {
return fmt.Errorf("value must be less than %f", *s.Maximum)
}
// TODO: Implement multipleOf validation
// TODO: Implement format validation
if s.Enum != nil {
found := false
for _, e := range s.Enum {
if e.Value == val {
found = true
break
}
}
if !found {
return fmt.Errorf("value must be one of (%s)", s.Enum.String())
}
}
return nil
}
func (s *NumberShape) inherit(source Shape) (Shape, error) {
ss, ok := source.(*NumberShape)
if !ok {
return nil, StacktraceNew("cannot inherit from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
if s.Minimum == nil {
s.Minimum = ss.Minimum
} else if ss.Minimum != nil && *s.Minimum < *ss.Minimum {
return nil, StacktraceNew("minimum constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.Minimum),
stacktrace.WithInfo("target", *s.Minimum))
}
if s.Maximum == nil {
s.Maximum = ss.Maximum
} else if ss.Maximum != nil && *s.Maximum > *ss.Maximum {
return nil, StacktraceNew("maximum constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.Maximum),
stacktrace.WithInfo("target", *s.Maximum))
}
// TODO: multipleOf validation
if ss.MultipleOf != nil {
// TODO: Disallow multipleOf 0 to avoid division by zero during validation
s.MultipleOf = ss.MultipleOf
}
if s.Enum == nil {
s.Enum = ss.Enum
} else if ss.Enum != nil && !isCompatibleEnum(ss.Enum, s.Enum) {
return nil, StacktraceNew("enum constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", ss.Enum.String()),
stacktrace.WithInfo("target", s.Enum.String()))
}
if s.Format == nil {
s.Format = ss.Format
} else if ss.Format != nil && *s.Format != *ss.Format {
return nil, StacktraceNew("format constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.Format),
stacktrace.WithInfo("target", *s.Format))
}
return s, nil
}
func (s *NumberShape) check() error {
if s.Minimum != nil && s.Maximum != nil && *s.Minimum > *s.Maximum {
return StacktraceNew("minimum must be less than or equal to maximum", s.Location,
stacktrace.WithPosition(&s.Position))
}
if s.Enum != nil {
for _, e := range s.Enum {
switch e.Value.(type) {
case int, uint, float64:
default:
return StacktraceNew("enum value must be int, uint, float64", s.Location,
stacktrace.WithPosition(&e.Position))
}
}
}
return nil
}
func (s *NumberShape) unmarshalYAMLNodes(v []*yaml.Node) error {
if len(v)%2 != 0 {
return StacktraceNew("odd number of nodes", s.Location)
}
for i := 0; i != len(v); i += 2 {
node := v[i]
valueNode := v[i+1]
switch node.Value {
case FacetMinimum:
if err := valueNode.Decode(&s.Minimum); err != nil {
return StacktraceNewWrapped("decode minimum", err, s.Location, WithNodePosition(valueNode))
}
case FacetMaximum:
if err := valueNode.Decode(&s.Maximum); err != nil {
return StacktraceNewWrapped("decode maximum", err, s.Location, WithNodePosition(valueNode))
}
case FacetFormat:
if _, ok := SetOfNumberFormats[valueNode.Value]; !ok {
return StacktraceNew("invalid format", s.Location, WithNodePosition(valueNode),
stacktrace.WithInfo("allowed_formats", SetOfNumberFormats))
}
if err := valueNode.Decode(&s.Format); err != nil {
return StacktraceNewWrapped("decode format", err, s.Location, WithNodePosition(valueNode))
}
case FacetEnum:
enums, err := s.raml.MakeEnum(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make enum", err, s.Location, WithNodePosition(valueNode))
}
s.Enum = enums
case FacetMultipleOf:
if err := valueNode.Decode(&s.MultipleOf); err != nil {
return StacktraceNewWrapped("decode multipleOf", err, s.Location, WithNodePosition(valueNode))
}
default:
n, err := s.raml.makeRootNode(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make node", err, s.Location, WithNodePosition(valueNode))
}
s.CustomShapeFacets.Set(node.Value, n)
}
}
return nil
}
type LengthFacets struct {
MaxLength *uint64
MinLength *uint64
}
type StringFacets struct {
LengthFacets
Pattern *regexp.Regexp
}
type StringShape struct {
scalarShape
*BaseShape
EnumFacets
StringFacets
}
func (s *StringShape) Base() *BaseShape {
return s.BaseShape
}
func (s *StringShape) cloneShallow(base *BaseShape) Shape {
return s.clone(base, nil)
}
func (s *StringShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
c := *s
c.BaseShape = base
return &c
}
func (s *StringShape) alias(source Shape) (Shape, error) {
ss, ok := source.(*StringShape)
if !ok {
return nil, StacktraceNew("cannot make alias from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
s.MinLength = ss.MinLength
s.MaxLength = ss.MaxLength
s.Pattern = ss.Pattern
return s, nil
}
func (s *StringShape) validate(v interface{}, _ string) error {
i, ok := v.(string)
if !ok {
return fmt.Errorf("invalid type, got %T, expected string", v)
}
strLen := uint64(len(i))
if s.MinLength != nil && strLen < *s.MinLength {
return fmt.Errorf("length must be greater than %d", *s.MinLength)
}
if s.MaxLength != nil && strLen > *s.MaxLength {
return fmt.Errorf("length must be less than %d", *s.MaxLength)
}
if s.Pattern != nil && !s.Pattern.MatchString(i) {
return fmt.Errorf("must match pattern %s", s.Pattern.String())
}
if s.Enum != nil {
found := false
for _, e := range s.Enum {
if e.Value == i {
found = true
break
}
}
if !found {
return fmt.Errorf("value must be one of (%s)", s.Enum.String())
}
}
return nil
}
func (s *StringShape) inherit(source Shape) (Shape, error) {
ss, ok := source.(*StringShape)
if !ok {
return nil, StacktraceNew("cannot inherit from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
if s.MinLength == nil {
s.MinLength = ss.MinLength
} else if ss.MinLength != nil && *s.MinLength < *ss.MinLength {
return nil, StacktraceNew("minLength constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.MinLength),
stacktrace.WithInfo("target", *s.MinLength))
}
if s.MaxLength == nil {
s.MaxLength = ss.MaxLength
} else if ss.MaxLength != nil && *s.MaxLength > *ss.MaxLength {
return nil, StacktraceNew("maxLength constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.MaxLength),
stacktrace.WithInfo("target", *s.MaxLength))
}
// FIXME: Patterns are merged unconditionally, but ideally they should be validated against intersection of their DFAs
if s.Pattern == nil {
s.Pattern = ss.Pattern
}
if s.Enum == nil {
s.Enum = ss.Enum
} else if ss.Enum != nil && !isCompatibleEnum(ss.Enum, s.Enum) {
return nil, StacktraceNew("enum constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", ss.Enum.String()),
stacktrace.WithInfo("target", s.Enum.String()))
}
return s, nil
}
func (s *StringShape) check() error {
if s.MinLength != nil && s.MaxLength != nil && *s.MinLength > *s.MaxLength {
return StacktraceNew("minLength must be less than or equal to maxLength",
s.Location, stacktrace.WithPosition(&s.Position))
}
if s.Enum != nil {
for _, e := range s.Enum {
if _, ok := e.Value.(string); !ok {
return StacktraceNew("enum value must be string",
s.Location, stacktrace.WithPosition(&e.Position))
}
}
}
return nil
}
func (s *StringShape) unmarshalYAMLNodes(v []*yaml.Node) error {
if len(v)%2 != 0 {
return StacktraceNew("odd number of nodes", s.Location)
}
for i := 0; i != len(v); i += 2 {
node := v[i]
valueNode := v[i+1]
switch node.Value {
case FacetMinLength:
if err := valueNode.Decode(&s.MinLength); err != nil {
return StacktraceNewWrapped("decode minLength", err, s.Location, WithNodePosition(valueNode))
}
case FacetMaxLength:
if err := valueNode.Decode(&s.MaxLength); err != nil {
return StacktraceNewWrapped("decode maxLength", err, s.Location, WithNodePosition(valueNode))
}
case FacetPattern:
if valueNode.Tag != TagStr {
return StacktraceNew("pattern must be string", s.Location, WithNodePosition(valueNode))
}
re, err := regexp.Compile(valueNode.Value)
if err != nil {
return StacktraceNewWrapped("decode pattern", err, s.Location, WithNodePosition(valueNode))
}
s.Pattern = re
case FacetEnum:
enums, err := s.raml.MakeEnum(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make enum", err, s.Location, WithNodePosition(valueNode))
}
s.Enum = enums
default:
n, err := s.raml.makeRootNode(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make node", err, s.Location, WithNodePosition(valueNode))
}
s.CustomShapeFacets.Set(node.Value, n)
}
}
return nil
}
type FileFacets struct {
FileTypes Nodes
}
type FileShape struct {
scalarShape
*BaseShape
LengthFacets
FileFacets
}
func (s *FileShape) Base() *BaseShape {
return s.BaseShape
}
func (s *FileShape) cloneShallow(base *BaseShape) Shape {
return s.clone(base, nil)
}
func (s *FileShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
c := *s
c.BaseShape = base
return &c
}
func (s *FileShape) alias(source Shape) (Shape, error) {
ss, ok := source.(*FileShape)
if !ok {
return nil, StacktraceNew("cannot make alias from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
s.MinLength = ss.MinLength
s.MaxLength = ss.MaxLength
s.FileTypes = ss.FileTypes
return s, nil
}
func (s *FileShape) validate(v interface{}, _ string) error {
i, ok := v.(string)
if !ok {
return fmt.Errorf("invalid type, got %T, expected string", v)
}
// TODO: What is compared, byte size or base64 string size?
strLen := uint64(len(i))
if s.MinLength != nil && strLen < *s.MinLength {
return fmt.Errorf("length must be greater than %d", *s.MinLength)
}
if s.MaxLength != nil && strLen > *s.MaxLength {
return fmt.Errorf("length must be less than %d", *s.MaxLength)
}
// TODO: Validation against file types
return nil
}
func (s *FileShape) inherit(source Shape) (Shape, error) {
ss, ok := source.(*FileShape)
if !ok {
return nil, StacktraceNew("cannot inherit from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
if s.MinLength == nil {
s.MinLength = ss.MinLength
} else if ss.MinLength != nil && *s.MinLength < *ss.MinLength {
return nil, StacktraceNew("minLength constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.MinLength),
stacktrace.WithInfo("target", *s.MinLength))
}
if s.MaxLength == nil {
s.MaxLength = ss.MaxLength
} else if ss.MaxLength != nil && *s.MaxLength > *ss.MaxLength {
return nil, StacktraceNew("maxLength constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.MaxLength),
stacktrace.WithInfo("target", *s.MaxLength))
}
if s.FileTypes == nil {
s.FileTypes = ss.FileTypes
} else if ss.FileTypes != nil && !isCompatibleEnum(ss.FileTypes, s.FileTypes) {
return nil, StacktraceNew("file types are incompatible", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", ss.FileTypes.String()),
stacktrace.WithInfo("target", s.FileTypes.String()))
}
return s, nil
}
func (s *FileShape) check() error {
if s.MinLength != nil && s.MaxLength != nil && *s.MinLength > *s.MaxLength {
return StacktraceNew("minLength must be less than or equal to maxLength", s.Location,
stacktrace.WithPosition(&s.Position))
}
if s.FileTypes != nil {
for _, e := range s.FileTypes {
if _, ok := e.Value.(string); !ok {
return StacktraceNew("file type must be string", s.Location,
stacktrace.WithPosition(&s.Position))
}
}
}
return nil
}
func (s *FileShape) unmarshalYAMLNodes(v []*yaml.Node) error {
if len(v)%2 != 0 {
return StacktraceNew("odd number of nodes", s.Location)
}
for i := 0; i != len(v); i += 2 {
node := v[i]
valueNode := v[i+1]
switch node.Value {
case FacetMinLength:
if err := valueNode.Decode(&s.MinLength); err != nil {
return StacktraceNewWrapped("decode minLength", err, s.Location, WithNodePosition(valueNode))
}
case FacetMaxLength:
if err := valueNode.Decode(&s.MaxLength); err != nil {
return StacktraceNewWrapped("decode maxLength", err, s.Location, WithNodePosition(valueNode))
}
case FacetFileTypes:
if valueNode.Kind != yaml.SequenceNode {
return StacktraceNew("fileTypes must be sequence node", s.Location, WithNodePosition(valueNode))
}
fileTypes := make(Nodes, len(valueNode.Content))
for i, v := range valueNode.Content {
if v.Tag != "!!str" {
return StacktraceNew("member of fileTypes must be string", s.Location, WithNodePosition(v))
}
n, err := s.raml.makeRootNode(v, s.Location)
if err != nil {
return StacktraceNewWrapped("make node fileTypes", err, s.Location, WithNodePosition(v))
}
fileTypes[i] = n
}
s.FileTypes = fileTypes
default:
n, err := s.raml.makeRootNode(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make node", err, s.Location, WithNodePosition(valueNode))
}
s.CustomShapeFacets.Set(node.Value, n)
}
}
return nil
}
type BooleanShape struct {
scalarShape
*BaseShape
EnumFacets
}
func (s *BooleanShape) Base() *BaseShape {
return s.BaseShape
}
func (s *BooleanShape) cloneShallow(base *BaseShape) Shape {
return s.clone(base, nil)
}
func (s *BooleanShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
c := *s
c.BaseShape = base
return &c
}
func (s *BooleanShape) alias(source Shape) (Shape, error) {
_, ok := source.(*BooleanShape)
if !ok {
return nil, StacktraceNew("cannot make alias from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
return s, nil
}
func (s *BooleanShape) validate(v interface{}, _ string) error {
i, ok := v.(bool)
if !ok {
return fmt.Errorf("invalid type, got %T, expected bool", v)
}
if s.Enum != nil {
found := false
for _, e := range s.Enum {
if e.Value == i {
found = true
break
}
}
if !found {
return fmt.Errorf("value must be one of (%s)", s.Enum.String())
}
}
return nil
}
func (s *BooleanShape) inherit(source Shape) (Shape, error) {
ss, ok := source.(*BooleanShape)
if !ok {
return nil, StacktraceNew("cannot inherit from different type", s.Location, stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type), stacktrace.WithInfo("target", s.Base().Type))
}
if s.Enum == nil {
s.Enum = ss.Enum
} else if ss.Enum != nil && !isCompatibleEnum(ss.Enum, s.Enum) {
return nil, StacktraceNew("enum constraint violation", s.Location, stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", ss.Enum.String()), stacktrace.WithInfo("target", s.Enum.String()))
}
return s, nil
}
func (s *BooleanShape) check() error {
if s.Enum != nil {
for _, e := range s.Enum {
if _, ok := e.Value.(bool); !ok {
return StacktraceNew("enum value must be boolean", s.Location, stacktrace.WithPosition(&e.Position))
}
}
}
return nil
}
func (s *BooleanShape) unmarshalYAMLNodes(v []*yaml.Node) error {
for i := 0; i != len(v); i += 2 {
node := v[i]
valueNode := v[i+1]
if node.Value == "enum" {
enums, err := s.raml.MakeEnum(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make enum", err, s.Location, WithNodePosition(valueNode))
}
s.Enum = enums
} else {
n, err := s.raml.makeRootNode(valueNode, s.Location)
if err != nil {
return StacktraceNewWrapped("make node", err, s.Location, WithNodePosition(valueNode))
}
s.CustomShapeFacets.Set(node.Value, n)
}
}
return nil
}
type DateTimeShape struct {
scalarShape
*BaseShape
FormatFacets
}
func (s *DateTimeShape) Base() *BaseShape {
return s.BaseShape
}
func (s *DateTimeShape) cloneShallow(base *BaseShape) Shape {
return s.clone(base, nil)
}
func (s *DateTimeShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
c := *s
c.BaseShape = base
return &c
}
func (s *DateTimeShape) alias(source Shape) (Shape, error) {
ss, ok := source.(*DateTimeShape)
if !ok {
return nil, StacktraceNew("cannot make alias from different type", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
s.Format = ss.Format
return s, nil
}
func (s *DateTimeShape) validate(v interface{}, _ string) error {
i, ok := v.(string)
if !ok {
return fmt.Errorf("invalid type, got %T, expected string", v)
}
if s.Format == nil {
if _, err := time.Parse(time.RFC3339, i); err != nil {
return fmt.Errorf("value must match format %s", time.RFC3339)
}
} else {
switch *s.Format {
case DateTimeFormatRFC3339:
if _, err := time.Parse(time.RFC3339, i); err != nil {
return fmt.Errorf("value must match format %s", time.RFC3339)
}
// TODO: https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1
case DateTimeFormatRFC2616:
if _, err := time.Parse(RFC2616, i); err != nil {
return fmt.Errorf("value must match format %s", RFC2616)
}
}
}
return nil
}
func (s *DateTimeShape) inherit(source Shape) (Shape, error) {
ss, ok := source.(*DateTimeShape)
if !ok {
return nil, StacktraceNew("cannot inherit from different type", s.Location, stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", source.Base().Type),
stacktrace.WithInfo("target", s.Base().Type))
}
if s.Format == nil {
s.Format = ss.Format
} else if ss.Format != nil && *s.Format != *ss.Format {
return nil, StacktraceNew("format constraint violation", s.Location, stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *ss.Format), stacktrace.WithInfo("target", *s.Format))
}
return s, nil
}
func (s *DateTimeShape) check() error {
return nil
}
func (s *DateTimeShape) unmarshalYAMLNodes(v []*yaml.Node) error {
for i := 0; i != len(v); i += 2 {
if i+1 >= len(v) {
return StacktraceNew("missing value", s.Location)