-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathutil.go
More file actions
147 lines (133 loc) · 3.46 KB
/
util.go
File metadata and controls
147 lines (133 loc) · 3.46 KB
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
package table
import (
"fmt"
"reflect"
"sort"
"strconv"
)
// AutoIndexColumnID returns a unique Column ID/Name for the given Column Number.
// The functionality is similar to what you get in an Excel spreadsheet w.r.t.
// the Column ID/Name.
func AutoIndexColumnID(colIdx int) string {
charIdx := colIdx % 26
out := string(rune(65 + charIdx))
colIdx = colIdx / 26
if colIdx > 0 {
return AutoIndexColumnID(colIdx-1) + out
}
return out
}
// WidthEnforcer is a function that helps enforce a width condition on a string.
type WidthEnforcer func(col string, maxLen int) string
// widthEnforcerNone returns the input string as is without any modifications.
func widthEnforcerNone(col string, _ int) string {
return col
}
// convertValueToString converts a value to string using fast type assertions
// for common numeric types before falling back to fmt.Sprint.
//
//gocyclo:ignore
func convertValueToString(v interface{}) string {
switch val := v.(type) {
case int:
return strconv.FormatInt(int64(val), 10)
case int8:
return strconv.FormatInt(int64(val), 10)
case int16:
return strconv.FormatInt(int64(val), 10)
case int32:
return strconv.FormatInt(int64(val), 10)
case int64:
return strconv.FormatInt(val, 10)
case uint:
return strconv.FormatUint(uint64(val), 10)
case uint8:
return strconv.FormatUint(uint64(val), 10)
case uint16:
return strconv.FormatUint(uint64(val), 10)
case uint32:
return strconv.FormatUint(uint64(val), 10)
case uint64:
return strconv.FormatUint(val, 10)
case float32:
return strconv.FormatFloat(float64(val), 'g', -1, 32)
case float64:
return strconv.FormatFloat(val, 'g', -1, 64)
case bool:
if val {
return "true"
}
return "false"
case string:
return val
default:
return fmt.Sprint(v)
}
}
// isNumber returns true if the argument is a numeric type; false otherwise.
func isNumber(x interface{}) bool {
if x == nil {
return false
}
switch reflect.TypeOf(x).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return true
}
return false
}
type mergedColumnIndices map[int]int
func objAsSlice(in interface{}) []interface{} {
var out []interface{}
if in != nil {
// dereference pointers
val := reflect.ValueOf(in)
if val.Kind() == reflect.Ptr && !val.IsNil() {
in = val.Elem().Interface()
}
if objIsSlice(in) {
v := reflect.ValueOf(in)
for i := 0; i < v.Len(); i++ {
// dereference pointers
v2 := v.Index(i)
if v2.Kind() == reflect.Ptr && !v2.IsNil() {
v2 = reflect.ValueOf(v2.Elem().Interface())
}
out = append(out, v2.Interface())
}
}
}
// remove trailing nil pointers
tailIdx := len(out)
for i := len(out) - 1; i >= 0; i-- {
val := reflect.ValueOf(out[i])
if val.Kind() != reflect.Ptr || !val.IsNil() {
break
}
tailIdx = i
}
return out[:tailIdx]
}
func objIsSlice(in interface{}) bool {
if in == nil {
return false
}
k := reflect.TypeOf(in).Kind()
return k == reflect.Slice || k == reflect.Array
}
func getSortedKeys(input map[int]map[int]int) ([]int, map[int][]int) {
keys := make([]int, 0, len(input))
subkeysMap := make(map[int][]int)
for key, subMap := range input {
keys = append(keys, key)
subkeys := make([]int, 0, len(subMap))
for subkey := range subMap {
subkeys = append(subkeys, subkey)
}
sort.Ints(subkeys)
subkeysMap[key] = subkeys
}
sort.Ints(keys)
return keys, subkeysMap
}