-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefine.go
164 lines (143 loc) · 3.44 KB
/
refine.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
package refine
import (
"errors"
"fmt"
"reflect"
)
var ErrNotStruct = errors.New("only struct types can be checked")
var ErrUnsupportedType = errors.New("unsupported type")
var ErrNotMet = errors.New("not met")
var ErrParse = errors.New("could not be parsed")
var ErrEval = errors.New("could not be evaluated")
type checkErr struct {
structType string
fieldName string
fieldValue any
refinement string
err error
}
func (e checkErr) Error() string {
return fmt.Sprintf(
"refine.Check: %s.%s = %#+v, %q %v",
e.structType, e.fieldName, e.fieldValue, e.refinement, e.err,
)
}
func (e checkErr) Unwrap() error {
return e.err
}
const tag = "refine"
var kindMap = map[reflect.Kind]kind{
reflect.String: boxString,
reflect.Bool: boxBool,
reflect.Int: boxInt,
reflect.Int8: boxInt,
reflect.Int16: boxInt,
reflect.Int32: boxInt,
reflect.Int64: boxInt,
reflect.Uint: boxUint,
reflect.Uint8: boxUint,
reflect.Uint16: boxUint,
reflect.Uint32: boxUint,
reflect.Uint64: boxUint,
reflect.Slice: boxSlice,
reflect.Map: boxMap,
reflect.Pointer: boxPointer,
reflect.Struct: boxStruct,
}
func Check(val any) error {
t := func() reflect.Type {
t := reflect.TypeOf(val)
if t.Kind() == reflect.Pointer {
return t.Elem()
} else {
return t
}
}()
v := func() reflect.Value {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Pointer {
return v.Elem()
} else {
return v
}
}()
if t.Kind() != reflect.Struct || v.Kind() != reflect.Struct {
return fmt.Errorf("refine.Check: %s is a %s, %w", t.Name(), v.Kind().String(), ErrNotStruct)
}
var ev = newEvaluator()
n := t.NumField()
// Populate the symbol table with values from the struct's fields.
for i := 0; i < n; i++ {
field := t.Field(i)
kind := field.Type.Kind()
value := v.Field(i)
boxKind, ok := kindMap[kind]
if !ok {
return fmt.Errorf("refine.Check: %s.%s %w %s", t.Name(), field.Name, ErrUnsupportedType, kind.String())
}
if kind == reflect.Pointer {
if value.IsNil() {
ev.symbols[field.Name] = box{
kind: boxPointer,
val: nil,
}
} else {
ev.symbols[field.Name] = box{
kind: boxKind,
val: value.Interface(),
}
}
} else {
ev.symbols[field.Name] = box{
kind: boxKind,
val: value.Interface(),
}
}
}
// Parse and evaluate the refinements on each field.
for i := 0; i < n; i++ {
field := t.Field(i)
refinement := field.Tag.Get(tag)
tokens := lex(field.Name, refinement)
expr, err := parse(tokens)
if err != nil {
return checkErr{
structType: t.Name(),
fieldName: field.Name,
fieldValue: ev.symbols[field.Name].val,
refinement: refinement,
err: fmt.Errorf("%w: %v", ErrParse, err),
}
}
expr.Accept(ev)
result, err := ev.Result, ev.Err
if err != nil {
return checkErr{
structType: t.Name(),
fieldName: field.Name,
fieldValue: ev.symbols[field.Name].val,
refinement: refinement,
err: fmt.Errorf("%w: %v", ErrEval, err),
}
}
if result.kind != boxBool {
return checkErr{
structType: t.Name(),
fieldName: field.Name,
fieldValue: ev.symbols[field.Name].val,
refinement: refinement,
err: fmt.Errorf("%w: %v", ErrEval, "not bool"),
}
}
if result.val.(bool) != true {
return checkErr{
structType: t.Name(),
fieldName: field.Name,
fieldValue: ev.symbols[field.Name].val,
refinement: refinement,
err: ErrNotMet,
}
}
}
return nil
}