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

Support nested array wildcard deletion #95

Closed
Closed
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
11 changes: 11 additions & 0 deletions gabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,17 @@ func (g *Container) Delete(hierarchy ...string) error {
if len(hierarchy) < 2 {
return errors.New("unable to delete array index at root of path")
}

// delete within one nested wildcard level
if hierarchy[1] == "*" {
for i := range array {
if obj, ok := array[i].(map[string]interface{}); ok {
delete(obj, target)
}
}
return nil
}

index, err := strconv.Atoi(target)
if err != nil {
return fmt.Errorf("failed to parse array index '%v': %v", target, err)
Expand Down
13 changes: 13 additions & 0 deletions gabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,19 @@ func TestDeletesWithArrays(t *testing.T) {
if actual := jsonParsed.String(); actual != expected {
t.Errorf("Unexpected result from array deletes: %v != %v", actual, expected)
}

jsonParsed, err = ParseJSON([]byte(rawJSON))
if err != nil {
t.Fatal(err)
}
if err = jsonParsed.Delete("outter", "*", "bar"); err != nil {
t.Error(err)
}

expected = `{"outter":[{"foo":{"value1":10,"value2":22,"value3":32}},{"baz":{"value1":null,"value2":null,"value3":null}}]}`
if actual := jsonParsed.String(); actual != expected {
t.Errorf("Unexpected result from array deletes: %v != %v", actual, expected)
}
}

func TestExamples(t *testing.T) {
Expand Down