This repository has been archived by the owner on Dec 30, 2024. It is now read-only.
generated from FrangipaneTeam/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_type.go
191 lines (152 loc) · 4.51 KB
/
map_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.MapTypable = MapType{}
var _ basetypes.MapTypable = MapTypeOf[struct{}]{}
// * MapType
type MapType struct {
basetypes.MapType
}
func (t MapType) Equal(o attr.Type) bool {
switch o.(type) {
case MapType:
other, ok := o.(MapType)
if !ok {
return false
}
return t.MapType.Equal(other.MapType)
case basetypes.ObjectType:
other, ok := o.(basetypes.ObjectType)
if !ok {
return false
}
return t.MapType.Equal(other)
default:
return false
}
}
func (t MapType) String() string {
return "supertypes.MapType[" + t.ElementType().String() + "]"
}
func (t MapType) ValueFromMap(_ context.Context, in basetypes.MapValue) (basetypes.MapValuable, diag.Diagnostics) {
value := MapValue{
MapValue: in,
}
return value, nil
}
func (t MapType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.MapType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
MapValue, ok := attrValue.(basetypes.MapValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
MapValuable, diags := t.ValueFromMap(ctx, MapValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting MapValue to MapValuable: %v", diags)
}
return MapValuable, nil
}
func (t MapType) TerraformType(ctx context.Context) tftypes.Type {
return tftypes.Map{
ElementType: t.ElementType().TerraformType(ctx),
}
}
func (t MapType) ElementType() attr.Type {
if t.MapType.ElemType == nil {
return missingType{}
}
return t.MapType.ElemType
}
func (t MapType) ValueType(ctx context.Context) attr.Value {
return MapValue{
MapValue: t.MapType.ValueType(ctx).(basetypes.MapValue),
}
}
// * -----------------
// * MapTypeOf is a type that implements MapTypable for a specific type.
type MapTypeOf[T any] struct {
basetypes.MapType
}
func NewMapTypeOf[T any](ctx context.Context) MapTypeOf[T] {
return MapTypeOf[T]{MapType: basetypes.MapType{ElemType: ElementTypeMust[T](ctx)}}
}
// Equal returns true if the given type is equal to this type.
func (t MapTypeOf[T]) Equal(o attr.Type) bool {
switch o.(type) {
case MapTypeOf[T]:
other, ok := o.(MapTypeOf[T])
if !ok {
return false
}
return t.MapType.Equal(other.MapType)
case basetypes.MapType:
other, ok := o.(basetypes.MapType)
if !ok {
return false
}
return t.MapType.Equal(other)
default:
return false
}
}
// ValueFromMap converts a basetypes.MapValue to a MapValueOf.
func (t MapTypeOf[T]) ValueFromMap(ctx context.Context, in basetypes.MapValue) (basetypes.MapValuable, diag.Diagnostics) {
var diags diag.Diagnostics
if in.IsNull() {
return NewMapValueOfNull[T](ctx), diags
}
if in.IsUnknown() {
return NewMapValueOfUnknown[T](ctx), diags
}
v, d := basetypes.NewMapValue(ElementTypeMust[T](ctx), in.Elements())
diags.Append(d...)
if diags.HasError() {
return NewMapValueOfUnknown[T](ctx), diags
}
value := MapValueOf[T]{
MapValue: v,
}
return value, diags
}
// String returns a string representation of the type.
func (t MapTypeOf[T]) String() string {
var zero T
return fmt.Sprintf("supertypes.MapTypeOf[%T]", zero)
}
// ValueFromTerraform converts a tftypes.Value to a MapValueOf.
func (t MapTypeOf[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.MapType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
MapValue, ok := attrValue.(basetypes.MapValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
MapValuable, diags := t.ValueFromMap(ctx, MapValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting MapValue to MapValuable: %v", diags)
}
return MapValuable, nil
}
// ValueType returns the value type of this Map.
func (t MapTypeOf[T]) ValueType(ctx context.Context) attr.Value {
return MapValueOf[T]{}
}
// ElementType returns the element type of this Map.
func (t MapTypeOf[T]) ElementType() attr.Type {
return ElementTypeMust[T](context.Background())
}