-
Notifications
You must be signed in to change notification settings - Fork 91
/
nested_block_object.go
118 lines (90 loc) · 3.07 KB
/
nested_block_object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package fwschema
import (
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// NestedBlockObject represents the Object inside a Block.
// Refer to the fwxschema package for validation and plan modification
// extensions to this interface.
type NestedBlockObject interface {
tftypes.AttributePathStepper
// Equal should return true if given NestedBlockObject is equivalent.
Equal(NestedBlockObject) bool
// GetAttributes should return the nested attributes of the object.
GetAttributes() UnderlyingAttributes
// GetBlocks should return the nested attributes of the object.
GetBlocks() map[string]Block
// Type should return the framework type of the object.
Type() types.ObjectTypable
}
// NestedBlockObjectApplyTerraform5AttributePathStep is a helper function to
// perform base tftypes.AttributePathStepper handling using the GetAttributes
// and GetBlocks methods. NestedBlockObject implementations should still
// include custom type functionality in addition to using this helper.
func NestedBlockObjectApplyTerraform5AttributePathStep(o NestedBlockObject, step tftypes.AttributePathStep) (any, error) {
name, ok := step.(tftypes.AttributeName)
if !ok {
return nil, fmt.Errorf("cannot apply AttributePathStep %T to NestedBlockObject", step)
}
attribute, ok := o.GetAttributes()[string(name)]
if ok {
return attribute, nil
}
block, ok := o.GetBlocks()[string(name)]
if ok {
return block, nil
}
return nil, fmt.Errorf("no attribute or block %q on NestedBlockObject", name)
}
// NestedBlockObjectEqual is a helper function to perform base equality testing
// on two NestedBlockObject. NestedBlockObject implementations should still
// compare the concrete types and other custom functionality in addition to
// using this helper.
func NestedBlockObjectEqual(a, b NestedBlockObject) bool {
if !a.Type().Equal(b.Type()) {
return false
}
if len(a.GetAttributes()) != len(b.GetAttributes()) {
return false
}
for name, aAttribute := range a.GetAttributes() {
bAttribute, ok := b.GetAttributes()[name]
if !ok {
return false
}
if !aAttribute.Equal(bAttribute) {
return false
}
}
if len(a.GetBlocks()) != len(b.GetBlocks()) {
return false
}
for name, aBlock := range a.GetBlocks() {
bBlock, ok := b.GetBlocks()[name]
if !ok {
return false
}
if !aBlock.Equal(bBlock) {
return false
}
}
return true
}
// NestedBlockObjectType is a helper function to perform base type handling
// using the GetAttributes and GetBlocks methods. NestedBlockObject
// implementations should still include custom type functionality in addition
// to using this helper.
func NestedBlockObjectType(o NestedBlockObject) types.ObjectTypable {
attrTypes := make(map[string]attr.Type, len(o.GetAttributes())+len(o.GetBlocks()))
for name, attribute := range o.GetAttributes() {
attrTypes[name] = attribute.GetType()
}
for name, block := range o.GetBlocks() {
attrTypes[name] = block.Type()
}
return types.ObjectType{
AttrTypes: attrTypes,
}
}