Skip to content

Commit

Permalink
Merge pull request #18 from datadrivers/fix/name-handling
Browse files Browse the repository at this point in the history
fix(designation_name): Fix handling of unset name variable
  • Loading branch information
anmoel authored Jun 30, 2022
2 parents 23aa3e7 + 4dc6833 commit 6aa0c38
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
terraform:
- '1.0.*'
- '1.1.*'
- '1.2.*'
steps:
- uses: actions/setup-go@v3
with:
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ func generateName(name string, inputs map[string]string, convention Convention)
var diags diag.Diagnostics

result := convention.Definition
variableNameConfigured := false

for _, variable := range convention.Variables {
block := fmt.Sprintf("(%s)", variable.Name.Value)
replacement := inputs[variable.Name.Value]
if variable.Name.Value == "name" {
replacement = name
variableNameConfigured = true
}
length := 0

Expand All @@ -64,5 +66,9 @@ func generateName(name string, inputs map[string]string, convention Convention)
result = strings.Replace(result, block, replacement, -1)
}

if !variableNameConfigured {
result = strings.Replace(result, "(name)", name, -1)
}

return types.String{Value: result}, diags
}
55 changes: 55 additions & 0 deletions internal/provider/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,61 @@ func TestValidateInputs(t *testing.T) {
}

func TestGenerateName(t *testing.T) {
name := "foobar"
convention := Convention{
Definition: "(name)-(not-generated)-(default)-(generated)",
Variables: []Variable{
{
Name: types.String{Value: "not-generated"},
Generated: types.Bool{
Null: true,
Value: false,
},
Default: types.String{
Null: true,
},
MaxLength: types.Int64{
Null: true,
},
},
{
Name: types.String{Value: "generated"},
Generated: types.Bool{
Value: true,
Null: false,
},
MaxLength: types.Int64{
Null: false,
Value: int64(4),
},
},
{
Name: types.String{Value: "default"},
Generated: types.Bool{
Null: true,
},
Default: types.String{
Null: false,
Value: "default",
},
MaxLength: types.Int64{
Null: true,
},
},
},
}

inputs := map[string]string{
"not-generated": "bar",
}

result, diags := generateName(name, inputs, convention)
assert.False(t, diags.HasError())
assert.Contains(t, result.Value, "foobar-bar-default-")
assert.Len(t, result.Value, 23)
}

func TestGenerateNameWithConfiguredNameVariable(t *testing.T) {
name := "foobar"
convention := Convention{
Definition: "(name)-(not-generated)-(default)-(generated)",
Expand Down

0 comments on commit 6aa0c38

Please sign in to comment.