generated from FrangipaneTeam/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
object_type.go
160 lines (125 loc) · 3.84 KB
/
object_type.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// -------------------------------------------------------------------- //
// ! DO NOT EDIT. This file is auto-generated from template ! //
// -------------------------------------------------------------------- //
package supertypes
import (
"context"
"fmt"
"sort"
"strings"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ basetypes.ObjectTypable = ObjectType{}
_ basetypes.ObjectTypable = ObjectTypeOf[struct{}]{}
)
type ObjectType struct {
basetypes.ObjectType
}
// ObjectTypeOf is the attribute type of an ObjectValueOf.
type ObjectTypeOf[T any] struct {
basetypes.ObjectType
}
func (t ObjectType) Equal(o attr.Type) bool {
other, ok := o.(ObjectType)
if !ok {
return false
}
return t.ObjectType.Equal(other.ObjectType)
}
func (t ObjectType) String() string {
var res strings.Builder
res.WriteString("types.ObjectType[")
keys := make([]string, 0, len(t.AttrTypes))
for k := range t.AttrTypes {
keys = append(keys, k)
}
sort.Strings(keys)
for pos, key := range keys {
if pos != 0 {
res.WriteString(", ")
}
res.WriteString(`"` + key + `":`)
res.WriteString(t.AttrTypes[key].String())
}
res.WriteString("]")
return res.String()
}
func (t ObjectType) ValueFromObject(_ context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
value := ObjectValue{
ObjectValue: in,
}
return value, nil
}
func (t ObjectType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.ObjectType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
ObjectValue, ok := attrValue.(basetypes.ObjectValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
ObjectValuable, diags := t.ValueFromObject(ctx, ObjectValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting ObjectValue to ObjectValuable: %v", diags)
}
return ObjectValuable, nil
}
func (t ObjectType) ValueType(_ context.Context) attr.Value {
return ObjectValue{}
}
func NewObjectTypeOf[T any](ctx context.Context) ObjectTypeOf[T] {
return ObjectTypeOf[T]{basetypes.ObjectType{AttrTypes: AttributeTypesMust[T](ctx)}}
}
func (t ObjectTypeOf[T]) Equal(o attr.Type) bool {
other, ok := o.(ObjectTypeOf[T])
if !ok {
return false
}
return t.ObjectType.Equal(other.ObjectType)
}
func (t ObjectTypeOf[T]) String() string {
var zero T
return fmt.Sprintf("ObjectTypeOf[%T]", zero)
}
func (t ObjectTypeOf[T]) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
var diags diag.Diagnostics
if in.IsNull() {
return NewObjectValueOfNull[T](ctx), diags
}
if in.IsUnknown() {
return NewObjectValueOfUnknown[T](ctx), diags
}
objectValue, d := basetypes.NewObjectValue(AttributeTypesMust[T](ctx), in.Attributes())
diags.Append(d...)
if diags.HasError() {
return NewObjectValueOfUnknown[T](ctx), diags
}
value := ObjectValueOf[T]{
ObjectValue: objectValue,
}
return value, diags
}
func (t ObjectTypeOf[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.ObjectType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
objectValue, ok := attrValue.(basetypes.ObjectValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
objectValuable, diags := t.ValueFromObject(ctx, objectValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting ObjectValue to ObjectValuable: %v", diags)
}
return objectValuable, nil
}
func (t ObjectTypeOf[T]) ValueType(ctx context.Context) attr.Value {
return ObjectValueOf[T]{}
}