-
Notifications
You must be signed in to change notification settings - Fork 26
/
types.go
53 lines (40 loc) · 1.02 KB
/
types.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
package utils
import (
"context"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func AsUpCloudBoolean(b types.Bool) upcloud.Boolean {
if b.IsNull() || b.IsUnknown() {
return upcloud.Empty
}
if b.ValueBool() {
return upcloud.True
}
return upcloud.False
}
func AsBool(b upcloud.Boolean) types.Bool {
if b.Empty() {
return types.BoolPointerValue(nil)
}
return types.BoolValue(b.Bool())
}
func SetAsSliceOfStrings(ctx context.Context, set types.Set) ([]string, diag.Diagnostics) {
if set.IsNull() || set.IsUnknown() {
return nil, nil
}
elements := make([]types.String, 0, len(set.Elements()))
strings := make([]string, 0, len(set.Elements()))
diags := set.ElementsAs(ctx, &elements, false)
for _, element := range elements {
strings = append(strings, element.ValueString())
}
return strings, diags
}
func NilAsEmptyList[T any](l []T) []T {
if l == nil {
return []T{}
}
return l
}