Skip to content

Commit

Permalink
Fixes use case for non-empty default and empty input
Browse files Browse the repository at this point in the history
  • Loading branch information
wbreza committed Oct 19, 2023
1 parent 3be173c commit c15dd21
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
20 changes: 16 additions & 4 deletions cli/azd/pkg/infra/provisioning/bicep/bicep_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ func (p *BicepProvider) ensureParameters(
// If a value is explicitly configured via a parameters file, use it.
// unless the parameter value inference is nil/empty
if v, has := parameters[key]; has {
paramValue := armParameterFileValue(p.mapBicepTypeToInterfaceType(param.Type), v.Value)
paramValue := armParameterFileValue(p.mapBicepTypeToInterfaceType(param.Type), v.Value, param.DefaultValue)
if paramValue != nil {
configuredParameters[key] = azure.ArmParameterValue{
Value: paramValue,
Expand Down Expand Up @@ -1934,7 +1934,7 @@ func (p *BicepProvider) ensureParameters(
}

// Convert the ARM parameters file value into a value suitable for deployment
func armParameterFileValue(paramType ParameterType, value any) any {
func armParameterFileValue(paramType ParameterType, value any, defaultValue any) any {
// Relax the handling of bool and number types to accept convertible strings
switch paramType {
case ParameterTypeBoolean:
Expand All @@ -1950,8 +1950,20 @@ func armParameterFileValue(paramType ParameterType, value any) any {
}
}
case ParameterTypeString:
if val, ok := value.(string); ok && val != "" {
return val
// Use Cases
// 1. Non-empty input value, return input value (no prompt)
// 2. Empty input value and no default - return nil (prompt user)
// 3. Empty input value and non-empty default - return empty input string (no prompt)
paramVal, paramValid := value.(string)
defaultVal, hasDefault := defaultValue.(string)
if hasDefault {
if paramValid && paramVal != defaultVal {
return paramVal
}
}

if paramValid && paramVal != "" {
return paramVal
}
// All other parameter types return the specified value as-is
default:
Expand Down
28 changes: 20 additions & 8 deletions cli/azd/pkg/infra/provisioning/bicep/bicep_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,47 +1024,59 @@ func TestUserDefinedTypes(t *testing.T) {

func Test_armParameterFileValue(t *testing.T) {
t.Run("NilValue", func(t *testing.T) {
actual := armParameterFileValue(ParameterTypeString, nil)
actual := armParameterFileValue(ParameterTypeString, nil, nil)
require.Nil(t, actual)
})

t.Run("StringWithValue", func(t *testing.T) {
expected := "value"
actual := armParameterFileValue(ParameterTypeString, expected)
actual := armParameterFileValue(ParameterTypeString, expected, nil)
require.Equal(t, expected, actual)
})

t.Run("EmptyString", func(t *testing.T) {
input := ""
actual := armParameterFileValue(ParameterTypeString, input, nil)
require.Nil(t, actual)
})

t.Run("EmptyStringWithNonEmptyDefault", func(t *testing.T) {
expected := ""
actual := armParameterFileValue(ParameterTypeString, expected)
actual := armParameterFileValue(ParameterTypeString, expected, "not-empty")
require.Equal(t, expected, actual)
})

t.Run("EmptyStringWithEmptyDefault", func(t *testing.T) {
input := ""
actual := armParameterFileValue(ParameterTypeString, input, "")
require.Nil(t, actual)
})

t.Run("ValidBool", func(t *testing.T) {
expected := true
actual := armParameterFileValue(ParameterTypeBoolean, "true")
actual := armParameterFileValue(ParameterTypeBoolean, "true", nil)
require.Equal(t, expected, actual)
})

t.Run("InvalidBool", func(t *testing.T) {
actual := armParameterFileValue(ParameterTypeBoolean, "NotABool")
actual := armParameterFileValue(ParameterTypeBoolean, "NotABool", nil)
require.Nil(t, actual)
})

t.Run("ValidInt", func(t *testing.T) {
var expected int64 = 42
actual := armParameterFileValue(ParameterTypeNumber, "42")
actual := armParameterFileValue(ParameterTypeNumber, "42", nil)
require.Equal(t, expected, actual)
})

t.Run("InvalidInt", func(t *testing.T) {
actual := armParameterFileValue(ParameterTypeNumber, "NotAnInt")
actual := armParameterFileValue(ParameterTypeNumber, "NotAnInt", nil)
require.Nil(t, actual)
})

t.Run("Array", func(t *testing.T) {
expected := []string{"a", "b", "c"}
actual := armParameterFileValue(ParameterTypeArray, expected)
actual := armParameterFileValue(ParameterTypeArray, expected, nil)
require.Equal(t, expected, actual)
})
}
Expand Down

0 comments on commit c15dd21

Please sign in to comment.