Skip to content

Commit

Permalink
Relax boolean conversions to accept varying cases (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored and Madhura Phadnis committed Apr 12, 2023
1 parent 9aabdb2 commit f94f367
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
27 changes: 18 additions & 9 deletions tpgtools/handwritten/expanders.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package google

import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func expandStringArray(v interface{}) []string {
arr, ok := v.([]string)
Expand Down Expand Up @@ -53,13 +57,18 @@ func expandEnumBool(v interface{}) *bool {
if !ok {
return nil
}
switch s {
case "TRUE":
b := true
return &b
case "FALSE":
b := false
return &b

switch {
case strings.EqualFold(s, "true"):
return boolPtr(true)
case strings.EqualFold(s, "false"):
return boolPtr(false)
default:
return nil
}
return nil
}

// boolPtr returns a pointer to the given boolean.
func boolPtr(b bool) *bool {
return &b
}
74 changes: 74 additions & 0 deletions tpgtools/handwritten/expanders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package google

import (
"reflect"
"testing"
)

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

cases := []struct {
name string
input string
exp *bool
}{
{
name: "true",
input: "true",
exp: boolPtr(true),
},
{
name: "TRUE",
input: "TRUE",
exp: boolPtr(true),
},
{
name: "True",
input: "True",
exp: boolPtr(true),
},
{
name: "false",
input: "false",
exp: boolPtr(false),
},
{
name: "FALSE",
input: "FALSE",
exp: boolPtr(false),
},
{
name: "False",
input: "False",
exp: boolPtr(false),
},
{
name: "empty_string",
input: "",
exp: nil,
},
{
name: "apple",
input: "apple",
exp: nil,
},
{
name: "unicode",
input: "🚀",
exp: nil,
},
}

for _, tc := range cases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if got, want := expandEnumBool(tc.input), tc.exp; !reflect.DeepEqual(got, want) {
t.Errorf("expected %v to be %v", got, want)
}
})
}
}

0 comments on commit f94f367

Please sign in to comment.