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
f318dc52
Commit
f318dc52
authored
Oct 18, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
linting
parent
60ebc564
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
43 deletions
+47
-43
key.go
key.go
+47
-43
No files found.
key.go
View file @
f318dc52
package
datastore
package
datastore
import
(
import
(
"github.com/jbenet/datastore.go/Godeps/_workspace/src/code.google.com/p/go-uuid/uuid"
"path"
"path"
"strings"
"strings"
"github.com/jbenet/datastore.go/Godeps/_workspace/src/code.google.com/p/go-uuid/uuid"
)
)
/*
/*
...
@@ -30,35 +31,36 @@ type Key struct {
...
@@ -30,35 +31,36 @@ type Key struct {
string
string
}
}
// NewKey constructs a key from string. it will clean the value.
func
NewKey
(
s
string
)
Key
{
func
NewKey
(
s
string
)
Key
{
k
:=
Key
{
s
}
k
:=
Key
{
s
}
k
.
Clean
()
k
.
Clean
()
return
k
return
k
}
}
// Clean
s
up a Key, using path.Clean.
// Clean up a Key, using path.Clean.
func
(
k
*
Key
)
Clean
()
{
func
(
k
*
Key
)
Clean
()
{
k
.
string
=
path
.
Clean
(
"/"
+
k
.
string
)
k
.
string
=
path
.
Clean
(
"/"
+
k
.
string
)
}
}
//
Return
s the string value of Key
//
Strings i
s the string value of Key
func
(
k
Key
)
String
()
string
{
func
(
k
Key
)
String
()
string
{
return
k
.
string
return
k
.
string
}
}
//
R
eturns the
bytes
value of Key
//
Bytes r
eturns the
string
value of Key
as a []byte
func
(
k
Key
)
Bytes
()
[]
byte
{
func
(
k
Key
)
Bytes
()
[]
byte
{
return
[]
byte
(
k
.
string
)
return
[]
byte
(
k
.
string
)
}
}
//
R
eturns the `list` representation of this Key.
//
List r
eturns the `list` representation of this Key.
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// ["Comedy", "MontyPythong", "Actor:JohnCleese"]
// ["Comedy", "MontyPythong", "Actor:JohnCleese"]
func
(
k
Key
)
List
()
[]
string
{
func
(
k
Key
)
List
()
[]
string
{
return
strings
.
Split
(
k
.
string
,
"/"
)[
1
:
]
return
strings
.
Split
(
k
.
string
,
"/"
)[
1
:
]
}
}
// Returns the reverse of this Key.
// Re
verse re
turns the reverse of this Key.
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").Reverse()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").Reverse()
// NewKey("/Actor:JohnCleese/MontyPython/Comedy")
// NewKey("/Actor:JohnCleese/MontyPython/Comedy")
func
(
k
Key
)
Reverse
()
Key
{
func
(
k
Key
)
Reverse
()
Key
{
...
@@ -70,14 +72,14 @@ func (k Key) Reverse() Key {
...
@@ -70,14 +72,14 @@ func (k Key) Reverse() Key {
return
NewKey
(
strings
.
Join
(
r
,
"/"
))
return
NewKey
(
strings
.
Join
(
r
,
"/"
))
}
}
//
R
eturns the `namespaces` making up this Key.
//
Namespaces r
eturns the `namespaces` making up this Key.
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// ["Comedy", "MontyPythong", "Actor:JohnCleese"]
// ["Comedy", "MontyPythong", "Actor:JohnCleese"]
func
(
k
Key
)
Namespaces
()
[]
string
{
func
(
k
Key
)
Namespaces
()
[]
string
{
return
k
.
List
()
return
k
.
List
()
}
}
//
R
eturns the "base" namespace of this key (
like
path.Base(filename))
//
BaseNamespace r
eturns the "base" namespace of this key (path.Base(filename))
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").BaseNamespace()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").BaseNamespace()
// "Actor:JohnCleese"
// "Actor:JohnCleese"
func
(
k
Key
)
BaseNamespace
()
string
{
func
(
k
Key
)
BaseNamespace
()
string
{
...
@@ -85,28 +87,28 @@ func (k Key) BaseNamespace() string {
...
@@ -85,28 +87,28 @@ func (k Key) BaseNamespace() string {
return
n
[
len
(
n
)
-
1
]
return
n
[
len
(
n
)
-
1
]
}
}
//
R
eturns the "type" of this key (value of last namespace).
//
Type r
eturns the "type" of this key (value of last namespace).
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// "Actor"
// "Actor"
func
(
k
Key
)
Type
()
string
{
func
(
k
Key
)
Type
()
string
{
return
NamespaceType
(
k
.
BaseNamespace
())
return
NamespaceType
(
k
.
BaseNamespace
())
}
}
//
R
eturns the "name" of this key (field of last namespace).
//
Name r
eturns the "name" of this key (field of last namespace).
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// "Actor"
// "Actor"
func
(
k
Key
)
Name
()
string
{
func
(
k
Key
)
Name
()
string
{
return
NamespaceValue
(
k
.
BaseNamespace
())
return
NamespaceValue
(
k
.
BaseNamespace
())
}
}
//
R
eturns an "instance" of this type key (appends value to namespace).
//
Instance r
eturns an "instance" of this type key (appends value to namespace).
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").List()
// "JohnCleese"
// "JohnCleese"
func
(
k
Key
)
Instance
(
s
string
)
Key
{
func
(
k
Key
)
Instance
(
s
string
)
Key
{
return
NewKey
(
k
.
string
+
":"
+
s
)
return
NewKey
(
k
.
string
+
":"
+
s
)
}
}
//
R
eturns the "path" of this key (parent + type).
//
Path r
eturns the "path" of this key (parent + type).
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").Path()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").Path()
// NewKey("/Comedy/MontyPython/Actor")
// NewKey("/Comedy/MontyPython/Actor")
func
(
k
Key
)
Path
()
Key
{
func
(
k
Key
)
Path
()
Key
{
...
@@ -114,7 +116,7 @@ func (k Key) Path() Key {
...
@@ -114,7 +116,7 @@ func (k Key) Path() Key {
return
NewKey
(
s
)
return
NewKey
(
s
)
}
}
//
R
eturns the `parent` Key of this Key.
//
Parent r
eturns the `parent` Key of this Key.
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").Parent()
// NewKey("/Comedy/MontyPython/Actor:JohnCleese").Parent()
// NewKey("/Comedy/MontyPython")
// NewKey("/Comedy/MontyPython")
func
(
k
Key
)
Parent
()
Key
{
func
(
k
Key
)
Parent
()
Key
{
...
@@ -125,14 +127,14 @@ func (k Key) Parent() Key {
...
@@ -125,14 +127,14 @@ func (k Key) Parent() Key {
return
NewKey
(
strings
.
Join
(
n
[
:
len
(
n
)
-
1
],
"/"
))
return
NewKey
(
strings
.
Join
(
n
[
:
len
(
n
)
-
1
],
"/"
))
}
}
//
R
eturns the `child` Key of this Key.
//
Child r
eturns the `child` Key of this Key.
// NewKey("/Comedy/MontyPython").Child("Actor:JohnCleese")
// NewKey("/Comedy/MontyPython").Child("Actor:JohnCleese")
// NewKey("/Comedy/MontyPython/Actor:JohnCleese")
// NewKey("/Comedy/MontyPython/Actor:JohnCleese")
func
(
k
Key
)
Child
(
s
string
)
Key
{
func
(
k
Key
)
Child
(
s
string
)
Key
{
return
NewKey
(
k
.
string
+
"/"
+
s
)
return
NewKey
(
k
.
string
+
"/"
+
s
)
}
}
//
R
eturns whether this key is a
n ancestor
of `other`
//
IsAncestorOf r
eturns whether this key is a
prefix
of `other`
// NewKey("/Comedy").IsAncestorOf("/Comedy/MontyPython")
// NewKey("/Comedy").IsAncestorOf("/Comedy/MontyPython")
// true
// true
func
(
k
Key
)
IsAncestorOf
(
other
Key
)
bool
{
func
(
k
Key
)
IsAncestorOf
(
other
Key
)
bool
{
...
@@ -142,7 +144,7 @@ func (k Key) IsAncestorOf(other Key) bool {
...
@@ -142,7 +144,7 @@ func (k Key) IsAncestorOf(other Key) bool {
return
strings
.
HasPrefix
(
other
.
string
,
k
.
string
)
return
strings
.
HasPrefix
(
other
.
string
,
k
.
string
)
}
}
//
R
eturns whether this key
is a descendent of `other`
//
IsDescendantOf r
eturns whether this key
contains another as a prefix.
// NewKey("/Comedy/MontyPython").IsDescendantOf("/Comedy")
// NewKey("/Comedy/MontyPython").IsDescendantOf("/Comedy")
// true
// true
func
(
k
Key
)
IsDescendantOf
(
other
Key
)
bool
{
func
(
k
Key
)
IsDescendantOf
(
other
Key
)
bool
{
...
@@ -152,11 +154,12 @@ func (k Key) IsDescendantOf(other Key) bool {
...
@@ -152,11 +154,12 @@ func (k Key) IsDescendantOf(other Key) bool {
return
strings
.
HasPrefix
(
k
.
string
,
other
.
string
)
return
strings
.
HasPrefix
(
k
.
string
,
other
.
string
)
}
}
// IsTopLevel returns whether this key has only one namespace.
func
(
k
Key
)
IsTopLevel
()
bool
{
func
(
k
Key
)
IsTopLevel
()
bool
{
return
len
(
k
.
List
())
==
1
return
len
(
k
.
List
())
==
1
}
}
// Returns a randomly (uuid) generated key.
// R
andomKey r
eturns a randomly (uuid) generated key.
// RandomKey()
// RandomKey()
// NewKey("/f98719ea086343f7b71f32ea9d9d521d")
// NewKey("/f98719ea086343f7b71f32ea9d9d521d")
func
RandomKey
()
Key
{
func
RandomKey
()
Key
{
...
@@ -175,6 +178,7 @@ A namespace can optionally include a type (delimited by ':')
...
@@ -175,6 +178,7 @@ A namespace can optionally include a type (delimited by ':')
Music:Song
Music:Song
*/
*/
// NamespaceType is the first component of a namespace. `foo` in `foo:bar`
func
NamespaceType
(
namespace
string
)
string
{
func
NamespaceType
(
namespace
string
)
string
{
parts
:=
strings
.
Split
(
namespace
,
":"
)
parts
:=
strings
.
Split
(
namespace
,
":"
)
if
len
(
parts
)
<
2
{
if
len
(
parts
)
<
2
{
...
...
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