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

Relax boolean conversions to accept varying cases #14240

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
3 changes: 3 additions & 0 deletions .changelog/7637.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
orgpolicy: accepted variable cases for booleans such as true, True, and TRUE in `google_org_policy_policy`
```
27 changes: 18 additions & 9 deletions google/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 google/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)
}
})
}
}