/
path.go
45 lines (40 loc) · 948 Bytes
/
path.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
package goconf
import "strings"
var KeyDelimiter = "."
// Combine paths to one path with KeyDelimiter
func CombinePath(pathSegments ...string) string {
if len(pathSegments) == 0 {
panic("pathSegments is empty")
}
paths := make([]string, 0)
for _, path := range pathSegments {
if len(path) == 0 {
continue
}
paths = append(paths, path)
}
return strings.Join(paths, KeyDelimiter)
}
// Get last path segment
func GetSectionKey(path string) string {
if len(path) == 0 {
panic("path is empty")
}
lastDelimiterIndex := strings.LastIndex(path, KeyDelimiter)
if lastDelimiterIndex == -1 {
return path
}
return path[lastDelimiterIndex+1:]
}
// Get parent path
func GetParentPath(path string) (string, bool) {
if len(path) == 0 {
panic("path is empty")
}
lastDelimiterIndex := strings.LastIndex(path, KeyDelimiter)
if lastDelimiterIndex == -1 {
return "", false
} else {
return path[:lastDelimiterIndex], true
}
}