-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hcl: When writting Module variables normalize the names
As in some cases, like 'tags', they have keys that cannot be converted direclty into valirables names so we have to normailze them. For this we reused the 'tag' logic and abstracted it to the 'util' lib so it can be shared between the 2 packages
- Loading branch information
Showing
6 changed files
with
74 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package util | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var invalidNameRegexp = regexp.MustCompile(`[^a-z0-9_]`) | ||
|
||
// NormalizeName will convert the n into an low case alphanumeric value | ||
// and the invalid characters will be replaced by '_' | ||
func NormalizeName(n string) string { | ||
return invalidNameRegexp.ReplaceAllString(strings.ToLower(n), "_") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cycloidio/terracognita/util" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNormalizeName(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
In string | ||
Expected string | ||
}{ | ||
{ | ||
Name: "NoChange", | ||
In: "in", | ||
Expected: "in", | ||
}, | ||
{ | ||
Name: "UpperCase", | ||
In: "IN", | ||
Expected: "in", | ||
}, | ||
{ | ||
Name: "Invalid", | ||
In: ":a", | ||
Expected: "_a", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
assert.Equal(t, tt.Expected, util.NormalizeName(tt.In)) | ||
}) | ||
} | ||
} |