Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove keys #6

Merged
merged 4 commits into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,22 @@ func (p *Properties) WriteComment(w io.Writer, prefix string, enc Encoding) (n i

// ----------------------------------------------------------------------------

// Delete removes the key and its comments
// abides by the rules of the builtin delete()
func (p *Properties) Delete(key string) () {
delete(p.m, key)
delete(p.c, key)
newKeys := []string{}
for _, k := range p.k {
if k != key {
newKeys = append(newKeys, key)
}
}
p.k = newKeys
}

// ----------------------------------------------------------------------------

// check expands all values and returns an error if a circular reference or
// a malformed expression was found.
func (p *Properties) check() error {
Expand Down
26 changes: 26 additions & 0 deletions properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,32 @@ func (s *TestSuite) TestPanicOn32BitUintOverflow(c *C) {
c.Assert(func() { p.MustGetUint("max") }, PanicMatches, ".* out of range")
}

func (s *TestSuite) TestDeleteKey(c *C) {
input := "#comments should also be gone\nkey=to-be-deleted\nsecond=key"
p, err := parse(input)
c.Assert(err, IsNil)
c.Check(len(p.m), Equals, 2)
c.Check(len(p.c), Equals, 1)
c.Check(len(p.k), Equals, 2)
p.Delete("key")
c.Check(len(p.m), Equals, 1)
c.Check(len(p.c), Equals, 0)
c.Check(len(p.k), Equals, 1)
}

func (s *TestSuite) TestDeleteUnknownKey(c *C) {
input := "#comments should also be gone\nkey=to-be-deleted"
p, err := parse(input)
c.Assert(err, IsNil)
c.Check(len(p.m), Equals, 1)
c.Check(len(p.c), Equals, 1)
c.Check(len(p.k), Equals, 1)
p.Delete("wrong-key")
c.Check(len(p.m), Equals, 1)
c.Check(len(p.c), Equals, 1)
c.Check(len(p.k), Equals, 1)
}

// ----------------------------------------------------------------------------

// tests all combinations of delimiters, leading and/or trailing whitespace and newlines.
Expand Down