-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey.go
More file actions
114 lines (97 loc) · 2.85 KB
/
key.go
File metadata and controls
114 lines (97 loc) · 2.85 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package config
import "strings"
//Key is the entity that allows access to values stored within a Values instance.
type Key []string
//NewKey creates a Key with all strings in parts in the returned Key.
//It essentially casts the string slice to a Key.
func NewKey(parts ...string) Key {
return Key(parts)
}
//NewKeySep returns a Key that is the result of strings.Split(source, sep).
func NewKeySep(source, sep string) Key {
return NewKey(strings.Split(source, sep)...)
}
//IsEmpty determines whether or not the length of k is 0.
func (k Key) IsEmpty() bool {
return k.Len() == 0
}
//Len returns the length of k.
func (k Key) Len() int {
return len(k)
}
//Equal determines whether or not k and other are the same length and all individual
//strings are identical at their respective indices.
func (k Key) Equal(other Key) bool {
if k.IsEmpty() && other.IsEmpty() {
return true
}
if len(k) != len(other) {
return false
}
for i, part := range k {
if part != other[i] {
return false
}
}
return true
}
//StartsWith determines whether or not k is at least the same length as other
//and all strings in other appear at the first consecutive indices of k.
func (k Key) StartsWith(other Key) bool {
if other.Len() > k.Len() {
return false
}
for i, part := range other {
if k[i] != part {
return false
}
}
return true
}
//EndsWith determines whether or not k is at least the same length as other
//and all strings in other appear at the last consecutive indices of k.
func (k Key) EndsWith(other Key) bool {
if other.Len() > k.Len() {
return false
}
for i := range other {
part := other[other.Len()-1-i]
if k[k.Len()-1-i] != part {
return false
}
}
return true
}
//Append returns a new Key with all strings from k and other.
func (k Key) Append(others ...Key) Key {
result := NewKey(k...)
for _, other := range others {
result = append(result, other...)
}
return result
}
//AppendStrings returns a new Key with all strings from k and others.
func (k Key) AppendStrings(others ...string) Key {
return k.Append(NewKey(others...))
}
//KeyParser defines an entity that can parse a string and turn it into a Key.
type KeyParser interface {
Parse(k string) Key
}
//KeyParserFunc is a func implementation of KeyParser that takes in a single string
//and returns a Key.
type KeyParserFunc func(k string) Key
//Parse simply calls pf(k).
func (pf KeyParserFunc) Parse(k string) Key {
return pf(k)
}
//SeparatorKeyParser is a KeyParser that creates Keys from the result of calling
//strings.Split() with k and string(SeparatorKeyParser).
type SeparatorKeyParser string
//Parse returns NewKeySep(k, string(p)).
func (p SeparatorKeyParser) Parse(k string) Key {
return NewKeySep(k, string(p))
}
//PeriodSeparatorKeyParser is the default KeyParser set to c.KeyParser in New().
//See SeparatorKeyParser.
const PeriodSeparatorKeyParser = SeparatorKeyParser(".")