-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
163 lines (132 loc) · 3.25 KB
/
schema.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
package buzz
import (
"fmt"
"reflect"
"unicode"
)
type BuzzSchemaValidateFunc[T any] func(T) error
type BuzzSchema[T any] struct {
name string
fields []BuzzField
validateFuncs []BuzzSchemaValidateFunc[T]
refType reflect.Type
}
func Schema[T any](fields ...BuzzField) *BuzzSchema[T] {
refObj := *new(T)
refType := reflect.TypeOf(refObj)
if refType.Kind() != reflect.Struct {
panic("buzz: reference object is not struct")
}
refFields := reflect.VisibleFields(refType)
var exportedFields []BuzzField
for _, field := range fields {
fieldName := field.Name()
fieldType := field.Type()
if unicode.IsLower(rune(fieldName[0])) {
continue
}
found := false
for _, refField := range refFields {
if refField.Name == fieldName {
if refField.Type != fieldType {
panic(fmt.Sprintf("buzz: field '%s' has mismatching types", fieldName))
}
exportedFields = append(exportedFields, field)
found = true
break
}
}
if !found {
panic(fmt.Sprintf("buzz: field '%s' not found in the reference object", fieldName))
}
}
return &BuzzSchema[T]{
fields: exportedFields,
refType: refType,
}
}
func (s *BuzzSchema[T]) Validate(obj any) error {
objT, ok := obj.(T)
if !ok {
return fmt.Errorf(invalidTypeMsg, s.refType, obj)
}
errAggr := NewFieldErrorAggregator()
valueObj := reflect.ValueOf(obj)
for _, f := range s.fields {
valueField := valueObj.FieldByName(f.Name())
if err := f.Validate(valueField.Interface()); err != nil {
if errAggr.Handle(err) != nil {
return err
}
}
}
for _, valFn := range s.validateFuncs {
if err := valFn(objT); err != nil {
if errAggr.Handle(err) != nil {
return err
}
}
}
return errAggr.OrNil()
}
func (s *BuzzSchema[T]) Name() string {
return s.name
}
func (s *BuzzSchema[T]) Type() reflect.Type {
return s.refType
}
func (s *BuzzSchema[T]) WithName(name string) BuzzField {
s.name = name
return s
}
func (s *BuzzSchema[T]) Clone() BuzzField {
return &BuzzSchema[T]{
name: s.name,
fields: s.fields,
validateFuncs: s.validateFuncs,
refType: s.refType,
}
}
func (s *BuzzSchema[T]) Fields() []BuzzField {
return s.fields
}
func (s *BuzzSchema[T]) Custom(fn BuzzSchemaValidateFunc[T]) *BuzzSchema[T] {
s.registerValidateFunc(fn)
return s
}
func (s *BuzzSchema[T]) registerValidateFunc(fn BuzzSchemaValidateFunc[T]) {
s.validateFuncs = append(s.validateFuncs, fn)
}
func Extend[T, K any](schema *BuzzSchema[K], fields ...BuzzField) *BuzzSchema[T] {
newFields := append(fields, schema.fields...)
return Schema[T](newFields...)
}
func Pick[T, K any](schema *BuzzSchema[K], fieldNames ...string) *BuzzSchema[T] {
var newFields []BuzzField
for _, name := range fieldNames {
for _, field := range schema.fields {
if field.Name() == name {
newFields = append(newFields, field)
break
}
}
}
return Schema[T](newFields...)
}
func Omit[T, K any](schema *BuzzSchema[K], fieldNames ...string) *BuzzSchema[T] {
var newFields []BuzzField
for _, field := range schema.fields {
fieldName := field.Name()
found := false
for _, name := range fieldNames {
if fieldName == name {
found = true
break
}
}
if !found {
newFields = append(newFields, field)
}
}
return Schema[T](newFields...)
}