-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_value.go
96 lines (71 loc) · 1.66 KB
/
utils_value.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
package jsonld_helper
import (
"reflect"
"strings"
)
func getValue(value any) any {
valueType := reflect.TypeOf(value)
if isString(valueType) || isNumber(valueType) || isBool(valueType) {
return value
}
if isMap(valueType) {
return getValueInMap(value)
}
if isArray(valueType) {
return getValueInArray(value)
}
return value
}
func getValueInMap(value any) any {
valueMap := value.(map[string]any)
if valueMap["@value"] != nil {
return valueMap["@value"]
}
if valueMap["@id"] != nil {
return valueMap["@id"]
}
return value
}
func getValueInArray(value any) any {
if _, ok := value.([]any); ok {
valueArray := value.([]any)
if len(valueArray) == 1 {
scope := valueArray[0]
if isMap(reflect.TypeOf(scope)) {
return getValueInMap(scope)
}
return scope
}
}
if _, ok := value.([]map[string]any); ok {
valueArray := value.([]map[string]any)
if len(valueArray) == 1 {
scope := valueArray[0]
if isMap(reflect.TypeOf(scope)) {
return getValueInMap(scope)
}
return scope
}
}
return value
}
func isString(reflectType reflect.Type) bool {
return strings.HasPrefix(reflectType.String(), "string")
}
func isNumber(reflectType reflect.Type) bool {
for _, typeName := range []string{"int", "uint", "float"} {
if strings.HasPrefix(reflectType.String(), typeName) {
return true
}
}
return false
}
func isBool(reflectType reflect.Type) bool {
return strings.HasPrefix(reflectType.String(), "bool")
}
func isArray(reflectType reflect.Type) bool {
return strings.HasPrefix(reflectType.String(), "[]")
}
func isMap(reflectType reflect.Type) bool {
return strings.HasPrefix(reflectType.String(), "map")
}