Skip to content

Commit 6a11d1f

Browse files
authored
Merge pull request kubernetes#93250 from liggitt/unstructured-int-float
Handle int -> float conversion in FromUnstructured
2 parents f65f868 + ef7ef21 commit 6a11d1f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

staging/src/k8s.io/apimachinery/pkg/runtime/converter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ func fromUnstructured(sv, dv reflect.Value) error {
186186
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
187187
dv.Set(sv.Convert(dt))
188188
return nil
189+
case reflect.Float32, reflect.Float64:
190+
dv.Set(sv.Convert(dt))
191+
return nil
189192
}
190193
case reflect.Float32, reflect.Float64:
191194
switch dt.Kind() {

staging/src/k8s.io/apimachinery/pkg/runtime/converter_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,28 @@ func TestFloatIntConversion(t *testing.T) {
544544
}
545545
}
546546

547+
func TestIntFloatConversion(t *testing.T) {
548+
unstr := map[string]interface{}{"ch": int64(3)}
549+
550+
var obj C
551+
if err := runtime.NewTestUnstructuredConverter(simpleEquality).FromUnstructured(unstr, &obj); err != nil {
552+
t.Errorf("Unexpected error in FromUnstructured: %v", err)
553+
}
554+
555+
data, err := json.Marshal(unstr)
556+
if err != nil {
557+
t.Fatalf("Error when marshaling unstructured: %v", err)
558+
}
559+
var unmarshalled C
560+
if err := json.Unmarshal(data, &unmarshalled); err != nil {
561+
t.Fatalf("Error when unmarshaling to object: %v", err)
562+
}
563+
564+
if !reflect.DeepEqual(obj, unmarshalled) {
565+
t.Errorf("Incorrect conversion, diff: %v", diff.ObjectReflectDiff(obj, unmarshalled))
566+
}
567+
}
568+
547569
func TestCustomToUnstructured(t *testing.T) {
548570
testcases := []struct {
549571
Data string

0 commit comments

Comments
 (0)