forked from viant/assertly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
136 lines (124 loc) · 2.98 KB
/
helper.go
File metadata and controls
136 lines (124 loc) · 2.98 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
package assertly
import (
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"strings"
)
func asDataStructure(candidate string) interface{} {
if isMultiline(candidate) {
lines := strings.Split(strings.TrimSpace(candidate), "\n")
if toolbox.IsCompleteJSON(lines[0]) && toolbox.IsCompleteJSON(lines[len(lines)-1]) {
var result = make([]interface{}, 0)
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
item, err := toolbox.JSONToInterface(line)
if err != nil {
result = []interface{}{}
break
}
result = append(result, item)
}
if len(result) > 0 {
return result
}
} else if toolbox.IsCompleteJSON(candidate) {
if result, err := toolbox.JSONToInterface(candidate); err == nil {
return result
}
}
} else if result, err := toolbox.JSONToInterface(candidate); err == nil {
return result
}
return candidate
}
func isMultiline(candidate string) bool {
return strings.Count(candidate, "\n") > 0
}
func reverseSlice(stringSlice []string) {
last := len(stringSlice) - 1
for i := 0; i < len(stringSlice)/2; i++ {
stringSlice[i], stringSlice[last-i] = stringSlice[last-i], stringSlice[i]
}
}
func mergeTextMap(source map[string]string, target *map[string]string) {
if len(source) == 0 {
return
}
if target == nil || len(*target) == 0 {
*target = make(map[string]string)
}
for k := range source {
(*target)[k] = source[k]
}
}
func mergeBoolMap(source map[string]bool, target *map[string]bool) {
if len(source) == 0 {
return
}
if target == nil || len(*target) == 0 {
*target = make(map[string]bool)
}
for k := range source {
(*target)[k] = source[k]
}
}
func keysValue(aMap data.Map, keys ...string) string {
var result = ""
for _, key := range keys {
value, ok := aMap.GetValue(key)
if !ok {
value = ""
}
if value != nil && toolbox.IsMap(value) {
valueMap := toolbox.AsMap(value)
for k := range valueMap {
result += k
}
} else {
result += toolbox.AsString(value)
}
}
return result
}
func keysPairValue(aMap map[string]interface{}, keys ...string) string {
var result = ""
for _, key := range keys {
value := aMap[key]
if len(result) > 0 {
result += ""
}
result += key + "(" + toolbox.AsString(value) + ")"
}
return result
}
func indexSliceBy(aSlice []interface{}, indexFields ...string) map[string]interface{} {
var result = make(map[string]interface{})
for _, item := range aSlice {
var value = keysValue(toolbox.AsMap(item), indexFields...)
result[value] = item
}
return result
}
func toStringSlice(source interface{}) []string {
if !toolbox.IsSlice(source) {
return strings.Split(toolbox.AsString(source), ",")
}
var result = make([]string, 0)
for _, item := range toolbox.AsSlice(source) {
result = append(result, toolbox.AsString(item))
}
return result
}
func isIndexable(source map[string]interface{}) bool {
for _, v := range source {
if v == nil {
continue
}
if toolbox.IsMap(v) {
return true
}
}
return false
}