forked from Nomon/gonfig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
57 lines (48 loc) · 1.32 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
package unicon
import (
"regexp"
"strconv"
"strings"
)
var nsSep = regexp.MustCompile("[-_:]")
func namespaceKey(key string, namespaces []string) string {
namespaced := nsSep.ReplaceAllString(key, ".")
for _, ns := range namespaces {
nsWith := strings.Join([]string{ns, "."}, "")
if strings.HasPrefix(strings.ToLower(namespaced), nsWith) {
return namespaced
}
}
return key
}
func nsSlice(namespaces []string) (lowered []string) {
for _, ns := range namespaces {
// put in lowercase
lowered = append(lowered, strings.ToLower(ns))
}
return
}
func unmarshal(segment interface{}, path string, output map[string]interface{}) {
switch segment := segment.(type) {
case map[string]interface{}:
path += "."
unmarshalMap(segment, path, output)
case []interface{}:
unmarshalArray(segment, path, output)
default:
output[path] = segment
}
}
func unmarshalMap(segment map[string]interface{}, segmentPath string, output map[string]interface{}) {
for k, v := range segment {
keyWithPath := segmentPath + k
unmarshal(v, keyWithPath, output)
}
}
func unmarshalArray(segment []interface{}, segmentPath string, output map[string]interface{}) {
for i, v := range segment {
keyWithPath := segmentPath + "[" + strconv.Itoa(i) + "]"
unmarshal(v, keyWithPath, output)
}
output[segmentPath+".length"] = len(segment)
}