Skip to content

Commit 772099c

Browse files
ewbankkitappilon
authored andcommitted
Convert 'MapValueLenBetween' to use diag.Diagnostics.
1 parent 367bd3e commit 772099c

File tree

2 files changed

+66
-35
lines changed

2 files changed

+66
-35
lines changed

helper/validation/map.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111
)
1212

13-
// MapKeyLenBetween returns a SchemaValidateFunc which tests if the provided value
13+
// MapKeyLenBetween returns a SchemaValidateDiagFunc which tests if the provided value
1414
// is of type map and the length of all keys are between min and max (inclusive)
1515
func MapKeyLenBetween(min, max int) schema.SchemaValidateDiagFunc {
1616
return func(v interface{}, path cty.Path) diag.Diagnostics {
@@ -21,8 +21,8 @@ func MapKeyLenBetween(min, max int) schema.SchemaValidateDiagFunc {
2121
if len < min || len > max {
2222
diags = append(diags, diag.Diagnostic{
2323
Severity: diag.Error,
24-
Summary: "Bad key length",
25-
Detail: fmt.Sprintf("Key length should be in the range (%d - %d): %s (length = %d)", min, max, key, len),
24+
Summary: "Bad map key length",
25+
Detail: fmt.Sprintf("Map key lengths should be in the range (%d - %d): %s (length = %d)", min, max, key, len),
2626
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal(key)}),
2727
})
2828
}
@@ -32,32 +32,39 @@ func MapKeyLenBetween(min, max int) schema.SchemaValidateDiagFunc {
3232
}
3333
}
3434

35-
// MapValueLenBetween returns a SchemaValidateFunc which tests if the provided value
35+
// MapValueLenBetween returns a SchemaValidateDiagFunc which tests if the provided value
3636
// is of type map and the length of all values are between min and max (inclusive)
37-
func MapValueLenBetween(min, max int) schema.SchemaValidateFunc {
38-
return func(i interface{}, k string) (warnings []string, errors []error) {
39-
v, ok := i.(map[string]interface{})
40-
if !ok {
41-
errors = append(errors, fmt.Errorf("expected type of %[1]q to be Map, got %[1]T", k))
42-
return warnings, errors
43-
}
37+
func MapValueLenBetween(min, max int) schema.SchemaValidateDiagFunc {
38+
return func(v interface{}, path cty.Path) diag.Diagnostics {
39+
var diags diag.Diagnostics
40+
41+
m := v.(map[string]interface{})
42+
43+
for _, key := range sortedKeys(m) {
44+
val := m[key]
4445

45-
for _, val := range v {
4646
if _, ok := val.(string); !ok {
47-
errors = append(errors, fmt.Errorf("expected all values of %[1]q to be strings, found %[2]v (type = %[2]T)", k, val))
48-
return warnings, errors
47+
diags = append(diags, diag.Diagnostic{
48+
Severity: diag.Error,
49+
Summary: "Bad map value type",
50+
Detail: fmt.Sprintf("Map values should be strings: %s => %v (type = %T)", key, val, val),
51+
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal(key)}),
52+
})
53+
continue
4954
}
50-
}
5155

52-
for _, val := range v {
5356
len := len(val.(string))
5457
if len < min || len > max {
55-
errors = append(errors, fmt.Errorf("expected the length of all values of %q to be in the range (%d - %d), got %q (length = %d)", k, min, max, val, len))
56-
return warnings, errors
58+
diags = append(diags, diag.Diagnostic{
59+
Severity: diag.Error,
60+
Summary: "Bad map value length",
61+
Detail: fmt.Sprintf("Map value lengths should be in the range (%d - %d): %s => %v (length = %d)", min, max, key, val, len),
62+
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal(key)}),
63+
})
5764
}
5865
}
5966

60-
return warnings, errors
67+
return diags
6168
}
6269
}
6370

helper/validation/map_test.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,54 +76,78 @@ func TestValidationMapKeyLenBetween(t *testing.T) {
7676

7777
func TestValidationMapValueLenBetween(t *testing.T) {
7878
cases := map[string]struct {
79-
Value interface{}
80-
Error bool
79+
Value interface{}
80+
ExpectedDiags diag.Diagnostics
8181
}{
82-
"NotMap": {
83-
Value: "the map is a lie",
84-
Error: true,
85-
},
8682
"NotStringValue": {
8783
Value: map[string]interface{}{
8884
"ABC": "123",
8985
"UVWXYZ": 123456,
9086
},
91-
Error: true,
87+
ExpectedDiags: diag.Diagnostics{
88+
{
89+
Severity: diag.Error,
90+
AttributePath: append(cty.Path{}, cty.IndexStep{Key: cty.StringVal("UVWXYZ")}),
91+
},
92+
},
9293
},
9394
"TooLong": {
9495
Value: map[string]interface{}{
9596
"ABC": "123",
9697
"UVWXYZ": "123456",
9798
},
98-
Error: true,
99+
ExpectedDiags: diag.Diagnostics{
100+
{
101+
Severity: diag.Error,
102+
AttributePath: append(cty.Path{}, cty.IndexStep{Key: cty.StringVal("UVWXYZ")}),
103+
},
104+
},
99105
},
100106
"TooShort": {
101107
Value: map[string]interface{}{
102108
"ABC": "123",
103109
"U": "1",
104110
},
105-
Error: true,
111+
ExpectedDiags: diag.Diagnostics{
112+
{
113+
Severity: diag.Error,
114+
AttributePath: append(cty.Path{}, cty.IndexStep{Key: cty.StringVal("U")}),
115+
},
116+
},
117+
},
118+
"TooLongAndTooShort": {
119+
Value: map[string]interface{}{
120+
"UVWXYZ": "123456",
121+
"ABC": "123",
122+
"U": "1",
123+
},
124+
ExpectedDiags: diag.Diagnostics{
125+
{
126+
Severity: diag.Error,
127+
AttributePath: append(cty.Path{}, cty.IndexStep{Key: cty.StringVal("U")}),
128+
},
129+
{
130+
Severity: diag.Error,
131+
AttributePath: append(cty.Path{}, cty.IndexStep{Key: cty.StringVal("UVWXYZ")}),
132+
},
133+
},
106134
},
107135
"AllGood": {
108136
Value: map[string]interface{}{
109137
"AB": "12",
110138
"UVWXY": "12345",
111139
},
112-
Error: false,
140+
ExpectedDiags: nil,
113141
},
114142
}
115143

116144
fn := MapValueLenBetween(2, 5)
117145

118146
for tn, tc := range cases {
119147
t.Run(tn, func(t *testing.T) {
120-
_, errors := fn(tc.Value, tn)
148+
diags := fn(tc.Value, cty.Path{})
121149

122-
if len(errors) > 0 && !tc.Error {
123-
t.Errorf("MapValueLenBetween(%s) produced an unexpected error", tc.Value)
124-
} else if len(errors) == 0 && tc.Error {
125-
t.Errorf("MapValueLenBetween(%s) did not error", tc.Value)
126-
}
150+
checkDiagnostics(t, tn, diags, tc.ExpectedDiags)
127151
})
128152
}
129153
}

0 commit comments

Comments
 (0)