forked from Oudwins/zog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
214 lines (188 loc) · 4.62 KB
/
utils.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
package zog
import (
"log"
"reflect"
"time"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
type Processor interface {
process(val any, dest any, path p.PathBuilder, ctx ParseCtx)
}
// ! Passing Types through
type ParseCtx = p.ParseCtx
type Test = p.Test
type ZogError = p.ZogError
type ZogErrMap = p.ZogErrMap
type ZogErrList = p.ZogErrList
// ! TESTS
func TestFunc(errCode zconst.ZogErrCode, validateFunc p.TestFunc) p.Test {
t := p.Test{
ErrCode: errCode,
ValidateFunc: validateFunc,
}
return t
}
// ! ERRORS
type errHelpers struct {
}
// Beware this API may change
var Errors = errHelpers{}
// Create error from (originValue any, destinationValue any, test *p.Test)
func (e *errHelpers) FromTest(o any, destType zconst.ZogType, t *p.Test, p ParseCtx) p.ZogError {
er := e.New(t.ErrCode, o, destType, t.Params, "", nil)
if t.ErrFmt != nil {
t.ErrFmt(er, p)
}
return er
}
// Create error from
func (e *errHelpers) FromErr(o any, destType zconst.ZogType, err error) p.ZogError {
return e.New(zconst.ErrCodeCustom, o, destType, nil, "", err)
}
func (e *errHelpers) WrapUnknown(o any, destType zconst.ZogType, err error) p.ZogError {
zerr, ok := err.(p.ZogError)
if !ok {
return e.FromErr(o, destType, err)
}
return zerr
}
func (e *errHelpers) New(code zconst.ZogErrCode, o any, destType zconst.ZogType, params map[string]any, msg string, err error) p.ZogError {
return &p.ZogErr{
C: code,
ParamsM: params,
Val: o,
Typ: destType,
Msg: msg,
Err: err,
}
}
func (e *errHelpers) SanitizeMap(m p.ZogErrMap) map[string][]string {
errs := make(map[string][]string, len(m))
for k, v := range m {
errs[k] = e.SanitizeList(v)
}
return errs
}
func (e *errHelpers) SanitizeList(l p.ZogErrList) []string {
errs := make([]string, len(l))
for i, err := range l {
errs[i] = err.Message()
}
return errs
}
// ! Data Providers
// Creates a new map data provider
func NewMapDataProvider[T any](m map[string]T) p.DataProvider {
return p.NewMapDataProvider(m)
}
// ! 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) {
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 := p.IsZeroValue(val)
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
}