Update error message

This commit is contained in:
tidwall 2021-09-04 08:00:45 -07:00
parent 0bc94ab89f
commit 64a7cf9bd5
1 changed files with 3 additions and 7 deletions

View File

@ -3,7 +3,6 @@ package sjson
import ( import (
jsongo "encoding/json" jsongo "encoding/json"
"errors"
"sort" "sort"
"strconv" "strconv"
"unsafe" "unsafe"
@ -548,8 +547,6 @@ func set(jstr, path, raw string,
return buf, nil return buf, nil
} }
} }
// parse the path, make sure that it does not contain invalid characters
// such as '#', '?', '*'
var paths []pathResult var paths []pathResult
r, simple := parsePath(path) r, simple := parsePath(path)
if simple { if simple {
@ -565,7 +562,7 @@ func set(jstr, path, raw string,
if !simple { if !simple {
if del { if del {
return []byte(jstr), return []byte(jstr),
errors.New("cannot delete value from a complex path") &errorType{"cannot delete value from a complex path"}
} }
return setComplexPath(jstr, path, raw, stringify) return setComplexPath(jstr, path, raw, stringify)
} }
@ -579,7 +576,7 @@ func set(jstr, path, raw string,
func setComplexPath(jstr, path, raw string, stringify bool) ([]byte, error) { func setComplexPath(jstr, path, raw string, stringify bool) ([]byte, error) {
res := gjson.Get(jstr, path) res := gjson.Get(jstr, path)
if !res.Exists() || !(res.Index != 0 || len(res.Indexes) != 0) { if !res.Exists() || !(res.Index != 0 || len(res.Indexes) != 0) {
return []byte(jstr), errors.New("no values found at path") return []byte(jstr), errNoChange
} }
if res.Index != 0 { if res.Index != 0 {
njson := []byte(jstr[:res.Index]) njson := []byte(jstr[:res.Index])
@ -602,8 +599,7 @@ func setComplexPath(jstr, path, raw string, stringify bool) ([]byte, error) {
return true return true
}) })
if len(res.Indexes) != len(vals) { if len(res.Indexes) != len(vals) {
return []byte(jstr), return []byte(jstr), errNoChange
errors.New("could not set value due to index mismatch")
} }
for i := 0; i < len(res.Indexes); i++ { for i := 0; i < len(res.Indexes); i++ {
vals[i].index = res.Indexes[i] vals[i].index = res.Indexes[i]