/
terraform_plugin_framework_value_creation.go
148 lines (131 loc) · 4.45 KB
/
terraform_plugin_framework_value_creation.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
package utils
import (
"context"
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"reflect"
)
// StringValueOrNull returns a types.String based on the supplied string. If the
// supplied string is empty, the returned types.String will be flagged as null.
func StringValueOrNull(_ context.Context, in string, _ *diag.Diagnostics) types.String {
if in == "" {
return types.StringNull()
}
return types.StringValue(in)
}
// StringValueWithNull returns a types.String based on the supplied inStr. If
// inStr matches nullStr or is empty, the returned types.String will be flagged
// as null.
func StringValueWithNull(ctx context.Context, inStr string, nullStr string, diags *diag.Diagnostics) types.String {
if inStr == nullStr {
return types.StringNull()
}
return StringValueOrNull(ctx, inStr, diags)
}
// MapValueOrNull returns a types.Map based on the supplied elements. If the
// supplied elements is empty, the returned types.Map will be flagged as null.
func MapValueOrNull[T any](ctx context.Context, elementType attr.Type, elements map[string]T, diags *diag.Diagnostics) types.Map {
if len(elements) == 0 {
return types.MapNull(elementType)
}
result, d := types.MapValueFrom(ctx, elementType, elements)
diags.Append(d...)
return result
}
// ListValueOrNull returns a types.List based on the supplied elements. If the
// supplied elements is empty, the returned types.List will be flagged as null.
func ListValueOrNull[T any](ctx context.Context, elementType attr.Type, elements []T, diags *diag.Diagnostics) types.List {
if len(elements) == 0 {
return types.ListNull(elementType)
}
result, d := types.ListValueFrom(ctx, elementType, elements)
diags.Append(d...)
return result
}
// SetValueOrNull returns a types.Set based on the supplied elements. If the
// supplied elements is empty, the returned types.Set will be flagged as null.
func SetValueOrNull[T any](ctx context.Context, elementType attr.Type, elements []T, diags *diag.Diagnostics) types.Set {
if len(elements) == 0 {
return types.SetNull(elementType)
}
result, d := types.SetValueFrom(ctx, elementType, elements)
diags.Append(d...)
return result
}
// ObjectValueOrNull returns a types.Object based on the supplied attributes. If the
// supplied attributes is nil, the returned types.Object will be flagged as null.
func ObjectValueOrNull(ctx context.Context, attrTypes map[string]attr.Type, attributes any, diags *diag.Diagnostics) types.Object {
if attributes == nil {
return types.ObjectNull(attrTypes)
}
result, d := types.ObjectValueFrom(ctx, attrTypes, attributes)
diags.Append(d...)
return result
}
func Int64ValueOrNull(_ context.Context, in any, diags *diag.Diagnostics) types.Int64 {
// when in is nil, return a null attr.Value
if in == nil {
return types.Int64Null()
}
// when in is a nil pointer, return a null attr.Value
if reflect.TypeOf(in).Kind() == reflect.Ptr && reflect.ValueOf(in).IsNil() {
return types.Int64Null()
}
switch in := in.(type) {
case *apstra.VNI:
return types.Int64Value(int64(*in))
case *apstra.Vlan:
return types.Int64Value(int64(*in))
case *int:
return types.Int64Value(int64(*in))
case *int8:
return types.Int64Value(int64(*in))
case *int16:
return types.Int64Value(int64(*in))
case *int32:
return types.Int64Value(int64(*in))
case *int64:
return types.Int64Value(*in)
case *uint:
return types.Int64Value(int64(*in))
case *uint8:
return types.Int64Value(int64(*in))
case *uint16:
return types.Int64Value(int64(*in))
case *uint32:
return types.Int64Value(int64(*in))
case *uint64:
return types.Int64Value(int64(*in))
case apstra.VNI:
return types.Int64Value(int64(in))
case apstra.Vlan:
return types.Int64Value(int64(in))
case int:
return types.Int64Value(int64(in))
case int8:
return types.Int64Value(int64(in))
case int16:
return types.Int64Value(int64(in))
case int32:
return types.Int64Value(int64(in))
case int64:
return types.Int64Value(in)
case uint:
return types.Int64Value(int64(in))
case uint8:
return types.Int64Value(int64(in))
case uint16:
return types.Int64Value(int64(in))
case uint32:
return types.Int64Value(int64(in))
case uint64:
return types.Int64Value(int64(in))
default:
diags.AddError("cannot convert interface to int64",
fmt.Sprintf("value is type %s", reflect.TypeOf(in).String()))
}
return types.Int64Null()
}