-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpointers.go
134 lines (118 loc) · 3.38 KB
/
pointers.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
package zog
import (
"reflect"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
var _ ComplexZogSchema = &PointerSchema{}
type PointerSchema struct {
// preTransforms []p.PreTransform
tests []p.Test
schema ZogSchema
required *p.Test
// postTransforms []p.PostTransform
// defaultVal *any
// catch *any
}
func (v *PointerSchema) getType() zconst.ZogType {
// return zconst.TypePtr
return v.schema.getType()
}
func (v *PointerSchema) setCoercer(c conf.CoercerFunc) {
v.schema.setCoercer(c)
}
// Ptr creates a pointer ZogSchema
func Ptr(schema ZogSchema) *PointerSchema {
return &PointerSchema{
tests: []p.Test{},
schema: schema,
}
}
// Parse the data into the destination pointer
func (v *PointerSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap {
errs := p.NewErrsMap()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
v.process(ctx.NewSchemaCtx(data, dest, path, v.getType()))
return errs.M
}
func (v *PointerSchema) process(ctx *p.SchemaCtx) {
// TODO this is a mess. But couldn't figure out a simple way to support top level optional structs without doing this.
// Companion code to this codde is in struct.go > process
subCtx := ctx.NewSchemaCtx(ctx.Val, nil, ctx.Path, v.schema.getType())
if fn, ok := ctx.Val.(p.DpFactory); ok {
val, err := fn()
if err != nil {
ctx.AddIssue(subCtx.IssueFromUnknownError(err))
return
}
ctx.Val = val
}
// End of messy code
isZero := p.IsParseZeroValue(ctx.Val, ctx)
if isZero {
if v.required != nil {
// We set the destination type to the schema type because pointer doesn't have any issue messages. They pass through to the schema type
ctx.AddIssue(ctx.IssueFromTest(v.required, ctx.Val).SetDType(v.schema.getType()))
}
return
}
rv := reflect.ValueOf(ctx.DestPtr)
destPtr := rv.Elem()
if destPtr.IsNil() {
// this sets the primitive also
newVal := reflect.New(destPtr.Type().Elem())
// this generates a new nil pointer
//newVal := reflect.Zero(destPtr.Type())
destPtr.Set(newVal)
}
di := destPtr.Interface()
subCtx.DestPtr = di
v.schema.process(subCtx)
}
// Validates a pointer pointer
func (v *PointerSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap {
errs := p.NewErrsMap()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
v.validate(ctx.NewValidateSchemaCtx(data, path, v.getType()))
return errs.M
}
func (v *PointerSchema) validate(ctx *p.SchemaCtx) {
rv := reflect.ValueOf(ctx.Val)
destPtr := rv.Elem()
if !destPtr.IsValid() || destPtr.IsNil() {
if v.required != nil {
// We set the destination type to the schema type because pointer doesn't have any issue messages. They pass through to the schema type
ctx.AddIssue(ctx.IssueFromTest(v.required, ctx.Val).SetDType(v.schema.getType()))
}
return
}
di := destPtr.Interface()
ctx.Val = di
v.schema.validate(ctx.NewValidateSchemaCtx(di, ctx.Path, v.schema.getType()))
}
// Validate Existing Pointer
func (v *PointerSchema) NotNil(options ...TestOption) *PointerSchema {
r := p.Test{
IssueCode: zconst.IssueCodeNotNil,
}
for _, opt := range options {
opt(&r)
}
v.required = &r
return v
}