forked from rs/rest-layer
/
utils.go
66 lines (62 loc) · 1.65 KB
/
utils.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
package schema
import "strings"
// isNumber takes an interface as input, and returns a float64 if the type is
// compatible (int* or float*)
func isNumber(n interface{}) (float64, bool) {
switch n := n.(type) {
case int:
return float64(n), true
case int8:
return float64(n), true
case int16:
return float64(n), true
case int32:
return float64(n), true
case int64:
return float64(n), true
case uint:
return float64(n), true
case uint8:
return float64(n), true
case uint16:
return float64(n), true
case uint32:
return float64(n), true
case uint64:
return float64(n), true
case float32:
return float64(n), true
case float64:
return n, true
default:
return 0, false
}
}
// getField gets the value of a given field by supporting sub-field path.
// A get on field.subfield is equivalent to payload["field"]["subfield].
func getField(payload map[string]interface{}, name string) interface{} {
val, found := getFieldExist(payload, name)
if !found {
return nil
}
return val
}
func getFieldExist(payload map[string]interface{}, name string) (interface{}, bool) {
// Split the name to get the current level name on first element and
// the rest of the path as second element if dot notation is used
// (i.e.: field.subfield.subsubfield -> field, subfield.subsubfield)
path := strings.SplitN(name, ".", 2)
if value, found := payload[path[0]]; found {
if len(path) == 2 {
if subPayload, ok := value.(map[string]interface{}); ok {
// Check next level
return getFieldExist(subPayload, path[1])
}
// The requested depth does not exist
return nil, false
}
// Full path has been found
return value, true
}
return nil, false
}