Documentation
¶
Overview ¶
Package key provides the Key interface and the KeySlice type, along with some utility functions around key.
Index ¶
- Variables
- func Compare(a, b Key) int
- func NamespaceType(namespace string) string
- func NamespaceValue(namespace string) string
- type BytesKey
- func (k BytesKey) Bytes() []byte
- func (k BytesKey) BytesUnsafe() []byte
- func (k BytesKey) Child(k2 Key) Key
- func (k BytesKey) ChildBytes(b []byte) Key
- func (k BytesKey) Equal(k2 Key) bool
- func (k BytesKey) HasPrefix(prefix Key) bool
- func (k BytesKey) HasSuffix(suffix Key) bool
- func (k BytesKey) IsAncestorOf(other Key) bool
- func (k BytesKey) IsDescendantOf(other Key) bool
- func (k BytesKey) KeyType() KeyType
- func (k BytesKey) Less(k2 Key) bool
- func (k BytesKey) MarshalJSON() ([]byte, error)
- func (k BytesKey) String() string
- func (k BytesKey) StringUnsafe() string
- func (k BytesKey) TrimPrefix(prefix Key) Key
- func (k BytesKey) TrimSuffix(suffix Key) Key
- func (k *BytesKey) UnmarshalJSON(data []byte) (err error)
- type Key
- func Clean(k Key) Key
- func EmptyKeyFromType(keyType KeyType) Key
- func NewKeyFromTypeAndBytes(keyType KeyType, bytes []byte) Key
- func NewKeyFromTypeAndString(keyType KeyType, s string) Key
- func QueryKeyFromTypeAndString(keyType KeyType, s string) Key
- func RandomBytesKey() Key
- func RandomKey() Keydeprecated
- func RandomStrKey() Key
- func StrsToBytesKeys(strs []string) []Key
- func StrsToKeys(strs []string) []Key
- func StrsToQueryKeys(strs []string) []Key
- func StrsToStrKeys(strs []string) []Key
- func TypeAndStrsToKeys(keyType KeyType, strs []string) []Key
- func TypeAndStrsToQueryKeys(keyType KeyType, strs []string) []Key
- type KeySlice
- type KeyType
- type StrKey
- func (k StrKey) BaseNamespace() string
- func (k StrKey) Bytes() []byte
- func (k StrKey) BytesUnsafe() []byte
- func (k StrKey) Child(k2 Key) Key
- func (k StrKey) ChildStrKey(k2 Key) StrKey
- func (k StrKey) ChildString(s string) Key
- func (k StrKey) ChildStringStrKey(s string) StrKey
- func (k *StrKey) Clean()
- func (k StrKey) Equal(k2 Key) bool
- func (k StrKey) HasPrefix(prefix Key) bool
- func (k StrKey) HasSuffix(suffix Key) bool
- func (k StrKey) Instance(s string) Key
- func (k StrKey) IsAncestorOf(other Key) bool
- func (k StrKey) IsDescendantOf(other Key) bool
- func (k StrKey) IsTopLevel() bool
- func (k StrKey) KeyType() KeyType
- func (k StrKey) Less(k2 Key) bool
- func (k StrKey) List() []string
- func (k StrKey) MarshalJSON() ([]byte, error)
- func (k StrKey) Name() string
- func (k StrKey) Namespaces() []string
- func (k StrKey) Parent() StrKey
- func (k StrKey) Path() Key
- func (k StrKey) Reverse() Key
- func (k StrKey) String() string
- func (k StrKey) StringUnsafe() string
- func (k StrKey) TrimPrefix(prefix Key) Key
- func (k StrKey) TrimSuffix(suffix Key) Key
- func (k StrKey) Type() string
- func (k *StrKey) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
var ( EmptyBytesKey = NewBytesKey([]byte{}) ErrNotBytesKey = errors.New("argument is not of type BytesKey") )
var ( EmptyStrKey = RawStrKey("/") ErrNotStrKey = errors.New("argument is not of type StrKey") )
var (
ErrKeyTypeNotSupported = errors.New("key type not supported")
)
Functions ¶
func Compare ¶
Compare returns an integer comparing two Keys lexicographically. The result will be 0 if a.Equal(b), -1 if a.Less(b), and +1 if b.Less(a).
func NamespaceType ¶
NamespaceType is the first component of a namespace. `foo` in `foo:bar`
func NamespaceValue ¶
NamespaceValue returns the last component of a namespace. `baz` in `f:b:baz`
Types ¶
type BytesKey ¶
type BytesKey struct {
// contains filtered or unexported fields
}
BytesKey is a Key implementation backed by byte slice. It could improve performance in some cases compared to StrKey by preventing type conversion and reducing key size.
func BytesKeyWithNamespaces ¶
KeyWithNamespaces constructs a key out of a namespace slice.
func NewBytesKey ¶
NewBytesKey constructs a BytesKey from byte slice.
func NewBytesKeyFromString ¶
NewBytesKeyFromString constructs a BytesKey from s.
func NewBytesKeyFromStringUnsafe ¶ added in v0.4.6
NewBytesKeyFromStringUnsafe constructs a BytesKey from `s` using "unsafe" package to avoid copying. Be cautious that `s` should be discarded right after calling this method because its value may change.
func (BytesKey) BytesUnsafe ¶ added in v0.4.6
Bytes returns the underlying byte slice of Key. You should probably not modify the returned byte slice as it may have unintended side effects.
func (BytesKey) Child ¶
Child returns the `child` Key of this Key.
NewBytesKey({{BYTES1}}).Child(NewBytesKey({{BYTES2}})) NewBytesKey({{BYTES1 || BYTES2}})
Panic if `k2` is not a BytesKey.
func (BytesKey) ChildBytes ¶
ChildBytes returns the `child` Key of this Key -- bytes helper.
NewBytesKey({{BYTES1}}).Child({{BYTES2}})) NewBytesKey({{BYTES1 || BYTES2}})
func (BytesKey) HasPrefix ¶
HasPrefix returns whether this key contains another as a prefix (including equals). Panic if `prefix` is not a BytesKey.
func (BytesKey) HasSuffix ¶
HasPrefix returns whether this key contains another as a suffix (including equals). Panic if `suffix` is not a BytesKey.
func (BytesKey) IsAncestorOf ¶
IsAncestorOf returns whether this key is a prefix of `other` (excluding equals).
NewBytesKey({{BYTES1}}).IsAncestorOf(NewBytesKey({{BYTES1 || BYTES2}})) true
Panic if `other` is not a BytesKey.
func (BytesKey) IsDescendantOf ¶
IsDescendantOf returns whether this key contains another as a prefix (excluding equals).
NewBytesKey({{BYTES1 || BYTES2}}).IsDescendantOf({{BYTES1}}) true
Panic if `other` is not a BytesKey.
func (BytesKey) Less ¶
Less checks whether this key is sorted lower than another. Panic if `k2` is not a BytesKey.
func (BytesKey) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface, keys are represented as base64-encoded JSON string
func (BytesKey) StringUnsafe ¶ added in v0.4.6
StringUnsafe gets the string value of Key using "unsafe" package to avoid copying. Be cautious that the string returned may change if the underlying byte slice is modified elsewhere, e.g. after being returned from BytesUnsafe.
func (BytesKey) TrimPrefix ¶
TrimPrefix returns a new key equals to this key without the provided leading prefix key. If `s` doesn't start with prefix, this key is returned unchanged. Panic if `prefix` is not a BytesKey.
func (BytesKey) TrimSuffix ¶
TrimSuffix returns a new key equals to this key without the provided trailing suffix key. If `s` doesn't end with suffix, this key is returned unchanged. Panic if `suffix` is not a BytesKey.
func (*BytesKey) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface, keys will decode base64-encoded JSON string to raw bytes
type Key ¶
type Key interface { fmt.Stringer // KeyType returns the key type. KeyType() KeyType // StringUnsafe returns the string value of Key. // It's the same as String for StrKey. StringUnsafe() string // Bytes returns the value of Key as a []byte. Bytes() []byte // Bytes returns the value of Key as a []byte. You should probably not // modify the returned byte slice as it may have unintended side effects. BytesUnsafe() []byte // Equal checks equality of two keys. Equal(k2 Key) bool // Less checks whether this key is sorted lower than another. Less(k2 Key) bool // Child returns the `child` Key of this Key. Child(k2 Key) Key // IsAncestorOf returns whether this key is a prefix of `other`. IsAncestorOf(other Key) bool // IsDescendantOf returns whether this key contains another as a prefix (excluding equals). IsDescendantOf(other Key) bool // HasPrefix returns whether this key contains another as a prefix (including equals). HasPrefix(prefix Key) bool // HasPrefix returns whether this key contains another as a suffix (including equals). HasSuffix(suffix Key) bool // TrimPrefix returns a new key equals to this key without the provided leading prefix key. // If s doesn't start with prefix, this key is returned unchanged. TrimPrefix(prefix Key) Key // TrimSuffix returns a new key equals to this key without the provided trailing suffix key. // If s doesn't end with suffix, this key is returned unchanged. TrimSuffix(suffix Key) Key // MarshalJSON implements the json.Marshaler interface, // keys are represented as JSON strings. MarshalJSON() ([]byte, error) }
A Key represents the unique identifier of an object. Keys are meant to be unique across a system. There are two Key implementations: StrKey backed by string and BytesKey backed by byte slice.
func EmptyKeyFromType ¶ added in v0.4.7
EmptyKeyFromType returns the empty key of keyType. for StrKey: RawStrKey("/") for BytesKey: NewBytesKey([]byte{})
func NewKeyFromTypeAndBytes ¶ added in v0.4.8
NewKeyFromTypeAndBytes constructs a Key of keyType from bytes.
func NewKeyFromTypeAndString ¶ added in v0.4.6
NewKeyFromTypeAndString constructs a Key of keyType from s.
func QueryKeyFromTypeAndString ¶ added in v0.4.6
QueryKeyFromTypeAndString constructs a Key of keyType from s. For StrKey, is uses QueryStrKey instead of NewStrKey.
func RandomBytesKey ¶
func RandomBytesKey() Key
RandomBytesKey returns a randomly (uuid) generated key.
RandomBytesKey() NewBytesKey([]byte("f98719ea086343f7b71f32ea9d9d521d"))
func RandomStrKey ¶
func RandomStrKey() Key
RandomStrKey returns a randomly (uuid) generated key.
RandomStrKey() NewStrKey("/f98719ea086343f7b71f32ea9d9d521d")
func StrsToBytesKeys ¶
func StrsToKeys ¶
StrsToKeys is an alias that proxies calls to StrsToStrKeys for backwards compatibility.
func StrsToQueryKeys ¶
func StrsToStrKeys ¶ added in v0.4.6
func TypeAndStrsToKeys ¶ added in v0.4.6
TypeAndStrsToKeys constructs a slice of Key of keyType from strs.
func TypeAndStrsToQueryKeys ¶ added in v0.4.6
type KeySlice ¶
type KeySlice []Key
KeySlice attaches the methods of sort.Interface to []Key, sorting in increasing order.
type StrKey ¶
type StrKey struct {
// contains filtered or unexported fields
}
A StrKey represents the unique identifier of an object. StrKeys are meant to be unique across a system.
Our Key scheme is inspired by file systems and Google App Engine key model. StrKeys are hierarchical, incorporating more and more specific namespaces. Thus keys can be deemed 'children' or 'ancestors' of other keys::
StrKey("/Comedy") StrKey("/Comedy/MontyPython")
Also, every namespace can be parametrized to embed relevant object information. For example, the StrKey `name` (most specific namespace) could include the object type::
StrKey("/Comedy/MontyPython/Actor:JohnCleese") StrKey("/Comedy/MontyPython/Sketch:CheeseShop") StrKey("/Comedy/MontyPython/Sketch:CheeseShop/Character:Mousebender")
func KeyWithNamespaces ¶
KeyWithNamespaces constructs a key out of a namespace slice.
func NewStrKeyFromBytes ¶ added in v0.4.8
func QueryStrKey ¶
QueryStrKey creates a new StrKey without safety checking the input, intended to be used for query. Use with care.
func (StrKey) BaseNamespace ¶
BaseNamespace returns the "base" namespace of this key (path.Base(filename))
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").BaseNamespace() "Actor:JohnCleese"
func (StrKey) BytesUnsafe ¶ added in v0.4.6
BytesUnsafe gets the value of Key as a []byte using "unsafe" package to avoid copying. You should probably not modify the returned byte slice as it may have unintended side effects.
func (StrKey) Child ¶
Child returns the `child` Key of this Key.
NewStrKey("/Comedy/MontyPython").Child(NewStrKey("Actor:JohnCleese")) NewStrKey("/Comedy/MontyPython/Actor:JohnCleese")
Panic if `k2` is not a StrKey.
func (StrKey) ChildStrKey ¶
ChildStrKey is the same as Child but returns a StrKey.
func (StrKey) ChildString ¶
ChildString returns the `child` Key of this Key -- string helper.
NewStrKey("/Comedy/MontyPython").ChildString("Actor:JohnCleese") NewStrKey("/Comedy/MontyPython/Actor:JohnCleese")
func (StrKey) ChildStringStrKey ¶
ChildStringStrKey is the same as ChildString but returns a StrKey.
func (StrKey) HasPrefix ¶
HasPrefix returns whether this key contains another as a prefix (including equals). Panic if `prefix` is not a StrKey.
func (StrKey) HasSuffix ¶
HasPrefix returns whether this key contains another as a suffix (including equals). Panic if `suffix` is not a StrKey.
func (StrKey) Instance ¶
Instance returns an "instance" of this type key (appends value to namespace).
NewStrKey("/Comedy/MontyPython/Actor").Instance("JohnClesse") NewStrKey("/Comedy/MontyPython/Actor:JohnCleese")
func (StrKey) IsAncestorOf ¶
IsAncestorOf returns whether this key is a prefix of `other` (excluding equals).
NewStrKey("/Comedy").IsAncestorOf("/Comedy/MontyPython") true
Panic if `other` is not a StrKey.
func (StrKey) IsDescendantOf ¶
IsDescendantOf returns whether this key contains another as a prefix (excluding equals).
NewStrKey("/Comedy/MontyPython").IsDescendantOf("/Comedy") true
Panic if `other` is not a StrKey.
func (StrKey) IsTopLevel ¶
IsTopLevel returns whether this key has only one namespace.
func (StrKey) Less ¶
Less checks whether this key is sorted lower than another. Panic if `k2` is not a StrKey.
func (StrKey) List ¶
List returns the `list` representation of this Key.
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").List() ["Comedy", "MontyPythong", "Actor:JohnCleese"]
func (StrKey) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface, keys are represented as JSON strings
func (StrKey) Name ¶
Name returns the "name" of this key (field of last namespace).
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").Name() "JohnCleese"
func (StrKey) Namespaces ¶
Namespaces returns the `namespaces` making up this Key.
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").Namespaces() ["Comedy", "MontyPython", "Actor:JohnCleese"]
func (StrKey) Parent ¶
Parent returns the `parent` Key of this Key.
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").Parent() NewStrKey("/Comedy/MontyPython")
func (StrKey) Path ¶
Path returns the "path" of this key (parent + type).
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").Path() NewStrKey("/Comedy/MontyPython/Actor")
func (StrKey) Reverse ¶
Reverse returns the reverse of this Key.
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").Reverse() NewStrKey("/Actor:JohnCleese/MontyPython/Comedy")
func (StrKey) StringUnsafe ¶ added in v0.4.6
StringUnsafe is the same as String
func (StrKey) TrimPrefix ¶
TrimPrefix returns a new key equals to this key without the provided leading prefix key. If s doesn't start with prefix, this key is returned unchanged. Panic if `prefix` is not a StrKey.
func (StrKey) TrimSuffix ¶
TrimSuffix returns a new key equals to this key without the provided trailing suffix key. If s doesn't end with suffix, this key is returned unchanged. Panic if `suffix` is not a StrKey.
func (StrKey) Type ¶
Type returns the "type" of this key (value of last namespace).
NewStrKey("/Comedy/MontyPython/Actor:JohnCleese").Type() "Actor"
func (*StrKey) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface, keys will parse any value specified as a key to a string