-
Notifications
You must be signed in to change notification settings - Fork 4
/
map.go
318 lines (284 loc) · 9.27 KB
/
map.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
package schema
import (
"fmt"
"reflect"
)
// Map holds the schema definition for key-value associations. This dataclass only has the ability to hold the
// configuration but cannot serialize, unserialize or validate. For that functionality please use MapType.
type Map[KeyType Type, ValueType Type] interface {
Type
Keys() KeyType
Values() ValueType
Min() *int64
Max() *int64
}
// UntypedMap is a map schema without specific underlying types.
type UntypedMap = Map[Type, Type]
// TypedMap is a map schema that can be unserialized in its underlying components.
type TypedMap[KeyType comparable, ValueType any] interface {
TypedType[map[KeyType]ValueType]
Map[TypedType[KeyType], TypedType[ValueType]]
}
// NewMapSchema creates a new map schema.
func NewMapSchema(keys Type, values Type, min *int64, max *int64) *MapSchema[Type, Type] {
switch keys.TypeID() {
case TypeIDString:
case TypeIDInt:
case TypeIDStringEnum:
case TypeIDIntEnum:
default:
panic(BadArgumentError{
Message: fmt.Sprintf("Invalid type ID for map: %s, expected one of: string, int", keys.TypeID()),
})
}
return &MapSchema[Type, Type]{
keys,
values,
min,
max,
}
}
// MapSchema is the implementation of tye map types.
type MapSchema[K Type, V Type] struct {
KeysValue K `json:"keys"`
ValuesValue V `json:"values"`
MinValue *int64 `json:"min"`
MaxValue *int64 `json:"max"`
}
func (m MapSchema[K, V]) TypeID() TypeID {
return TypeIDMap
}
func (m MapSchema[K, V]) ReflectedType() reflect.Type {
reflectedKey := m.KeysValue.ReflectedType()
reflectedValue := m.ValuesValue.ReflectedType()
return reflect.MapOf(reflectedKey, reflectedValue)
}
func (m MapSchema[K, V]) Keys() K {
return m.KeysValue
}
func (m MapSchema[K, V]) Values() V {
return m.ValuesValue
}
func (m MapSchema[K, V]) Min() *int64 {
return m.MinValue
}
func (m MapSchema[K, V]) Max() *int64 {
return m.MaxValue
}
func (m MapSchema[K, V]) ApplyNamespace(objects map[string]*ObjectSchema, namespace string) {
m.KeysValue.ApplyNamespace(objects, namespace)
m.ValuesValue.ApplyNamespace(objects, namespace)
}
func (m MapSchema[K, V]) ValidateReferences() error {
err := m.KeysValue.ValidateReferences()
if err != nil {
return err
}
return m.ValuesValue.ValidateReferences()
}
func (m MapSchema[K, V]) Unserialize(data any) (any, error) {
v := reflect.ValueOf(data)
if v.Kind() != reflect.Map {
return nil, &ConstraintError{
Message: fmt.Sprintf("Must be a map, %T given", data),
}
}
if m.MinValue != nil && *m.MinValue > int64(v.Len()) {
return nil, &ConstraintError{
Message: fmt.Sprintf("Must have at least %d items, %d given", *m.MinValue, v.Len()),
}
}
if m.MaxValue != nil && *m.MaxValue < int64(v.Len()) {
return nil, &ConstraintError{
Message: fmt.Sprintf("Must have at most %d items, %d given", *m.MaxValue, v.Len()),
}
}
t := m.ReflectedType()
result := reflect.MakeMapWithSize(t, v.Len())
for _, k := range v.MapKeys() {
val := v.MapIndex(k)
unserializedKey, err := m.KeysValue.Unserialize(k.Interface())
if err != nil {
return nil, ConstraintErrorAddPathSegment(err, fmt.Sprintf("{%v}", k.Interface()))
}
unserializedValue, err := m.ValuesValue.Unserialize(val.Interface())
if err != nil {
return nil, ConstraintErrorAddPathSegment(err, fmt.Sprintf("[%v]", k.Interface()))
}
result.SetMapIndex(reflect.ValueOf(unserializedKey), reflect.ValueOf(unserializedValue))
}
return result.Interface(), nil
}
func (m MapSchema[K, V]) validateSchemaCompatibility(schemaType Type) error {
if schemaType.TypeID() != TypeIDMap {
return &ConstraintError{
Message: fmt.Sprintf("unsupported data type for 'map' type: %T", schemaType),
}
}
// Now check the map schema fields and types.
value := reflect.ValueOf(schemaType)
if reflect.Indirect(value).Kind() != reflect.Struct {
return &ConstraintError{
Message: fmt.Sprintf("unsupported data type for 'map' type: %T. Is not map or map schema",
schemaType),
}
}
keysField := reflect.Indirect(value).MethodByName("Keys")
valuesField := reflect.Indirect(value).MethodByName("Values")
minField := reflect.Indirect(value).MethodByName("Min")
maxField := reflect.Indirect(value).MethodByName("Max")
if !keysField.IsValid() || !valuesField.IsValid() || !minField.IsValid() ||
!maxField.IsValid() {
return &ConstraintError{
Message: fmt.Sprintf(
"unsupported data type for 'map' type: '%T'. Missing keys, values, min, or max methods",
schemaType),
}
}
keysType := keysField.Call([]reflect.Value{})[0].Interface()
err := m.Keys().ValidateCompatibility(keysType)
if err != nil {
return &ConstraintError{
Message: fmt.Sprintf("unsupported data type for map key: %T, expected %T (%s)", keysType, m.Keys(), err),
}
}
valuesType := valuesField.Call([]reflect.Value{})[0].Interface()
err = m.Values().ValidateCompatibility(valuesType)
if err != nil {
return &ConstraintError{
Message: fmt.Sprintf("unsupported data type for map values: %T, expected %T (%s)",
valuesType, m.Values(), err),
}
}
// Must have size overlap.
minValue := minField.Call([]reflect.Value{})[0].Interface().(*int64)
maxValue := maxField.Call([]reflect.Value{})[0].Interface().(*int64)
if (m.MinValue != nil && maxValue != nil && (*minValue) > (*m.MaxValue)) ||
(m.MaxValue != nil && minValue != nil && (*maxValue) < (*m.MinValue)) {
return &ConstraintError{
Message: "mutually exclusive lengths between map schemas",
}
}
return nil
}
func (m MapSchema[K, V]) ValidateCompatibility(typeOrData any) error {
// Check if it's a schema.Type. If it is, verify it. If not, verify it as data.
schemaType, ok := typeOrData.(Type)
if ok {
return m.validateSchemaCompatibility(schemaType)
}
// It's not a schema type, so now check if it's an actual map
v := reflect.ValueOf(typeOrData)
if v.Kind() != reflect.Map {
return &ConstraintError{
Message: fmt.Sprintf("Must be a map or map schema, %T given", typeOrData),
}
}
if m.MinValue != nil && *m.MinValue > int64(v.Len()) {
return &ConstraintError{
Message: fmt.Sprintf("Must have at least %d items, %d given", *m.MinValue, v.Len()),
}
}
if m.MaxValue != nil && *m.MaxValue < int64(v.Len()) {
return &ConstraintError{
Message: fmt.Sprintf("Must have at most %d items, %d given", *m.MaxValue, v.Len()),
}
}
for _, k := range v.MapKeys() {
if err := m.KeysValue.ValidateCompatibility(k.Interface()); err != nil {
return ConstraintErrorAddPathSegment(err, fmt.Sprintf("{%v}", k))
}
if err := m.ValuesValue.ValidateCompatibility(v.MapIndex(k).Interface()); err != nil {
return ConstraintErrorAddPathSegment(err, fmt.Sprintf("[%v]", k))
}
}
return nil
}
func (m MapSchema[K, V]) Validate(data any) error {
v := reflect.ValueOf(data)
if v.Kind() != reflect.Map {
return &ConstraintError{
Message: fmt.Sprintf("Must be a map, %T given", data),
}
}
if m.MinValue != nil && *m.MinValue > int64(v.Len()) {
return &ConstraintError{
Message: fmt.Sprintf("Must have at least %d items, %d given", *m.MinValue, v.Len()),
}
}
if m.MaxValue != nil && *m.MaxValue < int64(v.Len()) {
return &ConstraintError{
Message: fmt.Sprintf("Must have at most %d items, %d given", *m.MaxValue, v.Len()),
}
}
for _, k := range v.MapKeys() {
if err := m.KeysValue.Validate(k.Interface()); err != nil {
return ConstraintErrorAddPathSegment(err, fmt.Sprintf("{%v}", k))
}
if err := m.ValuesValue.Validate(v.MapIndex(k).Interface()); err != nil {
return ConstraintErrorAddPathSegment(err, fmt.Sprintf("[%v]", k))
}
}
return nil
}
func (m MapSchema[K, V]) Serialize(data any) (any, error) {
if err := m.Validate(data); err != nil {
return nil, err
}
v := reflect.ValueOf(data)
result := make(map[any]any, v.Len())
for _, k := range v.MapKeys() {
serializedKey, err := m.KeysValue.Serialize(k.Interface())
if err != nil {
return nil, ConstraintErrorAddPathSegment(err, fmt.Sprintf("{%v}", k))
}
serializedValue, err := m.ValuesValue.Serialize(v.MapIndex(k).Interface())
if err != nil {
return nil, ConstraintErrorAddPathSegment(err, fmt.Sprintf("[%v]", k))
}
result[serializedKey] = serializedValue
}
return result, nil
}
// NewTypedMapSchema creates a new map schema with a defined underlying type.
func NewTypedMapSchema[KeyType comparable, ValueType any](
keys TypedType[KeyType],
values TypedType[ValueType],
min *int64,
max *int64,
) *TypedMapSchema[KeyType, ValueType] {
switch keys.TypeID() {
case TypeIDString:
case TypeIDInt:
case TypeIDStringEnum:
case TypeIDIntEnum:
default:
panic(BadArgumentError{
Message: fmt.Sprintf("Invalid type ID for map: %s, expected one of: string, int", keys.TypeID()),
})
}
return &TypedMapSchema[KeyType, ValueType]{
MapSchema[TypedType[KeyType], TypedType[ValueType]]{
keys,
values,
min,
max,
},
}
}
type TypedMapSchema[KeyType comparable, ValueType any] struct {
MapSchema[TypedType[KeyType], TypedType[ValueType]]
}
func (m TypedMapSchema[KeyType, ValueType]) UnserializeType(data any) (result map[KeyType]ValueType, err error) {
unserialized, err := m.Unserialize(data)
if err != nil {
return result, err
}
return unserialized.(map[KeyType]ValueType), nil
}
func (m TypedMapSchema[KeyType, ValueType]) ValidateType(data map[KeyType]ValueType) error {
return m.Validate(data)
}
func (m TypedMapSchema[KeyType, ValueType]) SerializeType(data map[KeyType]ValueType) (any, error) {
return m.Serialize(data)
}