-
Notifications
You must be signed in to change notification settings - Fork 30
/
helper.go
101 lines (93 loc) · 2.51 KB
/
helper.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
package data
import (
"fmt"
"github.com/viant/toolbox"
"reflect"
"sort"
"strings"
"unicode"
)
func ExtractPath(expression string) string {
var result = ""
for _, r := range expression {
aChar := string(r)
if unicode.IsLetter(r) || unicode.IsDigit(r) || aChar == "[" || aChar == "]" || aChar == "." || aChar == "_" || aChar == "{" || aChar == "}" {
result += aChar
}
}
return strings.Trim(result, "{}")
}
func recordToMap(fields []*Field, record []interface{}, aMap map[string]interface{}) {
for _, field := range fields {
index := field.index
var value = record[index]
if value == nil {
continue
}
aMap[field.Name] = value
}
}
func indexValue(indexBy []int, record []interface{}) interface{} {
if len(indexBy) == 1 {
return record[indexBy[0]]
}
var values = make([]string, len(indexBy))
for i, fieldIndex := range indexBy {
values[i] = toolbox.AsString(record[fieldIndex])
}
return strings.Join(values, "-")
}
func intsToGenericSlice(keyType reflect.Type, aSlice []int) []interface{} {
var result = make([]interface{}, len(aSlice))
for i, item := range aSlice {
result[i] = reflect.ValueOf(item).Convert(keyType).Interface()
}
return result
}
func floatsToGenericSlice(keyType reflect.Type, aSlice []float64) []interface{} {
var result = make([]interface{}, len(aSlice))
for i, item := range aSlice {
result[i] = reflect.ValueOf(item).Convert(keyType).Interface()
}
return result
}
func stringsToGenericSlice(aSlice []string) []interface{} {
var result = make([]interface{}, len(aSlice))
for i, item := range aSlice {
result[i] = toolbox.AsString(item)
}
return result
}
func sortKeys(key interface{}, aMap map[interface{}][]interface{}) ([]interface{}, error) {
if len(aMap) == 0 {
return []interface{}{}, nil
}
var i = 0
switch key.(type) {
case int, uint, uint8, uint16, uint32, uint64, int8, int16, int32, int64:
var aSlice = make([]int, len(aMap))
for k := range aMap {
aSlice[i] = toolbox.AsInt(k)
i++
}
sort.Ints(aSlice)
return intsToGenericSlice(reflect.TypeOf(key), aSlice), nil
case float64, float32:
var aSlice = make([]float64, len(aMap))
for k := range aMap {
aSlice[i] = toolbox.AsFloat(k)
i++
}
sort.Float64s(aSlice)
return floatsToGenericSlice(reflect.TypeOf(key), aSlice), nil
case string:
var aSlice = make([]string, len(aMap))
for k := range aMap {
aSlice[i] = toolbox.AsString(k)
i++
}
sort.Strings(aSlice)
return stringsToGenericSlice(aSlice), nil
}
return nil, fmt.Errorf("unable sort, unsupported type: %T", key)
}