-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract.go
More file actions
240 lines (227 loc) · 6.84 KB
/
Copy pathcontract.go
File metadata and controls
240 lines (227 loc) · 6.84 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
package tycl
import (
"fmt"
"strings"
"github.com/pt-main/tycl/shared"
)
func CheckContract(conf *shared.Config, cont *shared.Contract) error {
if cont == nil {
return nil
}
if cont.Type == shared.ContractDynamic {
return nil
}
var errs []string
for _, key := range cont.BoolV {
if _, ok := conf.BoolV[key]; !ok {
errs = append(errs, fmt.Sprintf("required bool key %q not found", key))
}
}
for _, key := range cont.IntV {
if _, ok := conf.IntV[key]; !ok {
errs = append(errs, fmt.Sprintf("required int key %q not found", key))
}
}
for _, key := range cont.FloatV {
if _, ok := conf.FloatV[key]; !ok {
errs = append(errs, fmt.Sprintf("required float key %q not found", key))
}
}
for _, key := range cont.StringV {
if _, ok := conf.StringV[key]; !ok {
errs = append(errs, fmt.Sprintf("required string key %q not found", key))
}
}
for _, key := range cont.BoolArrV {
if _, ok := conf.BoolArrV[key]; !ok {
errs = append(errs, fmt.Sprintf("required bool array key %q not found", key))
}
}
for _, key := range cont.IntArrV {
if _, ok := conf.IntArrV[key]; !ok {
errs = append(errs, fmt.Sprintf("required int array key %q not found", key))
}
}
for _, key := range cont.FloatArrV {
if _, ok := conf.FloatArrV[key]; !ok {
errs = append(errs, fmt.Sprintf("required float array key %q not found", key))
}
}
for _, key := range cont.StringArrV {
if _, ok := conf.StringArrV[key]; !ok {
errs = append(errs, fmt.Sprintf("required string array key %q not found", key))
}
}
for key, subCont := range cont.Inner {
subConf, ok := conf.InnerV[key]
if !ok {
errs = append(errs, fmt.Sprintf("required inner object key %q not found", key))
} else {
if err := CheckContract(subConf, subCont); err != nil {
errs = append(errs, fmt.Sprintf("inner object %q: %v", key, err))
}
}
}
for key, subCont := range cont.InnerArrV {
arr, ok := conf.InnerArrV[key]
if !ok {
errs = append(errs, fmt.Sprintf("required inner array key %q not found", key))
continue
}
for i, item := range arr {
if err := CheckContract(item, subCont); err != nil {
errs = append(errs, fmt.Sprintf("inner array %q index %d: %v", key, i, err))
}
}
}
if cont.Type == shared.ContractFlexible {
if len(errs) > 0 {
return fmt.Errorf("contract validation failed:\n%s", strings.Join(errs, "\n"))
}
return nil
}
contBoolSet := make(map[string]bool)
for _, k := range cont.BoolV {
contBoolSet[k] = true
}
contIntSet := make(map[string]bool)
for _, k := range cont.IntV {
contIntSet[k] = true
}
contFloatSet := make(map[string]bool)
for _, k := range cont.FloatV {
contFloatSet[k] = true
}
contStringSet := make(map[string]bool)
for _, k := range cont.StringV {
contStringSet[k] = true
}
contBoolArrSet := make(map[string]bool)
for _, k := range cont.BoolArrV {
contBoolArrSet[k] = true
}
contIntArrSet := make(map[string]bool)
for _, k := range cont.IntArrV {
contIntArrSet[k] = true
}
contFloatArrSet := make(map[string]bool)
for _, k := range cont.FloatArrV {
contFloatArrSet[k] = true
}
contStringArrSet := make(map[string]bool)
for _, k := range cont.StringArrV {
contStringArrSet[k] = true
}
contInnerSet := make(map[string]bool)
for k := range cont.Inner {
contInnerSet[k] = true
}
contInnerArrSet := make(map[string]bool)
for k := range cont.InnerArrV {
contInnerArrSet[k] = true
}
for key := range conf.BoolV {
if !contBoolSet[key] {
errs = append(errs, fmt.Sprintf("extra bool key %q not allowed in strict contract", key))
}
}
for key := range conf.IntV {
if !contIntSet[key] {
errs = append(errs, fmt.Sprintf("extra int key %q not allowed in strict contract", key))
}
}
for key := range conf.FloatV {
if !contFloatSet[key] {
errs = append(errs, fmt.Sprintf("extra float key %q not allowed in strict contract", key))
}
}
for key := range conf.StringV {
if !contStringSet[key] {
errs = append(errs, fmt.Sprintf("extra string key %q not allowed in strict contract", key))
}
}
for key := range conf.BoolArrV {
if !contBoolArrSet[key] {
errs = append(errs, fmt.Sprintf("extra bool array key %q not allowed in strict contract", key))
}
}
for key := range conf.IntArrV {
if !contIntArrSet[key] {
errs = append(errs, fmt.Sprintf("extra int array key %q not allowed in strict contract", key))
}
}
for key := range conf.FloatArrV {
if !contFloatArrSet[key] {
errs = append(errs, fmt.Sprintf("extra float array key %q not allowed in strict contract", key))
}
}
for key := range conf.StringArrV {
if !contStringArrSet[key] {
errs = append(errs, fmt.Sprintf("extra string array key %q not allowed in strict contract", key))
}
}
for key := range conf.InnerV {
if !contInnerSet[key] {
errs = append(errs, fmt.Sprintf("extra inner object key %q not allowed in strict contract", key))
}
}
for key := range conf.InnerArrV {
if !contInnerArrSet[key] {
errs = append(errs, fmt.Sprintf("extra inner array key %q not allowed in strict contract", key))
}
}
for key, typ := range conf.NullV {
if !shared.IsTypeValid(typ) {
errs = append(errs, fmt.Sprintf("invalid type %q for null key %q", typ, key))
continue
}
switch typ {
case "bool":
if !contBoolSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type bool not in contract bool list", key))
}
case "int":
if !contIntSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type int not in contract int list", key))
}
case "float":
if !contFloatSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type float not in contract float list", key))
}
case "string":
if !contStringSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type string not in contract string list", key))
}
case "object":
if !contInnerSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type object not in contract inner list", key))
}
case "bools":
if !contBoolArrSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type bools not in contract bool array list", key))
}
case "ints":
if !contIntArrSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type ints not in contract int array list", key))
}
case "floats":
if !contFloatArrSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type floats not in contract float array list", key))
}
case "strings":
if !contStringArrSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type strings not in contract string array list", key))
}
case "objects":
if !contInnerArrSet[key] {
errs = append(errs, fmt.Sprintf("null key %q with type objects not in contract inner array list", key))
}
default:
errs = append(errs, fmt.Sprintf("unsupported type %q for null key %q", typ, key))
}
}
if len(errs) > 0 {
return fmt.Errorf("strict contract validation failed:\n%s", strings.Join(errs, "\n"))
}
return nil
}