-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.go
82 lines (70 loc) · 2.03 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
package zog
import (
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
// TestFunc is a helper function to define a custom test. It takes the error code which will be used for the error message and a validate function. Usage:
//
// schema.Test(z.TestFunc(zconst.ErrCodeCustom, func(val any, ctx ParseCtx) bool {
// return val == "hello"
// }))
func TestFunc(errCode zconst.ZogErrCode, validateFunc p.TestFunc) p.Test {
t := p.Test{
ErrCode: errCode,
ValidateFunc: validateFunc,
}
return t
}
// ! ERRORS
type errHelpers struct {
}
// Helper struct for dealing with zog errors. 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)
}