-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathrecursive.go
More file actions
58 lines (49 loc) · 1.45 KB
/
recursive.go
File metadata and controls
58 lines (49 loc) · 1.45 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
package zog
import (
"sync"
p "github.com/Oudwins/zog/pkgs/internals"
zss "github.com/Oudwins/zog/pkgs/zss/core"
"github.com/Oudwins/zog/zconst"
)
type lazySchema struct {
innerSchema ZogSchema
fn func() ZogSchema
once sync.Once
}
var _ ZogSchema = &lazySchema{}
func (l *lazySchema) get() ZogSchema {
l.once.Do(func() {
l.innerSchema = l.fn()
})
return l.innerSchema
}
func (l *lazySchema) process(ctx *p.SchemaCtx) {
l.get().process(ctx)
}
func (l *lazySchema) validate(ctx *p.SchemaCtx) {
l.get().validate(ctx)
}
func (l *lazySchema) getType() zconst.ZogType { return l.get().getType() }
func (l *lazySchema) setCoercer(c CoercerFunc) { l.get().setCoercer(c) }
func (l *lazySchema) toZSS() *zss.ZSSSchema { return l.get().toZSS() }
func lazy(fn func() ZogSchema) *lazySchema {
return &lazySchema{fn: fn}
}
type RecursiveSchemaUpdater[T ZogSchema] func(self T) T
type RecursiveSchema[T ZogSchema] func(updaters ...RecursiveSchemaUpdater[T]) ZogSchema
type RecursiveSchemaBuilder[T ZogSchema] func(self RecursiveSchema[T]) T
// Experimental API.
// Do not use unless you know what you are doing.
func EXPERIMENTAL_RECURSIVE[T ZogSchema](build RecursiveSchemaBuilder[T]) T {
var self T
var lazyBuilder RecursiveSchema[T] = func(updaters ...RecursiveSchemaUpdater[T]) ZogSchema {
return lazy(func() ZogSchema {
if len(updaters) > 0 {
return updaters[0](self)
}
return self
})
}
self = build(lazyBuilder)
return self
}