Merge pull request #93250 from liggitt/unstructured-int-float

Handle int -> float conversion in FromUnstructured
This commit is contained in:
Kubernetes Prow Robot 2020-08-29 17:44:23 -07:00 committed by GitHub
commit 6a11d1f8aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -186,6 +186,9 @@ func fromUnstructured(sv, dv reflect.Value) error {
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dv.Set(sv.Convert(dt)) dv.Set(sv.Convert(dt))
return nil return nil
case reflect.Float32, reflect.Float64:
dv.Set(sv.Convert(dt))
return nil
} }
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
switch dt.Kind() { switch dt.Kind() {

View File

@ -544,6 +544,28 @@ func TestFloatIntConversion(t *testing.T) {
} }
} }
func TestIntFloatConversion(t *testing.T) {
unstr := map[string]interface{}{"ch": int64(3)}
var obj C
if err := runtime.NewTestUnstructuredConverter(simpleEquality).FromUnstructured(unstr, &obj); err != nil {
t.Errorf("Unexpected error in FromUnstructured: %v", err)
}
data, err := json.Marshal(unstr)
if err != nil {
t.Fatalf("Error when marshaling unstructured: %v", err)
}
var unmarshalled C
if err := json.Unmarshal(data, &unmarshalled); err != nil {
t.Fatalf("Error when unmarshaling to object: %v", err)
}
if !reflect.DeepEqual(obj, unmarshalled) {
t.Errorf("Incorrect conversion, diff: %v", diff.ObjectReflectDiff(obj, unmarshalled))
}
}
func TestCustomToUnstructured(t *testing.T) { func TestCustomToUnstructured(t *testing.T) {
testcases := []struct { testcases := []struct {
Data string Data string