-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueexpr.go
187 lines (150 loc) · 5.01 KB
/
valueexpr.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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validator
import (
"fmt"
"reflect"
"regexp"
)
const (
valueTag = "value"
valueRequiredTag = "required"
valueRequiredIfTag = "requiredIf:"
valueOptionalTag = "optional"
valueOptionalIfTag = "optionalIf:"
valueDefaultTag = "default:"
valueCoalesceTag = "coalesce:"
valueExprDelim = '='
)
type valueExprType int
const (
valueRequired valueExprType = iota + 1
valueRequiredIf
valueOptional
valueOptionalIf
valueDefault
)
type (
valueExpression struct {
exprType valueExprType
fName fieldName
arg string
}
valueConditionalExpression struct {
r *regexp.Regexp
condField fieldName
}
valueRequiredMap map[fieldIndex]bool
valueConditionalMap map[fieldIndex]valueConditionalExpression
valueDefaultMap map[fieldIndex]string
valueExpressions struct {
required valueRequiredMap
condRequired valueConditionalMap
condOptional valueConditionalMap
defaultVal valueDefaultMap
}
)
func newValueExpressions() *valueExpressions {
return &valueExpressions{
required: make(valueRequiredMap),
condRequired: make(valueConditionalMap),
condOptional: make(valueConditionalMap),
defaultVal: make(valueDefaultMap),
}
}
func checkValueExpr(value, valueExprTag string) bool {
return len(value) > len(valueExprTag) && value[:len(valueExprTag)] == valueExprTag
}
func invalidValueExprError(msg, value, sName string, fName fieldName) error {
return fmt.Errorf("invalid value expression '%s' in '%s.%s': %s", value, sName, fName, msg)
}
func parseCondValueExpr(value, tag, sName string, nameIdxMap nameIndexMap, fName fieldName) (fieldName, string) {
n, pattern, ok := splitOne(value[len(tag):], valueExprDelim)
targetField := fieldName(n)
if targetField == fName {
panic(invalidValueExprError(fmt.Sprintf("field '%s' is not allowed in the expression", fName),
value, sName, fName))
}
if !ok {
panic(invalidValueExprError("parsing failed", value, sName, fName))
}
if _, ok := nameIdxMap[targetField]; !ok {
panic(invalidValueExprError(fmt.Sprintf("no field named '%s' found", targetField), value, sName, fName))
}
return targetField, pattern
}
func parseValueExpr(value string, selfStructName string, fName fieldName, nameIdxMap nameIndexMap) valueExpression {
var expr valueExpression
if value == valueOptionalTag {
expr.exprType = valueOptional
} else if value == valueRequiredTag {
expr.exprType = valueRequired
} else if checkValueExpr(value, valueDefaultTag) {
expr.exprType = valueDefault
expr.arg = value[len(valueDefaultTag):]
} else if checkValueExpr(value, valueRequiredIfTag) {
expr.exprType = valueRequiredIf
expr.fName, expr.arg = parseCondValueExpr(value, valueRequiredIfTag, selfStructName, nameIdxMap, fName)
} else if checkValueExpr(value, valueOptionalIfTag) {
expr.exprType = valueOptionalIf
expr.fName, expr.arg = parseCondValueExpr(value, valueOptionalIfTag, selfStructName, nameIdxMap, fName)
} else {
panic(invalidValueExprError(fmt.Sprintf("unrecognized value expression"), value, selfStructName, fName))
}
return expr
}
func buildValueExpressions(t reflect.Type, idxNameMap indexNameMap, nameIdxMap nameIndexMap) *valueExpressions {
vs := newValueExpressions()
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
value, ok := ft.Tag.Lookup(valueTag)
if !ok {
continue
}
fIdx := fieldIndex(i)
expr := parseValueExpr(value, t.Name(), idxNameMap[fieldIndex(i)], nameIdxMap)
switch expr.exprType {
case valueOptional:
vs.required[fIdx] = false
case valueRequired:
vs.required[fIdx] = true
case valueDefault:
vs.defaultVal[fIdx] = expr.arg
case valueRequiredIf:
vs.condRequired[fIdx] = valueConditionalExpression{condField: expr.fName, r: regexp.MustCompile(expr.arg)}
case valueOptionalIf:
vs.condOptional[fIdx] = valueConditionalExpression{condField: expr.fName, r: regexp.MustCompile(expr.arg)}
}
}
return vs
}
func resolveConditionalReq(condExpr valueConditionalExpression, data map[string]string) bool {
condValue, ok := data[string(condExpr.condField)]
if !ok {
return false
}
return condExpr.r.MatchString(condValue)
}
func requiresValue(i fieldIndex, ve *valueExpressions, data map[string]string) bool {
if required, ok := ve.required[i]; ok {
return required
}
if condExpr, ok := ve.condRequired[i]; ok {
return resolveConditionalReq(condExpr, data)
}
if condExpr, ok := ve.condOptional[i]; ok {
return !resolveConditionalReq(condExpr, data)
}
// By default, all fields are required
return true
}