forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
324 lines (292 loc) · 8.04 KB
/
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
319
320
321
322
323
324
package data
import (
"bytes"
"encoding/csv"
"encoding/json"
"reflect"
"strings"
"github.com/joho/godotenv"
"github.com/Shopify/ejson"
ejsonJson "github.com/Shopify/ejson/json"
"github.com/hairyhenderson/gomplate/env"
// XXX: replace once https://github.com/BurntSushi/toml/pull/179 is merged
"github.com/hairyhenderson/toml"
"github.com/pkg/errors"
"github.com/ugorji/go/codec"
// XXX: replace once https://github.com/go-yaml/yaml/issues/139 is solved
yaml "gopkg.in/hairyhenderson/yaml.v2"
)
func init() {
// XXX: remove once https://github.com/go-yaml/yaml/issues/139 is solved
*yaml.DefaultMapType = reflect.TypeOf(map[string]interface{}{})
}
func unmarshalObj(obj map[string]interface{}, in string, f func([]byte, interface{}) error) (map[string]interface{}, error) {
err := f([]byte(in), &obj)
if err != nil {
return nil, errors.Wrapf(err, "Unable to unmarshal object %s", in)
}
return obj, nil
}
func unmarshalArray(obj []interface{}, in string, f func([]byte, interface{}) error) ([]interface{}, error) {
err := f([]byte(in), &obj)
if err != nil {
return nil, errors.Wrapf(err, "Unable to unmarshal array %s", in)
}
return obj, nil
}
// JSON - Unmarshal a JSON Object. Can be ejson-encrypted.
func JSON(in string) (map[string]interface{}, error) {
obj := make(map[string]interface{})
out, err := unmarshalObj(obj, in, yaml.Unmarshal)
if err != nil {
return out, err
}
_, ok := out[ejsonJson.PublicKeyField]
if ok {
out, err = decryptEJSON(in)
}
return out, err
}
// decryptEJSON - decrypts an ejson input, and unmarshals it, stripping the _public_key field.
func decryptEJSON(in string) (map[string]interface{}, error) {
keyDir := env.Getenv("EJSON_KEYDIR", "/opt/ejson/keys")
key := env.Getenv("EJSON_KEY")
rIn := bytes.NewBufferString(in)
rOut := &bytes.Buffer{}
err := ejson.Decrypt(rIn, rOut, keyDir, key)
if err != nil {
return nil, errors.WithStack(err)
}
obj := make(map[string]interface{})
out, err := unmarshalObj(obj, rOut.String(), yaml.Unmarshal)
if err != nil {
return nil, errors.WithStack(err)
}
delete(out, ejsonJson.PublicKeyField)
return out, nil
}
// JSONArray - Unmarshal a JSON Array
func JSONArray(in string) ([]interface{}, error) {
obj := make([]interface{}, 1)
return unmarshalArray(obj, in, yaml.Unmarshal)
}
// YAML - Unmarshal a YAML Object
func YAML(in string) (map[string]interface{}, error) {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, yaml.Unmarshal)
}
// YAMLArray - Unmarshal a YAML Array
func YAMLArray(in string) ([]interface{}, error) {
obj := make([]interface{}, 1)
return unmarshalArray(obj, in, yaml.Unmarshal)
}
// TOML - Unmarshal a TOML Object
func TOML(in string) (interface{}, error) {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, toml.Unmarshal)
}
// dotEnv - Unmarshal a dotenv file
func dotEnv(in string) (interface{}, error) {
env, err := godotenv.Unmarshal(in)
if err != nil {
return nil, err
}
out := make(map[string]interface{})
for k, v := range env {
out[k] = v
}
return out, nil
}
func parseCSV(args ...string) ([][]string, []string, error) {
in, delim, hdr := csvParseArgs(args...)
c := csv.NewReader(strings.NewReader(in))
c.Comma = rune(delim[0])
records, err := c.ReadAll()
if err != nil {
return nil, nil, err
}
if len(records) > 0 {
if hdr == nil {
hdr = records[0]
records = records[1:]
} else if len(hdr) == 0 {
hdr = make([]string, len(records[0]))
for i := range hdr {
hdr[i] = autoIndex(i)
}
}
}
return records, hdr, nil
}
func csvParseArgs(args ...string) (in, delim string, hdr []string) {
delim = ","
if len(args) == 1 {
in = args[0]
}
if len(args) == 2 {
in = args[1]
if len(args[0]) == 1 {
delim = args[0]
} else if len(args[0]) == 0 {
hdr = []string{}
} else {
hdr = strings.Split(args[0], delim)
}
}
if len(args) == 3 {
delim = args[0]
hdr = strings.Split(args[1], delim)
in = args[2]
}
return in, delim, hdr
}
// autoIndex - calculates a default string column name given a numeric value
func autoIndex(i int) string {
s := ""
for n := 0; n <= i/26; n++ {
s += string('A' + i%26)
}
return s
}
// CSV - Unmarshal CSV
// parameters:
// delim - (optional) the (single-character!) field delimiter, defaults to ","
// in - the CSV-format string to parse
// returns:
// an array of rows, which are arrays of cells (strings)
func CSV(args ...string) ([][]string, error) {
records, hdr, err := parseCSV(args...)
if err != nil {
return nil, err
}
records = append(records, nil)
copy(records[1:], records)
records[0] = hdr
return records, nil
}
// CSVByRow - Unmarshal CSV in a row-oriented form
// parameters:
// delim - (optional) the (single-character!) field delimiter, defaults to ","
// hdr - (optional) comma-separated list of column names,
// set to "" to get auto-named columns (A-Z), omit
// to use the first line
// in - the CSV-format string to parse
// returns:
// an array of rows, indexed by the header name
func CSVByRow(args ...string) (rows []map[string]string, err error) {
records, hdr, err := parseCSV(args...)
if err != nil {
return nil, err
}
for _, record := range records {
m := make(map[string]string)
for i, v := range record {
m[hdr[i]] = v
}
rows = append(rows, m)
}
return rows, nil
}
// CSVByColumn - Unmarshal CSV in a Columnar form
// parameters:
// delim - (optional) the (single-character!) field delimiter, defaults to ","
// hdr - (optional) comma-separated list of column names,
// set to "" to get auto-named columns (A-Z), omit
// to use the first line
// in - the CSV-format string to parse
// returns:
// a map of columns, indexed by the header name. values are arrays of strings
func CSVByColumn(args ...string) (cols map[string][]string, err error) {
records, hdr, err := parseCSV(args...)
if err != nil {
return nil, err
}
cols = make(map[string][]string)
for _, record := range records {
for i, v := range record {
cols[hdr[i]] = append(cols[hdr[i]], v)
}
}
return cols, nil
}
// ToCSV -
func ToCSV(args ...interface{}) (string, error) {
delim := ","
var in [][]string
if len(args) == 2 {
var ok bool
delim, ok = args[0].(string)
if !ok {
return "", errors.Errorf("Can't parse ToCSV delimiter (%v) - must be string (is a %T)", args[0], args[0])
}
args = args[1:]
}
if len(args) == 1 {
var ok bool
in, ok = args[0].([][]string)
if !ok {
return "", errors.Errorf("Can't parse ToCSV input - must be of type [][]string")
}
}
b := &bytes.Buffer{}
c := csv.NewWriter(b)
c.Comma = rune(delim[0])
// We output RFC4180 CSV, so force this to CRLF
c.UseCRLF = true
err := c.WriteAll(in)
if err != nil {
return "", err
}
return b.String(), nil
}
func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) (string, error) {
b, err := f(obj)
if err != nil {
return "", errors.Wrapf(err, "Unable to marshal object %s", obj)
}
return string(b), nil
}
func toJSONBytes(in interface{}) ([]byte, error) {
h := &codec.JsonHandle{}
h.Canonical = true
buf := new(bytes.Buffer)
err := codec.NewEncoder(buf, h).Encode(in)
if err != nil {
return nil, errors.Wrapf(err, "Unable to marshal %s", in)
}
return buf.Bytes(), nil
}
// ToJSON - Stringify a struct as JSON
func ToJSON(in interface{}) (string, error) {
s, err := toJSONBytes(in)
if err != nil {
return "", err
}
return string(s), nil
}
// ToJSONPretty - Stringify a struct as JSON (indented)
func ToJSONPretty(indent string, in interface{}) (string, error) {
out := new(bytes.Buffer)
b, err := toJSONBytes(in)
if err != nil {
return "", err
}
err = json.Indent(out, b, "", indent)
if err != nil {
return "", errors.Wrapf(err, "Unable to indent JSON %s", b)
}
return out.String(), nil
}
// ToYAML - Stringify a struct as YAML
func ToYAML(in interface{}) (string, error) {
return marshalObj(in, yaml.Marshal)
}
// ToTOML - Stringify a struct as TOML
func ToTOML(in interface{}) (string, error) {
buf := new(bytes.Buffer)
err := toml.NewEncoder(buf).Encode(in)
if err != nil {
return "", errors.Wrapf(err, "Unable to marshal %s", in)
}
return buf.String(), nil
}