feat(users): add roles APIs (#63)
* feat(kvstore): add namespace operations for bool * feat(userstore): add methods for roles * chore(multiusers): remove useless todo * feat(multiusers): add apis for roles * test(roles): add e2e tests for role APIs * test(e2e/files): enable files tests
This commit is contained in:
parent
4b6f6d9e1f
commit
9748d0cab4
10 changed files with 316 additions and 11 deletions
|
@ -77,10 +77,14 @@ func (bp *BoltPvd) Close() error {
|
|||
}
|
||||
|
||||
func (bp *BoltPvd) GetBool(key string) (bool, bool) {
|
||||
return bp.GetBoolIn("bools", key)
|
||||
}
|
||||
|
||||
func (bp *BoltPvd) GetBoolIn(ns, key string) (bool, bool) {
|
||||
buf, ok := make([]byte, 1), false
|
||||
|
||||
bp.db.View(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte("bools"))
|
||||
b := tx.Bucket([]byte(ns))
|
||||
v := b.Get([]byte(key))
|
||||
copy(buf, v)
|
||||
ok = v != nil
|
||||
|
@ -92,23 +96,52 @@ func (bp *BoltPvd) GetBool(key string) (bool, bool) {
|
|||
}
|
||||
|
||||
func (bp *BoltPvd) SetBool(key string, val bool) error {
|
||||
return bp.SetBoolIn("bools", key, val)
|
||||
}
|
||||
|
||||
func (bp *BoltPvd) SetBoolIn(ns, key string, val bool) error {
|
||||
var bVal byte = 0
|
||||
if val {
|
||||
bVal = 1
|
||||
}
|
||||
return bp.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte("bools"))
|
||||
b := tx.Bucket([]byte(ns))
|
||||
return b.Put([]byte(key), []byte{bVal})
|
||||
})
|
||||
}
|
||||
|
||||
func (bp *BoltPvd) DelBool(key string) error {
|
||||
return bp.DelBoolIn("bools", key)
|
||||
}
|
||||
|
||||
func (bp *BoltPvd) DelBoolIn(ns, key string) error {
|
||||
return bp.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte("bools"))
|
||||
b := tx.Bucket([]byte(ns))
|
||||
return b.Delete([]byte(key))
|
||||
})
|
||||
}
|
||||
|
||||
func (bp *BoltPvd) ListBools() (map[string]bool, error) {
|
||||
return bp.ListBoolsIn("bools")
|
||||
}
|
||||
|
||||
func (bp *BoltPvd) ListBoolsIn(ns string) (map[string]bool, error) {
|
||||
list := map[string]bool{}
|
||||
err := bp.db.View(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte(ns))
|
||||
if b == nil {
|
||||
return ErrBucketNotFound
|
||||
}
|
||||
|
||||
b.ForEach(func(k, v []byte) error {
|
||||
list[string(k)] = (v[0] == 1)
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
})
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (bp *BoltPvd) GetInt(key string) (int, bool) {
|
||||
x, ok := bp.GetInt64(key)
|
||||
return int(x), ok
|
||||
|
@ -195,7 +228,7 @@ func (bp *BoltPvd) ListInt64sIn(ns string) (map[string]int64, error) {
|
|||
if n < 0 {
|
||||
return fmt.Errorf("fail to parse int64 for key (%s)", k)
|
||||
}
|
||||
list[fmt.Sprintf("%s", k)] = x
|
||||
list[string(k)] = x
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
|
@ -340,7 +373,7 @@ func (bp *BoltPvd) ListStringsIn(ns string) (map[string]string, error) {
|
|||
}
|
||||
|
||||
b.ForEach(func(k, v []byte) error {
|
||||
kv[fmt.Sprintf("%s", k)] = fmt.Sprintf("%s", v)
|
||||
kv[string(k)] = string(v)
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
|
|
|
@ -9,8 +9,13 @@ type IKVStore interface {
|
|||
AddNamespace(nsName string) error
|
||||
DelNamespace(nsName string) error
|
||||
GetBool(key string) (bool, bool)
|
||||
GetBoolIn(ns, key string) (bool, bool)
|
||||
SetBool(key string, val bool) error
|
||||
SetBoolIn(ns, key string, val bool) error
|
||||
DelBool(key string) error
|
||||
DelBoolIn(ns, key string) error
|
||||
ListBools() (map[string]bool, error)
|
||||
ListBoolsIn(ns string) (map[string]bool, error)
|
||||
GetInt(key string) (int, bool)
|
||||
SetInt(key string, val int) error
|
||||
DelInt(key string) error
|
||||
|
|
|
@ -15,6 +15,7 @@ func TestKVStoreProviders(t *testing.T) {
|
|||
var err error
|
||||
var ok bool
|
||||
key, boolV, intV, int64V, floatV, stringV := "key", true, 2027, int64(2027), 3.1415, "foobar"
|
||||
key2, boolV2 := "key2", false
|
||||
|
||||
kvstoreTest := func(store kvstore.IKVStore, t *testing.T) {
|
||||
// test bools
|
||||
|
@ -26,6 +27,19 @@ func TestKVStoreProviders(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Errorf("there should be no error %v", err)
|
||||
}
|
||||
err = store.SetBool(key2, boolV2)
|
||||
if err != nil {
|
||||
t.Errorf("there should be no error %v", err)
|
||||
}
|
||||
boolList, err := store.ListBools()
|
||||
if err != nil {
|
||||
t.Errorf("there should be no error %v", err)
|
||||
}
|
||||
if boolList[key] != boolV {
|
||||
t.Error("listBool incorrect val1")
|
||||
} else if boolList[key2] != boolV2 {
|
||||
t.Error("listBool incorrect val2")
|
||||
}
|
||||
boolVGot, ok := store.GetBool(key)
|
||||
if !ok {
|
||||
t.Error("value should exit")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue