Skip to content

Commit b4fda75

Browse files
ewbankkitappilon
authored andcommitted
Convert 'MapKeyLenBetween' to use diag.Diagnostics.
1 parent ac40997 commit b4fda75

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

helper/validation/map.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ import (
44
"fmt"
55
"regexp"
66

7+
"github.com/hashicorp/go-cty/cty"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
810
)
911

1012
// MapKeyLenBetween returns a SchemaValidateFunc which tests if the provided value
1113
// is of type map and the length of all keys are between min and max (inclusive)
12-
func MapKeyLenBetween(min, max int) schema.SchemaValidateFunc {
13-
return func(i interface{}, k string) (warnings []string, errors []error) {
14-
v, ok := i.(map[string]interface{})
15-
if !ok {
16-
errors = append(errors, fmt.Errorf("expected type of %[1]q to be Map, got %[1]T", k))
17-
return warnings, errors
18-
}
19-
20-
for key := range v {
14+
func MapKeyLenBetween(min, max int) schema.SchemaValidateDiagFunc {
15+
return func(v interface{}, path cty.Path) diag.Diagnostics {
16+
for key := range v.(map[string]interface{}) {
2117
len := len(key)
2218
if len < min || len > max {
23-
errors = append(errors, fmt.Errorf("expected the length of all keys of %q to be in the range (%d - %d), got %q (length = %d)", k, min, max, key, len))
24-
return warnings, errors
19+
return diag.Diagnostics{{
20+
Severity: diag.Error,
21+
Summary: fmt.Sprintf("expected the length of all keys to be in the range (%d - %d)", min, max),
22+
Detail: fmt.Sprintf("length = %d", len),
23+
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal(key)}),
24+
}}
2525
}
2626
}
2727

28-
return warnings, errors
28+
return nil
2929
}
3030
}
3131

helper/validation/map_test.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,65 @@ package validation
33
import (
44
"regexp"
55
"testing"
6+
7+
"github.com/hashicorp/go-cty/cty"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
69
)
710

811
func TestValidationMapKeyLenBetween(t *testing.T) {
912
cases := map[string]struct {
10-
Value interface{}
11-
Error bool
13+
Value interface{}
14+
ExpectedDiags diag.Diagnostics
1215
}{
13-
"NotMap": {
14-
Value: "the map is a lie",
15-
Error: true,
16-
},
1716
"TooLong": {
1817
Value: map[string]interface{}{
1918
"ABC": "123",
2019
"UVWXYZ": "123456",
2120
},
22-
Error: true,
21+
ExpectedDiags: diag.Diagnostics{
22+
{
23+
Severity: diag.Error,
24+
AttributePath: append(cty.Path{}, cty.IndexStep{Key: cty.StringVal("UVWXYZ")}),
25+
},
26+
},
2327
},
2428
"TooShort": {
2529
Value: map[string]interface{}{
2630
"ABC": "123",
2731
"U": "1",
2832
},
29-
Error: true,
33+
ExpectedDiags: diag.Diagnostics{
34+
{
35+
Severity: diag.Error,
36+
AttributePath: append(cty.Path{}, cty.IndexStep{Key: cty.StringVal("U")}),
37+
},
38+
},
3039
},
3140
"AllGood": {
3241
Value: map[string]interface{}{
3342
"AB": "12",
3443
"UVWXY": "12345",
3544
},
36-
Error: false,
45+
ExpectedDiags: nil,
3746
},
3847
}
3948

4049
fn := MapKeyLenBetween(2, 5)
4150

4251
for tn, tc := range cases {
4352
t.Run(tn, func(t *testing.T) {
44-
_, errors := fn(tc.Value, tn)
53+
diags := fn(tc.Value, cty.Path{})
4554

46-
if len(errors) > 0 && !tc.Error {
47-
t.Errorf("MapKeyLenBetween(%s) produced an unexpected error", tc.Value)
48-
} else if len(errors) == 0 && tc.Error {
49-
t.Errorf("MapKeyLenBetween(%s) did not error", tc.Value)
55+
if len(diags) != len(tc.ExpectedDiags) {
56+
t.Fatalf("%s: wrong number of diags, expected %d, got %d", tn, len(tc.ExpectedDiags), len(diags))
57+
}
58+
for j := range diags {
59+
if diags[j].Severity != tc.ExpectedDiags[j].Severity {
60+
t.Fatalf("%s: expected severity %v, got %v", tn, tc.ExpectedDiags[j].Severity, diags[j].Severity)
61+
}
62+
if !diags[j].AttributePath.Equals(tc.ExpectedDiags[j].AttributePath) {
63+
t.Fatalf("%s: attribute paths do not match expected: %v, got %v", tn, tc.ExpectedDiags[j].AttributePath, diags[j].AttributePath)
64+
}
5065
}
5166
})
5267
}

0 commit comments

Comments
 (0)