Skip to content

Commit

Permalink
SimpleSchemaSetFunc: Fix 'panic: interface conversion: interface {} i…
Browse files Browse the repository at this point in the history
…s nil, not map[string]interface {}'.
  • Loading branch information
ewbankkit committed Apr 17, 2024
1 parent 9e31aac commit ab7f5e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
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 ab7f5e5

Please sign in to comment.