-
Notifications
You must be signed in to change notification settings - Fork 11
/
tablib_util.go
65 lines (59 loc) · 1.29 KB
/
tablib_util.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
package tablib
import (
"fmt"
"strconv"
"time"
)
// internalLoadFromDict creates a Dataset from an array of map representing columns.
func internalLoadFromDict(input []map[string]interface{}) (*Dataset, error) {
// retrieve columns
headers := make([]string, 0, 10)
for h := range input[0] {
headers = append(headers, h)
}
ds := NewDataset(headers)
for _, e := range input {
row := make([]interface{}, 0, len(headers))
for _, h := range headers {
row = append(row, e[h])
}
ds.AppendValues(row...)
}
return ds, nil
}
// isTagged checks if a tag is in an array of tags.
func isTagged(tag string, tags []string) bool {
for _, t := range tags {
if t == tag {
return true
}
}
return false
}
// asString returns a value as a string.
func (d *Dataset) asString(vv interface{}) string {
var v string
switch vv.(type) {
case string:
v = vv.(string)
case int:
v = strconv.Itoa(vv.(int))
case int64:
v = strconv.FormatInt(vv.(int64), 10)
case uint64:
v = strconv.FormatUint(vv.(uint64), 10)
case bool:
v = strconv.FormatBool(vv.(bool))
case float64:
v = strconv.FormatFloat(vv.(float64), 'G', -1, 32)
case time.Time:
v = vv.(time.Time).Format(time.RFC3339)
default:
if d.EmptyValue != "" {
v = d.EmptyValue
} else {
v = fmt.Sprintf("%s", v)
}
}
return v
}