generated from FrangipaneTeam/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_value.go
186 lines (147 loc) · 5.05 KB
/
map_value.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
// -------------------------------------------------------------------- //
// ! DO NOT EDIT. This file is auto-generated from template ! //
// -------------------------------------------------------------------- //
package supertypes
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
// Ensure the implementation satisfies the expected interfaces.
var _ basetypes.MapValuable = MapValue{}
var _ basetypes.MapValuable = MapValueOf[struct{}]{}
// * MapType
type MapValue struct {
basetypes.MapValue
}
func (v MapValue) Equal(o attr.Value) bool {
other, ok := o.(MapValue)
if !ok {
return false
}
return v.MapValue.Equal(other.MapValue)
}
func (v MapValue) Type(ctx context.Context) attr.Type {
return MapType{
MapType: v.MapValue.Type(ctx).(basetypes.MapType),
}
}
func (v MapValue) ToMapValue(_ context.Context) (basetypes.MapValue, diag.Diagnostics) {
return v.MapValue, nil
}
func NewMapNull(elementType attr.Type) MapValue {
return MapValue{
MapValue: basetypes.NewMapNull(elementType),
}
}
func NewMapUnknown(elementType attr.Type) MapValue {
return MapValue{
MapValue: basetypes.NewMapUnknown(elementType),
}
}
func NewMapValueMust(elementType attr.Type, elements map[string]attr.Value) MapValue {
return MapValue{
MapValue: basetypes.NewMapValueMust(elementType, elements),
}
}
func NewMapValue(elementType attr.Type, elements map[string]attr.Value) (MapValue, diag.Diagnostics) {
x, d := basetypes.NewMapValue(elementType, elements)
return MapValue{
MapValue: x,
}, d
}
func NewMapValueFrom(ctx context.Context, elementType attr.Type, elements any) (MapValue, diag.Diagnostics) {
x, d := basetypes.NewMapValueFrom(ctx, elementType, elements)
return MapValue{
MapValue: x,
}, d
}
// * CustomFunc
func (v *MapValue) Get(ctx context.Context, target interface{}, allowUnhandled bool) (diag diag.Diagnostics) {
return v.MapValue.ElementsAs(ctx, target, allowUnhandled)
}
func (v *MapValue) Set(ctx context.Context, elements any) diag.Diagnostics {
var d diag.Diagnostics
v.MapValue, d = types.MapValueFrom(ctx, v.ElementType(ctx), elements)
return d
}
func (v *MapValue) SetNull(ctx context.Context) {
v.MapValue = basetypes.NewMapNull(v.ElementType(ctx))
}
func (v *MapValue) SetUnknown(ctx context.Context) {
v.MapValue = basetypes.NewMapUnknown(v.ElementType(ctx))
}
func (v MapValue) IsKnown() bool {
return !v.MapValue.IsNull() && !v.MapValue.IsUnknown()
}
// * MapTypeOf
type MapValueOf[T any] struct {
basetypes.MapValue
}
// ToMapValue converts the given value to a MapValue.
func (v MapValueOf[T]) ToMapValue(_ context.Context) (basetypes.MapValue, diag.Diagnostics) {
return v.MapValue, nil
}
// Equal returns true if the given value is equal to this value.
func (v MapValueOf[T]) Equal(o attr.Value) bool {
other, ok := o.(MapValueOf[T])
if !ok {
return false
}
return v.MapValue.Equal(other.MapValue)
}
// Type returns the type of this value.
func (v MapValueOf[T]) Type(ctx context.Context) attr.Type {
return NewMapTypeOf[T](ctx)
}
// Get returns a MapValueOf from the given value.
func (v MapValueOf[T]) Get(ctx context.Context) (values map[string]T, diags diag.Diagnostics) {
values = make(map[string]T, len(v.MapValue.Elements()))
diags.Append(v.MapValue.ElementsAs(ctx, &values, false)...)
return
}
// Set sets the value of this value.
func (v *MapValueOf[T]) Set(ctx context.Context, elements map[string]T) diag.Diagnostics {
var d diag.Diagnostics
v.MapValue, d = types.MapValueFrom(ctx, v.ElementType(ctx), elements)
return d
}
// NewMapValueOfUnknown returns a new MapValueOf with an unknown value.
func NewMapValueOfUnknown[T any](ctx context.Context) MapValueOf[T] {
return MapValueOf[T]{
MapValue: basetypes.NewMapUnknown(ElementTypeMust[T](ctx)),
}
}
// NewMapValueOfNull returns a new MapValueOf with a null value.
func NewMapValueOfNull[T any](ctx context.Context) MapValueOf[T] {
return MapValueOf[T]{
MapValue: basetypes.NewMapNull(ElementTypeMust[T](ctx)),
}
}
// newMapValueOf is a helper function to create a new MapValueOf.
func newMapValueOf[T any](ctx context.Context, elements any) MapValueOf[T] {
return MapValueOf[T]{
MapValue: MustDiag(types.MapValueFrom(ctx, ElementTypeMust[T](ctx), elements)),
}
}
// NewMapValueOfMap returns a new MapValueOf with the given map value.
func NewMapValueOfMap[T any](ctx context.Context, elements map[string]T) (MapValueOf[T], diag.Diagnostics) {
v, d := types.MapValueFrom(ctx, ElementTypeMust[T](ctx), elements)
return MapValueOf[T]{
MapValue: v,
}, d
}
// IsKnown returns true if the value is known.
func (v MapValueOf[T]) IsKnown() bool {
return !v.MapValue.IsNull() && !v.MapValue.IsUnknown()
}
// SetNull sets the value to null.
func (v *MapValueOf[T]) SetNull(ctx context.Context) {
(*v) = NewMapValueOfNull[T](ctx)
}
// SetUnknown sets the value to unknown.
func (v *MapValueOf[T]) SetUnknown(ctx context.Context) {
(*v) = NewMapValueOfUnknown[T](ctx)
}