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 13a0009 commit 35d3697
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
10 changes: 5 additions & 5 deletions db_ex.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func (c *Compounds) Reset() {
*c = (*c)[0:0]
}

func (c *Compounds) remove(s int) Compounds {
return append((*c)[:s], (*c)[s+1:]...)
}

func (c *Compounds) Delete(keys ...interface{}) {
for _, key := range keys {
for i, v := range *c {
Expand All @@ -185,11 +189,7 @@ func (c *Compounds) Delete(keys ...interface{}) {
}
delete(r, key)
if len(r) == 0 {
if i == 0 {
*c = (*c)[i+1:]
} else {
*c = append((*c)[0:i-1], (*c)[i+1:]...)
}
*c = c.remove(i)
}
}
}
Expand Down
40 changes: 25 additions & 15 deletions db_ex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCompounds(t *testing.T) {
func TestCompoundsDelete(t *testing.T) {
c := NewCompounds()
c.AddKV(`name_a`, `value_a`)
c.AddKV(`name_b`, `value_b`)
Expand All @@ -16,26 +16,36 @@ func TestCompounds(t *testing.T) {
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
toMap := func() map[string]interface{} {
data := map[string]interface{}{}
for _, _v := range c.Slice() {
cd := _v.(Cond)
for k, v := range cd {
data[fmt.Sprint(k)] = v
}
}
return data
}
assert.Equal(t, map[string]interface{}{
"name_d": "value_d",
"name_e": "value_e",
"name_f": "value_f",
}, data)
}, toMap())
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, map[string]interface{}{}, toMap())
assert.Equal(t, 0, c.Size())

c.AddKV(`name_e`, `value_e`)
assert.Equal(t, 1, c.Size())
c.AddKV(`name_f`, `value_f`)
assert.Equal(t, 2, c.Size())
assert.Equal(t, map[string]interface{}{
`name_e`: `value_e`,
"name_f": "value_f",
}, toMap())
c.Delete(`name_f`)
assert.Equal(t, 1, c.Size())
assert.Equal(t, map[string]interface{}{
`name_e`: `value_e`,
}, toMap())
}

0 comments on commit 35d3697

Please sign in to comment.