generated from FrangipaneTeam/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_type.go
191 lines (152 loc) · 4.51 KB
/
set_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// -------------------------------------------------------------------- //
// ! DO NOT EDIT. This file is auto-generated from template ! //
// -------------------------------------------------------------------- //
package supertypes
import (
"context"
"fmt"
"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.SetTypable = SetType{}
var _ basetypes.SetTypable = SetTypeOf[struct{}]{}
// * SetType
type SetType struct {
basetypes.SetType
}
func (t SetType) Equal(o attr.Type) bool {
switch o.(type) {
case SetType:
other, ok := o.(SetType)
if !ok {
return false
}
return t.SetType.Equal(other.SetType)
case basetypes.ObjectType:
other, ok := o.(basetypes.ObjectType)
if !ok {
return false
}
return t.SetType.Equal(other)
default:
return false
}
}
func (t SetType) String() string {
return "supertypes.SetType[" + t.ElementType().String() + "]"
}
func (t SetType) ValueFromSet(_ context.Context, in basetypes.SetValue) (basetypes.SetValuable, diag.Diagnostics) {
value := SetValue{
SetValue: in,
}
return value, nil
}
func (t SetType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.SetType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
SetValue, ok := attrValue.(basetypes.SetValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
SetValuable, diags := t.ValueFromSet(ctx, SetValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting SetValue to SetValuable: %v", diags)
}
return SetValuable, nil
}
func (t SetType) TerraformType(ctx context.Context) tftypes.Type {
return tftypes.Set{
ElementType: t.ElementType().TerraformType(ctx),
}
}
func (t SetType) ElementType() attr.Type {
if t.SetType.ElemType == nil {
return missingType{}
}
return t.SetType.ElemType
}
func (t SetType) ValueType(ctx context.Context) attr.Value {
return SetValue{
SetValue: t.SetType.ValueType(ctx).(basetypes.SetValue),
}
}
// * -----------------
// * SetTypeOf is a type that implements SetTypable for a specific type.
type SetTypeOf[T any] struct {
basetypes.SetType
}
func NewSetTypeOf[T any](ctx context.Context) SetTypeOf[T] {
return SetTypeOf[T]{SetType: basetypes.SetType{ElemType: ElementTypeMust[T](ctx)}}
}
// Equal returns true if the given type is equal to this type.
func (t SetTypeOf[T]) Equal(o attr.Type) bool {
switch o.(type) {
case SetTypeOf[T]:
other, ok := o.(SetTypeOf[T])
if !ok {
return false
}
return t.SetType.Equal(other.SetType)
case basetypes.SetType:
other, ok := o.(basetypes.SetType)
if !ok {
return false
}
return t.SetType.Equal(other)
default:
return false
}
}
// ValueFromSet converts a basetypes.SetValue to a SetValueOf.
func (t SetTypeOf[T]) ValueFromSet(ctx context.Context, in basetypes.SetValue) (basetypes.SetValuable, diag.Diagnostics) {
var diags diag.Diagnostics
if in.IsNull() {
return NewSetValueOfNull[T](ctx), diags
}
if in.IsUnknown() {
return NewSetValueOfUnknown[T](ctx), diags
}
v, d := basetypes.NewSetValue(ElementTypeMust[T](ctx), in.Elements())
diags.Append(d...)
if diags.HasError() {
return NewSetValueOfUnknown[T](ctx), diags
}
value := SetValueOf[T]{
SetValue: v,
}
return value, diags
}
// String returns a string representation of the type.
func (t SetTypeOf[T]) String() string {
var zero T
return fmt.Sprintf("supertypes.SetTypeOf[%T]", zero)
}
// ValueFromTerraform converts a tftypes.Value to a SetValueOf.
func (t SetTypeOf[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.SetType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
SetValue, ok := attrValue.(basetypes.SetValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
SetValuable, diags := t.ValueFromSet(ctx, SetValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting SetValue to SetValuable: %v", diags)
}
return SetValuable, nil
}
// ValueType returns the value type of this Set.
func (t SetTypeOf[T]) ValueType(ctx context.Context) attr.Value {
return SetValueOf[T]{}
}
// ElementType returns the element type of this Set.
func (t SetTypeOf[T]) ElementType() attr.Type {
return ElementTypeMust[T](context.Background())
}