Skip to content

Commit 6fbc5d8

Browse files
committed
Use reflect IsZero() method for checking zero values
1 parent 93ba7a7 commit 6fbc5d8

File tree

1 file changed

+1
-38
lines changed

1 file changed

+1
-38
lines changed

pkg/validation/internal/typecheck.go

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package internal
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"reflect"
75
"strings"
86

@@ -31,7 +29,7 @@ func checkEmptyFields(result *errors.ManifestResult, v reflect.Value, parentStru
3129
// Omitted field tags will contain ",omitempty", and ignored tags will
3230
// match "-" exactly, respectively.
3331
isOptionalField := strings.Contains(tag, ",omitempty") || tag == "-"
34-
emptyVal := isEmptyValue(fieldValue)
32+
emptyVal := fieldValue.IsZero()
3533

3634
newParentStructName := fieldType.Name
3735
if parentStructName != "" {
@@ -59,38 +57,3 @@ func updateResult(result *errors.ManifestResult, typeName string, newParentStruc
5957
result.Add(errors.ErrFieldMissing("required field missing", newParentStructName, typeName))
6058
}
6159
}
62-
63-
// Uses reflect package to check if the value of the object passed is null, returns a boolean accordingly.
64-
// TODO: replace with reflect.Kind.IsZero() in go 1.13
65-
func isEmptyValue(v reflect.Value) bool {
66-
switch v.Kind() {
67-
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
68-
// Check if the value for 'Spec.InstallStrategy.StrategySpecRaw' field is present. This field is a RawMessage value type. Without a value, the key is explicitly set to 'null'.
69-
if fieldValue, ok := v.Interface().(json.RawMessage); ok {
70-
valString := string(fieldValue)
71-
if valString == "null" {
72-
return true
73-
}
74-
}
75-
return v.Len() == 0
76-
// Currently the only CSV field with integer type is containerPort. Operator Verification Library raises a warning if containerPort field is missisng or if its value is 0.
77-
// It is an optional field so the user can ignore the warning saying this field is missing if they intend to use port 0.
78-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
79-
return v.Int() == 0
80-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
81-
return v.Uint() == 0
82-
case reflect.Float32, reflect.Float64:
83-
return v.Float() == 0
84-
case reflect.Interface, reflect.Ptr:
85-
return v.IsNil()
86-
case reflect.Struct:
87-
for i, n := 0, v.NumField(); i < n; i++ {
88-
if !isEmptyValue(v.Field(i)) {
89-
return false
90-
}
91-
}
92-
return true
93-
default:
94-
panic(fmt.Sprintf("%v kind is not supported.", v.Kind()))
95-
}
96-
}

0 commit comments

Comments
 (0)