Skip to content

Commit

Permalink
Handle int -> float conversion in FromUnstructured
Browse files Browse the repository at this point in the history
Kubernetes-commit: 1c0ec4bb2b69b6d7390ca99d258f20e2260db227
  • Loading branch information
liggitt authored and k8s-publishing-bot committed Jul 20, 2020
1 parent 530c680 commit d28ef75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/runtime/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 22 additions & 0 deletions pkg/runtime/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d28ef75

Please sign in to comment.