Skip to content

Commit

Permalink
Merge pull request #36967 from hashicorp/b-SimpleSchemaSetFunc-crash
Browse files Browse the repository at this point in the history
Fix `panic: interface conversion: interface {} is nil, not map[string]interface {}` in `sdkv2.SimpleSchemaSetFunc`
  • Loading branch information
ewbankkit authored Apr 18, 2024
2 parents 30c3298 + 6443217 commit 322215b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .changelog/36967.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_db_proxy: Fix `interface conversion: interface {} is nil, not map[string]interface {}` panic when `auth` is empty (`{}`)
```
27 changes: 14 additions & 13 deletions internal/sdkv2/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ func SimpleSchemaSetFunc(keys ...string) schema.SchemaSetFunc {
return func(v interface{}) int {
var str strings.Builder

m := v.(map[string]interface{})
for _, key := range keys {
if v, ok := m[key]; ok {
switch v := v.(type) {
case bool:
str.WriteRune('-')
str.WriteString(strconv.FormatBool(v))
case int:
str.WriteRune('-')
str.WriteString(strconv.Itoa(v))
case string:
str.WriteRune('-')
str.WriteString(v)
if m, ok := v.(map[string]interface{}); ok {
for _, key := range keys {
if v, ok := m[key]; ok {
switch v := v.(type) {
case bool:
str.WriteRune('-')
str.WriteString(strconv.FormatBool(v))
case int:
str.WriteRune('-')
str.WriteString(strconv.Itoa(v))
case string:
str.WriteRune('-')
str.WriteString(v)
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/sdkv2/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func TestStringCaseInsensitiveSetFunc(t *testing.T) {
}
}

func TestSimpleSchemaSetFuncNil(t *testing.T) {
t.Parallel()

var v interface{}
f := SimpleSchemaSetFunc("key1", "key3", "key4")

if got, want := f(v), 0; got != want {
t.Errorf("SimpleSchemaSetFunc(%q) err %q, want %q", v, got, want)
}
}

func TestSimpleSchemaSetFunc(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 322215b

Please sign in to comment.