-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
247 lines (213 loc) · 7.92 KB
/
Copy pathvalidator.go
File metadata and controls
247 lines (213 loc) · 7.92 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// MIT License
//
// # Copyright (c) 2026 Christopher J Bearman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package openprinttag
import (
"fmt"
"reflect"
"strconv"
"github.com/cjbearman/openprinttag/structtags"
st "github.com/cjbearman/openprinttag/structtags"
)
// Validate checks for missing required (error) or recommended (warnings)
// fields in all regions
func (o *OpenPrintTag) Validate() (errors []string, warnings []string) {
if o.meta != nil {
e, w := validateRegion(o.meta)
errors = append(errors, e...)
warnings = append(warnings, w...)
}
if o.main != nil {
e, w := validateRegion(o.main)
errors = append(errors, e...)
warnings = append(warnings, w...)
}
if o.aux != nil {
e, w := validateRegion(o.aux)
errors = append(errors, e...)
warnings = append(warnings, w...)
if o.auxRegionSize != 0 {
if o.auxRegionSize < 16 {
errors = append(errors, fmt.Sprintf("aux region size is set to %d bytes, specification requires a minimum of 16 bytes", o.auxRegionSize))
} else if o.auxRegionSize < 32 {
warnings = append(warnings, fmt.Sprintf("aux region size is set to %d bytes, specification recommends at least 32 bytes for practical use", o.auxRegionSize))
}
}
}
return
}
// Validate will return any errors or warnings for a specific region
func validateRegion(region Region) (errors, warnings []string) {
defer func() {
// Just in case something goes catastrophically wrong
if r := recover(); r != nil {
errors = append(errors, fmt.Sprintf("panic during validation: %v", r))
}
}()
// This is an interface, get it's value
obj := reflect.ValueOf(region).Elem()
// It must have an internal field, get that
internal := obj.FieldByName("internal")
// Iterate through all fields in the internal struct
for i := 0; i < internal.NumField(); i++ {
// The field name
name := internal.Type().Field(i).Name
// The field (as reflect.Field)
field := internal.Type().Field(i)
// Decode the opt tag into it's constituent parts
tagMap := structtags.ReadOptTags(field.Tag)
// The native field name
nativeName := tagMap[st.OptTagName]
// The native field key (string encoded integer)
key := tagMap[st.OptTagKey]
// The field instance value
fieldValue := internal.Field(i)
// Decode the value, which is a pointer to the actual value where set
var valueIsNil = true
if !fieldValue.IsNil() {
valueIsNil = false
}
// Is this required, if so error if nil
_, isRequired := tagMap[st.OptTagRequired]
if isRequired && valueIsNil {
errors = append(errors, genErrorOrWarning(name, key, nativeName, "is required"))
}
// Is this recommended, if so warning if nil
_, isRecommended := tagMap[st.OptTagRecommended]
if isRecommended && valueIsNil {
warnings = append(warnings, genErrorOrWarning(name, key, nativeName, "is recommended"))
}
}
return
}
// OptCheck checks options for warnings and errors in all present regions
func (o *OpenPrintTag) OptCheck() (errors []string, warnings []string) {
if o.meta != nil {
e, w := optCheck(o.meta)
errors = append(errors, e...)
warnings = append(warnings, w...)
}
if o.main != nil {
e, w := optCheck(o.main)
errors = append(errors, e...)
warnings = append(warnings, w...)
}
if o.aux != nil {
e, w := optCheck(o.aux)
errors = append(errors, e...)
warnings = append(warnings, w...)
}
return
}
// optCheck returns errors and warnings for a specific region
func optCheck(region Region) (errors, warnings []string) {
defer func() {
// Just in case something goes catastrophically wrong
if r := recover(); r != nil {
errors = append(errors, fmt.Sprintf("panic during validation: %v", r))
}
}()
// This is an interface, get it's value
obj := reflect.ValueOf(region).Elem()
// It must have an internal field, get that
internal := obj.FieldByName("internal")
// Iterate through all fields in the internal struct
for i := 0; i < internal.NumField(); i++ {
// The field name
name := internal.Type().Field(i).Name
// The field (as reflect.Field)
field := internal.Type().Field(i)
// Decode the opt tag into it's constituent parts
tagMap := structtags.ReadOptTags(field.Tag)
// The native field name
nativeName := tagMap[st.OptTagName]
// The native field key (string encoded integer)
key := tagMap[st.OptTagKey]
// The field instance value
fieldValue := internal.Field(i)
// Decode the value, which is a pointer to the actual value where set
var value reflect.Value
if fieldValue.IsNil() {
continue
}
value = fieldValue.Elem()
// Check for max length
maxLenStr, found := tagMap[st.OptTagMaxLength]
if found {
maxLen, err := strconv.Atoi(maxLenStr)
if err == nil {
var length int
// max_length is applicable only to string and slice fields
if value.Kind() == reflect.String || value.Kind() == reflect.Slice {
length = value.Len()
if length > maxLen {
errors = append(errors, genErrorOrWarning(name, key, nativeName, "has length %d which exceeds maximum length of %d for this field", length, maxLen))
}
}
}
}
// Check for rgba
_, rgba := tagMap[st.OptTagRGBA]
if rgba {
length := value.Len()
if length != 3 && length != 4 {
errors = append(errors, genErrorOrWarning(name, key, nativeName, "has length %d which is not valid for RGBA fields (must be 3 or 4)", length))
}
}
}
customErrors, customWarnings := getCustomErrorsAndWarnings(region)
errors = append(errors, customErrors...)
warnings = append(warnings, customWarnings...)
return
}
// GetCustomErrorsAndWarnings returns errors/warnings that cannot be auto-derived and must be hand coded
func getCustomErrorsAndWarnings(region Region) (errors, warnings []string) {
if main, ok := region.(*MainRegion); ok {
brand, brandSet := main.GetBrandName()
brandUUID, brandUUIDSet := main.GetBrandUuid()
if brandSet && brandUUIDSet {
calculatedBrandUUID := BrandUUID(brand)
if calculatedBrandUUID.String() == brandUUID.String() {
warnings = append(warnings, "brand_uuid is identical to the auto-generated version, and thus can be omitted to save space")
}
}
material, materialSet := main.GetMaterialName()
materialUUID, materialUUIDSet := main.GetMaterialUuid()
if materialSet && materialUUIDSet && (brandSet || brandUUIDSet) {
if !brandUUIDSet {
brandUUID = BrandUUID(brand)
}
calculatedMaterialUUID := MaterialUUID(material, brandUUID)
if calculatedMaterialUUID.String() == materialUUID.String() {
warnings = append(warnings, "material_uuid is identical to the auto-generated version, and thus can be omitted to save space")
}
}
// TODO: Gtin and package need to be added
}
return
}
// genErrorOrWarning will return a suitably formatted field based error or warning
// given the name, key and native name of the field (for consistency)
// and additional format and args
func genErrorOrWarning(name, key, nativeName, format string, v ...any) string {
preamble := fmt.Sprintf("field %s (%s/%s) ", name, nativeName, key)
return fmt.Sprintf(preamble+format, v...)
}