From 9836da9bed691ffd2efbbafac2aae3fc068a3d5f Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 21 Jun 2024 13:06:25 +0200 Subject: [PATCH] Allow the any type to be set to nil in `convert.FromTyped` --- libs/dyn/convert/from_typed.go | 3 +++ libs/dyn/convert/from_typed_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/libs/dyn/convert/from_typed.go b/libs/dyn/convert/from_typed.go index ae491d8ab7..523b28162d 100644 --- a/libs/dyn/convert/from_typed.go +++ b/libs/dyn/convert/from_typed.go @@ -72,6 +72,9 @@ func fromTyped(src any, ref dyn.Value, options ...fromTypedOptions) (dyn.Value, return fromTypedInt(srcv, ref, options...) case reflect.Float32, reflect.Float64: return fromTypedFloat(srcv, ref, options...) + case reflect.Invalid: + // If the value is untyped and not set (e.g. any type with nil value), we return nil. + return dyn.NilValue, nil } return dyn.InvalidValue, fmt.Errorf("unsupported type: %s", srcv.Kind()) diff --git a/libs/dyn/convert/from_typed_test.go b/libs/dyn/convert/from_typed_test.go index f75470f420..7a0dad84b1 100644 --- a/libs/dyn/convert/from_typed_test.go +++ b/libs/dyn/convert/from_typed_test.go @@ -619,3 +619,11 @@ func TestFromTypedFloatTypeError(t *testing.T) { _, err := FromTyped(src, ref) require.Error(t, err) } + +func TestFromTypedAnyNil(t *testing.T) { + var src any = nil + var ref = dyn.NilValue + nv, err := FromTyped(src, ref) + require.NoError(t, err) + assert.Equal(t, dyn.NilValue, nv) +}