Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-datastore
Commits
aad33fb0
Commit
aad33fb0
authored
Oct 18, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
key: namespaces, ==, <, tests
parent
f318dc52
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
1 deletion
+68
-1
key.go
key.go
+42
-1
key_test.go
key_test.go
+26
-0
No files found.
key.go
View file @
aad33fb0
...
...
@@ -38,6 +38,11 @@ func NewKey(s string) Key {
return
k
}
// KeyWithNamespaces constructs a key out of a namespace slice.
func
KeyWithNamespaces
(
ns
[]
string
)
Key
{
return
NewKey
(
strings
.
Join
(
ns
,
"/"
))
}
// Clean up a Key, using path.Clean.
func
(
k
*
Key
)
Clean
()
{
k
.
string
=
path
.
Clean
(
"/"
+
k
.
string
)
...
...
@@ -53,6 +58,33 @@ func (k Key) Bytes() []byte {
return
[]
byte
(
k
.
string
)
}
// Equal checks equality of two keys
func
(
k
Key
)
Equal
(
k2
Key
)
bool
{
return
k
.
string
==
k2
.
string
}
// Less checks whether this key is sorted lower than another.
func
(
k
Key
)
Less
(
k2
Key
)
bool
{
list1
:=
k
.
List
()
list2
:=
k2
.
List
()
for
i
,
c1
:=
range
list1
{
if
len
(
list2
)
<
(
i
+
1
)
{
return
false
}
c2
:=
list2
[
i
]
if
c1
<
c2
{
return
true
}
else
if
c1
>
c2
{
return
false
}
// c1 == c2, continue
}
// list1 is shorter or exactly the same.
return
len
(
list1
)
<
len
(
list2
)
}
// List returns the `list` representation of this Key.
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// ["Comedy", "MontyPythong", "Actor:JohnCleese"]
...
...
@@ -69,7 +101,7 @@ func (k Key) Reverse() Key {
for
i
,
e
:=
range
l
{
r
[
len
(
l
)
-
i
-
1
]
=
e
}
return
NewKey
(
strings
.
Join
(
r
,
"/"
)
)
return
KeyWithNamespaces
(
r
)
}
// Namespaces returns the `namespaces` making up this Key.
...
...
@@ -187,7 +219,16 @@ func NamespaceType(namespace string) string {
return
strings
.
Join
(
parts
[
0
:
len
(
parts
)
-
1
],
":"
)
}
// NamespaceValue returns the last component of a namespace. `baz` in `f:b:baz`
func
NamespaceValue
(
namespace
string
)
string
{
parts
:=
strings
.
Split
(
namespace
,
":"
)
return
parts
[
len
(
parts
)
-
1
]
}
// KeySlice attaches the methods of sort.Interface to []Key,
// sorting in increasing order.
type
KeySlice
[]
Key
func
(
p
KeySlice
)
Len
()
int
{
return
len
(
p
)
}
func
(
p
KeySlice
)
Less
(
i
,
j
int
)
bool
{
return
p
[
i
]
.
Less
(
p
[
j
])
}
func
(
p
KeySlice
)
Swap
(
i
,
j
int
)
{
p
[
i
],
p
[
j
]
=
p
[
j
],
p
[
i
]
}
key_test.go
View file @
aad33fb0
...
...
@@ -62,6 +62,14 @@ func (ks *KeySuite) SubtestKey(s string, c *C) {
for
i
,
e
:=
range
NewKey
(
s
)
.
List
()
{
c
.
Check
(
namespaces
[
i
],
Equals
,
e
)
}
c
.
Check
(
NewKey
(
s
),
Equals
,
NewKey
(
s
))
c
.
Check
(
NewKey
(
s
)
.
Equal
(
NewKey
(
s
)),
Equals
,
true
)
c
.
Check
(
NewKey
(
s
)
.
Equal
(
NewKey
(
"/fdsafdsa/"
+
s
)),
Equals
,
false
)
// less
c
.
Check
(
NewKey
(
s
)
.
Less
(
NewKey
(
s
)
.
Parent
()),
Equals
,
false
)
c
.
Check
(
NewKey
(
s
)
.
Less
(
NewKey
(
s
)
.
Child
(
"foo"
)),
Equals
,
true
)
}
func
(
ks
*
KeySuite
)
TestKeyBasic
(
c
*
C
)
{
...
...
@@ -126,3 +134,21 @@ func (ks *KeySuite) TestRandom(c *C) {
}
CheckTrue
(
c
,
len
(
keys
)
==
1000
)
}
func
(
ks
*
KeySuite
)
TestLess
(
c
*
C
)
{
checkLess
:=
func
(
a
,
b
string
)
{
ak
:=
NewKey
(
a
)
bk
:=
NewKey
(
b
)
c
.
Check
(
ak
.
Less
(
bk
),
Equals
,
true
)
c
.
Check
(
bk
.
Less
(
ak
),
Equals
,
false
)
}
checkLess
(
"/a/b/c"
,
"/a/b/c/d"
)
checkLess
(
"/a/b"
,
"/a/b/c/d"
)
checkLess
(
"/a"
,
"/a/b/c/d"
)
checkLess
(
"/a/a/c"
,
"/a/b/c"
)
checkLess
(
"/a/a/d"
,
"/a/b/c"
)
checkLess
(
"/a/b/c/d/e/f/g/h"
,
"/b"
)
checkLess
(
"/"
,
"/a"
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment