-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
92 lines (75 loc) · 2.25 KB
/
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
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
package whatnot
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
const (
pathDelimeter = "/"
rootId = "[ROOT]"
)
// PathString is a string representation of all or part of
// a hierarchical set of resources in a namespace
type PathString string
// SubPath is a path element identifier
// akin to the directory names between path delimeters
type SubPath string
// AbsolutePath is the fully-qualified path to a single PathElement
// from the root of the Namespace it resides in
type AbsolutePath []SubPath
// RelativePath is the path to a single PathElement
// relative to a single PathElement somewhere in its parent chain
type RelativePath []SubPath
// ToAbsolutePath converts a PathString into an AbsolutePath
// (a slice of ordered SubPath sections)
func (m PathString) ToAbsolutePath() AbsolutePath {
if strings.HasPrefix(string(m), pathDelimeter) == false {
return []SubPath{pathDelimeter}
}
return splitPath(m)
}
// ToRelativePath breaks down a path string into a relative Path
func (m PathString) ToRelativePath() RelativePath {
return splitPath(m)
}
// ToPathString converts an absolute path back into
// a delimited string
func (m AbsolutePath) ToPathString() PathString {
return PathString(fmt.Sprintf("/%s", joinPath(m)))
}
// SubtractPath removes the right-hand-size RelativePath from the AbsolutePath
func (m AbsolutePath) SubtractPath(path AbsolutePath) PathString {
return ""
}
func splitPath(path PathString) (sections []SubPath) {
s := strings.Split(string(path), pathDelimeter)
// was this an absolute path, with a leading slash? if so, remove it
if len(s) > 0 {
if s[0] == "" {
s = s[1:]
}
}
for _, ps := range s {
sections = append(sections, SubPath(ps))
}
// SANITY CHECKS
return sections
}
func joinPath(sections []SubPath) (newPath PathString) {
strs := make([]string, len(sections))
for i, p := range sections {
strs[i] = string(p)
}
newPath = PathString(strings.Join(strs, pathDelimeter))
return
}
// Validate confirms that this SubPath entry is usable
// to construct a valid location within an AbsolutePath
// for a given Path Element
func (m SubPath) Validate() error {
// hard rule for preventing insanity
if strings.Contains(string(m), pathDelimeter) {
return errors.Errorf("refusing to access")
}
return nil
}