-
Notifications
You must be signed in to change notification settings - Fork 11
/
convert_data.go
318 lines (283 loc) · 8.45 KB
/
convert_data.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package collections
import (
"fmt"
"os"
"reflect"
"regexp"
"strings"
"unicode"
"unicode/utf8"
"github.com/coveooss/multilogger/errors"
)
// TypeConverters is used to register the available converters
var TypeConverters = make(map[string]func([]byte, interface{}) error)
// ConvertData returns a go representation of the supplied string (YAML, JSON or HCL)
func ConvertData(data string, out interface{}) (err error) {
trySimplified := func() error {
if strings.Count(data, "=") == 0 {
return fmt.Errorf("not simplifiable")
}
// Special case where we want to have a map and the supplied string is simplified such as "a = 10 b = string"
// so we try transform the supplied string in valid YAML
simplified := regexp.MustCompile(`[ \t]*=[ \t]*`).ReplaceAllString(data, ":")
simplified = regexp.MustCompile(`[ \t]+`).ReplaceAllString(simplified, "\n")
simplified = strings.Replace(simplified, ":", ": ", -1) + "\n"
return ConvertData(simplified, out)
}
var errs errors.Array
defer func() {
if err == nil {
// YAML converter returns a string if it encounter invalid data, so we check the result to ensure that is is different from the input.
if out, isItf := out.(*interface{}); isItf && data == fmt.Sprint(*out) && strings.ContainsAny(data, "=:{}") {
if _, isString := (*out).(string); isString {
if trySimplified() == nil && data != fmt.Sprint(*out) {
err = nil
return
}
err = errs
*out = nil
}
}
} else {
if _, e := TryAsList(out); e == nil && trySimplified() == nil {
err = nil
}
}
}()
for _, key := range AsDictionary(TypeConverters).KeysAsString() {
err = TypeConverters[key.Str()]([]byte(data), out)
if err == nil {
return
}
errs = append(errs, fmt.Errorf("trying %s: %w", key, err))
}
return errs.AsError()
}
// LoadData returns a go representation of the supplied file name (YAML, JSON or HCL)
func LoadData(filename string, out interface{}) (err error) {
var content []byte
if content, err = os.ReadFile(filename); err == nil {
return ConvertData(string(content), out)
}
return
}
// ToBash returns the bash 4 variable representation of value
func ToBash(value interface{}) string {
return toBash(must(MarshalGo(value)), 0)
}
func toBash(value interface{}, level int) (result string) {
if value, isString := value.(string); isString {
result = value
if strings.ContainsAny(value, " \t\n[]()") {
result = fmt.Sprintf("%q", value)
}
return
}
if value, err := TryAsList(value); err == nil {
results := value.Strings()
for i := range results {
results[i] = quote(results[i])
}
switch level {
case 2:
result = strings.Join(results, ",")
default:
result = fmt.Sprintf("(%s)", strings.Join(results, " "))
}
return
}
if value, err := TryAsDictionary(value); err == nil {
results := make([]string, value.Len())
vMap := value.AsMap()
switch level {
case 0:
for i, key := range value.KeysAsString() {
key := key.Str()
val := toBash(vMap[key], level+1)
if _, err := TryAsList(vMap[key]); err == nil {
results[i] = fmt.Sprintf("declare -a %[1]s\n%[1]s=%[2]v", key, val)
} else if _, err := TryAsDictionary(vMap[key]); err == nil {
results[i] = fmt.Sprintf("declare -A %[1]s\n%[1]s=%[2]v", key, val)
} else {
results[i] = fmt.Sprintf("%s=%v", key, val)
}
}
result = strings.Join(results, "\n")
case 1:
for i, key := range value.KeysAsString() {
key := key.Str()
val := toBash(vMap[key], level+1)
val = strings.Replace(val, `$`, `\$`, -1)
results[i] = fmt.Sprintf("[%s]=%s", key, val)
}
result = fmt.Sprintf("(%s)", strings.Join(results, " "))
default:
for i, key := range value.KeysAsString() {
key := key.Str()
val := toBash(vMap[key], level+1)
results[i] = fmt.Sprintf("%s=%s", key, quote(val))
}
result = strings.Join(results, ",")
}
return
}
return fmt.Sprint(value)
}
// GoMarshaler is the interface that could be implemented on object that want to customize
// the marshaling to a native go representation.
type GoMarshaler interface {
MarshalGo(interface{}) (interface{}, error)
}
// MarshalGo converts any object to native go type (literals, maps, slices).
func MarshalGo(value interface{}) (result interface{}, err error) {
if value == nil {
return
}
typ, val := reflect.TypeOf(value), reflect.ValueOf(value)
if typ.Kind() == reflect.Ptr {
if val.IsNil() {
return
}
val = val.Elem()
typ = val.Type()
}
if val.CanInterface() && val.Type().Implements(reflect.TypeOf((*GoMarshaler)(nil)).Elem()) {
// The object implement a custom marshaller, so we let it generate its stuff
return val.Interface().(GoMarshaler).MarshalGo(value)
}
switch typ.Kind() {
case reflect.String:
result = val.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
result = int(val.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
result = uint(val.Uint())
case reflect.Int64:
result = val.Int()
case reflect.Uint64:
result = val.Uint()
case reflect.Float32, reflect.Float64:
result = val.Float()
case reflect.Bool:
result = val.Bool()
case reflect.Slice, reflect.Array:
array := make([]interface{}, val.Len())
for i := range array {
if array[i], err = MarshalGo(val.Index(i).Interface()); err != nil {
return
}
}
result = array
case reflect.Map:
m := make(map[string]interface{}, val.Len())
for _, key := range val.MapKeys() {
if m[fmt.Sprint(key)], err = MarshalGo(val.MapIndex(key).Interface()); err != nil {
return
}
}
result = m
case reflect.Struct:
m := make(map[string]interface{}, typ.NumField())
info, key, err := getTags(typ)
if err != nil {
return nil, err
}
for i := 0; i < typ.NumField(); i++ {
sf := typ.Field(i)
if sf.Anonymous {
t := sf.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
// If embedded, StructField.PkgPath is not a reliable
// indicator of whether the field is exported.
// See https://golang.org/issue/21122
if !IsExported(t.Name()) && t.Kind() != reflect.Struct {
// Ignore embedded fields of unexported non-struct collections.
// Do not ignore embedded fields of unexported struct types
// since they may have exported fields.
continue
}
} else if sf.PkgPath != "" {
// Ignore unexported non-embedded fields.
continue
}
tag := info[i]
if tag.name == "-" || !IsExported(sf.Name) || tag.options["omitempty"] && IsEmptyValue(val.Field(i)) {
continue
} else if tag.name == "" {
tag.name = sf.Name
}
if tag.options["inline"] || tag.options["squash"] {
if subMap, err := MarshalGo(val.Field(i).Interface()); err != nil {
return nil, err
} else if subMap, ok := subMap.(map[string]interface{}); ok {
for key, value := range subMap {
m[key] = value
}
} else {
return nil, fmt.Errorf("cannot apply inline or squash to non struct on field '%s'", sf.Name)
}
} else {
if value, err = MarshalGo(val.Field(i).Interface()); err != nil {
return nil, err
}
v := reflect.ValueOf(value)
if IsEmptyValue(v) && (v.Kind() == reflect.Struct || v.Kind() == reflect.Map) && tag.options["omitempty"] {
continue
}
if key >= 0 {
m[val.Field(key).String()] = map[string]interface{}{tag.name: v.Interface()}
} else {
m[tag.name] = v.Interface()
}
}
}
result = m
default:
fmt.Fprintf(os.Stderr, "Unknown type %T %v : %v\n", value, typ.Kind(), value)
result = fmt.Sprint(value)
}
return
}
type tagInfo struct {
name string
options map[string]bool
}
func getTags(t reflect.Type) (result []tagInfo, key int, err error) {
key = -1
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
var tag string
for _, category := range []string{"hcl", "json", "yaml", "xml", "toml"} {
if value := sf.Tag.Get(category); value != "" {
tag = value
break
}
}
split := strings.Split(tag, ",")
options := make(map[string]bool, len(split[1:]))
for _, key := range split[1:] {
options[key] = true
}
if options["key"] {
if key != -1 {
err = fmt.Errorf("multiple keys defined on struct '%s' ('%s' and '%s')", t.Name(), t.Field(key).Name, sf.Name)
}
key = i
}
result = append(result, tagInfo{split[0], options})
}
return
}
// IsExported reports whether the identifier is exported.
func IsExported(id string) bool {
r, _ := utf8.DecodeRuneInString(id)
return unicode.IsUpper(r)
}
func quote(s string) string {
if strings.ContainsAny(s, " \t,[]()") {
s = fmt.Sprintf("%q", s)
}
return s
}