-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathzogSchema.go
222 lines (201 loc) · 5.38 KB
/
zogSchema.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
package zog
import (
"log"
"reflect"
"time"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
// The ZogSchema is the interface all schemas must implement
// This is most useful for internal use. If you are looking to pass schemas around, use the ComplexZogSchema or PrimitiveZogSchema interfaces if possible.
type ZogSchema interface {
process(val any, dest any, path p.PathBuilder, ctx ParseCtx)
validate(val any, path p.PathBuilder, ctx ParseCtx)
setCoercer(c conf.CoercerFunc)
getType() zconst.ZogType
}
// This is a common interface for all complex schemas (i.e structs, slices, pointers...)
// You can use this to pass any complex schema around
type ComplexZogSchema interface {
ZogSchema
Parse(val any, dest any, options ...ParsingOption) ZogErrMap
}
// This is a common interface for all primitive schemas (i.e strings, numbers, booleans, time.Time...)
// You can use this to pass any primitive schema around
type PrimitiveZogSchema[T p.ZogPrimitive] interface {
ZogSchema
Parse(val any, dest *T, options ...ParsingOption) ZogErrList
}
// ! PRIMITIVE PROCESSING
func getDestType(dest any) zconst.ZogType {
switch reflect.TypeOf(dest).Kind() {
case reflect.Slice:
return zconst.TypeSlice
case reflect.Struct:
if reflect.TypeOf(dest) == reflect.TypeOf(time.Time{}) {
return zconst.TypeTime
}
return zconst.TypeStruct
case reflect.Float64:
case reflect.Int:
return zconst.TypeNumber
case reflect.Bool:
return zconst.TypeBool
case reflect.String:
return zconst.TypeString
default:
log.Fatal("Unsupported destination type")
}
// should never get here
return zconst.TypeString
}
func primitiveProcessor[T p.ZogPrimitive](val any, dest any, path p.PathBuilder, ctx ParseCtx, preTransforms []p.PreTransform, tests []p.Test, postTransforms []p.PostTransform, defaultVal *T, required *p.Test, catch *T, coercer conf.CoercerFunc, isZeroFunc p.IsZeroValueFunc) {
canCatch := catch != nil
hasCatched := false
destPtr := dest.(*T)
destType := getDestType(*destPtr)
// 1. preTransforms
for _, fn := range preTransforms {
nVal, err := fn(val, ctx)
// bail if error in preTransform
if err != nil {
if canCatch {
*destPtr = *catch
hasCatched = true
break
}
ctx.NewError(path, Errors.WrapUnknown(val, destType, err))
return
}
val = nVal
}
// 4. postTransforms
defer func() {
// only run posttransforms on success
if !ctx.HasErrored() {
for _, fn := range postTransforms {
err := fn(destPtr, ctx)
if err != nil {
ctx.NewError(path, Errors.WrapUnknown(val, destType, err))
return
}
}
}
}()
if hasCatched {
return
}
// 2. cast data to string & handle default/required
isZeroVal := isZeroFunc(val, ctx)
if isZeroVal {
if defaultVal != nil {
*destPtr = *defaultVal
} else if required == nil {
// This handles optional case
return
} else {
// is required & zero value
// required
if catch != nil {
*destPtr = *catch
hasCatched = true
} else {
ctx.NewError(path, Errors.FromTest(val, destType, required, ctx))
return
}
}
} else {
newVal, err := coercer(val)
if err == nil {
*destPtr = newVal.(T)
} else {
if canCatch {
*destPtr = *catch
hasCatched = true
} else {
ctx.NewError(path, Errors.New(zconst.ErrCodeCoerce, val, destType, nil, "", err))
return
}
}
}
if hasCatched {
return
}
// 3. tests
for _, test := range tests {
if !test.ValidateFunc(*destPtr, ctx) {
// catching the first error if catch is set
if catch != nil {
*destPtr = *catch
hasCatched = true //nolint
break
}
ctx.NewError(path, Errors.FromTest(val, destType, &test, ctx))
}
}
// 4. postTransforms -> Done above on defer
}
func primitiveValidator[T p.ZogPrimitive](val any, path p.PathBuilder, ctx ParseCtx, preTransforms []p.PreTransform, tests []p.Test, postTransforms []p.PostTransform, defaultVal *T, required *p.Test, catch *T) {
canCatch := catch != nil
valPtr := val.(*T)
// 4. postTransforms
defer func() {
// only run posttransforms on success
if !ctx.HasErrored() {
for _, fn := range postTransforms {
err := fn(val, ctx)
if err != nil {
ctx.NewError(path, Errors.WrapUnknown(val, zconst.TypeBool, err))
return
}
}
}
}()
// 1. preTransforms
for _, fn := range preTransforms {
nVal, err := fn(*valPtr, ctx)
// bail if error in preTransform
if err != nil {
if canCatch {
*valPtr = *catch
return
}
ctx.NewError(path, Errors.WrapUnknown(val, zconst.TypeBool, err))
return
}
*valPtr = nVal.(T)
}
// 2. cast data to string & handle default/required
// Warning. This uses generic IsZeroValue because for Validate we treat zero values as invalid for required fields. This is different from Parse.
isZeroVal := p.IsZeroValue(*valPtr)
if isZeroVal {
if defaultVal != nil {
*valPtr = *defaultVal
} else if required == nil {
// This handles optional case
return
} else {
// is required & zero value
// required
if catch != nil {
*valPtr = *catch
return
} else {
ctx.NewError(path, Errors.FromTest(val, zconst.TypeBool, required, ctx))
return
}
}
}
// 3. tests
for _, test := range tests {
if !test.ValidateFunc(*valPtr, ctx) {
// catching the first error if catch is set
if canCatch {
*valPtr = *catch
return
}
ctx.NewError(path, Errors.FromTest(val, zconst.TypeBool, &test, ctx))
}
}
}