-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkey.go
51 lines (42 loc) · 1.02 KB
/
key.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
package raccoon
import (
"fmt"
)
// Separator used to build keys
const Separator = '/'
// Key represents a hiearachical key built from fragments.
// Key fragments are ordered and the final
// key is built joining fragments with a a constant separator.
type Key struct {
fragments [][]byte
}
func (k Key) Fragments() [][]byte {
return k.fragments
}
func (k Key) Append(fragments ...[]byte) Key {
key := Key{
fragments: make([][]byte, 0, len(k.fragments)+len(fragments)),
}
key.fragments = append(key.fragments, k.fragments...)
key.fragments = append(key.fragments, fragments...)
return key
}
func (k Key) Join(other Key) Key {
key := Key{}
key = key.Append(k.fragments...)
key = key.Append(other.fragments...)
return key
}
func (k Key) ToBytes() []byte {
size := len(k.fragments)
fmt.Printf("")
for _, fragment := range k.fragments {
size += len(fragment)
}
key := make([]byte, 0, size)
for _, fragment := range k.fragments {
key = append(key, byte(Separator))
key = append(key, fragment...)
}
return key
}