Skip to content

Commit

Permalink
Fixed Del method for Section
Browse files Browse the repository at this point in the history
  • Loading branch information
LongHairedHacker committed Jun 27, 2019
1 parent 2fd31ce commit a970c4b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
13 changes: 5 additions & 8 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,13 @@ func (s *section) Del(name string) bool {
break
}
}
switch l := len(s.Options); {
case i == 0: // at the end, cut tail
s.Options = s.Options[i+1:]
case i == l-1: // at the beginning, move head
s.Options = s.Options[:l]
case i < l: // somewhere in the middle; move items around
s.Options = append(s.Options[:i], s.Options[i+1:]...)
default: // i == l; option does not exist

if i == len(s.Options) {
return false
}

s.Options = append(s.Options[:i], s.Options[i+1:]...)

return true
}

Expand Down
2 changes: 2 additions & 0 deletions uci.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ func (t *tree) Del(config, section, option string) {
// nor config do exist. hence, we've reached our desired state
return
}

sec := cfg.Get(section)
if sec == nil {
// same logic applies here
return
}

if sec.Del(option) {
cfg.tainted = true
}
Expand Down
28 changes: 28 additions & 0 deletions uci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,31 @@ func TestGetSections(t *testing.T) {
assert.True(exists)
assert.ElementsMatch(names, []string{"ntp"})
}

func TestSingleDelete(t *testing.T) {
assert := assert.New(t)

r := NewTree("testdata")

val, _ := r.Get("system", "ntp", "enable_server")
assert.NotEmpty(val)

r.Del("system", "ntp", "enable_server")

val, _ = r.Get("system", "ntp", "enable_server")
assert.Empty(val)
}

func TestListDelete(t *testing.T) {
assert := assert.New(t)

r := NewTree("testdata")

val, _ := r.Get("system", "ntp", "server")
assert.NotEmpty(val)

r.Del("system", "ntp", "server")

val, _ = r.Get("system", "ntp", "server")
assert.Empty(val)
}

0 comments on commit a970c4b

Please sign in to comment.