/
nested_attribute_object.go
90 lines (70 loc) · 2.72 KB
/
nested_attribute_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
package fwschema
import (
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// NestedAttributeObject represents the Object inside a NestedAttribute.
// Refer to the fwxschema package for validation and plan modification
// extensions to this interface.
type NestedAttributeObject interface {
tftypes.AttributePathStepper
// Equal should return true if given NestedAttributeObject is equivalent.
Equal(NestedAttributeObject) bool
// GetAttributes should return the nested attributes of an attribute.
GetAttributes() UnderlyingAttributes
// Type should return the framework type of the object.
Type() basetypes.ObjectTypable
}
// NestedAttributeObjectApplyTerraform5AttributePathStep is a helper function
// to perform base tftypes.AttributePathStepper handling using the
// GetAttributes method. NestedAttributeObject implementations should still
// include custom type functionality in addition to using this helper.
func NestedAttributeObjectApplyTerraform5AttributePathStep(o NestedAttributeObject, step tftypes.AttributePathStep) (any, error) {
name, ok := step.(tftypes.AttributeName)
if !ok {
return nil, fmt.Errorf("cannot apply AttributePathStep %T to NestedAttributeObject", step)
}
attribute, ok := o.GetAttributes()[string(name)]
if ok {
return attribute, nil
}
return nil, fmt.Errorf("no attribute %q on NestedAttributeObject", name)
}
// NestedAttributeObjectEqual is a helper function to perform base equality testing
// on two NestedAttributeObject. NestedAttributeObject implementations should still
// compare the concrete types and other custom functionality in addition to
// using this helper.
func NestedAttributeObjectEqual(a, b NestedAttributeObject) 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
}
}
return true
}
// NestedAttributeObjectType is a helper function to perform base type handling
// using the GetAttributes and GetBlocks methods. NestedAttributeObject
// implementations should still include custom type functionality in addition
// to using this helper.
func NestedAttributeObjectType(o NestedAttributeObject) basetypes.ObjectTypable {
attrTypes := make(map[string]attr.Type, len(o.GetAttributes()))
for name, attribute := range o.GetAttributes() {
attrTypes[name] = attribute.GetType()
}
return types.ObjectType{
AttrTypes: attrTypes,
}
}