Skip to content

Commit 1ac220c

Browse files
committed
Checking for summing of unknown attributes and using variadic args (#20)
1 parent 49cdefb commit 1ac220c

6 files changed

+154
-8
lines changed

int64validator/at_least_sum_of.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
4444
}
4545

4646
var sumOfAttribs int64
47+
var numUnknownAttribsToSum int
4748

4849
for _, path := range validator.attributesToSumPaths {
4950
var attribToSum types.Int64
@@ -53,9 +54,22 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
5354
return
5455
}
5556

57+
if attribToSum.Null {
58+
continue
59+
}
60+
61+
if attribToSum.Unknown {
62+
numUnknownAttribsToSum++
63+
continue
64+
}
65+
5666
sumOfAttribs += attribToSum.Value
5767
}
5868

69+
if numUnknownAttribsToSum == len(validator.attributesToSumPaths) {
70+
return
71+
}
72+
5973
if i < sumOfAttribs {
6074

6175
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
@@ -75,7 +89,7 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
7589
// - Is exclusively at least the sum of the given attributes.
7690
//
7791
// Null (unconfigured) and unknown (known after apply) values are skipped.
78-
func AtLeastSumOf(attributesToSum []*tftypes.AttributePath) tfsdk.AttributeValidator {
92+
func AtLeastSumOf(attributesToSum ...*tftypes.AttributePath) tfsdk.AttributeValidator {
7993
return atLeastSumOfValidator{
8094
attributesToSumPaths: attributesToSum,
8195
}

int64validator/at_least_sum_of_test.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ func TestAtLeastSumOfValidator(t *testing.T) {
8686
"two": tftypes.NewValue(tftypes.Number, nil),
8787
},
8888
},
89+
"valid integer as Int64 returns error when all attributes to sum are null": {
90+
val: types.Int64{Value: -1},
91+
attributesToSumPaths: []*tftypes.AttributePath{
92+
tftypes.NewAttributePath().WithAttributeName("one"),
93+
tftypes.NewAttributePath().WithAttributeName("two"),
94+
},
95+
requestConfigRaw: map[string]tftypes.Value{
96+
"one": tftypes.NewValue(tftypes.Number, nil),
97+
"two": tftypes.NewValue(tftypes.Number, nil),
98+
},
99+
expectError: true,
100+
},
89101
"valid integer as Int64 greater than sum of attributes, when one summed attribute is unknown": {
90102
val: types.Int64{Value: 10},
91103
attributesToSumPaths: []*tftypes.AttributePath{
@@ -108,6 +120,29 @@ func TestAtLeastSumOfValidator(t *testing.T) {
108120
"two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
109121
},
110122
},
123+
"valid integer as Int64 does not return error when all attributes to sum are unknown": {
124+
val: types.Int64{Value: -1},
125+
attributesToSumPaths: []*tftypes.AttributePath{
126+
tftypes.NewAttributePath().WithAttributeName("one"),
127+
tftypes.NewAttributePath().WithAttributeName("two"),
128+
},
129+
requestConfigRaw: map[string]tftypes.Value{
130+
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
131+
"two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
132+
},
133+
},
134+
"error when attribute to sum is not Number": {
135+
val: types.Int64{Value: 9},
136+
attributesToSumPaths: []*tftypes.AttributePath{
137+
tftypes.NewAttributePath().WithAttributeName("one"),
138+
tftypes.NewAttributePath().WithAttributeName("two"),
139+
},
140+
requestConfigRaw: map[string]tftypes.Value{
141+
"one": tftypes.NewValue(tftypes.Bool, true),
142+
"two": tftypes.NewValue(tftypes.Number, 9),
143+
},
144+
expectError: true,
145+
},
111146
}
112147

113148
for name, test := range tests {
@@ -130,7 +165,7 @@ func TestAtLeastSumOfValidator(t *testing.T) {
130165

131166
response := tfsdk.ValidateAttributeResponse{}
132167

133-
AtLeastSumOf(test.attributesToSumPaths).Validate(context.Background(), request, &response)
168+
AtLeastSumOf(test.attributesToSumPaths...).Validate(context.Background(), request, &response)
134169

135170
if !response.Diagnostics.HasError() && test.expectError {
136171
t.Fatal("expected error, got no error")

int64validator/at_most_sum_of.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
4444
}
4545

4646
var sumOfAttribs int64
47+
var numUnknownAttribsToSum int
4748

4849
for _, path := range validator.attributesToSumPaths {
4950
var attribToSum types.Int64
@@ -53,9 +54,22 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
5354
return
5455
}
5556

57+
if attribToSum.Null {
58+
continue
59+
}
60+
61+
if attribToSum.Unknown {
62+
numUnknownAttribsToSum++
63+
continue
64+
}
65+
5666
sumOfAttribs += attribToSum.Value
5767
}
5868

69+
if numUnknownAttribsToSum == len(validator.attributesToSumPaths) {
70+
return
71+
}
72+
5973
if i > sumOfAttribs {
6074

6175
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
@@ -75,7 +89,7 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
7589
// - Is exclusively at most the sum of the given attributes.
7690
//
7791
// Null (unconfigured) and unknown (known after apply) values are skipped.
78-
func AtMostSumOf(attributesToSum []*tftypes.AttributePath) tfsdk.AttributeValidator {
92+
func AtMostSumOf(attributesToSum ...*tftypes.AttributePath) tfsdk.AttributeValidator {
7993
return atMostSumOfValidator{
8094
attributesToSumPaths: attributesToSum,
8195
}

int64validator/at_most_sum_of_test.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ func TestAtMostSumOfValidator(t *testing.T) {
8686
"two": tftypes.NewValue(tftypes.Number, nil),
8787
},
8888
},
89+
"valid integer as Int64 returns error when all attributes to sum are null": {
90+
val: types.Int64{Value: 1},
91+
attributesToSumPaths: []*tftypes.AttributePath{
92+
tftypes.NewAttributePath().WithAttributeName("one"),
93+
tftypes.NewAttributePath().WithAttributeName("two"),
94+
},
95+
requestConfigRaw: map[string]tftypes.Value{
96+
"one": tftypes.NewValue(tftypes.Number, nil),
97+
"two": tftypes.NewValue(tftypes.Number, nil),
98+
},
99+
expectError: true,
100+
},
89101
"valid integer as Int64 less than sum of attributes, when one summed attribute is unknown": {
90102
val: types.Int64{Value: 8},
91103
attributesToSumPaths: []*tftypes.AttributePath{
@@ -108,6 +120,29 @@ func TestAtMostSumOfValidator(t *testing.T) {
108120
"two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
109121
},
110122
},
123+
"valid integer as Int64 does not return error when all attributes to sum are unknown": {
124+
val: types.Int64{Value: 1},
125+
attributesToSumPaths: []*tftypes.AttributePath{
126+
tftypes.NewAttributePath().WithAttributeName("one"),
127+
tftypes.NewAttributePath().WithAttributeName("two"),
128+
},
129+
requestConfigRaw: map[string]tftypes.Value{
130+
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
131+
"two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
132+
},
133+
},
134+
"error when attribute to sum is not Number": {
135+
val: types.Int64{Value: 9},
136+
attributesToSumPaths: []*tftypes.AttributePath{
137+
tftypes.NewAttributePath().WithAttributeName("one"),
138+
tftypes.NewAttributePath().WithAttributeName("two"),
139+
},
140+
requestConfigRaw: map[string]tftypes.Value{
141+
"one": tftypes.NewValue(tftypes.Bool, true),
142+
"two": tftypes.NewValue(tftypes.Number, 9),
143+
},
144+
expectError: true,
145+
},
111146
}
112147

113148
for name, test := range tests {
@@ -130,7 +165,7 @@ func TestAtMostSumOfValidator(t *testing.T) {
130165

131166
response := tfsdk.ValidateAttributeResponse{}
132167

133-
AtMostSumOf(test.attributesToSumPaths).Validate(context.Background(), request, &response)
168+
AtMostSumOf(test.attributesToSumPaths...).Validate(context.Background(), request, &response)
134169

135170
if !response.Diagnostics.HasError() && test.expectError {
136171
t.Fatal("expected error, got no error")

int64validator/equal_to_sum_of.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (validator equalToSumOfValidator) Validate(ctx context.Context, request tfs
4444
}
4545

4646
var sumOfAttribs int64
47+
var numUnknownAttribsToSum int
4748

4849
for _, path := range validator.attributesToSumPaths {
4950
var attribToSum types.Int64
@@ -53,11 +54,23 @@ func (validator equalToSumOfValidator) Validate(ctx context.Context, request tfs
5354
return
5455
}
5556

57+
if attribToSum.Null {
58+
continue
59+
}
60+
61+
if attribToSum.Unknown {
62+
numUnknownAttribsToSum++
63+
continue
64+
}
65+
5666
sumOfAttribs += attribToSum.Value
5767
}
5868

59-
if i != sumOfAttribs {
69+
if numUnknownAttribsToSum == len(validator.attributesToSumPaths) {
70+
return
71+
}
6072

73+
if i != sumOfAttribs {
6174
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
6275
request.AttributePath,
6376
validator.Description(ctx),
@@ -72,10 +85,10 @@ func (validator equalToSumOfValidator) Validate(ctx context.Context, request tfs
7285
// attribute value:
7386
//
7487
// - Is a number, which can be represented by a 64-bit integer.
75-
// - Is exclusively equal to the sum of the given attributes.
88+
// - Is equal to the sum of the given attributes.
7689
//
7790
// Null (unconfigured) and unknown (known after apply) values are skipped.
78-
func EqualToSumOf(attributesToSum []*tftypes.AttributePath) tfsdk.AttributeValidator {
91+
func EqualToSumOf(attributesToSum ...*tftypes.AttributePath) tfsdk.AttributeValidator {
7992
return equalToSumOfValidator{
8093
attributesToSumPaths: attributesToSum,
8194
}

int64validator/equal_to_sum_of_test.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ func TestEqualToSumOfValidator(t *testing.T) {
8787
"two": tftypes.NewValue(tftypes.Number, nil),
8888
},
8989
},
90+
"valid integer as Int64 returns error when all attributes to sum are null": {
91+
val: types.Int64{Value: 1},
92+
attributesToSumPaths: []*tftypes.AttributePath{
93+
tftypes.NewAttributePath().WithAttributeName("one"),
94+
tftypes.NewAttributePath().WithAttributeName("two"),
95+
},
96+
requestConfigRaw: map[string]tftypes.Value{
97+
"one": tftypes.NewValue(tftypes.Number, nil),
98+
"two": tftypes.NewValue(tftypes.Number, nil),
99+
},
100+
expectError: true,
101+
},
90102
"valid integer as Int64 equal to sum of attributes, when one summed attribute is unknown": {
91103
val: types.Int64{Value: 8},
92104
attributesToSumPaths: []*tftypes.AttributePath{
@@ -109,6 +121,29 @@ func TestEqualToSumOfValidator(t *testing.T) {
109121
"two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
110122
},
111123
},
124+
"valid integer as Int64 does not return error when all attributes to sum are unknown": {
125+
val: types.Int64{Value: 1},
126+
attributesToSumPaths: []*tftypes.AttributePath{
127+
tftypes.NewAttributePath().WithAttributeName("one"),
128+
tftypes.NewAttributePath().WithAttributeName("two"),
129+
},
130+
requestConfigRaw: map[string]tftypes.Value{
131+
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
132+
"two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
133+
},
134+
},
135+
"error when attribute to sum is not Number": {
136+
val: types.Int64{Value: 9},
137+
attributesToSumPaths: []*tftypes.AttributePath{
138+
tftypes.NewAttributePath().WithAttributeName("one"),
139+
tftypes.NewAttributePath().WithAttributeName("two"),
140+
},
141+
requestConfigRaw: map[string]tftypes.Value{
142+
"one": tftypes.NewValue(tftypes.Bool, true),
143+
"two": tftypes.NewValue(tftypes.Number, 9),
144+
},
145+
expectError: true,
146+
},
112147
}
113148

114149
for name, test := range tests {
@@ -131,7 +166,7 @@ func TestEqualToSumOfValidator(t *testing.T) {
131166

132167
response := tfsdk.ValidateAttributeResponse{}
133168

134-
EqualToSumOf(test.attributesToSumPaths).Validate(context.Background(), request, &response)
169+
EqualToSumOf(test.attributesToSumPaths...).Validate(context.Background(), request, &response)
135170

136171
if !response.Diagnostics.HasError() && test.expectError {
137172
t.Fatal("expected error, got no error")

0 commit comments

Comments
 (0)