/
underlying_attributes.go
67 lines (50 loc) · 1.53 KB
/
underlying_attributes.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
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"
)
// Ensure UnderlyingAttributes satisfies the expected interfaces.
var _ tftypes.AttributePathStepper = UnderlyingAttributes{}
// UnderlyingAttributes represents attributes under a nested attribute.
type UnderlyingAttributes map[string]Attribute
// ApplyTerraform5AttributePathStep performs an AttributeName step on the
// underlying attributes or returns an error.
func (u UnderlyingAttributes) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (any, error) {
name, ok := step.(tftypes.AttributeName)
if !ok {
return nil, fmt.Errorf("can't apply %T to Attributes", step)
}
attribute, ok := u[string(name)]
if !ok {
return nil, fmt.Errorf("no attribute %q on Attributes", name)
}
return attribute, nil
}
// Equal returns true if all underlying attributes are equal.
func (u UnderlyingAttributes) Equal(o UnderlyingAttributes) bool {
if len(u) != len(o) {
return false
}
for name, uAttribute := range u {
oAttribute, ok := o[name]
if !ok {
return false
}
if !uAttribute.Equal(oAttribute) {
return false
}
}
return true
}
// Type returns the framework type of the underlying attributes.
func (u UnderlyingAttributes) Type() attr.Type {
attrTypes := make(map[string]attr.Type, len(u))
for name, attr := range u {
attrTypes[name] = attr.GetType()
}
return types.ObjectType{
AttrTypes: attrTypes,
}
}