From ef7ef215d630c93372f093d83d2b073ac5062005 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Mon, 20 Jul 2020 10:41:20 -0400 Subject: [PATCH] Handle int -> float conversion in FromUnstructured --- .../apimachinery/pkg/runtime/converter.go | 3 +++ .../pkg/runtime/converter_test.go | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go b/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go index 31f6e00b0f7..3c020ba1c8e 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go @@ -186,6 +186,9 @@ func fromUnstructured(sv, dv reflect.Value) error { reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: dv.Set(sv.Convert(dt)) return nil + case reflect.Float32, reflect.Float64: + dv.Set(sv.Convert(dt)) + return nil } case reflect.Float32, reflect.Float64: switch dt.Kind() { diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/converter_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/converter_test.go index 0e0ccfc9a16..224b2838ba5 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/converter_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/converter_test.go @@ -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) { testcases := []struct { Data string