Key.s string -> []byte

This commit is contained in:
mossid 2018-09-09 21:20:37 +09:00
parent cc0e2c9523
commit 61bc1a98c4
1 changed files with 8 additions and 7 deletions

View File

@ -8,19 +8,20 @@ import (
// Wrapper for key string // Wrapper for key string
type Key struct { type Key struct {
s string s []byte
} }
// Appending two keys with '/' as separator // Appending two keys with '/' as separator
// Checks alpanumericity // Checks alphanumericity
func (k Key) Append(keys ...string) (res Key) { func (k Key) Append(keys ...string) (res Key) {
res = k res.s = make([]byte, len(k.s))
copy(res.s, k.s)
for _, key := range keys { for _, key := range keys {
if !tmlibs.IsASCIIText(key) { if !tmlibs.IsASCIIText(key) {
panic("parameter key expressions can only contain alphanumeric characters") panic("parameter key expressions can only contain alphanumeric characters")
} }
res.s = res.s + "/" + key res.s = append(append(res.s, byte('/')), []byte(key)...)
} }
return return
} }
@ -30,19 +31,19 @@ func NewKey(keys ...string) (res Key) {
if len(keys) < 1 { if len(keys) < 1 {
panic("length of parameter keys must not be zero") panic("length of parameter keys must not be zero")
} }
res = Key{keys[0]} res = Key{[]byte(keys[0])}
return res.Append(keys[1:]...) return res.Append(keys[1:]...)
} }
// KeyBytes make KVStore key bytes from Key // KeyBytes make KVStore key bytes from Key
func (k Key) Bytes() []byte { func (k Key) Bytes() []byte {
return []byte(k.s) return k.s
} }
// Human readable string // Human readable string
func (k Key) String() string { func (k Key) String() string {
return k.s return string(k.s)
} }
// Used for associating paramstore key and field of param structs // Used for associating paramstore key and field of param structs