Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent panic on null values when iterating over map elements #413

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func Map(rawValue cty.Value) map[string]interface{} {

m := make(map[string]interface{})
for key, value := range rawValue.AsValueMap() {
if value.IsNull() {
continue
}
m[key] = value.AsString()
}

Expand All @@ -96,6 +99,9 @@ func MapOfStrings(rawValue cty.Value) *map[string]string {

m := make(map[string]string)
for key, value := range rawValue.AsValueMap() {
if value.IsNull() {
continue
}
m[key] = value.AsString()
}

Expand Down
30 changes: 30 additions & 0 deletions internal/value/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ func TestMap(t *testing.T) {
require.NotNil(t, actual)
assert.Equal(t, expected, actual)
})

t.Run("it ignores null values", func(t *testing.T) {
expected := map[string]interface{}{
"logout": "https://app.domain.com/logout",
}

testInput := map[string]cty.Value{
"logout": cty.StringVal("https://app.domain.com/logout"),
"login": cty.NullVal(cty.String),
}

actual := Map(cty.MapVal(testInput))
require.NotNil(t, actual)
assert.Equal(t, expected, actual)
})
}

func TestMapOfStrings(t *testing.T) {
Expand Down Expand Up @@ -189,6 +204,21 @@ func TestMapOfStrings(t *testing.T) {
require.NotNil(t, actual)
assert.Equal(t, expected, *actual)
})

t.Run("it ignores null values", func(t *testing.T) {
expected := map[string]string{
"logout": "https://app.domain.com/logout",
}

testInput := map[string]cty.Value{
"logout": cty.StringVal("https://app.domain.com/logout"),
"login": cty.NullVal(cty.String),
}

actual := MapOfStrings(cty.MapVal(testInput))
require.NotNil(t, actual)
assert.Equal(t, expected, *actual)
})
}

func TestMapFromJSON(t *testing.T) {
Expand Down