From 3d2f7622bc89a7146914888295708a736c3bcb28 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Tue, 2 Jul 2024 14:40:39 +0200 Subject: [PATCH] Fixed bundle not loading when empty variable is defined (#1552) ## Changes Fixes #1544 ## Tests Added regression test --- bundle/tests/variables/empty/databricks.yml | 7 +++++++ bundle/tests/variables_test.go | 6 ++++++ libs/dyn/convert/to_typed.go | 5 +++++ libs/dyn/convert/to_typed_test.go | 7 +++++++ 4 files changed, 25 insertions(+) create mode 100644 bundle/tests/variables/empty/databricks.yml diff --git a/bundle/tests/variables/empty/databricks.yml b/bundle/tests/variables/empty/databricks.yml new file mode 100644 index 000000000..f90f6211c --- /dev/null +++ b/bundle/tests/variables/empty/databricks.yml @@ -0,0 +1,7 @@ +variables: + a: + description: empty variable + default: + +bundle: + name: empty${var.a} diff --git a/bundle/tests/variables_test.go b/bundle/tests/variables_test.go index 7cf0f72f0..51a23e5d5 100644 --- a/bundle/tests/variables_test.go +++ b/bundle/tests/variables_test.go @@ -193,3 +193,9 @@ func TestVariableTargetOverrides(t *testing.T) { }) } } + +func TestBundleWithEmptyVariableLoads(t *testing.T) { + b := load(t, "./variables/empty") + diags := bundle.Apply(context.Background(), b, mutator.SetVariables()) + require.ErrorContains(t, diags.Error(), "no value assigned to required variable a") +} diff --git a/libs/dyn/convert/to_typed.go b/libs/dyn/convert/to_typed.go index 8febe87ae..181c88cc9 100644 --- a/libs/dyn/convert/to_typed.go +++ b/libs/dyn/convert/to_typed.go @@ -282,6 +282,11 @@ func toTypedFloat(dst reflect.Value, src dyn.Value) error { } func toTypedInterface(dst reflect.Value, src dyn.Value) error { + if src.Kind() == dyn.KindNil { + dst.Set(reflect.Zero(dst.Type())) + return nil + } + dst.Set(reflect.ValueOf(src.AsAny())) return nil } diff --git a/libs/dyn/convert/to_typed_test.go b/libs/dyn/convert/to_typed_test.go index 5e37f2863..37d85539c 100644 --- a/libs/dyn/convert/to_typed_test.go +++ b/libs/dyn/convert/to_typed_test.go @@ -533,3 +533,10 @@ func TestToTypedAnyWithMap(t *testing.T) { require.NoError(t, err) assert.Equal(t, map[string]any{"foo": "bar", "bar": "baz"}, out) } + +func TestToTypedAnyWithNil(t *testing.T) { + var out any + err := ToTyped(&out, dyn.NilValue) + require.NoError(t, err) + assert.Equal(t, nil, out) +}