Skip to content

Commit f23e367

Browse files
committed
move checkMissingFields to its own file, as this is a general function
1 parent 0d05777 commit f23e367

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

pkg/validation/internal/csv.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,46 +58,6 @@ func checkFields(csv *operatorsv1alpha1.ClusterServiceVersion) (errs []errors.Er
5858
return append(result.Errors, result.Warnings...)
5959
}
6060

61-
// Recursive function that traverses a nested struct passed in as reflect value, and reports for errors/warnings
62-
// in case of null struct field values.
63-
func checkMissingFields(result *errors.ManifestResult, v reflect.Value, parentStructName string) {
64-
if v.Kind() != reflect.Struct {
65-
return
66-
}
67-
typ := v.Type()
68-
69-
for i := 0; i < v.NumField(); i++ {
70-
fieldValue := v.Field(i)
71-
fieldType := typ.Field(i)
72-
73-
tag := fieldType.Tag.Get("json")
74-
// Ignore fields that are subsets of a primitive field.
75-
if tag == "" {
76-
continue
77-
}
78-
79-
// Omitted field tags will contain ",omitempty", and ignored tags will
80-
// match "-" exactly, respectively.
81-
isOptionalField := strings.Contains(tag, ",omitempty") || tag == "-"
82-
emptyVal := isEmptyValue(fieldValue)
83-
84-
newParentStructName := fieldType.Name
85-
if parentStructName != "" {
86-
newParentStructName = parentStructName + "." + newParentStructName
87-
}
88-
89-
switch fieldValue.Kind() {
90-
case reflect.Struct:
91-
updateLog(result, "struct", newParentStructName, emptyVal, isOptionalField)
92-
if !emptyVal {
93-
checkMissingFields(result, fieldValue, newParentStructName)
94-
}
95-
default:
96-
updateLog(result, "field", newParentStructName, emptyVal, isOptionalField)
97-
}
98-
}
99-
}
100-
10161
// Returns updated error log with missing optional/mandatory field/struct objects.
10262
func updateLog(result *errors.ManifestResult, typeName string, newParentStructName string, emptyVal bool, isOptionalField bool) {
10363
if !emptyVal {

pkg/validation/internal/typecheck.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package internal
2+
3+
import (
4+
"reflect"
5+
"strings"
6+
7+
"github.com/operator-framework/api/pkg/validation/errors"
8+
)
9+
10+
// Recursive function that traverses a nested struct passed in as reflect
11+
// value, and reports for errors/warnings in case of nil struct field values.
12+
// TODO: make iterative
13+
func checkMissingFields(result *errors.ManifestResult, v reflect.Value, parentStructName string) {
14+
if v.Kind() != reflect.Struct {
15+
return
16+
}
17+
typ := v.Type()
18+
19+
for i := 0; i < v.NumField(); i++ {
20+
fieldValue := v.Field(i)
21+
fieldType := typ.Field(i)
22+
23+
tag := fieldType.Tag.Get("json")
24+
// Ignore fields that are subsets of a primitive field.
25+
if tag == "" {
26+
continue
27+
}
28+
29+
// Omitted field tags will contain ",omitempty", and ignored tags will
30+
// match "-" exactly, respectively.
31+
isOptionalField := strings.Contains(tag, ",omitempty") || tag == "-"
32+
emptyVal := isEmptyValue(fieldValue)
33+
34+
newParentStructName := fieldType.Name
35+
if parentStructName != "" {
36+
newParentStructName = parentStructName + "." + newParentStructName
37+
}
38+
39+
switch fieldValue.Kind() {
40+
case reflect.Struct:
41+
updateLog(result, "struct", newParentStructName, emptyVal, isOptionalField)
42+
if !emptyVal {
43+
checkMissingFields(result, fieldValue, newParentStructName)
44+
}
45+
default:
46+
updateLog(result, "field", newParentStructName, emptyVal, isOptionalField)
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)