-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcustom.go
More file actions
140 lines (119 loc) · 3.5 KB
/
Copy pathcustom.go
File metadata and controls
140 lines (119 loc) · 3.5 KB
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
package zog
import (
"fmt"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/pkgs/internals"
zss "github.com/Oudwins/zog/pkgs/zss/core"
"github.com/Oudwins/zog/zconst"
)
type Custom[T any] struct {
test p.Test[*T]
}
func CustomFunc[T any](fn func(ptr *T, ctx Ctx) bool, opts ...TestOption) *Custom[T] {
test := &p.Test[*T]{}
p.TestFuncFromBool(func(val *T, ctx Ctx) bool {
return fn(val, ctx)
}, test)
for _, opt := range opts {
opt(test)
}
return &Custom[T]{test: *test}
}
func (c *Custom[T]) Parse(data any, destPtr *T, options ...ExecOption) ZogIssueList {
errs := p.NewErrsList()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
sctx := ctx.NewSchemaCtx(data, destPtr, path, c.getType())
defer sctx.Free()
c.process(sctx)
return errs.List
}
func (c *Custom[T]) process(ctx *p.SchemaCtx) {
ctx.Processor = &c.test
// set the value
d, ok := ctx.Data.(T)
if !ok {
ctx.AddIssue(ctx.IssueFromCoerce(fmt.Errorf("expected %T, got %T", new(T), ctx.Data)))
return
}
ptr, ok := ctx.ValPtr.(*T)
if !ok {
// We have to go directly to the exec context as that is what formats. We cannot use ctx because it will try to catch the issue and this is an uncatchable issue
// since we cannot set the value as its not of type *T
ctx.ExecCtx.AddIssue(ctx.IssueFromInvalidType("pointer matching custom schema type", ctx.ValPtr, "parsing a custom schema"))
return
}
*ptr = d
// run the test
c.test.Func(ptr, ctx)
}
func (c *Custom[T]) Validate(dataPtr *T, options ...ExecOption) ZogIssueList {
errs := p.NewErrsList()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
sctx := ctx.NewSchemaCtx(dataPtr, dataPtr, path, c.getType())
defer sctx.Free()
c.validate(sctx)
return errs.List
}
func (c *Custom[T]) validate(ctx *p.SchemaCtx) {
ctx.Processor = &c.test
ptr, ok := ctx.ValPtr.(*T)
if !ok {
// We have to go directly to the exec context as that is what formats. We cannot use ctx because it will try to catch the issue and this is an uncatchable issue
// since we cannot set the value as its not of type *T
ctx.ExecCtx.AddIssue(ctx.IssueFromInvalidType("pointer matching custom schema type", ctx.ValPtr, "validating a custom schema"))
return
}
c.test.Func(ptr, ctx)
}
func (c *Custom[T]) setCoercer(coercer CoercerFunc) {
// no op
}
func (c *Custom[T]) getType() zconst.ZogType {
return "custom"
}
// Experimental API. Expect breaking changes and no documentation unfortunately for now
type EXPERIMENTAL_PUBLIC_ZOG_SCHEMA interface {
Process(ctx *p.SchemaCtx)
Validate(ctx *p.SchemaCtx)
GetType() zconst.ZogType
SetCoercer(c CoercerFunc)
ToZSS(ctx *ZSSSerializeCtx) *zss.ZSSSchema
}
// Experimental API
func Use(schema EXPERIMENTAL_PUBLIC_ZOG_SCHEMA) *CustomSchema {
return &CustomSchema{schema: schema}
}
// Experimental API
type CustomSchema struct {
schema EXPERIMENTAL_PUBLIC_ZOG_SCHEMA
}
var _ ZogSchema = &CustomSchema{}
func (c *CustomSchema) process(ctx *p.SchemaCtx) {
c.schema.Process(ctx)
}
func (c *CustomSchema) validate(ctx *p.SchemaCtx) {
c.schema.Validate(ctx)
}
func (c *CustomSchema) getType() zconst.ZogType {
return c.schema.GetType()
}
func (c *CustomSchema) setCoercer(coercer CoercerFunc) {
c.schema.SetCoercer(coercer)
}
func (c *CustomSchema) toZSS(ctx *ZSSSerializeCtx) *zss.ZSSSchema {
return c.schema.ToZSS(ctx)
}