-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
195 lines (165 loc) · 5.46 KB
/
config.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
192
193
194
195
package acctest
import (
"fmt"
"strings"
"sync"
"time"
"github.com/dcarbone/terraform-plugin-framework-utils/v3/internal/util"
)
type ConfigLiteral string
type ConfigValueFunc func(interface{}) string
var (
configValueFuncsMu sync.RWMutex
configValueFuncs map[string]ConfigValueFunc
)
func SetConfigValueFunc(t interface{}, fn ConfigValueFunc) {
configValueFuncsMu.Lock()
defer configValueFuncsMu.Unlock()
configValueFuncs[util.KeyFN(t)] = fn
}
func GetConfigValueFunc(t interface{}) (ConfigValueFunc, bool) {
configValueFuncsMu.RLock()
defer configValueFuncsMu.RUnlock()
fn, ok := configValueFuncs[util.KeyFN(t)]
return fn, ok
}
func DefaultConfigValueFuncs() map[string]ConfigValueFunc {
return map[string]ConfigValueFunc{
// nil
util.KeyFN(nil): func(_ interface{}) string { return "null" },
// values to print literally
util.KeyFN(ConfigLiteral("")): func(v interface{}) string { return string(v.(ConfigLiteral)) },
// simple conversions
util.KeyFN(false): func(v interface{}) string { return fmt.Sprintf("%t", v.(bool)) },
util.KeyFN(0): func(v interface{}) string { return fmt.Sprintf("%d", v.(int)) },
util.KeyFN(float64(0)): func(v interface{}) string { return fmt.Sprintf("%f", v.(float64)) },
// handle single and multi-line strings
util.KeyFN(""): func(v interface{}) string {
if strings.Contains(v.(string), "\n") {
return fmt.Sprintf("<<EOD\n%s\nEOD\n", v.(string))
} else {
return fmt.Sprintf("%q", v.(string))
}
},
// time values
util.KeyFN(time.Nanosecond): func(v interface{}) string {
return ConfigValue(v.(time.Duration).String())
},
// slices
util.KeyFN(make([]interface{}, 0)): func(v interface{}) string {
formatted := make([]string, 0)
for _, v := range v.([]interface{}) {
formatted = append(formatted, ConfigValue(v))
}
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
},
util.KeyFN(make([]string, 0)): func(v interface{}) string {
formatted := make([]string, 0)
for _, v := range v.([]string) {
formatted = append(formatted, ConfigValue(v))
}
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
},
util.KeyFN(make([]int, 0)): func(v interface{}) string {
formatted := make([]string, 0)
for _, v := range v.([]int) {
formatted = append(formatted, ConfigValue(v))
}
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
},
util.KeyFN(make([]float64, 0)): func(v interface{}) string {
formatted := make([]string, 0)
for _, v := range v.([]float64) {
formatted = append(formatted, ConfigValue(v))
}
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
},
util.KeyFN(make([]map[string]interface{}, 0)): func(v interface{}) string {
formatted := make([]string, 0)
for _, v := range v.([]map[string]interface{}) {
formatted = append(formatted, ConfigValue(v))
}
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
},
util.KeyFN(make([]map[string]string, 0)): func(v interface{}) string {
formatted := make([]string, 0)
for _, v := range v.([]map[string]string) {
formatted = append(formatted, ConfigValue(v))
}
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
},
// maps
util.KeyFN(make(map[string]interface{})): func(v interface{}) string {
inner := "{"
for k, v := range v.(map[string]interface{}) {
inner = fmt.Sprintf("%s\n%s = %s", inner, k, ConfigValue(v))
}
return fmt.Sprintf("%s\n}", inner)
},
util.KeyFN(make(map[string]string)): func(v interface{}) string {
inner := "{"
for k, v := range v.(map[string]string) {
inner = fmt.Sprintf("%s\n%s = %s", inner, k, ConfigValue(v))
}
return fmt.Sprintf("%s\n}", inner)
},
}
}
func init() {
configValueFuncs = DefaultConfigValueFuncs()
}
// ConfigValue attempts to convert the provided input to a Terraform config safe representation of its value
func ConfigValue(in interface{}) string {
if fn, ok := GetConfigValueFunc(in); ok {
return fn(in)
} else {
panic(fmt.Sprintf("Unable to handle config values of type %T", in))
}
}
func JoinConfigs(confs ...string) string {
return strings.Join(confs, "\n")
}
func CompileConfig(header string, fieldMaps ...map[string]interface{}) string {
const f = `
%s {
%s
}`
fields := ""
for k, v := range MergeMaps(fieldMaps...) {
fields = fmt.Sprintf("%s%s = %s\n", fields, k, ConfigValue(v))
}
return fmt.Sprintf(f, header, fields)
}
func ProviderHeader(name string) string {
const f = `provider %q`
return fmt.Sprintf(f, name)
}
func ResourceHeader(resourceType, resourceName string) string {
const f = `resource %q %q`
return fmt.Sprintf(f, resourceType, resourceName)
}
func DataSourceHeader(dataSourceType, dataSourceName string) string {
const f = `data %q %q`
return fmt.Sprintf(f, dataSourceType, dataSourceName)
}
func CompileProviderConfig(providerName string, fieldMaps ...map[string]interface{}) string {
return CompileConfig(
ProviderHeader(providerName),
fieldMaps...,
)
}
func CompileResourceConfig(resourceType, resourceName string, fieldMaps ...map[string]interface{}) string {
return CompileConfig(
ResourceHeader(resourceType, resourceName),
fieldMaps...,
)
}
func CompileDataSourceConfig(dataSourceType, dataSourceName string, fieldMaps ...map[string]interface{}) string {
return CompileConfig(
DataSourceHeader(dataSourceType, dataSourceName),
fieldMaps...,
)
}
func CompileLocalsConfig(fieldMaps ...map[string]interface{}) string {
return CompileConfig("locals", fieldMaps...)
}