2016-10-19 01:52:46 +03:00
|
|
|
// Package sjson provides setting json values.
|
|
|
|
package sjson
|
|
|
|
|
|
|
|
import (
|
|
|
|
jsongo "encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type errorType struct {
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *errorType) Error() string {
|
|
|
|
return err.msg
|
|
|
|
}
|
|
|
|
|
2016-11-04 16:00:44 +03:00
|
|
|
// Options represents additional options for the Set and Delete functions.
|
2016-10-25 21:10:26 +03:00
|
|
|
type Options struct {
|
|
|
|
// Optimistic is a hint that the value likely exists which
|
|
|
|
// allows for the sjson to perform a fast-track search and replace.
|
|
|
|
Optimistic bool
|
2016-10-25 22:42:02 +03:00
|
|
|
// ReplaceInPlace is a hint to replace the input json rather than
|
|
|
|
// allocate a new json byte slice. When this field is specified
|
|
|
|
// the input json will not longer be valid and it should not be used
|
2016-11-04 16:00:44 +03:00
|
|
|
// In the case when the destination slice doesn't have enough free
|
|
|
|
// bytes to replace the data in place, a new bytes slice will be
|
|
|
|
// created under the hood.
|
2016-10-25 22:42:02 +03:00
|
|
|
// The Optimistic flag must be set to true and the input must be a
|
|
|
|
// byte slice in order to use this field.
|
|
|
|
ReplaceInPlace bool
|
2016-10-25 21:10:26 +03:00
|
|
|
}
|
|
|
|
|
2016-10-19 01:52:46 +03:00
|
|
|
type pathResult struct {
|
|
|
|
part string // current key part
|
2018-09-17 19:56:37 +03:00
|
|
|
gpart string // gjson get part
|
2016-10-19 01:52:46 +03:00
|
|
|
path string // remaining path
|
|
|
|
force bool // force a string key
|
|
|
|
more bool // there is more path to parse
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePath(path string) (pathResult, error) {
|
|
|
|
var r pathResult
|
|
|
|
if len(path) > 0 && path[0] == ':' {
|
|
|
|
r.force = true
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
for i := 0; i < len(path); i++ {
|
|
|
|
if path[i] == '.' {
|
|
|
|
r.part = path[:i]
|
2018-09-17 19:56:37 +03:00
|
|
|
r.gpart = path[:i]
|
2016-10-19 01:52:46 +03:00
|
|
|
r.path = path[i+1:]
|
|
|
|
r.more = true
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
if path[i] == '*' || path[i] == '?' {
|
|
|
|
return r, &errorType{"wildcard characters not allowed in path"}
|
|
|
|
} else if path[i] == '#' {
|
|
|
|
return r, &errorType{"array access character not allowed in path"}
|
|
|
|
}
|
|
|
|
if path[i] == '\\' {
|
|
|
|
// go into escape mode. this is a slower path that
|
|
|
|
// strips off the escape character from the part.
|
|
|
|
epart := []byte(path[:i])
|
2018-09-17 19:56:37 +03:00
|
|
|
gpart := []byte(path[:i+1])
|
2016-10-19 01:52:46 +03:00
|
|
|
i++
|
|
|
|
if i < len(path) {
|
|
|
|
epart = append(epart, path[i])
|
2018-09-17 19:56:37 +03:00
|
|
|
gpart = append(gpart, path[i])
|
2016-10-19 01:52:46 +03:00
|
|
|
i++
|
|
|
|
for ; i < len(path); i++ {
|
|
|
|
if path[i] == '\\' {
|
2018-09-17 19:56:37 +03:00
|
|
|
gpart = append(gpart, '\\')
|
2016-10-19 01:52:46 +03:00
|
|
|
i++
|
|
|
|
if i < len(path) {
|
|
|
|
epart = append(epart, path[i])
|
2018-09-17 19:56:37 +03:00
|
|
|
gpart = append(gpart, path[i])
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if path[i] == '.' {
|
|
|
|
r.part = string(epart)
|
2018-09-17 19:56:37 +03:00
|
|
|
r.gpart = string(gpart)
|
2016-10-19 01:52:46 +03:00
|
|
|
r.path = path[i+1:]
|
|
|
|
r.more = true
|
|
|
|
return r, nil
|
|
|
|
} else if path[i] == '*' || path[i] == '?' {
|
2016-10-25 22:42:02 +03:00
|
|
|
return r, &errorType{
|
|
|
|
"wildcard characters not allowed in path"}
|
2016-10-19 01:52:46 +03:00
|
|
|
} else if path[i] == '#' {
|
2016-10-25 22:42:02 +03:00
|
|
|
return r, &errorType{
|
|
|
|
"array access character not allowed in path"}
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
epart = append(epart, path[i])
|
2018-09-17 19:56:37 +03:00
|
|
|
gpart = append(gpart, path[i])
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// append the last part
|
|
|
|
r.part = string(epart)
|
2018-09-17 19:56:37 +03:00
|
|
|
r.gpart = string(gpart)
|
2016-10-19 01:52:46 +03:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.part = path
|
2018-09-17 19:56:37 +03:00
|
|
|
r.gpart = path
|
2016-10-19 01:52:46 +03:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2016-10-25 22:42:02 +03:00
|
|
|
func mustMarshalString(s string) bool {
|
2016-10-19 01:52:46 +03:00
|
|
|
for i := 0; i < len(s); i++ {
|
2018-07-04 04:32:43 +03:00
|
|
|
if s[i] < ' ' || s[i] > 0x7f || s[i] == '"' || (s[i] == '\\' && i == len(s)-1) {
|
2016-10-25 22:42:02 +03:00
|
|
|
return true
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-25 22:42:02 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendStringify makes a json string and appends to buf.
|
|
|
|
func appendStringify(buf []byte, s string) []byte {
|
|
|
|
if mustMarshalString(s) {
|
|
|
|
b, _ := jsongo.Marshal(s)
|
|
|
|
return append(buf, b...)
|
|
|
|
}
|
2016-10-19 01:52:46 +03:00
|
|
|
buf = append(buf, '"')
|
|
|
|
buf = append(buf, s...)
|
|
|
|
buf = append(buf, '"')
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendBuild builds a json block from a json path.
|
2016-10-25 22:42:02 +03:00
|
|
|
func appendBuild(buf []byte, array bool, paths []pathResult, raw string,
|
|
|
|
stringify bool) []byte {
|
2016-10-19 01:52:46 +03:00
|
|
|
if !array {
|
|
|
|
buf = appendStringify(buf, paths[0].part)
|
|
|
|
buf = append(buf, ':')
|
|
|
|
}
|
|
|
|
if len(paths) > 1 {
|
|
|
|
n, numeric := atoui(paths[1])
|
2016-12-12 19:53:56 +03:00
|
|
|
if numeric || (!paths[1].force && paths[1].part == "-1") {
|
2016-10-19 01:52:46 +03:00
|
|
|
buf = append(buf, '[')
|
|
|
|
buf = appendRepeat(buf, "null,", n)
|
|
|
|
buf = appendBuild(buf, true, paths[1:], raw, stringify)
|
|
|
|
buf = append(buf, ']')
|
|
|
|
} else {
|
|
|
|
buf = append(buf, '{')
|
|
|
|
buf = appendBuild(buf, false, paths[1:], raw, stringify)
|
|
|
|
buf = append(buf, '}')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if stringify {
|
|
|
|
buf = appendStringify(buf, raw)
|
|
|
|
} else {
|
|
|
|
buf = append(buf, raw...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
// atoui does a rip conversion of string -> unigned int.
|
|
|
|
func atoui(r pathResult) (n int, ok bool) {
|
|
|
|
if r.force {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(r.part); i++ {
|
|
|
|
if r.part[i] < '0' || r.part[i] > '9' {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
n = n*10 + int(r.part[i]-'0')
|
|
|
|
}
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendRepeat repeats string "n" times and appends to buf.
|
|
|
|
func appendRepeat(buf []byte, s string, n int) []byte {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
buf = append(buf, s...)
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim does a rip trim
|
|
|
|
func trim(s string) string {
|
|
|
|
for len(s) > 0 {
|
|
|
|
if s[0] <= ' ' {
|
|
|
|
s = s[1:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for len(s) > 0 {
|
|
|
|
if s[len(s)-1] <= ' ' {
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-10-20 22:06:19 +03:00
|
|
|
// deleteTailItem deletes the previous key or comma.
|
|
|
|
func deleteTailItem(buf []byte) ([]byte, bool) {
|
|
|
|
loop:
|
|
|
|
for i := len(buf) - 1; i >= 0; i-- {
|
|
|
|
// look for either a ',',':','['
|
|
|
|
switch buf[i] {
|
|
|
|
case '[':
|
|
|
|
return buf, true
|
|
|
|
case ',':
|
|
|
|
return buf[:i], false
|
|
|
|
case ':':
|
|
|
|
// delete tail string
|
|
|
|
i--
|
|
|
|
for ; i >= 0; i-- {
|
|
|
|
if buf[i] == '"' {
|
|
|
|
i--
|
|
|
|
for ; i >= 0; i-- {
|
|
|
|
if buf[i] == '"' {
|
|
|
|
i--
|
2018-09-17 18:50:59 +03:00
|
|
|
if i >= 0 && buf[i] == '\\' {
|
2016-10-20 22:06:19 +03:00
|
|
|
i--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for ; i >= 0; i-- {
|
|
|
|
// look for either a ',','{'
|
|
|
|
switch buf[i] {
|
|
|
|
case '{':
|
|
|
|
return buf[:i+1], true
|
|
|
|
case ',':
|
|
|
|
return buf[:i], false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var errNoChange = &errorType{"no change"}
|
|
|
|
|
2016-10-25 22:42:02 +03:00
|
|
|
func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string,
|
|
|
|
stringify, del bool) ([]byte, error) {
|
2016-10-19 01:52:46 +03:00
|
|
|
var err error
|
2016-10-20 22:06:19 +03:00
|
|
|
var res gjson.Result
|
|
|
|
var found bool
|
|
|
|
if del {
|
|
|
|
if paths[0].part == "-1" && !paths[0].force {
|
|
|
|
res = gjson.Get(jstr, "#")
|
|
|
|
if res.Int() > 0 {
|
|
|
|
res = gjson.Get(jstr, strconv.FormatInt(int64(res.Int()-1), 10))
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2018-09-17 19:56:37 +03:00
|
|
|
res = gjson.Get(jstr, paths[0].gpart)
|
2016-10-20 22:06:19 +03:00
|
|
|
}
|
2016-10-19 01:52:46 +03:00
|
|
|
if res.Index > 0 {
|
|
|
|
if len(paths) > 1 {
|
|
|
|
buf = append(buf, jstr[:res.Index]...)
|
2016-10-25 22:42:02 +03:00
|
|
|
buf, err = appendRawPaths(buf, res.Raw, paths[1:], raw,
|
|
|
|
stringify, del)
|
2016-10-19 01:52:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf = append(buf, jstr[res.Index+len(res.Raw):]...)
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
buf = append(buf, jstr[:res.Index]...)
|
2016-11-04 16:00:44 +03:00
|
|
|
var exidx int // additional forward stripping
|
2016-10-20 22:06:19 +03:00
|
|
|
if del {
|
|
|
|
var delNextComma bool
|
|
|
|
buf, delNextComma = deleteTailItem(buf)
|
|
|
|
if delNextComma {
|
2016-10-25 22:42:02 +03:00
|
|
|
i, j := res.Index+len(res.Raw), 0
|
|
|
|
for ; i < len(jstr); i, j = i+1, j+1 {
|
2016-10-20 22:06:19 +03:00
|
|
|
if jstr[i] <= ' ' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if jstr[i] == ',' {
|
|
|
|
exidx = j + 1
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 01:52:46 +03:00
|
|
|
} else {
|
2016-10-20 22:06:19 +03:00
|
|
|
if stringify {
|
|
|
|
buf = appendStringify(buf, raw)
|
|
|
|
} else {
|
|
|
|
buf = append(buf, raw...)
|
|
|
|
}
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
2016-10-20 22:06:19 +03:00
|
|
|
buf = append(buf, jstr[res.Index+len(res.Raw)+exidx:]...)
|
2016-10-19 01:52:46 +03:00
|
|
|
return buf, nil
|
|
|
|
}
|
2016-10-20 22:06:19 +03:00
|
|
|
if del {
|
|
|
|
return nil, errNoChange
|
|
|
|
}
|
2016-10-19 01:52:46 +03:00
|
|
|
n, numeric := atoui(paths[0])
|
|
|
|
isempty := true
|
|
|
|
for i := 0; i < len(jstr); i++ {
|
|
|
|
if jstr[i] > ' ' {
|
|
|
|
isempty = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isempty {
|
|
|
|
if numeric {
|
|
|
|
jstr = "[]"
|
|
|
|
} else {
|
|
|
|
jstr = "{}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jsres := gjson.Parse(jstr)
|
|
|
|
if jsres.Type != gjson.JSON {
|
2016-10-21 02:16:18 +03:00
|
|
|
if numeric {
|
|
|
|
jstr = "[]"
|
|
|
|
} else {
|
|
|
|
jstr = "{}"
|
|
|
|
}
|
|
|
|
jsres = gjson.Parse(jstr)
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
var comma bool
|
|
|
|
for i := 1; i < len(jsres.Raw); i++ {
|
|
|
|
if jsres.Raw[i] <= ' ' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if jsres.Raw[i] == '}' || jsres.Raw[i] == ']' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
comma = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch jsres.Raw[0] {
|
|
|
|
default:
|
|
|
|
return nil, &errorType{"json must be an object or array"}
|
|
|
|
case '{':
|
|
|
|
buf = append(buf, '{')
|
|
|
|
buf = appendBuild(buf, false, paths, raw, stringify)
|
|
|
|
if comma {
|
|
|
|
buf = append(buf, ',')
|
|
|
|
}
|
|
|
|
buf = append(buf, jsres.Raw[1:]...)
|
|
|
|
return buf, nil
|
|
|
|
case '[':
|
|
|
|
var appendit bool
|
|
|
|
if !numeric {
|
|
|
|
if paths[0].part == "-1" && !paths[0].force {
|
|
|
|
appendit = true
|
|
|
|
} else {
|
2016-10-25 22:42:02 +03:00
|
|
|
return nil, &errorType{
|
|
|
|
"cannot set array element for non-numeric key '" +
|
|
|
|
paths[0].part + "'"}
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if appendit {
|
|
|
|
njson := trim(jsres.Raw)
|
|
|
|
if njson[len(njson)-1] == ']' {
|
|
|
|
njson = njson[:len(njson)-1]
|
|
|
|
}
|
|
|
|
buf = append(buf, njson...)
|
|
|
|
if comma {
|
|
|
|
buf = append(buf, ',')
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = appendBuild(buf, true, paths, raw, stringify)
|
|
|
|
buf = append(buf, ']')
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
buf = append(buf, '[')
|
|
|
|
ress := jsres.Array()
|
|
|
|
for i := 0; i < len(ress); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
buf = append(buf, ',')
|
|
|
|
}
|
|
|
|
buf = append(buf, ress[i].Raw...)
|
|
|
|
}
|
|
|
|
if len(ress) == 0 {
|
|
|
|
buf = appendRepeat(buf, "null,", n-len(ress))
|
|
|
|
} else {
|
|
|
|
buf = appendRepeat(buf, ",null", n-len(ress))
|
|
|
|
if comma {
|
|
|
|
buf = append(buf, ',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf = appendBuild(buf, true, paths, raw, stringify)
|
|
|
|
buf = append(buf, ']')
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 21:10:26 +03:00
|
|
|
func isOptimisticPath(path string) bool {
|
|
|
|
for i := 0; i < len(path); i++ {
|
|
|
|
if path[i] < '.' || path[i] > 'z' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if path[i] > '9' && path[i] < 'A' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if path[i] > 'z' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-10-25 22:42:02 +03:00
|
|
|
func set(jstr, path, raw string,
|
|
|
|
stringify, del, optimistic, inplace bool) ([]byte, error) {
|
2016-10-19 01:52:46 +03:00
|
|
|
if path == "" {
|
|
|
|
return nil, &errorType{"path cannot be empty"}
|
|
|
|
}
|
2016-10-25 21:10:26 +03:00
|
|
|
if !del && optimistic && isOptimisticPath(path) {
|
|
|
|
res := gjson.Get(jstr, path)
|
|
|
|
if res.Exists() && res.Index > 0 {
|
|
|
|
sz := len(jstr) - len(res.Raw) + len(raw)
|
|
|
|
if stringify {
|
|
|
|
sz += 2
|
|
|
|
}
|
2016-10-25 22:42:02 +03:00
|
|
|
if inplace && sz <= len(jstr) {
|
|
|
|
if !stringify || !mustMarshalString(raw) {
|
|
|
|
jsonh := *(*reflect.StringHeader)(unsafe.Pointer(&jstr))
|
|
|
|
jsonbh := reflect.SliceHeader{
|
|
|
|
Data: jsonh.Data, Len: jsonh.Len, Cap: jsonh.Len}
|
|
|
|
jbytes := *(*[]byte)(unsafe.Pointer(&jsonbh))
|
|
|
|
if stringify {
|
|
|
|
jbytes[res.Index] = '"'
|
|
|
|
copy(jbytes[res.Index+1:], []byte(raw))
|
|
|
|
jbytes[res.Index+1+len(raw)] = '"'
|
|
|
|
copy(jbytes[res.Index+1+len(raw)+1:],
|
|
|
|
jbytes[res.Index+len(res.Raw):])
|
|
|
|
} else {
|
|
|
|
copy(jbytes[res.Index:], []byte(raw))
|
|
|
|
copy(jbytes[res.Index+len(raw):],
|
|
|
|
jbytes[res.Index+len(res.Raw):])
|
|
|
|
}
|
|
|
|
return jbytes[:sz], nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-10-25 21:10:26 +03:00
|
|
|
buf := make([]byte, 0, sz)
|
|
|
|
buf = append(buf, jstr[:res.Index]...)
|
|
|
|
if stringify {
|
|
|
|
buf = appendStringify(buf, raw)
|
|
|
|
} else {
|
|
|
|
buf = append(buf, raw...)
|
|
|
|
}
|
|
|
|
buf = append(buf, jstr[res.Index+len(res.Raw):]...)
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// parse the path, make sure that it does not contain invalid characters
|
|
|
|
// such as '#', '?', '*'
|
2016-10-19 01:52:46 +03:00
|
|
|
paths := make([]pathResult, 0, 4)
|
|
|
|
r, err := parsePath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
paths = append(paths, r)
|
|
|
|
for r.more {
|
|
|
|
if r, err = parsePath(r.path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
paths = append(paths, r)
|
|
|
|
}
|
|
|
|
|
2016-10-20 22:06:19 +03:00
|
|
|
njson, err := appendRawPaths(nil, jstr, paths, raw, stringify, del)
|
2016-10-19 01:52:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return njson, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a json value for the specified path.
|
|
|
|
// A path is in dot syntax, such as "name.last" or "age".
|
|
|
|
// This function expects that the json is well-formed, and does not validate.
|
|
|
|
// Invalid json will not panic, but it may return back unexpected results.
|
|
|
|
// An error is returned if the path is not valid.
|
|
|
|
//
|
2016-11-04 16:00:44 +03:00
|
|
|
// A path is a series of keys separated by a dot.
|
2016-10-19 01:52:46 +03:00
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "name": {"first": "Tom", "last": "Anderson"},
|
|
|
|
// "age":37,
|
|
|
|
// "children": ["Sara","Alex","Jack"],
|
|
|
|
// "friends": [
|
|
|
|
// {"first": "James", "last": "Murphy"},
|
|
|
|
// {"first": "Roger", "last": "Craig"}
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
// "name.last" >> "Anderson"
|
|
|
|
// "age" >> 37
|
|
|
|
// "children.1" >> "Alex"
|
|
|
|
//
|
|
|
|
func Set(json, path string, value interface{}) (string, error) {
|
2016-10-25 21:10:26 +03:00
|
|
|
return SetOptions(json, path, value, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOptions sets a json value for the specified path with options.
|
|
|
|
// A path is in dot syntax, such as "name.last" or "age".
|
|
|
|
// This function expects that the json is well-formed, and does not validate.
|
|
|
|
// Invalid json will not panic, but it may return back unexpected results.
|
|
|
|
// An error is returned if the path is not valid.
|
|
|
|
func SetOptions(json, path string, value interface{},
|
|
|
|
opts *Options) (string, error) {
|
2016-10-25 22:42:02 +03:00
|
|
|
if opts != nil {
|
|
|
|
if opts.ReplaceInPlace {
|
|
|
|
// it's not safe to replace bytes in-place for strings
|
|
|
|
// copy the Options and set options.ReplaceInPlace to false.
|
|
|
|
nopts := *opts
|
|
|
|
opts = &nopts
|
|
|
|
opts.ReplaceInPlace = false
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 01:52:46 +03:00
|
|
|
jsonh := *(*reflect.StringHeader)(unsafe.Pointer(&json))
|
|
|
|
jsonbh := reflect.SliceHeader{Data: jsonh.Data, Len: jsonh.Len}
|
|
|
|
jsonb := *(*[]byte)(unsafe.Pointer(&jsonbh))
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err := SetBytesOptions(jsonb, path, value, opts)
|
2016-10-19 01:52:46 +03:00
|
|
|
return string(res), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBytes sets a json value for the specified path.
|
2016-10-25 21:10:26 +03:00
|
|
|
// If working with bytes, this method preferred over
|
|
|
|
// Set(string(data), path, value)
|
2016-10-19 01:52:46 +03:00
|
|
|
func SetBytes(json []byte, path string, value interface{}) ([]byte, error) {
|
2016-10-25 21:10:26 +03:00
|
|
|
return SetBytesOptions(json, path, value, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBytesOptions sets a json value for the specified path with options.
|
|
|
|
// If working with bytes, this method preferred over
|
|
|
|
// SetOptions(string(data), path, value)
|
|
|
|
func SetBytesOptions(json []byte, path string, value interface{},
|
|
|
|
opts *Options) ([]byte, error) {
|
2016-10-25 22:42:02 +03:00
|
|
|
var optimistic, inplace bool
|
2016-10-25 21:10:26 +03:00
|
|
|
if opts != nil {
|
|
|
|
optimistic = opts.Optimistic
|
2016-10-25 22:42:02 +03:00
|
|
|
inplace = opts.ReplaceInPlace
|
2016-10-25 21:10:26 +03:00
|
|
|
}
|
2016-10-19 01:52:46 +03:00
|
|
|
jstr := *(*string)(unsafe.Pointer(&json))
|
|
|
|
var res []byte
|
|
|
|
var err error
|
|
|
|
switch v := value.(type) {
|
|
|
|
default:
|
|
|
|
b, err := jsongo.Marshal(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
raw := *(*string)(unsafe.Pointer(&b))
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err = set(jstr, path, raw, false, false, optimistic, inplace)
|
2016-10-20 22:06:19 +03:00
|
|
|
case dtype:
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err = set(jstr, path, "", false, true, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case string:
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err = set(jstr, path, v, true, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case []byte:
|
|
|
|
raw := *(*string)(unsafe.Pointer(&v))
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err = set(jstr, path, raw, true, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case bool:
|
|
|
|
if v {
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err = set(jstr, path, "true", false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
} else {
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err = set(jstr, path, "false", false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
case int8:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case int16:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case int32:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case int64:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case uint8:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case uint16:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case uint32:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case uint64:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case float32:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatFloat(float64(v), 'f', -1, 64),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-19 01:52:46 +03:00
|
|
|
case float64:
|
2016-10-25 21:10:26 +03:00
|
|
|
res, err = set(jstr, path, strconv.FormatFloat(float64(v), 'f', -1, 64),
|
2016-10-25 22:42:02 +03:00
|
|
|
false, false, optimistic, inplace)
|
2016-10-20 22:06:19 +03:00
|
|
|
}
|
|
|
|
if err == errNoChange {
|
|
|
|
return json, nil
|
2016-10-19 01:52:46 +03:00
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
2016-10-25 19:23:56 +03:00
|
|
|
|
2016-10-25 21:10:26 +03:00
|
|
|
// SetRaw sets a raw json value for the specified path.
|
|
|
|
// This function works the same as Set except that the value is set as a
|
|
|
|
// raw block of json. This allows for setting premarshalled json objects.
|
2016-10-25 19:23:56 +03:00
|
|
|
func SetRaw(json, path, value string) (string, error) {
|
2016-10-25 21:10:26 +03:00
|
|
|
return SetRawOptions(json, path, value, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawOptions sets a raw json value for the specified path with options.
|
|
|
|
// This furnction works the same as SetOptions except that the value is set
|
|
|
|
// as a raw block of json. This allows for setting premarshalled json objects.
|
|
|
|
func SetRawOptions(json, path, value string, opts *Options) (string, error) {
|
|
|
|
var optimistic bool
|
|
|
|
if opts != nil {
|
|
|
|
optimistic = opts.Optimistic
|
|
|
|
}
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err := set(json, path, value, false, false, optimistic, false)
|
2016-10-25 19:23:56 +03:00
|
|
|
if err == errNoChange {
|
|
|
|
return json, nil
|
|
|
|
}
|
|
|
|
return string(res), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawBytes sets a raw json value for the specified path.
|
2016-10-25 21:10:26 +03:00
|
|
|
// If working with bytes, this method preferred over
|
|
|
|
// SetRaw(string(data), path, value)
|
2016-10-25 19:23:56 +03:00
|
|
|
func SetRawBytes(json []byte, path string, value []byte) ([]byte, error) {
|
2016-10-25 21:10:26 +03:00
|
|
|
return SetRawBytesOptions(json, path, value, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawBytesOptions sets a raw json value for the specified path with options.
|
|
|
|
// If working with bytes, this method preferred over
|
|
|
|
// SetRawOptions(string(data), path, value, opts)
|
|
|
|
func SetRawBytesOptions(json []byte, path string, value []byte,
|
|
|
|
opts *Options) ([]byte, error) {
|
2016-10-25 19:23:56 +03:00
|
|
|
jstr := *(*string)(unsafe.Pointer(&json))
|
|
|
|
vstr := *(*string)(unsafe.Pointer(&value))
|
2016-10-25 22:42:02 +03:00
|
|
|
var optimistic, inplace bool
|
2016-10-25 21:10:26 +03:00
|
|
|
if opts != nil {
|
|
|
|
optimistic = opts.Optimistic
|
2016-10-25 22:42:02 +03:00
|
|
|
inplace = opts.ReplaceInPlace
|
2016-10-25 21:10:26 +03:00
|
|
|
}
|
2016-10-25 22:42:02 +03:00
|
|
|
res, err := set(jstr, path, vstr, false, false, optimistic, inplace)
|
2016-10-25 19:23:56 +03:00
|
|
|
if err == errNoChange {
|
|
|
|
return json, nil
|
|
|
|
}
|
2016-12-09 07:30:51 +03:00
|
|
|
return res, err
|
2016-10-25 19:23:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type dtype struct{}
|
|
|
|
|
|
|
|
// Delete deletes a value from json for the specified path.
|
|
|
|
func Delete(json, path string) (string, error) {
|
|
|
|
return Set(json, path, dtype{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBytes deletes a value from json for the specified path.
|
|
|
|
func DeleteBytes(json []byte, path string) ([]byte, error) {
|
|
|
|
return SetBytes(json, path, dtype{})
|
|
|
|
}
|