Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Apr 16, 2022
1 parent b4c9389 commit 13a0009
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
23 changes: 23 additions & 0 deletions db_ex.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ func (c *Compounds) Reset() {
*c = (*c)[0:0]
}

func (c *Compounds) Delete(keys ...interface{}) {
for _, key := range keys {
for i, v := range *c {
r, y := v.(Cond)
if !y {
continue
}
_, ok := r[key]
if !ok {
continue
}
delete(r, key)
if len(r) == 0 {
if i == 0 {
*c = (*c)[i+1:]
} else {
*c = append((*c)[0:i-1], (*c)[i+1:]...)
}
}
}
}
}

type TableName interface {
TableName() string
}
Expand Down
41 changes: 41 additions & 0 deletions db_ex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package db

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCompounds(t *testing.T) {
c := NewCompounds()
c.AddKV(`name_a`, `value_a`)
c.AddKV(`name_b`, `value_b`)
c.AddKV(`name_c`, `value_c`)
c.AddKV(`name_d`, `value_d`)
c.AddKV(`name_e`, `value_e`)
c.AddKV(`name_f`, `value_f`)
c.Delete(`name_a`, `name_b`, `name_c`)
data := map[string]interface{}{}
for _, _v := range c.Slice() {
cd := _v.(Cond)
for k, v := range cd {
data[fmt.Sprint(k)] = v
}
}
assert.Equal(t, map[string]interface{}{
"name_d": "value_d",
"name_e": "value_e",
"name_f": "value_f",
}, data)
c.Delete(`name_c`, `name_d`, `name_e`, `name_f`)
data = map[string]interface{}{}
for _, _v := range c.Slice() {
cd := _v.(Cond)
for k, v := range cd {
data[fmt.Sprint(k)] = v
}
}
assert.Equal(t, map[string]interface{}{}, data)
assert.Equal(t, 0, c.Size())
}

0 comments on commit 13a0009

Please sign in to comment.