Skip to content

Commit

Permalink
Merge pull request hashicorp#32090 from hashicorp/td-nullable-bool
Browse files Browse the repository at this point in the history
Deprecate "legacy" behaviour when parsing nullable Bool values
  • Loading branch information
gdavison authored Jun 21, 2023
2 parents b3dc32e + eb96751 commit d7a24ea
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
5 changes: 5 additions & 0 deletions internal/experimental/nullable/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func ValidateTypeStringNullableBool(v interface{}, k string) (ws []string, es []

if _, err := strconv.ParseBool(value); err != nil {
es = append(es, fmt.Errorf("%s: cannot parse '%s' as boolean: %w", k, value, err))
return
}

if value != "true" && value != "false" {
ws = append(ws, fmt.Sprintf(`%s: the use of values other than "true" and "false" is deprecated and will be removed in a future version of the provider`, k))
}

return
Expand Down
9 changes: 5 additions & 4 deletions internal/experimental/nullable/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ func TestValidationBool(t *testing.T) {
f: ValidateTypeStringNullableBool,
},
{
val: "1",
f: ValidateTypeStringNullableBool,
val: "1",
f: ValidateTypeStringNullableBool,
expectedWarning: regexp.MustCompile(`^\w+: the use of values other than "true" and "false" is deprecated and will be removed in a future version of the provider$`),
},
{
val: "A",
f: ValidateTypeStringNullableBool,
expectedErr: regexp.MustCompile(`[\w]+: cannot parse 'A' as boolean`),
expectedErr: regexp.MustCompile(`^\w+: cannot parse 'A' as boolean: .+$`),
},
{
val: 1,
f: ValidateTypeStringNullableBool,
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
expectedErr: regexp.MustCompile(`^expected type of \w+ to be string$`),
},
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/experimental/nullable/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func TestValidationFloat(t *testing.T) {
{
val: "A",
f: ValidateTypeStringNullableFloat,
expectedErr: regexp.MustCompile(`[\w]+: cannot parse 'A' as float: .*`),
expectedErr: regexp.MustCompile(`^\w+: cannot parse 'A' as float: .+$`),
},
{
val: 1,
f: ValidateTypeStringNullableFloat,
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
expectedErr: regexp.MustCompile(`^expected type of \w+ to be string$`),
},
})
}
8 changes: 4 additions & 4 deletions internal/experimental/nullable/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func TestValidationInt(t *testing.T) {
{
val: "A",
f: ValidateTypeStringNullableInt,
expectedErr: regexp.MustCompile(`[\w]+: cannot parse 'A' as int: .*`),
expectedErr: regexp.MustCompile(`^\w+: cannot parse 'A' as int: .*`),
},
{
val: 1,
f: ValidateTypeStringNullableInt,
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
expectedErr: regexp.MustCompile(`^expected type of \w+ to be string`),
},
})
}
Expand All @@ -95,12 +95,12 @@ func TestValidationIntAtLeast(t *testing.T) {
{
val: "1",
f: ValidateTypeStringNullableIntAtLeast(2),
expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(2\), got 1`),
expectedErr: regexp.MustCompile(`expected \w+ to be at least \(2\), got 1`),
},
{
val: 1,
f: ValidateTypeStringNullableIntAtLeast(2),
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
expectedErr: regexp.MustCompile(`expected type of \w+ to be string`),
},
})
}
36 changes: 25 additions & 11 deletions internal/experimental/nullable/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type testCase struct {
val interface{}
f schema.SchemaValidateFunc
expectedErr *regexp.Regexp
val interface{}
f schema.SchemaValidateFunc
expectedErr *regexp.Regexp
expectedWarning *regexp.Regexp
}

func runValidationTestCases(t *testing.T, cases []testCase) {
Expand All @@ -27,19 +28,32 @@ func runValidationTestCases(t *testing.T, cases []testCase) {
return false
}

matchWarning := func(warnings []string, r *regexp.Regexp) bool {
// warning must match one provided
for _, warning := range warnings {
if r.MatchString(warning) {
return true
}
}

return false
}

for i, tc := range cases {
_, errs := tc.f(tc.val, "test_property")
warnings, errs := tc.f(tc.val, "test_property")

if len(errs) == 0 && tc.expectedErr == nil {
continue
if tc.expectedErr == nil && len(errs) != 0 {
t.Errorf("expected test case %d to produce no errors, got %v", i, errs)
}

if len(errs) != 0 && tc.expectedErr == nil {
t.Fatalf("expected test case %d to produce no errors, got %v", i, errs)
if tc.expectedErr != nil && !matchErr(errs, tc.expectedErr) {
t.Errorf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
}

if !matchErr(errs, tc.expectedErr) {
t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
if tc.expectedWarning == nil && len(warnings) != 0 {
t.Errorf("expected test case %d to produce no warnings, got %v", i, warnings)
}
if tc.expectedWarning != nil && !matchWarning(warnings, tc.expectedWarning) {
t.Errorf("expected test case %d to produce warning matching \"%s\", got %v", i, tc.expectedWarning, warnings)
}
}
}

0 comments on commit d7a24ea

Please sign in to comment.