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

add Delete function to tree #256

Merged
merged 4 commits into from Mar 1, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions toml.go
Expand Up @@ -296,6 +296,34 @@ func (t *Tree) SetPathWithComment(keys []string, comment string, commented bool,
subtree.values[keys[len(keys)-1]] = toInsert
}

// Delete removes a key from the tree.
// Key is a dot-separated path (e.g. a.b.c).
func (t *Tree) Delete(key string) error {
keys, err := parseKey(key)
if err != nil {
return err
}
return t.DeletePath(keys)
}

// Delete removes a key from the tree.
// Keys is an array of path elements (e.g. {"a","b","c"}).
func (t *Tree) DeletePath(keys []string) error {
keyLen := len(keys)
if keyLen == 1 {
delete(t.values, keys[0])
return nil
}
tree := t.GetPath(keys[:keyLen-1])
item := keys[keyLen-1]
switch node := tree.(type) {
case *Tree:
delete(node.values, item)
return nil
}
return errors.New("no such key to delete")
}

// createSubTree takes a tree and a key and create the necessary intermediate
// subtrees to create a subtree at that point. In-place.
//
Expand Down
54 changes: 54 additions & 0 deletions toml_test.go
Expand Up @@ -69,6 +69,60 @@ func TestTomlHasPath(t *testing.T) {
}
}

func TestTomlDelete(t *testing.T) {
tree, _ := Load(`
key = "value"
`)
err := tree.Delete("key")
if err != nil {
t.Errorf("Delete - unexpected error while deleting key: %s", err.Error())
}

if tree.Get("key") != nil {
t.Errorf("Delete should have removed key but did not.")
}

}

func TestTomlDeleteUnparsableKey(t *testing.T) {
tree, _ := Load(`
key = "value"
`)
err := tree.Delete(".")
if err == nil {
t.Errorf("Delete should error")
}
}

func TestTomlDeleteNestedKey(t *testing.T) {
tree, _ := Load(`
[foo]
[foo.bar]
key = "value"
`)
err := tree.Delete("foo.bar.key")
if err != nil {
t.Errorf("Error while deleting nested key: %s", err.Error())
}

if tree.Get("key") != nil {
t.Errorf("Delete should have removed nested key but did not.")
}

}

func TestTomlDeleteNonexistentNestedKey(t *testing.T) {
tree, _ := Load(`
[foo]
[foo.bar]
key = "value"
`)
err := tree.Delete("foo.not.there.key")
if err == nil {
t.Errorf("Delete should have thrown an error trying to delete key in nonexistent tree")
}
}

func TestTomlGetPath(t *testing.T) {
node := newTree()
//TODO: set other node data
Expand Down