-
Notifications
You must be signed in to change notification settings - Fork 5
/
custom_types.go
143 lines (119 loc) · 3.54 KB
/
custom_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
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
package test
import (
"context"
fmt "fmt"
"strings"
time "time"
"github.com/hashicorp/terraform-plugin-framework/attr"
diag "github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Duration custom duration type
type Duration int64
// String returns duration string representation, must be implemented for custom duration type
func (d Duration) String() string {
return time.Duration(d).String()
}
// BoolCustom custom bool array
type BoolCustom bool
// GenSchemaBoolSpecial generates custom field schema (bool list)
func GenSchemaBoolSpecial(_ context.Context) tfsdk.Attribute {
return tfsdk.Attribute{
Type: types.ListType{
ElemType: types.BoolType,
},
}
}
// CopyFromBoolSpecial copies target value to the source
func CopyFromBoolSpecial(diags diag.Diagnostics, tf attr.Value, obj *[]BoolCustom) {
v, ok := tf.(types.List)
if !ok {
diags.AddError("Error reading value from Terraform", fmt.Sprintf("Failed to cast %T to types.List", tf))
return
}
arr := make([]BoolCustom, len(v.Elems))
for i, raw := range v.Elems {
el, ok := raw.(types.Bool)
if !ok {
diags.AddError("Error reading value from Terraform", fmt.Sprintf("Failed to cast %T to types.Bool", raw))
return
}
if !el.Null && !el.Unknown {
arr[i] = BoolCustom(el.Value)
}
}
*obj = arr
}
// CopyToBoolSpecial copies source value to the target
func CopyToBoolSpecial(diags diag.Diagnostics, obj []BoolCustom, t attr.Type, v attr.Value) attr.Value {
value, ok := v.(types.List)
if !ok {
value = types.List{
Null: true,
Unknown: false,
ElemType: types.BoolType,
}
}
if len(obj) > 0 {
if value.Elems == nil {
value.Elems = make([]attr.Value, len(obj))
}
for i, b := range obj {
value.Elems[i] = types.Bool{Null: false, Unknown: false, Value: bool(b)}
}
}
return value
}
// StringCustom is a custom type that maps a Terraform List of string, onto a
// single go string by joining all elements with "/".
// GenSchemaStringCustom returns the StringCustom schema.
func GenSchemaStringCustom(_ context.Context) tfsdk.Attribute {
return tfsdk.Attribute{
Type: types.ListType{
ElemType: types.StringType,
},
}
}
// CopyFromStringCustom copies the value from Terraform (a list of strings) into
// the source (a single string) by joining all elements with "/".
func CopyFromStringCustom(diags diag.Diagnostics, tf attr.Value, obj *string) {
v, ok := tf.(types.List)
if !ok {
diags.AddError("Error reading value from Terraform", fmt.Sprintf("Failed to cast %T to types.List", tf))
return
}
items := make([]string, 0)
for _, raw := range v.Elems {
el, ok := raw.(types.String)
if !ok {
diags.AddError("Error reading value from Terraform", fmt.Sprintf("Failed to cast %T to types.Bool", raw))
return
}
if !el.Null && !el.Unknown {
items = append(items, el.Value)
}
}
*obj = strings.Join(items, "/")
}
// CopyToStringCustom copies a source value (single string) into the Terraform
// value (a list of strings) by splitting the string on every "/".
func CopyToStringCustom(diags diag.Diagnostics, obj string, t attr.Type, v attr.Value) attr.Value {
value, ok := v.(types.List)
if !ok {
value = types.List{
Null: true,
Unknown: false,
ElemType: types.StringType,
}
}
if len(obj) > 0 {
if value.Elems == nil {
value.Elems = make([]attr.Value, len(obj))
}
for i, b := range strings.Split(obj, "/") {
value.Elems[i] = types.String{Null: false, Unknown: false, Value: b}
}
}
return value
}