From f94f36783c0a9453e2cf8aa1da97bd5871059a5e Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 6 Apr 2023 12:44:16 -0400 Subject: [PATCH] Relax boolean conversions to accept varying cases (#7637) --- tpgtools/handwritten/expanders.go | 27 ++++++---- tpgtools/handwritten/expanders_test.go | 74 ++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 tpgtools/handwritten/expanders_test.go diff --git a/tpgtools/handwritten/expanders.go b/tpgtools/handwritten/expanders.go index 43b113a8f481..3f47248811a9 100644 --- a/tpgtools/handwritten/expanders.go +++ b/tpgtools/handwritten/expanders.go @@ -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) @@ -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 } diff --git a/tpgtools/handwritten/expanders_test.go b/tpgtools/handwritten/expanders_test.go new file mode 100644 index 000000000000..e3170fe2b6d2 --- /dev/null +++ b/tpgtools/handwritten/expanders_test.go @@ -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) + } + }) + } +}