Skip to content

Commit

Permalink
Allow the any type to be set to nil in convert.FromTyped
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Jun 21, 2024
1 parent 274688d commit 9836da9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libs/dyn/convert/from_typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 8 additions & 0 deletions libs/dyn/convert/from_typed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 9836da9

Please sign in to comment.