-
Notifications
You must be signed in to change notification settings - Fork 16
/
repeated.go
85 lines (66 loc) · 1.97 KB
/
repeated.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
package specs
import (
"errors"
"fmt"
)
// Repeated represents an array type of fixed size.
type Repeated []Template
// Template returns a Template for a given array. It checks the types of internal
// elements to detect the data type(s).
// Note that all the references must be resolved before calling this method.
func (repeated Repeated) Template() (Template, error) {
var template Template
// check if all element types are the same
// TODO: remove once "oneOf" support is added
for position := range repeated {
if position == 0 {
template = repeated[position]
continue
}
if err := template.Compare(repeated[position]); err != nil {
return Template{}, fmt.Errorf("all the elements inside the array must have the same type: %w", err)
}
}
// get rid of default value if scalar type
if template.Scalar != nil {
template = template.Clone()
template.Scalar.Default = nil
}
return template, nil
}
// Clone repeated.
func (repeated Repeated) Clone() Repeated {
var clone = make([]Template, len(repeated))
for index, template := range repeated {
clone[index] = template.Clone()
}
return clone
}
// Compare given repeated to the provided one returning the first mismatch.
func (repeated Repeated) Compare(expected Repeated) error {
if expected == nil && repeated == nil {
return nil
}
if expected == nil && repeated != nil {
return errors.New("expected to be nil")
}
if expected != nil && repeated == nil {
return fmt.Errorf("expected to be an array, got %v", nil)
}
if len(expected) != len(repeated) {
return fmt.Errorf("expected to have %d elements, got %d", len(expected), len(repeated))
}
left, err := repeated.Template()
if err != nil {
return fmt.Errorf("unkown repeated property template: %w", err)
}
right, err := expected.Template()
if err != nil {
return fmt.Errorf("unkown expected property template: %w", err)
}
err = left.Compare(right)
if err != nil {
return fmt.Errorf("repeated property: %w", err)
}
return nil
}