-
Notifications
You must be signed in to change notification settings - Fork 7
/
time.go
219 lines (187 loc) · 4.99 KB
/
time.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
215
216
217
218
219
package zog
import (
"time"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
// ! INTERNALS
var _ ZogSchema = &timeProcessor{}
type timeProcessor struct {
preTransforms []p.PreTransform
tests []p.Test
postTransforms []p.PostTransform
defaultVal *time.Time
required *p.Test
catch *time.Time
coercer conf.CoercerFunc
}
// internal processes the data
func (v *timeProcessor) process(val any, dest any, path p.PathBuilder, ctx ParseCtx) {
primitiveProcessor(val, dest, path, ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch, v.coercer, p.IsParseZeroValue)
}
// Returns the type of the schema
func (v *timeProcessor) getType() zconst.ZogType {
return zconst.TypeTime
}
// Sets the coercer for the schema
func (v *timeProcessor) setCoercer(c conf.CoercerFunc) {
v.coercer = c
}
type TimeFunc func(opts ...SchemaOption) *timeProcessor
// ! USER FACING FUNCTIONS
// Returns a new Time Schema
var Time TimeFunc = func(opts ...SchemaOption) *timeProcessor {
t := &timeProcessor{
coercer: conf.Coercers.Time,
}
for _, opt := range opts {
opt(t)
}
return t
}
// Sets the format function for the time schema
// Usage is:
//
// z.Time(z.Time.FormatFunc(func(data string) (time.Time, error) {
// return time.Parse(time.RFC3339, data)
// }))
func (t TimeFunc) FormatFunc(format func(data string) (time.Time, error)) SchemaOption {
return func(s ZogSchema) {
s.setCoercer(conf.TimeCoercerFactory(format))
}
}
// Sets the string format for the time schema
// Usage is:
// z.Time(z.Time.Format(time.RFC3339))
func (t TimeFunc) Format(format string) SchemaOption {
return t.FormatFunc(func(data string) (time.Time, error) {
return time.Parse(format, data)
})
}
// Parses the data into the destination time.Time. Returns a list of errors
func (v *timeProcessor) Parse(data any, dest *time.Time, options ...ParsingOption) p.ZogErrList {
errs := p.NewErrsList()
ctx := p.NewParseCtx(errs, conf.ErrorFormatter)
for _, opt := range options {
opt(ctx)
}
path := p.PathBuilder("")
v.process(data, dest, path, ctx)
return errs.List
}
// Adds pretransform function to schema
func (v *timeProcessor) PreTransform(transform p.PreTransform) *timeProcessor {
if v.preTransforms == nil {
v.preTransforms = []p.PreTransform{}
}
v.preTransforms = append(v.preTransforms, transform)
return v
}
// Adds posttransform function to schema
func (v *timeProcessor) PostTransform(transform p.PostTransform) *timeProcessor {
if v.postTransforms == nil {
v.postTransforms = []p.PostTransform{}
}
v.postTransforms = append(v.postTransforms, transform)
return v
}
// ! MODIFIERS
// marks field as required
func (v *timeProcessor) Required(options ...TestOption) *timeProcessor {
r := p.Required()
for _, opt := range options {
opt(&r)
}
v.required = &r
return v
}
// marks field as optional
func (v *timeProcessor) Optional() *timeProcessor {
v.required = nil
return v
}
// sets the default value
func (v *timeProcessor) Default(val time.Time) *timeProcessor {
v.defaultVal = &val
return v
}
// sets the catch value (i.e the value to use if the validation fails)
func (v *timeProcessor) Catch(val time.Time) *timeProcessor {
v.catch = &val
return v
}
// GLOBAL METHODS
// custom test function call it -> schema.Test("error_code", func(val any, ctx ParseCtx) bool {return true})
func (v *timeProcessor) Test(t p.Test, opts ...TestOption) *timeProcessor {
for _, opt := range opts {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// UNIQUE METHODS
// Checks that the value is after the given time
func (v *timeProcessor) After(t time.Time, opts ...TestOption) *timeProcessor {
r := p.Test{
ErrCode: zconst.ErrCodeAfter,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(time.Time)
if !ok {
return false
}
return val.After(t)
},
}
r.Params[zconst.ErrCodeAfter] = t
for _, opt := range opts {
opt(&r)
}
for _, opt := range v.tests {
r.ErrFmt = opt.ErrFmt
}
v.tests = append(v.tests, r)
return v
}
// Checks that the value is before the given time
func (v *timeProcessor) Before(t time.Time, opts ...TestOption) *timeProcessor {
r :=
p.Test{
ErrCode: zconst.ErrCodeBefore,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(time.Time)
if !ok {
return false
}
return val.Before(t)
},
}
r.Params[zconst.ErrCodeBefore] = t
for _, opt := range opts {
opt(&r)
}
v.tests = append(v.tests, r)
return v
}
// Checks that the value is equal to the given time
func (v *timeProcessor) EQ(t time.Time, opts ...TestOption) *timeProcessor {
r := p.Test{
ErrCode: zconst.ErrCodeEQ,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(time.Time)
if !ok {
return false
}
return val.Equal(t)
},
}
r.Params[zconst.ErrCodeEQ] = t
for _, opt := range opts {
opt(&r)
}
v.tests = append(v.tests, r)
return v
}