Skip to content

Commit

Permalink
allow delete via array accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-carbonne committed Aug 7, 2019
1 parent 9f96af9 commit 7ec7b97
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
23 changes: 17 additions & 6 deletions sjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,12 @@ func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string,
if !found {
res = gjson.Get(jstr, paths[0].gpart)
}
if strings.HasPrefix(paths[0].gpart, "#") && strings.HasSuffix(paths[0].gpart, "#") {
if len(paths) > 1 && res.IsArray() {
buf = append(buf, []byte("[")...)
jstr = jstr[1:]

if strings.HasPrefix(paths[0].gpart, "#") && strings.HasSuffix(paths[0].gpart, "#") && res.IsArray() {
if len(paths) > 1 {
for _, ares := range res.Array() {
var tmpBuf []byte
tmpBuf, err = appendRawPaths(tmpBuf, ares.Raw, paths[1:], raw,
stringify, del)

start := strings.Index(jstr, ares.Raw)
end := len(ares.Raw)
jstr = jstr[:start] + string(tmpBuf) + jstr[start+end:]
Expand All @@ -267,6 +263,21 @@ func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string,
buf = append(buf, []byte(jstr)...)
return buf, nil
}
if del {
for _, ares := range res.Array() {
start := strings.Index(jstr, ares.Raw)
end := len(ares.Raw)
if jstr[:start][start-1:] == "," {
start--
end++
} else if jstr[start+end:][:1] == "," {
end++
}
jstr = jstr[:start] + jstr[start+end:]
}
buf = append(buf, []byte(jstr)...)
return buf, nil
}
}
if res.Index > 0 {
if len(paths) > 1 {
Expand Down
7 changes: 7 additions & 0 deletions sjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,17 @@ func TestDeleteArrayFirstMatch(t *testing.T) {
func TestDeleteArrayAllMatches(t *testing.T) {
// You can also query an array for find all matches with #(...)#

testRaw(t, setDelete, `["111111111","22222222"]`, `["111111111","22222222","333"]`, `#[="333"]#`, nil)
testRaw(t, setDelete, `["22222222","333"]`, `["111111111","22222222","333"]`, `#[="111111111"]#`, nil)
testRaw(t, setDelete, `["111111111","333"]`, `["111111111","22222222","333"]`, `#[="22222222"]#`, nil)

json := `{"friends":[{"first":"Dale","last":"Murphy","age":44,"nets":["ig","fb","tw"]},{"first":"Roger","last":"Craig","age":68,"nets":["fb","tw"]},{"first":"Jane","last":"Murphy","age":47,"nets":["ig","tw"]}]}`
expected := `{"friends":[{"first":"Dale","last":"Murphy","age":44,"nets":["ig","fb","tw"]},{"first":"Roger","age":68,"nets":["fb","tw"]},{"first":"Jane","age":47,"nets":["ig","tw"]}]}`
testRaw(t, setDelete, expected, json, `friends.#[age>45]#.last`, nil)

expected = `{"friends":[{"first":"Dale","last":"Murphy","age":44,"nets":["ig","fb","tw"]}]}`
testRaw(t, setDelete, expected, json, `friends.#[age>45]#`, nil)

}

var basicJSON = `{"age":100, "name":{"here":"B\\\"R"},
Expand Down

0 comments on commit 7ec7b97

Please sign in to comment.