forked from yourheropaul/inj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structpath.go
52 lines (38 loc) · 950 Bytes
/
structpath.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
package inj
import "strings"
type structPath string
// Create a new, empty struct path
func emptyStructPath() structPath {
return ""
}
// Create a seprate struct path with a parent name
func (s structPath) Branch(parent string) structPath {
return s + structPath("."+parent)
}
// Remove the first element from the path, and return it
// and the resultant path
func (s structPath) Shift() (string, structPath) {
if s.Empty() {
return "", emptyStructPath()
}
parts := strings.Split(string(s), ".")
// Make a new structpath
var s2 structPath
if len(parts) > 2 {
s2 = structPath("." + strings.Join(parts[2:], "."))
} else {
s2 = structPath(strings.Join(parts[2:], "."))
}
return parts[1], s2
}
// Returns true if the path is empty
func (s structPath) Empty() bool {
if string(s) == "" {
return true
}
return false
}
// Implementation of the Stringer interface
func (s structPath) String() string {
return string(s)
}