Skip to content

Commit de23465

Browse files
committed
Switching to using native framework path for AtLeastSumOf, AtMostSumOf and EqualToSumOf validators (#20)
1 parent c33ee2e commit de23465

6 files changed

+126
-123
lines changed

int64validator/at_least_sum_of.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/hashicorp/terraform-plugin-framework/path"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
10-
"github.com/hashicorp/terraform-plugin-go/tftypes"
1111

12-
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
12+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
1313
)
1414

1515
var _ tfsdk.AttributeValidator = atLeastSumOfValidator{}
1616

1717
// atLeastSumOfValidator validates that an integer Attribute's value is at least the sum of one
1818
// or more integer Attributes.
1919
type atLeastSumOfValidator struct {
20-
attributesToSumPaths []*tftypes.AttributePath
20+
attributesToSumPaths []path.Path
2121
}
2222

2323
// Description describes the validation in plain text formatting.
2424
func (validator atLeastSumOfValidator) Description(_ context.Context) string {
2525
var attributePaths []string
26-
for _, path := range validator.attributesToSumPaths {
27-
attributePaths = append(attributePaths, path.String())
26+
for _, p := range validator.attributesToSumPaths {
27+
attributePaths = append(attributePaths, p.String())
2828
}
2929

3030
return fmt.Sprintf("value must be at least sum of %s", strings.Join(attributePaths, " + "))
@@ -46,10 +46,10 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
4646
var sumOfAttribs int64
4747
var numUnknownAttribsToSum int
4848

49-
for _, path := range validator.attributesToSumPaths {
49+
for _, p := range validator.attributesToSumPaths {
5050
var attribToSum types.Int64
5151

52-
response.Diagnostics.Append(request.Config.GetAttribute(ctx, path, &attribToSum)...)
52+
response.Diagnostics.Append(request.Config.GetAttribute(ctx, p, &attribToSum)...)
5353
if response.Diagnostics.HasError() {
5454
return
5555
}
@@ -72,7 +72,7 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
7272

7373
if i < sumOfAttribs {
7474

75-
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
75+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
7676
request.AttributePath,
7777
validator.Description(ctx),
7878
fmt.Sprintf("%d", i),
@@ -89,7 +89,7 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
8989
// - Is exclusively at least the sum of the given attributes.
9090
//
9191
// Null (unconfigured) and unknown (known after apply) values are skipped.
92-
func AtLeastSumOf(attributesToSum ...*tftypes.AttributePath) tfsdk.AttributeValidator {
92+
func AtLeastSumOf(attributesToSum ...path.Path) tfsdk.AttributeValidator {
9393
return atLeastSumOfValidator{
9494
attributesToSumPaths: attributesToSum,
9595
}

int64validator/at_least_sum_of_test.go

+33-32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/hashicorp/terraform-plugin-framework/attr"
8+
"github.com/hashicorp/terraform-plugin-framework/path"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
1011
"github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -15,7 +16,7 @@ func TestAtLeastSumOfValidator(t *testing.T) {
1516

1617
type testCase struct {
1718
val attr.Value
18-
attributesToSumPaths []*tftypes.AttributePath
19+
attributesToSumPaths []path.Path
1920
requestConfigRaw map[string]tftypes.Value
2021
expectError bool
2122
}
@@ -32,9 +33,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
3233
},
3334
"valid integer as Int64 less than sum of attributes": {
3435
val: types.Int64{Value: 10},
35-
attributesToSumPaths: []*tftypes.AttributePath{
36-
tftypes.NewAttributePath().WithAttributeName("one"),
37-
tftypes.NewAttributePath().WithAttributeName("two"),
36+
attributesToSumPaths: []path.Path{
37+
path.Root("one"),
38+
path.Root("two"),
3839
},
3940
requestConfigRaw: map[string]tftypes.Value{
4041
"one": tftypes.NewValue(tftypes.Number, 15),
@@ -44,9 +45,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
4445
},
4546
"valid integer as Int64 equal to sum of attributes": {
4647
val: types.Int64{Value: 10},
47-
attributesToSumPaths: []*tftypes.AttributePath{
48-
tftypes.NewAttributePath().WithAttributeName("one"),
49-
tftypes.NewAttributePath().WithAttributeName("two"),
48+
attributesToSumPaths: []path.Path{
49+
path.Root("one"),
50+
path.Root("two"),
5051
},
5152
requestConfigRaw: map[string]tftypes.Value{
5253
"one": tftypes.NewValue(tftypes.Number, 5),
@@ -55,9 +56,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
5556
},
5657
"valid integer as Int64 greater than sum of attributes": {
5758
val: types.Int64{Value: 10},
58-
attributesToSumPaths: []*tftypes.AttributePath{
59-
tftypes.NewAttributePath().WithAttributeName("one"),
60-
tftypes.NewAttributePath().WithAttributeName("two"),
59+
attributesToSumPaths: []path.Path{
60+
path.Root("one"),
61+
path.Root("two"),
6162
},
6263
requestConfigRaw: map[string]tftypes.Value{
6364
"one": tftypes.NewValue(tftypes.Number, 4),
@@ -66,9 +67,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
6667
},
6768
"valid integer as Int64 greater than sum of attributes, when one summed attribute is null": {
6869
val: types.Int64{Value: 10},
69-
attributesToSumPaths: []*tftypes.AttributePath{
70-
tftypes.NewAttributePath().WithAttributeName("one"),
71-
tftypes.NewAttributePath().WithAttributeName("two"),
70+
attributesToSumPaths: []path.Path{
71+
path.Root("one"),
72+
path.Root("two"),
7273
},
7374
requestConfigRaw: map[string]tftypes.Value{
7475
"one": tftypes.NewValue(tftypes.Number, nil),
@@ -77,9 +78,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
7778
},
7879
"valid integer as Int64 does not return error when all attributes are null": {
7980
val: types.Int64{Null: true},
80-
attributesToSumPaths: []*tftypes.AttributePath{
81-
tftypes.NewAttributePath().WithAttributeName("one"),
82-
tftypes.NewAttributePath().WithAttributeName("two"),
81+
attributesToSumPaths: []path.Path{
82+
path.Root("one"),
83+
path.Root("two"),
8384
},
8485
requestConfigRaw: map[string]tftypes.Value{
8586
"one": tftypes.NewValue(tftypes.Number, nil),
@@ -88,9 +89,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
8889
},
8990
"valid integer as Int64 returns error when all attributes to sum are null": {
9091
val: types.Int64{Value: -1},
91-
attributesToSumPaths: []*tftypes.AttributePath{
92-
tftypes.NewAttributePath().WithAttributeName("one"),
93-
tftypes.NewAttributePath().WithAttributeName("two"),
92+
attributesToSumPaths: []path.Path{
93+
path.Root("one"),
94+
path.Root("two"),
9495
},
9596
requestConfigRaw: map[string]tftypes.Value{
9697
"one": tftypes.NewValue(tftypes.Number, nil),
@@ -100,9 +101,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
100101
},
101102
"valid integer as Int64 greater than sum of attributes, when one summed attribute is unknown": {
102103
val: types.Int64{Value: 10},
103-
attributesToSumPaths: []*tftypes.AttributePath{
104-
tftypes.NewAttributePath().WithAttributeName("one"),
105-
tftypes.NewAttributePath().WithAttributeName("two"),
104+
attributesToSumPaths: []path.Path{
105+
path.Root("one"),
106+
path.Root("two"),
106107
},
107108
requestConfigRaw: map[string]tftypes.Value{
108109
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
@@ -111,9 +112,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
111112
},
112113
"valid integer as Int64 does not return error when all attributes are unknown": {
113114
val: types.Int64{Unknown: true},
114-
attributesToSumPaths: []*tftypes.AttributePath{
115-
tftypes.NewAttributePath().WithAttributeName("one"),
116-
tftypes.NewAttributePath().WithAttributeName("two"),
115+
attributesToSumPaths: []path.Path{
116+
path.Root("one"),
117+
path.Root("two"),
117118
},
118119
requestConfigRaw: map[string]tftypes.Value{
119120
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
@@ -122,9 +123,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
122123
},
123124
"valid integer as Int64 does not return error when all attributes to sum are unknown": {
124125
val: types.Int64{Value: -1},
125-
attributesToSumPaths: []*tftypes.AttributePath{
126-
tftypes.NewAttributePath().WithAttributeName("one"),
127-
tftypes.NewAttributePath().WithAttributeName("two"),
126+
attributesToSumPaths: []path.Path{
127+
path.Root("one"),
128+
path.Root("two"),
128129
},
129130
requestConfigRaw: map[string]tftypes.Value{
130131
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
@@ -133,9 +134,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
133134
},
134135
"error when attribute to sum is not Number": {
135136
val: types.Int64{Value: 9},
136-
attributesToSumPaths: []*tftypes.AttributePath{
137-
tftypes.NewAttributePath().WithAttributeName("one"),
138-
tftypes.NewAttributePath().WithAttributeName("two"),
137+
attributesToSumPaths: []path.Path{
138+
path.Root("one"),
139+
path.Root("two"),
139140
},
140141
requestConfigRaw: map[string]tftypes.Value{
141142
"one": tftypes.NewValue(tftypes.Bool, true),
@@ -149,7 +150,7 @@ func TestAtLeastSumOfValidator(t *testing.T) {
149150
name, test := name, test
150151
t.Run(name, func(t *testing.T) {
151152
request := tfsdk.ValidateAttributeRequest{
152-
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
153+
AttributePath: path.Root("test"),
153154
AttributeConfig: test.val,
154155
Config: tfsdk.Config{
155156
Raw: tftypes.NewValue(tftypes.Object{}, test.requestConfigRaw),

int64validator/at_most_sum_of.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/hashicorp/terraform-plugin-framework/path"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
10-
"github.com/hashicorp/terraform-plugin-go/tftypes"
1111

12-
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
12+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
1313
)
1414

1515
var _ tfsdk.AttributeValidator = atMostSumOfValidator{}
1616

1717
// atMostSumOfValidator validates that an integer Attribute's value is at most the sum of one
1818
// or more integer Attributes.
1919
type atMostSumOfValidator struct {
20-
attributesToSumPaths []*tftypes.AttributePath
20+
attributesToSumPaths []path.Path
2121
}
2222

2323
// Description describes the validation in plain text formatting.
2424
func (validator atMostSumOfValidator) Description(_ context.Context) string {
2525
var attributePaths []string
26-
for _, path := range validator.attributesToSumPaths {
27-
attributePaths = append(attributePaths, path.String())
26+
for _, p := range validator.attributesToSumPaths {
27+
attributePaths = append(attributePaths, p.String())
2828
}
2929

3030
return fmt.Sprintf("value must be at most sum of %s", strings.Join(attributePaths, " + "))
@@ -46,10 +46,10 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
4646
var sumOfAttribs int64
4747
var numUnknownAttribsToSum int
4848

49-
for _, path := range validator.attributesToSumPaths {
49+
for _, p := range validator.attributesToSumPaths {
5050
var attribToSum types.Int64
5151

52-
response.Diagnostics.Append(request.Config.GetAttribute(ctx, path, &attribToSum)...)
52+
response.Diagnostics.Append(request.Config.GetAttribute(ctx, p, &attribToSum)...)
5353
if response.Diagnostics.HasError() {
5454
return
5555
}
@@ -72,7 +72,7 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
7272

7373
if i > sumOfAttribs {
7474

75-
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
75+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
7676
request.AttributePath,
7777
validator.Description(ctx),
7878
fmt.Sprintf("%d", i),
@@ -89,7 +89,7 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
8989
// - Is exclusively at most the sum of the given attributes.
9090
//
9191
// Null (unconfigured) and unknown (known after apply) values are skipped.
92-
func AtMostSumOf(attributesToSum ...*tftypes.AttributePath) tfsdk.AttributeValidator {
92+
func AtMostSumOf(attributesToSum ...path.Path) tfsdk.AttributeValidator {
9393
return atMostSumOfValidator{
9494
attributesToSumPaths: attributesToSum,
9595
}

0 commit comments

Comments
 (0)