-
Notifications
You must be signed in to change notification settings - Fork 62
[jjo] diff: handle empty config values #180
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"fmt" | ||
"io" | ||
"os" | ||
"reflect" | ||
"sort" | ||
|
||
isatty "github.com/mattn/go-isatty" | ||
|
@@ -102,6 +103,26 @@ func (c DiffCmd) Run(apiObjects []*unstructured.Unstructured, out io.Writer) err | |
return nil | ||
} | ||
|
||
// isEmptyValue is taken from the encoding/json package in the | ||
// standard library. | ||
func isEmptyValue(v reflect.Value) bool { | ||
switch v.Kind() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fwiw, this only has to deal with the types that func isEmptyValue(i interface{}) bool {
switch v := i.(type) {
case []interface{}:
return len(v) == 0
case map[string]interface{}:
return len(v) == 0
case bool:
return !v
case float64:
return v == 0
case string:
return v == ""
case nil:
return true
default:
panic("Found unexpected type %T in json unmarshal (value=%v)", i, i)
}
} ... and then invoke with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anguslees: nopes, that won't support nested FWIW using above simpler implementation would expectedly fail with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anguslees nvm on above reply (early morning low caffeine disclaimer :P), |
||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String: | ||
return v.Len() == 0 | ||
case reflect.Bool: | ||
return !v.Bool() | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return v.Int() == 0 | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return v.Uint() == 0 | ||
case reflect.Float32, reflect.Float64: | ||
return v.Float() == 0 | ||
case reflect.Interface, reflect.Ptr: | ||
return v.IsNil() | ||
} | ||
return false | ||
} | ||
|
||
func removeFields(config, live interface{}) interface{} { | ||
switch c := config.(type) { | ||
case map[string]interface{}: | ||
|
@@ -118,6 +139,9 @@ func removeMapFields(config, live map[string]interface{}) map[string]interface{} | |
for k, v1 := range config { | ||
v2, ok := live[k] | ||
if !ok { | ||
if isEmptyValue(reflect.ValueOf(v1)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps a comment wouldn't hurt here (so one doesn't have to git blame through the full future chain of commits until one hits this PR with the relevant context) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, done. |
||
result[k] = v1 | ||
} | ||
continue | ||
} | ||
result[k] = removeFields(v1, v2) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please mention golang/go#7501
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, done.