2016-05-17 21:37:15 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-05-17 21:38:06 +00:00
|
|
|
package data
|
2016-05-17 21:37:15 +00:00
|
|
|
|
|
|
|
import (
|
2016-09-06 11:39:33 +00:00
|
|
|
"fmt"
|
2016-05-17 21:37:15 +00:00
|
|
|
"reflect"
|
2016-09-06 11:39:33 +00:00
|
|
|
"strconv"
|
2016-05-17 21:37:15 +00:00
|
|
|
"strings"
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-02-20 09:46:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2016-09-12 15:47:22 +00:00
|
|
|
"github.com/abcum/surreal/util/deep"
|
2016-09-06 11:39:33 +00:00
|
|
|
"github.com/abcum/surreal/util/pack"
|
2016-05-17 21:37:15 +00:00
|
|
|
)
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
const (
|
|
|
|
one int8 = iota
|
|
|
|
many
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
choose int8 = iota
|
|
|
|
remove
|
|
|
|
)
|
|
|
|
|
2016-05-17 21:38:06 +00:00
|
|
|
// Doc holds a reference to the core data object, or a selected path.
|
2016-05-17 21:37:15 +00:00
|
|
|
type Doc struct {
|
|
|
|
data interface{}
|
2017-11-28 00:39:00 +00:00
|
|
|
call Fetcher
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:39:00 +00:00
|
|
|
// Fetcher is used when fetching values.
|
|
|
|
type Fetcher func(key string, val interface{}) interface{}
|
|
|
|
|
|
|
|
// Iterator is used when iterating over items.
|
|
|
|
type Iterator func(key string, val interface{}) error
|
|
|
|
|
2016-05-17 21:38:06 +00:00
|
|
|
// New creates a new data object.
|
|
|
|
func New() *Doc {
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: map[string]interface{}{}}
|
2016-05-17 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Consume converts a GO interface into a data object.
|
|
|
|
func Consume(input interface{}) *Doc {
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: input}
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Data returns the internal data object as an interface.
|
|
|
|
func (d *Doc) Data() interface{} {
|
|
|
|
return d.data
|
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-28 11:33:07 +00:00
|
|
|
// Copy returns a duplicated copy of the internal data object.
|
2017-11-16 19:08:03 +00:00
|
|
|
func (d *Doc) Copy() *Doc {
|
|
|
|
return &Doc{data: deep.Copy(d.data)}
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-11-28 00:39:00 +00:00
|
|
|
// Fetch adds a function to be used for retrieving and converting values.
|
|
|
|
func (d *Doc) Fetch(fetcher Fetcher) {
|
|
|
|
d.call = fetcher
|
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Encode encodes the data object to a byte slice.
|
|
|
|
func (d *Doc) Encode() (dst []byte) {
|
2016-10-01 23:17:19 +00:00
|
|
|
dst = pack.Encode(d.data)
|
2016-09-06 11:39:33 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Decode decodes the byte slice into a data object.
|
|
|
|
func (d *Doc) Decode(src []byte) *Doc {
|
2016-09-19 11:02:10 +00:00
|
|
|
pack.Decode(src, &d.data)
|
2016-09-06 11:39:33 +00:00
|
|
|
return d
|
2016-05-17 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 09:46:26 +00:00
|
|
|
func (d *Doc) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(d.Data())
|
|
|
|
}
|
|
|
|
|
2017-11-24 10:55:13 +00:00
|
|
|
func (d *Doc) UnmarshalJSON(data []byte) error {
|
|
|
|
return json.Unmarshal(data, &d.data)
|
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
func (d *Doc) path(path ...string) (paths []string) {
|
2017-02-04 11:13:11 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
for _, p := range path {
|
2017-02-04 11:13:11 +00:00
|
|
|
for j, i, o := 0, 0, false; i < len(p); i++ {
|
|
|
|
switch {
|
|
|
|
case i == len(p)-1:
|
|
|
|
if len(p[j:]) > 0 {
|
|
|
|
paths = append(paths, p[j:])
|
|
|
|
}
|
|
|
|
case p[i] == '.':
|
|
|
|
if len(p[j:i]) > 0 {
|
|
|
|
paths = append(paths, p[j:i])
|
|
|
|
}
|
|
|
|
j, i = i+1, i+0
|
|
|
|
case p[i] == '[':
|
|
|
|
if len(p[j:i]) > 0 {
|
|
|
|
paths = append(paths, p[j:i])
|
|
|
|
}
|
|
|
|
j, i, o = i, i+1, true
|
|
|
|
case p[i] == ']' && o:
|
|
|
|
if len(p[j:i+1]) > 0 {
|
|
|
|
paths = append(paths, p[j:i+1])
|
|
|
|
}
|
|
|
|
j, i, o = i+1, i+0, false
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2017-02-04 11:13:11 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
return
|
2017-02-04 11:13:11 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-02-23 15:42:58 +00:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
func trim(s string) string {
|
|
|
|
if s[0] == '[' && s[len(s)-1] == ']' {
|
|
|
|
return s[1 : len(s)-1]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Doc) what(p string, a []interface{}, t int8) (o []interface{}, i []int, r int8) {
|
|
|
|
|
|
|
|
p = trim(p)
|
|
|
|
|
|
|
|
i = []int{}
|
|
|
|
|
|
|
|
o = []interface{}{}
|
|
|
|
|
|
|
|
// If there are no items in the
|
|
|
|
// original array, then return
|
|
|
|
// an empty array immediately.
|
|
|
|
|
|
|
|
if len(a) == 0 {
|
|
|
|
if strings.ContainsAny(p, ":*") {
|
|
|
|
return o, i, many
|
|
|
|
} else {
|
|
|
|
return o, i, one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the array index is a star
|
|
|
|
// or a colon only, then return
|
|
|
|
// the full array immediately
|
|
|
|
|
|
|
|
if p == "*" || p == ":" {
|
|
|
|
switch t {
|
|
|
|
case choose:
|
|
|
|
for k := range a {
|
|
|
|
i = append(i, k)
|
|
|
|
}
|
|
|
|
return a, i, many
|
|
|
|
case remove:
|
|
|
|
for k := range a {
|
|
|
|
i = append(i, k)
|
|
|
|
}
|
|
|
|
return o, i, many
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the specified array index
|
2017-02-23 15:42:58 +00:00
|
|
|
// by colons, so that we can get
|
2017-02-07 17:22:57 +00:00
|
|
|
// the specified array items.
|
|
|
|
|
|
|
|
c := strings.Count(p, ":")
|
|
|
|
|
|
|
|
if c == 0 {
|
|
|
|
|
|
|
|
switch p {
|
|
|
|
case "0", "first":
|
|
|
|
if t == choose {
|
|
|
|
i = append(i, 0)
|
|
|
|
o = append(o, a[0])
|
|
|
|
} else {
|
|
|
|
for k := range a[1:] {
|
|
|
|
i = append(i, k)
|
|
|
|
}
|
|
|
|
o = append(o, a[1:]...)
|
|
|
|
}
|
|
|
|
case "$", "last":
|
|
|
|
if t == choose {
|
|
|
|
i = append(i, len(a)-1)
|
|
|
|
o = append(o, a[len(a)-1])
|
|
|
|
} else {
|
|
|
|
for k := range a[:len(a)-1] {
|
|
|
|
i = append(i, k)
|
|
|
|
}
|
|
|
|
o = append(o, a[:len(a)-1]...)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if z, e := strconv.Atoi(p); e == nil {
|
|
|
|
if len(a) > z {
|
2017-11-16 19:09:12 +00:00
|
|
|
switch t {
|
|
|
|
case choose:
|
2017-02-07 17:22:57 +00:00
|
|
|
i = append(i, z)
|
|
|
|
o = append(o, a[z])
|
2017-11-16 19:09:12 +00:00
|
|
|
case remove:
|
2017-02-07 17:22:57 +00:00
|
|
|
for k := range append(a[:z], a[z+1:]...) {
|
|
|
|
i = append(i, k)
|
|
|
|
}
|
|
|
|
o = append(o, append(a[:z], a[z+1:]...)...)
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-16 19:09:12 +00:00
|
|
|
switch t {
|
|
|
|
case remove:
|
2017-02-07 17:22:57 +00:00
|
|
|
for k := range a {
|
|
|
|
i = append(i, k)
|
|
|
|
}
|
|
|
|
o = append(o, a[:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o, i, one
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == 1 {
|
|
|
|
|
|
|
|
var e error
|
2017-02-23 15:42:58 +00:00
|
|
|
var s, f int
|
2017-02-07 17:22:57 +00:00
|
|
|
|
|
|
|
b := []int{0, len(a)}
|
|
|
|
x := strings.Split(p, ":")
|
|
|
|
|
|
|
|
for k := range x {
|
|
|
|
switch x[k] {
|
|
|
|
case "":
|
|
|
|
case "0", "first":
|
|
|
|
b[k] = 0
|
|
|
|
case "$", "last":
|
|
|
|
b[k] = len(a)
|
|
|
|
default:
|
|
|
|
if b[k], e = strconv.Atoi(x[k]); e != nil {
|
|
|
|
return nil, nil, many
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 15:42:58 +00:00
|
|
|
s = b[0]
|
|
|
|
s = max(s, 0)
|
|
|
|
s = min(s, len(a))
|
|
|
|
|
|
|
|
f = b[1]
|
|
|
|
f = max(f, 0)
|
|
|
|
f = min(f, len(a))
|
|
|
|
|
|
|
|
if t == choose {
|
|
|
|
for k, v := range a[s:f] {
|
|
|
|
i = append(i, k)
|
|
|
|
o = append(o, v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := range append(a[:s], a[f+1:]...) {
|
|
|
|
i = append(i, k)
|
|
|
|
o = append(o, v)
|
|
|
|
}
|
2017-02-07 17:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return o, i, many
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, many
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Reset empties and resets the data at the specified path.
|
|
|
|
func (d *Doc) Reset(path ...string) (*Doc, error) {
|
2017-11-16 19:08:48 +00:00
|
|
|
return d.Set(map[string]interface{}{}, path...)
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-10-24 13:16:33 +00:00
|
|
|
// Valid checks whether the value at the specified path is nil.
|
|
|
|
func (d *Doc) Valid(path ...string) bool {
|
|
|
|
if !d.Exists(path...) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return d.Get(path...).Data() != nil
|
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Array sets the specified path to an array.
|
|
|
|
func (d *Doc) Array(path ...string) (*Doc, error) {
|
|
|
|
return d.Set([]interface{}{}, path...)
|
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Object sets the specified path to an object.
|
|
|
|
func (d *Doc) Object(path ...string) (*Doc, error) {
|
|
|
|
return d.Set(map[string]interface{}{}, path...)
|
2016-05-17 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// New sets the value at the specified path if it does not exist.
|
|
|
|
func (d *Doc) New(value interface{}, path ...string) (*Doc, error) {
|
|
|
|
if !d.Exists(path...) {
|
|
|
|
return d.Set(value, path...)
|
|
|
|
}
|
|
|
|
return d.Get(path...), nil
|
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-12-03 00:44:07 +00:00
|
|
|
// Iff sets the value at the specified path if it is not nil, or deletes it.
|
2016-09-06 11:39:33 +00:00
|
|
|
func (d *Doc) Iff(value interface{}, path ...string) (*Doc, error) {
|
|
|
|
if value != nil {
|
|
|
|
return d.Set(value, path...)
|
|
|
|
}
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: nil}, d.Del(path...)
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-09 17:55:25 +00:00
|
|
|
// Keys retrieves the object keys at the specified path.
|
|
|
|
func (d *Doc) Keys(path ...string) *Doc {
|
|
|
|
|
|
|
|
path = d.path(path...)
|
|
|
|
|
|
|
|
out := []interface{}{}
|
|
|
|
|
|
|
|
if m, ok := d.Get(path...).Data().(map[string]interface{}); ok {
|
2016-10-14 06:52:33 +00:00
|
|
|
for k := range m {
|
2016-09-09 17:55:25 +00:00
|
|
|
out = append(out, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: out}
|
2016-09-09 17:55:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vals retrieves the object values at the specified path.
|
|
|
|
func (d *Doc) Vals(path ...string) *Doc {
|
|
|
|
|
|
|
|
path = d.path(path...)
|
|
|
|
|
|
|
|
out := []interface{}{}
|
|
|
|
|
|
|
|
if m, ok := d.Get(path...).Data().(map[string]interface{}); ok {
|
|
|
|
for _, v := range m {
|
|
|
|
out = append(out, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: out}
|
2016-09-09 17:55:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Exists checks whether the specified path exists.
|
|
|
|
func (d *Doc) Exists(path ...string) bool {
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
path = d.path(path...)
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is undefined, then just
|
|
|
|
// return false immediately
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
if d.data == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Define the temporary object so
|
|
|
|
// that we can loop over and traverse
|
|
|
|
// down the path parts of the data
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
object := d.data
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Loop over each part of the path
|
|
|
|
// whilst detecting if the data at
|
|
|
|
// the current path is an {} or []
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
for k, p := range path {
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-11-27 18:30:20 +00:00
|
|
|
p = trim(p)
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an object, then move
|
|
|
|
// to the next part of the path
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
if m, ok := object.(map[string]interface{}); ok {
|
2017-11-27 18:30:20 +00:00
|
|
|
if object, ok = m[p]; !ok {
|
2016-09-06 11:39:33 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an array, then perform
|
|
|
|
// the query on the specified items
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
if a, ok := object.([]interface{}); ok {
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if p == "length" {
|
|
|
|
return len(a) > 0
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
c, _, r := d.what(p, a, choose)
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if len(c) == 0 {
|
|
|
|
return false
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == one {
|
2017-11-28 00:39:00 +00:00
|
|
|
if d.call != nil && len(path[k+1:]) > 0 {
|
|
|
|
c[0] = d.call(p, c[0])
|
|
|
|
}
|
2017-02-07 17:22:57 +00:00
|
|
|
return Consume(c[0]).Exists(path[k+1:]...)
|
|
|
|
}
|
2016-05-17 21:38:06 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == many {
|
|
|
|
for _, v := range c {
|
2017-11-28 00:39:00 +00:00
|
|
|
if d.call != nil && len(path[k+1:]) > 0 {
|
|
|
|
v = d.call(p, v)
|
|
|
|
}
|
2017-02-07 17:22:57 +00:00
|
|
|
if !Consume(v).Exists(path[k+1:]...) {
|
|
|
|
return false
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-07 17:22:57 +00:00
|
|
|
return true
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2016-05-17 21:37:15 +00:00
|
|
|
|
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Get gets the value or values at a specified path.
|
|
|
|
func (d *Doc) Get(path ...string) *Doc {
|
2016-05-17 21:37:15 +00:00
|
|
|
|
|
|
|
path = d.path(path...)
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is undefined, then just
|
|
|
|
// return false immediately
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
if d.data == nil {
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: nil}
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Define the temporary object so
|
|
|
|
// that we can loop over and traverse
|
|
|
|
// down the path parts of the data
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
object := d.data
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Loop over each part of the path
|
|
|
|
// whilst detecting if the data at
|
|
|
|
// the current path is an {} or []
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
for k, p := range path {
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2017-11-27 18:30:20 +00:00
|
|
|
p = trim(p)
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an object, then move
|
|
|
|
// to the next part of the path
|
2016-06-16 12:32:54 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
if m, ok := object.(map[string]interface{}); ok {
|
2017-11-28 00:39:00 +00:00
|
|
|
if d.call != nil && len(path[k+1:]) > 0 {
|
|
|
|
object = d.call(p, m[p])
|
|
|
|
} else {
|
|
|
|
object = m[p]
|
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-06-16 12:32:54 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an array, then perform
|
|
|
|
// the query on the specified items
|
|
|
|
|
|
|
|
if a, ok := object.([]interface{}); ok {
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if p == "length" {
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: len(a)}
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
c, _, r := d.what(p, a, choose)
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if len(c) == 0 {
|
|
|
|
return &Doc{data: nil}
|
2016-06-16 12:32:54 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == one {
|
2017-11-28 00:39:00 +00:00
|
|
|
if d.call != nil && len(path[k+1:]) > 0 {
|
|
|
|
c[0] = d.call(p, c[0])
|
|
|
|
}
|
2017-02-07 17:22:57 +00:00
|
|
|
return Consume(c[0]).Get(path[k+1:]...)
|
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == many {
|
2016-09-06 11:39:33 +00:00
|
|
|
out := []interface{}{}
|
2017-02-07 17:22:57 +00:00
|
|
|
for _, v := range c {
|
2017-11-28 00:39:00 +00:00
|
|
|
if d.call != nil && len(path[k+1:]) > 0 {
|
|
|
|
v = d.call(p, v)
|
|
|
|
}
|
2017-02-07 17:22:57 +00:00
|
|
|
res := Consume(v).Get(path[k+1:]...)
|
|
|
|
if res.data != nil {
|
|
|
|
out = append(out, res.data)
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: out}
|
2016-06-16 12:32:54 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2016-06-16 12:32:54 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: nil}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2016-06-16 12:32:54 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: object}
|
2016-06-16 12:32:54 +00:00
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Set sets the value or values at a specified path.
|
2016-05-17 21:37:15 +00:00
|
|
|
func (d *Doc) Set(value interface{}, path ...string) (*Doc, error) {
|
|
|
|
|
|
|
|
path = d.path(path...)
|
|
|
|
|
|
|
|
if len(path) == 0 {
|
|
|
|
d.data = value
|
|
|
|
return d, nil
|
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
// If the value found at the current
|
|
|
|
// path part is undefined, then ensure
|
|
|
|
// that it is an object
|
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
if d.data == nil {
|
|
|
|
d.data = map[string]interface{}{}
|
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
// Define the temporary object so
|
|
|
|
// that we can loop over and traverse
|
|
|
|
// down the path parts of the data
|
|
|
|
|
|
|
|
object := d.data
|
|
|
|
|
|
|
|
// Loop over each part of the path
|
|
|
|
// whilst detecting if the data at
|
|
|
|
// the current path is an {} or []
|
|
|
|
|
|
|
|
for k, p := range path {
|
|
|
|
|
2017-11-27 18:30:20 +00:00
|
|
|
p = trim(p)
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an object, then move
|
|
|
|
// to the next part of the path
|
|
|
|
|
|
|
|
if m, ok := object.(map[string]interface{}); ok {
|
|
|
|
if k == len(path)-1 {
|
2017-11-27 18:30:20 +00:00
|
|
|
m[p] = value
|
2016-09-06 11:39:33 +00:00
|
|
|
} else if m[p] == nil {
|
2017-11-27 18:30:20 +00:00
|
|
|
m[p] = map[string]interface{}{}
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2017-11-27 18:30:20 +00:00
|
|
|
object = m[p]
|
2017-02-07 17:22:57 +00:00
|
|
|
continue
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
// If the value found at the current
|
|
|
|
// path part is an array, then perform
|
|
|
|
// the query on the specified items
|
|
|
|
|
|
|
|
if a, ok := object.([]interface{}); ok {
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
c, i, r := d.what(p, a, choose)
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if len(c) == 0 {
|
|
|
|
return &Doc{data: nil}, nil
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == one {
|
2016-09-06 11:39:33 +00:00
|
|
|
if k == len(path)-1 {
|
2017-02-07 17:22:57 +00:00
|
|
|
a[i[0]] = value
|
|
|
|
object = a[i[0]]
|
2016-09-06 11:39:33 +00:00
|
|
|
} else {
|
2017-02-07 17:22:57 +00:00
|
|
|
return Consume(a[i[0]]).Set(value, path[k+1:]...)
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == many {
|
2016-09-06 11:39:33 +00:00
|
|
|
out := []interface{}{}
|
2017-02-07 17:22:57 +00:00
|
|
|
for j, v := range c {
|
2016-09-06 11:39:33 +00:00
|
|
|
if k == len(path)-1 {
|
2017-02-07 17:22:57 +00:00
|
|
|
a[i[j]] = value
|
|
|
|
out = append(out, value)
|
2016-09-06 11:39:33 +00:00
|
|
|
} else {
|
2017-02-07 17:22:57 +00:00
|
|
|
res, _ := Consume(v).Set(value, path[k+1:]...)
|
2016-09-06 11:39:33 +00:00
|
|
|
if res.data != nil {
|
|
|
|
out = append(out, res.data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: out}, nil
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: object}, nil
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Del deletes the value or values at a specified path.
|
2016-05-17 21:37:15 +00:00
|
|
|
func (d *Doc) Del(path ...string) error {
|
|
|
|
|
|
|
|
path = d.path(path...)
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is undefined, then return
|
|
|
|
// a not an object error
|
2016-05-17 21:37:15 +00:00
|
|
|
|
|
|
|
if d.data == nil {
|
2016-09-06 11:39:33 +00:00
|
|
|
return fmt.Errorf("Item is not an object")
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
// Define the temporary object so
|
|
|
|
// that we can loop over and traverse
|
|
|
|
// down the path parts of the data
|
|
|
|
|
|
|
|
object := d.data
|
|
|
|
|
|
|
|
// Loop over each part of the path
|
|
|
|
// whilst detecting if the data at
|
|
|
|
// the current path is an {} or []
|
|
|
|
|
|
|
|
for k, p := range path {
|
|
|
|
|
2017-11-27 18:30:20 +00:00
|
|
|
p = trim(p)
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an object, then move
|
|
|
|
// to the next part of the path
|
|
|
|
|
|
|
|
if m, ok := object.(map[string]interface{}); ok {
|
|
|
|
if k == len(path)-1 {
|
2017-11-27 18:30:20 +00:00
|
|
|
delete(m, p)
|
|
|
|
} else if m[p] == nil {
|
2016-09-06 11:39:33 +00:00
|
|
|
return fmt.Errorf("Item at path %s is not an object", path)
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2017-11-27 18:30:20 +00:00
|
|
|
object = m[p]
|
2017-02-07 17:22:57 +00:00
|
|
|
continue
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an array, then perform
|
|
|
|
// the query on the specified items
|
|
|
|
|
|
|
|
if a, ok := object.([]interface{}); ok {
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
var r int8
|
|
|
|
var c []interface{}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if k == len(path)-1 {
|
|
|
|
c, _, r = d.what(p, a, remove)
|
2016-09-06 11:39:33 +00:00
|
|
|
} else {
|
2017-02-07 17:22:57 +00:00
|
|
|
c, _, r = d.what(p, a, choose)
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == one {
|
2016-09-06 11:39:33 +00:00
|
|
|
if k == len(path)-1 {
|
2017-02-07 17:22:57 +00:00
|
|
|
d.Set(c, path[:len(path)-1]...)
|
2016-09-06 11:39:33 +00:00
|
|
|
} else {
|
2017-02-07 17:22:57 +00:00
|
|
|
if len(c) != 0 {
|
|
|
|
return Consume(c[0]).Del(path[k+1:]...)
|
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == many {
|
|
|
|
if k == len(path)-1 {
|
|
|
|
d.Set(c, path[:len(path)-1]...)
|
|
|
|
} else {
|
|
|
|
for _, v := range c {
|
|
|
|
Consume(v).Del(path[k+1:]...)
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
2017-02-07 17:22:57 +00:00
|
|
|
break
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
return nil
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-05-17 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
2017-11-16 19:09:27 +00:00
|
|
|
// Append appends an item or an array of items to an array at the specified path.
|
|
|
|
func (d *Doc) Append(value interface{}, path ...string) (*Doc, error) {
|
|
|
|
|
|
|
|
a, ok := d.Get(path...).Data().([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return &Doc{data: nil}, fmt.Errorf("Not an array")
|
|
|
|
}
|
|
|
|
|
|
|
|
if values, ok := value.([]interface{}); ok {
|
|
|
|
for _, value := range values {
|
|
|
|
a = append(a, value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
a = append(a, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Set(a, path...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-09 17:57:16 +00:00
|
|
|
// ArrayAdd appends an item or an array of items to an array at the specified path.
|
2016-09-06 11:39:33 +00:00
|
|
|
func (d *Doc) ArrayAdd(value interface{}, path ...string) (*Doc, error) {
|
2016-05-17 21:37:15 +00:00
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
a, ok := d.Get(path...).Data().([]interface{})
|
2016-05-17 21:37:15 +00:00
|
|
|
if !ok {
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: nil}, fmt.Errorf("Not an array")
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
if values, ok := value.([]interface{}); ok {
|
|
|
|
outer:
|
|
|
|
for _, value := range values {
|
|
|
|
for _, v := range a {
|
|
|
|
if reflect.DeepEqual(v, value) {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a = append(a, value)
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
} else {
|
|
|
|
for _, v := range a {
|
|
|
|
if reflect.DeepEqual(v, value) {
|
2017-11-16 19:08:48 +00:00
|
|
|
return Consume(a), nil
|
2016-09-06 11:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
a = append(a, value)
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
return d.Set(a, path...)
|
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// ArrayDel deletes an item or an array of items from an array at the specified path.
|
|
|
|
func (d *Doc) ArrayDel(value interface{}, path ...string) (*Doc, error) {
|
|
|
|
|
|
|
|
a, ok := d.Get(path...).Data().([]interface{})
|
2016-05-17 21:37:15 +00:00
|
|
|
if !ok {
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: nil}, fmt.Errorf("Not an array")
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
if values, ok := value.([]interface{}); ok {
|
|
|
|
for _, value := range values {
|
|
|
|
for i := len(a) - 1; i >= 0; i-- {
|
|
|
|
v := a[i]
|
|
|
|
if reflect.DeepEqual(v, value) {
|
|
|
|
copy(a[i:], a[i+1:])
|
|
|
|
a[len(a)-1] = nil
|
|
|
|
a = a[:len(a)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := len(a) - 1; i >= 0; i-- {
|
|
|
|
v := a[i]
|
|
|
|
if reflect.DeepEqual(v, value) {
|
|
|
|
copy(a[i:], a[i+1:])
|
|
|
|
a[len(a)-1] = nil
|
|
|
|
a = a[:len(a)-1]
|
|
|
|
}
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
return d.Set(a, path...)
|
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Contains checks whether the value exists within the array at the specified path.
|
|
|
|
func (d *Doc) Contains(value interface{}, path ...string) bool {
|
|
|
|
|
|
|
|
a, ok := d.Get(path...).Data().([]interface{})
|
2016-05-17 21:37:15 +00:00
|
|
|
if !ok {
|
2016-09-06 11:39:33 +00:00
|
|
|
return false
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
for _, v := range a {
|
|
|
|
if reflect.DeepEqual(v, value) {
|
|
|
|
return true
|
|
|
|
}
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Inc increments an item, or appends an item to an array at the specified path.
|
|
|
|
func (d *Doc) Inc(value interface{}, path ...string) (*Doc, error) {
|
|
|
|
|
|
|
|
switch cur := d.Get(path...).Data().(type) {
|
|
|
|
case nil:
|
|
|
|
switch inc := value.(type) {
|
|
|
|
case int64:
|
|
|
|
return d.Set(0+inc, path...)
|
|
|
|
case float64:
|
|
|
|
return d.Set(0+inc, path...)
|
|
|
|
}
|
|
|
|
case int64:
|
|
|
|
switch inc := value.(type) {
|
|
|
|
case int64:
|
|
|
|
return d.Set(cur+inc, path...)
|
|
|
|
case float64:
|
|
|
|
return d.Set(float64(cur)+inc, path...)
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
switch inc := value.(type) {
|
|
|
|
case int64:
|
|
|
|
return d.Set(cur+float64(inc), path...)
|
|
|
|
case float64:
|
|
|
|
return d.Set(cur+inc, path...)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
return d.ArrayAdd(value, path...)
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: nil}, fmt.Errorf("Not possible to increment.")
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:39:33 +00:00
|
|
|
// Dec decrements an item, or removes an item from an array at the specified path.
|
|
|
|
func (d *Doc) Dec(value interface{}, path ...string) (*Doc, error) {
|
|
|
|
|
|
|
|
switch cur := d.Get(path...).Data().(type) {
|
|
|
|
case nil:
|
|
|
|
switch inc := value.(type) {
|
|
|
|
case int64:
|
|
|
|
return d.Set(0-inc, path...)
|
|
|
|
case float64:
|
|
|
|
return d.Set(0-inc, path...)
|
|
|
|
}
|
|
|
|
case int64:
|
|
|
|
switch inc := value.(type) {
|
|
|
|
case int64:
|
|
|
|
return d.Set(cur-inc, path...)
|
|
|
|
case float64:
|
|
|
|
return d.Set(float64(cur)-inc, path...)
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
switch inc := value.(type) {
|
|
|
|
case int64:
|
|
|
|
return d.Set(cur-float64(inc), path...)
|
|
|
|
case float64:
|
|
|
|
return d.Set(cur-inc, path...)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
return d.ArrayDel(value, path...)
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2017-02-04 11:00:13 +00:00
|
|
|
return &Doc{data: nil}, fmt.Errorf("Not possible to decrement.")
|
2016-09-06 11:39:33 +00:00
|
|
|
|
2016-05-17 21:37:15 +00:00
|
|
|
}
|
2016-10-24 13:16:53 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
2017-02-04 11:23:30 +00:00
|
|
|
func (d *Doc) Diff(n *Doc) map[string]interface{} {
|
|
|
|
|
|
|
|
var initial = make(map[string]interface{})
|
|
|
|
var current = make(map[string]interface{})
|
|
|
|
var changes = make(map[string]interface{})
|
|
|
|
|
|
|
|
d.Each(func(key string, val interface{}) error {
|
|
|
|
initial[key] = val
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
n.Each(func(key string, val interface{}) error {
|
|
|
|
current[key] = val
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
for k, v := range current {
|
|
|
|
if o, ok := initial[k]; ok {
|
|
|
|
if reflect.DeepEqual(o, v) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changes[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range initial {
|
|
|
|
if _, ok := current[k]; !ok {
|
|
|
|
changes[k] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
2016-10-24 13:16:53 +00:00
|
|
|
func (d *Doc) join(parts ...[]string) string {
|
|
|
|
var path []string
|
|
|
|
for _, part := range parts {
|
|
|
|
path = append(path, part...)
|
|
|
|
}
|
|
|
|
return strings.Join(path, ".")
|
|
|
|
}
|
|
|
|
|
2017-02-04 11:22:53 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Each loops through the values in the data doc.
|
|
|
|
func (d *Doc) Each(exec Iterator) error {
|
|
|
|
|
|
|
|
return d.each(exec, nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Doc) each(exec Iterator, prev []string) error {
|
|
|
|
|
|
|
|
// Define the temporary object so
|
|
|
|
// that we can loop over and traverse
|
|
|
|
// down the path parts of the data
|
|
|
|
|
|
|
|
object := d.data
|
|
|
|
|
|
|
|
// If the value found at the current
|
|
|
|
// path part is an object, then move
|
|
|
|
// to the next part of the path
|
|
|
|
|
|
|
|
if m, ok := object.(map[string]interface{}); ok {
|
2017-02-07 17:22:57 +00:00
|
|
|
exec(d.join(prev), make(map[string]interface{}))
|
2017-02-04 11:22:53 +00:00
|
|
|
for k, v := range m {
|
|
|
|
var keep []string
|
|
|
|
keep = append(keep, prev...)
|
|
|
|
keep = append(keep, k)
|
|
|
|
Consume(v).each(exec, keep)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the value found at the current
|
|
|
|
// path part is an array, then perform
|
|
|
|
// the query on the specified items
|
|
|
|
|
|
|
|
if a, ok := object.([]interface{}); ok {
|
2017-02-07 17:22:57 +00:00
|
|
|
exec(d.join(prev), make([]interface{}, len(a)))
|
2017-02-04 11:22:53 +00:00
|
|
|
for i, v := range a {
|
|
|
|
var keep []string
|
|
|
|
keep = append(keep, prev...)
|
2017-02-07 17:22:57 +00:00
|
|
|
keep = append(keep, fmt.Sprintf("[%d]", i))
|
2017-02-04 11:22:53 +00:00
|
|
|
Consume(v).each(exec, keep)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec(d.join(prev), object)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Walk walks the value or values at a specified path.
|
|
|
|
func (d *Doc) Walk(exec Iterator, path ...string) error {
|
2016-10-24 13:16:53 +00:00
|
|
|
|
|
|
|
path = d.path(path...)
|
|
|
|
|
2017-02-04 11:22:53 +00:00
|
|
|
return d.walk(exec, nil, path...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Doc) walk(exec Iterator, prev []string, path ...string) error {
|
|
|
|
|
2016-10-24 13:16:53 +00:00
|
|
|
if len(path) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the value found at the current
|
|
|
|
// path part is undefined, then ensure
|
|
|
|
// that it is an object
|
|
|
|
|
|
|
|
if d.data == nil {
|
|
|
|
d.data = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the temporary object so
|
|
|
|
// that we can loop over and traverse
|
|
|
|
// down the path parts of the data
|
|
|
|
|
|
|
|
object := d.data
|
|
|
|
|
|
|
|
// Loop over each part of the path
|
|
|
|
// whilst detecting if the data at
|
|
|
|
// the current path is an {} or []
|
|
|
|
|
|
|
|
for k, p := range path {
|
|
|
|
|
2017-11-27 18:30:20 +00:00
|
|
|
p = trim(p)
|
|
|
|
|
2016-10-24 13:16:53 +00:00
|
|
|
// If the value found at the current
|
|
|
|
// path part is an object, then move
|
|
|
|
// to the next part of the path
|
|
|
|
|
|
|
|
if m, ok := object.(map[string]interface{}); ok {
|
2017-11-27 18:30:20 +00:00
|
|
|
if object, ok = m[p]; !ok {
|
2016-10-24 13:16:53 +00:00
|
|
|
return exec(d.join(prev, path), nil)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the value found at the current
|
|
|
|
// path part is an array, then perform
|
|
|
|
// the query on the specified items
|
|
|
|
|
|
|
|
if a, ok := object.([]interface{}); ok {
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
c, i, r := d.what(p, a, choose)
|
2016-10-24 13:16:53 +00:00
|
|
|
|
2017-12-06 20:19:28 +00:00
|
|
|
if r == one && len(c) == 0 {
|
2017-02-07 17:22:57 +00:00
|
|
|
return fmt.Errorf("No item with index %s in array, using path %s", p, path)
|
2016-10-24 13:16:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == one {
|
2016-10-24 13:16:53 +00:00
|
|
|
if k == len(path)-1 {
|
2017-02-07 17:22:57 +00:00
|
|
|
var keep []string
|
|
|
|
keep = append(keep, prev...)
|
|
|
|
keep = append(keep, path[:k]...)
|
|
|
|
keep = append(keep, fmt.Sprintf("[%d]", i[0]))
|
|
|
|
return exec(d.join(keep), c[0])
|
2016-10-24 13:16:53 +00:00
|
|
|
} else {
|
|
|
|
var keep []string
|
|
|
|
keep = append(keep, prev...)
|
|
|
|
keep = append(keep, path[:k]...)
|
2017-02-07 17:22:57 +00:00
|
|
|
keep = append(keep, fmt.Sprintf("[%d]", i[0]))
|
|
|
|
return Consume(c[0]).walk(exec, keep, path[k+1:]...)
|
2016-10-24 13:16:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:22:57 +00:00
|
|
|
if r == many {
|
|
|
|
for j, v := range c {
|
2016-10-24 13:16:53 +00:00
|
|
|
if k == len(path)-1 {
|
|
|
|
var keep []string
|
|
|
|
keep = append(keep, prev...)
|
|
|
|
keep = append(keep, path[:k]...)
|
2017-02-07 17:22:57 +00:00
|
|
|
keep = append(keep, fmt.Sprintf("[%d]", i[j]))
|
|
|
|
if err := exec(d.join(keep), v); err != nil {
|
2016-10-24 13:16:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var keep []string
|
|
|
|
keep = append(keep, prev...)
|
|
|
|
keep = append(keep, path[:k]...)
|
2017-02-07 17:22:57 +00:00
|
|
|
keep = append(keep, fmt.Sprintf("[%d]", i[j]))
|
|
|
|
if err := Consume(v).walk(exec, keep, path[k+1:]...); err != nil {
|
2016-10-24 13:16:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// The current path item is not an object or an array
|
|
|
|
// but there are still other items in the search path.
|
|
|
|
|
|
|
|
return fmt.Errorf("Can not get path %s from %v", path, object)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec(d.join(prev, path), object)
|
|
|
|
|
|
|
|
}
|