Skip to content

Commit 4aa117c

Browse files
committed
Refactor write_only_nested_attribute_validation.go and write_only_nested_attribute_validation_test.go
1 parent 1ef9cb2 commit 4aa117c

File tree

6 files changed

+151
-328
lines changed

6 files changed

+151
-328
lines changed

internal/fwtype/write_only_nested_attribute_validation.go renamed to internal/fwschema/write_only_nested_attribute_validation.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,52 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
package fwtype
4+
package fwschema
55

66
import (
77
"fmt"
88

99
"github.com/hashicorp/terraform-plugin-framework/diag"
1010
"github.com/hashicorp/terraform-plugin-framework/path"
11-
"github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
1211
)
1312

1413
// ContainsAllWriteOnlyChildAttributes will return true if all child attributes for the
1514
// given nested attribute have WriteOnly set to true.
16-
func ContainsAllWriteOnlyChildAttributes(nestedAttr metaschema.NestedAttribute) bool {
17-
if !nestedAttr.IsWriteOnly() {
18-
return false
19-
}
15+
func ContainsAllWriteOnlyChildAttributes(nestedAttr NestedAttribute) bool {
2016
nestedObjAttrs := nestedAttr.GetNestedObject().GetAttributes()
2117

2218
for _, childAttr := range nestedObjAttrs {
23-
nestedAttribute, ok := childAttr.(metaschema.NestedAttribute)
19+
if !childAttr.IsWriteOnly() {
20+
return false
21+
}
22+
23+
nestedAttribute, ok := childAttr.(NestedAttribute)
2424
if ok {
2525
if !ContainsAllWriteOnlyChildAttributes(nestedAttribute) {
2626
return false
2727
}
2828
}
29-
30-
if !childAttr.IsWriteOnly() {
31-
return false
32-
}
3329
}
3430

3531
return true
3632
}
3733

3834
// ContainsAnyWriteOnlyChildAttributes will return true if any child attribute for the
3935
// given nested attribute has WriteOnly set to true.
40-
func ContainsAnyWriteOnlyChildAttributes(nestedAttr metaschema.NestedAttribute) bool {
41-
if nestedAttr.IsWriteOnly() {
42-
return true
43-
}
36+
func ContainsAnyWriteOnlyChildAttributes(nestedAttr NestedAttribute) bool {
4437
nestedObjAttrs := nestedAttr.GetNestedObject().GetAttributes()
4538

4639
for _, childAttr := range nestedObjAttrs {
47-
nestedAttribute, ok := childAttr.(metaschema.NestedAttribute)
40+
if childAttr.IsWriteOnly() {
41+
return true
42+
}
43+
44+
nestedAttribute, ok := childAttr.(NestedAttribute)
4845
if ok {
4946
if ContainsAnyWriteOnlyChildAttributes(nestedAttribute) {
5047
return true
5148
}
5249
}
53-
54-
if childAttr.IsWriteOnly() {
55-
return true
56-
}
5750
}
5851

5952
return false

resource/schema/list_nested_attribute.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ func (a ListNestedAttribute) ValidateImplementation(ctx context.Context, req fws
302302
resp.Diagnostics.Append(fwtype.AttributeCollectionWithDynamicTypeDiag(req.Path))
303303
}
304304

305-
if a.IsWriteOnly() && !fwtype.ContainsAllWriteOnlyChildAttributes(a) {
306-
resp.Diagnostics.Append(fwtype.InvalidWriteOnlyNestedAttributeDiag(req.Path))
305+
if a.IsWriteOnly() && !fwschema.ContainsAllWriteOnlyChildAttributes(a) {
306+
resp.Diagnostics.Append(fwschema.InvalidWriteOnlyNestedAttributeDiag(req.Path))
307307
}
308308

309-
if a.IsComputed() && fwtype.ContainsAnyWriteOnlyChildAttributes(a) {
310-
resp.Diagnostics.Append(fwtype.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
309+
if a.IsComputed() && fwschema.ContainsAnyWriteOnlyChildAttributes(a) {
310+
resp.Diagnostics.Append(fwschema.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
311311
}
312312

313313
if a.ListDefaultValue() != nil {

resource/schema/map_nested_attribute.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ func (a MapNestedAttribute) ValidateImplementation(ctx context.Context, req fwsc
302302
resp.Diagnostics.Append(fwtype.AttributeCollectionWithDynamicTypeDiag(req.Path))
303303
}
304304

305-
if a.IsWriteOnly() && !fwtype.ContainsAllWriteOnlyChildAttributes(a) {
306-
resp.Diagnostics.Append(fwtype.InvalidWriteOnlyNestedAttributeDiag(req.Path))
305+
if a.IsWriteOnly() && !fwschema.ContainsAllWriteOnlyChildAttributes(a) {
306+
resp.Diagnostics.Append(fwschema.InvalidWriteOnlyNestedAttributeDiag(req.Path))
307307
}
308308

309-
if a.IsComputed() && fwtype.ContainsAnyWriteOnlyChildAttributes(a) {
310-
resp.Diagnostics.Append(fwtype.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
309+
if a.IsComputed() && fwschema.ContainsAnyWriteOnlyChildAttributes(a) {
310+
resp.Diagnostics.Append(fwschema.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
311311
}
312312

313313
if a.MapDefaultValue() != nil {

resource/schema/set_nested_attribute.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,12 @@ func (a SetNestedAttribute) ValidateImplementation(ctx context.Context, req fwsc
297297
resp.Diagnostics.Append(fwtype.AttributeCollectionWithDynamicTypeDiag(req.Path))
298298
}
299299

300-
if a.IsWriteOnly() && !fwtype.ContainsAllWriteOnlyChildAttributes(a) {
301-
resp.Diagnostics.Append(fwtype.InvalidWriteOnlyNestedAttributeDiag(req.Path))
300+
if a.IsWriteOnly() && !fwschema.ContainsAllWriteOnlyChildAttributes(a) {
301+
resp.Diagnostics.Append(fwschema.InvalidWriteOnlyNestedAttributeDiag(req.Path))
302302
}
303303

304-
if a.IsComputed() && fwtype.ContainsAnyWriteOnlyChildAttributes(a) {
305-
resp.Diagnostics.Append(fwtype.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
304+
if a.IsComputed() && fwschema.ContainsAnyWriteOnlyChildAttributes(a) {
305+
resp.Diagnostics.Append(fwschema.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
306306
}
307307

308308
if a.SetDefaultValue() != nil {

resource/schema/single_nested_attribute.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/attr"
1313
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1414
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
15-
"github.com/hashicorp/terraform-plugin-framework/internal/fwtype"
1615
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
1716
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1817
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -311,12 +310,12 @@ func (a SingleNestedAttribute) ValidateImplementation(ctx context.Context, req f
311310
resp.Diagnostics.Append(nonComputedAttributeWithDefaultDiag(req.Path))
312311
}
313312

314-
if a.IsWriteOnly() && !fwtype.ContainsAllWriteOnlyChildAttributes(a) {
315-
resp.Diagnostics.Append(fwtype.InvalidWriteOnlyNestedAttributeDiag(req.Path))
313+
if a.IsWriteOnly() && !fwschema.ContainsAllWriteOnlyChildAttributes(a) {
314+
resp.Diagnostics.Append(fwschema.InvalidWriteOnlyNestedAttributeDiag(req.Path))
316315
}
317316

318-
if a.IsComputed() && fwtype.ContainsAnyWriteOnlyChildAttributes(a) {
319-
resp.Diagnostics.Append(fwtype.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
317+
if a.IsComputed() && fwschema.ContainsAnyWriteOnlyChildAttributes(a) {
318+
resp.Diagnostics.Append(fwschema.InvalidComputedNestedAttributeWithWriteOnlyDiag(req.Path))
320319
}
321320

322321
if a.ObjectDefaultValue() != nil {

0 commit comments

Comments
 (0)