-
Notifications
You must be signed in to change notification settings - Fork 11
/
convert_data.go
275 lines (246 loc) · 7.29 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
package collections
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/coveo/gotemplate/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: %v", key, err))
}
switch len(errs) {
case 0:
return nil
case 1:
return errs[0]
default:
return errs
}
}
// 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 = ioutil.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(ToNativeRepresentation(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])
}
fmt.Println(results)
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)
}
// ToNativeRepresentation converts any object to native (literals, maps, slices)
func ToNativeRepresentation(value interface{}) interface{} {
if value == nil {
return nil
}
typ, val := reflect.TypeOf(value), reflect.ValueOf(value)
if typ.Kind() == reflect.Ptr {
if val.IsNil() {
return nil
}
val = val.Elem()
typ = val.Type()
}
switch typ.Kind() {
case reflect.String:
return reflect.ValueOf(value).String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
return int(val.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
return uint(val.Uint())
case reflect.Int64:
return val.Int()
case reflect.Uint64:
return val.Uint()
case reflect.Float32, reflect.Float64:
return must(strconv.ParseFloat(fmt.Sprint(value), 64)).(float64)
case reflect.Bool:
return must(strconv.ParseBool(fmt.Sprint(value))).(bool)
case reflect.Slice, reflect.Array:
result := make([]interface{}, val.Len())
for i := range result {
result[i] = ToNativeRepresentation(val.Index(i).Interface())
}
if len(result) == 1 && reflect.TypeOf(result[0]).Kind() == reflect.Map {
// If the result is an array of one map, we just return the inner element
return result[0]
}
return result
case reflect.Map:
result := make(map[string]interface{}, val.Len())
for _, key := range val.MapKeys() {
result[fmt.Sprintf("%v", key)] = ToNativeRepresentation(val.MapIndex(key).Interface())
}
return result
case reflect.Struct:
result := make(map[string]interface{}, typ.NumField())
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 := sf.Tag.Get("hcl")
if tag == "" {
// If there is no hcl specific tag, we rely on json tag if there is
tag = sf.Tag.Get("json")
}
if tag == "-" {
continue
}
split := strings.Split(tag, ",")
name := split[0]
if name == "" {
name = sf.Name
}
options := make(map[string]bool, len(split[1:]))
for i := range split[1:] {
options[split[i+1]] = true
}
if !IsExported(name) || options["omitempty"] && IsEmptyValue(val.Field(i)) {
continue
}
if options["inline"] {
for key, value := range ToNativeRepresentation(val.Field(i).Interface()).(map[string]interface{}) {
result[key] = value
}
} else {
result[name] = ToNativeRepresentation(val.Field(i).Interface())
}
}
return result
default:
fmt.Fprintf(os.Stderr, "Unknown type %T %v : %v\n", value, typ.Kind(), value)
return fmt.Sprintf("%v", value)
}
}
// 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
}