forked from viant/neatly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
87 lines (82 loc) · 1.98 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
package neatly
import (
"fmt"
"github.com/viant/toolbox"
"strings"
)
func unescapeSpecialCharacters(input string) (string, bool) {
result := strings.TrimSpace(input)
unescaped := false
if len(input) < 2 {
return input, unescaped
}
firstSequence := string(result[:2])
switch firstSequence {
case "@@":
fallthrough
case "$$":
fallthrough
case "##":
fallthrough
case "[[":
fallthrough
case "{{":
result = string(result[1:])
unescaped = true
}
lastSequence := string(result[len(result)-2:])
switch lastSequence {
case "]]":
fallthrough
case "}}":
result = string(result[:len(result)-1])
unescaped = true
}
return result, unescaped
}
func asDataStructure(value string) (interface{}, error) {
if len(value) == 0 {
return nil, nil
}
if value, unescaped := unescapeSpecialCharacters(value); unescaped {
return value, nil
}
if strings.HasPrefix(value, "{") {
if toolbox.IsNewLineDelimitedJSON(value) {
return value, nil
}
jsonFactory := toolbox.NewJSONDecoderFactory()
var jsonObject = make(map[string]interface{})
err := jsonFactory.Create(strings.NewReader(value)).Decode(&jsonObject)
if err != nil {
return nil, fmt.Errorf("failed to decode: %v %T, %v", value, value, err)
}
return jsonObject, nil
} else if strings.HasPrefix(value, "[") {
var jsonArray = make([]interface{}, 0)
err := toolbox.NewJSONDecoderFactory().Create(strings.NewReader(value)).Decode(&jsonArray)
if err != nil {
return nil, fmt.Errorf("failed to decode: %v %v", value, err)
}
return jsonArray, nil
}
return value, nil
}
func getAssetURIs(value string) []string {
var separator = " "
if strings.Contains(value, "[") || strings.Contains(value, "{") {
separator = "|"
}
if separator != "|" {
value = strings.Replace(value, "|", separator, len(value))
}
var result = make([]string, 0)
for _, item := range strings.Split(value, separator) {
item = strings.TrimSpace(item)
if item == "" {
continue
}
result = append(result, item)
}
return result
}