/
data.go
256 lines (232 loc) · 6.19 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
package data
import (
"bytes"
"encoding/csv"
"encoding/json"
"log"
"strings"
// XXX: replace once https://github.com/BurntSushi/toml/pull/179 is merged
"github.com/hairyhenderson/toml"
"github.com/ugorji/go/codec"
yaml "gopkg.in/yaml.v2"
)
func unmarshalObj(obj map[string]interface{}, in string, f func([]byte, interface{}) error) map[string]interface{} {
err := f([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal object %s: %v", in, err)
}
return obj
}
func unmarshalArray(obj []interface{}, in string, f func([]byte, interface{}) error) []interface{} {
err := f([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal array %s: %v", in, err)
}
return obj
}
// JSON - Unmarshal a JSON Object
func JSON(in string) map[string]interface{} {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, yaml.Unmarshal)
}
// JSONArray - Unmarshal a JSON Array
func JSONArray(in string) []interface{} {
obj := make([]interface{}, 1)
return unmarshalArray(obj, in, yaml.Unmarshal)
}
// YAML - Unmarshal a YAML Object
func YAML(in string) map[string]interface{} {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, yaml.Unmarshal)
}
// YAMLArray - Unmarshal a YAML Array
func YAMLArray(in string) []interface{} {
obj := make([]interface{}, 1)
return unmarshalArray(obj, in, yaml.Unmarshal)
}
// TOML - Unmarshal a TOML Object
func TOML(in string) interface{} {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, toml.Unmarshal)
}
func parseCSV(args ...string) ([][]string, []string) {
in, delim, hdr := csvParseArgs(args...)
c := csv.NewReader(strings.NewReader(in))
c.Comma = rune(delim[0])
records, err := c.ReadAll()
if err != nil {
log.Fatal(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
}
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 {
records, hdr := parseCSV(args...)
records = append(records, nil)
copy(records[1:], records)
records[0] = hdr
return records
}
// 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) {
records, hdr := parseCSV(args...)
for _, record := range records {
m := make(map[string]string)
for i, v := range record {
m[hdr[i]] = v
}
rows = append(rows, m)
}
return rows
}
// 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) {
records, hdr := parseCSV(args...)
cols = make(map[string][]string)
for _, record := range records {
for i, v := range record {
cols[hdr[i]] = append(cols[hdr[i]], v)
}
}
return cols
}
// ToCSV -
func ToCSV(args ...interface{}) string {
delim := ","
var in [][]string
if len(args) == 2 {
d, ok := args[0].(string)
if ok {
delim = d
} else {
log.Fatalf("Can't parse ToCSV delimiter (%v) - must be string (is a %T)", args[0], args[0])
}
in, ok = args[1].([][]string)
if !ok {
log.Fatal("Can't parse ToCSV input - must be of type [][]string")
}
}
if len(args) == 1 {
var ok bool
in, ok = args[0].([][]string)
if !ok {
log.Fatal("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 {
log.Fatal(err)
}
return b.String()
}
func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) string {
b, err := f(obj)
if err != nil {
log.Fatalf("Unable to marshal object %s: %v", obj, err)
}
return string(b)
}
func toJSONBytes(in interface{}) []byte {
h := &codec.JsonHandle{}
h.Canonical = true
buf := new(bytes.Buffer)
err := codec.NewEncoder(buf, h).Encode(in)
if err != nil {
log.Fatalf("Unable to marshal %s: %v", in, err)
}
return buf.Bytes()
}
// ToJSON - Stringify a struct as JSON
func ToJSON(in interface{}) string {
return string(toJSONBytes(in))
}
// ToJSONPretty - Stringify a struct as JSON (indented)
func ToJSONPretty(indent string, in interface{}) string {
out := new(bytes.Buffer)
b := toJSONBytes(in)
err := json.Indent(out, b, "", indent)
if err != nil {
log.Fatalf("Unable to indent JSON %s: %v", b, err)
}
return out.String()
}
// ToYAML - Stringify a struct as YAML
func ToYAML(in interface{}) string {
return marshalObj(in, yaml.Marshal)
}
// ToTOML - Stringify a struct as TOML
func ToTOML(in interface{}) string {
buf := new(bytes.Buffer)
err := toml.NewEncoder(buf).Encode(in)
if err != nil {
log.Fatalf("Unable to marshal %s: %v", in, err)
}
return buf.String()
}