add to from string
parent
b3a24aca42
commit
97b53aab90
49
db.go
49
db.go
|
|
@ -1,5 +1,7 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
type DB interface {
|
type DB interface {
|
||||||
Get(string) ([]byte, error)
|
Get(string) ([]byte, error)
|
||||||
Set(string, []byte) error
|
Set(string, []byte) error
|
||||||
|
|
@ -7,3 +9,50 @@ type DB interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultNamespace = "namespace"
|
var DefaultNamespace = "namespace"
|
||||||
|
|
||||||
|
type Type int
|
||||||
|
|
||||||
|
const (
|
||||||
|
MAP = Type(iota)
|
||||||
|
BOLT = Type(iota)
|
||||||
|
CACHE = Type(iota)
|
||||||
|
LEVELDB = Type(iota)
|
||||||
|
MEMCACHE = Type(iota)
|
||||||
|
MEMCACHECLUSTER = Type(iota)
|
||||||
|
MONGO = Type(iota)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t Type) String() string {
|
||||||
|
switch t {
|
||||||
|
case MAP:
|
||||||
|
return "map"
|
||||||
|
case BOLT:
|
||||||
|
return "bolt"
|
||||||
|
case CACHE:
|
||||||
|
return "cache"
|
||||||
|
case LEVELDB:
|
||||||
|
return "leveldb"
|
||||||
|
case MEMCACHE:
|
||||||
|
return "memcache"
|
||||||
|
case MEMCACHECLUSTER:
|
||||||
|
return "memcachecluster"
|
||||||
|
case MONGO:
|
||||||
|
return "mongo"
|
||||||
|
}
|
||||||
|
return "<unknown>"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TypeFromString(key string) Type {
|
||||||
|
key = strings.ToLower(key)
|
||||||
|
for i := 0; i < 30; i++ {
|
||||||
|
t := Type(i)
|
||||||
|
if t.String() == key {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Type(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(key Type) (DB, error) {
|
||||||
|
return nil, ErrNotImpl
|
||||||
|
}
|
||||||
|
|
|
||||||
45
db_test.go
45
db_test.go
|
|
@ -135,3 +135,48 @@ func TestImplementations(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToFromString(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
key string
|
||||||
|
t Type
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
key: "map",
|
||||||
|
t: MAP,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "bolt",
|
||||||
|
t: BOLT,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "cache",
|
||||||
|
t: CACHE,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "leveldb",
|
||||||
|
t: LEVELDB,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "memcache",
|
||||||
|
t: MEMCACHE,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "memcachecluster",
|
||||||
|
t: MEMCACHECLUSTER,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "mongo",
|
||||||
|
t: MONGO,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
if TypeFromString(c.key) != c.t {
|
||||||
|
t.Errorf("wrong type for %v: got %v", c.key, TypeFromString(c.key))
|
||||||
|
}
|
||||||
|
if c.key != c.t.String() {
|
||||||
|
t.Errorf("wrong string for %v (%v): got %v", int(c.t), c.key, c.t.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue