diff --git a/properties.go b/properties.go index 6201f8b..694c84a 100644 --- a/properties.go +++ b/properties.go @@ -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 { diff --git a/properties_test.go b/properties_test.go index 6ae0b42..8d4c7c0 100644 --- a/properties_test.go +++ b/properties_test.go @@ -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.