Skip to content

quotation fix #30

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 oliver
Copyright (c) 2021, 2015; DoltHub Authors, oliver

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/dolthub/jsonpath

go 1.15

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.0
gopkg.in/src-d/go-errors.v1 v1.0.0
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/src-d/go-errors.v1 v1.0.0 h1:cooGdZnCjYbeS1zb1s6pVAAimTdKceRrpn7aKOnNIfc=
gopkg.in/src-d/go-errors.v1 v1.0.0/go.mod h1:q1cBlomlw2FnDBDNGlnh6X0jPihy+QxZfMMNxPCbdYg=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
112 changes: 107 additions & 5 deletions jsonpath.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2015, 2021; oliver, DoltHub Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package jsonpath

import (
Expand All @@ -7,11 +13,16 @@ import (
"go/types"
"reflect"
"regexp"
"sort"
"strconv"
"strings"

errKind "gopkg.in/src-d/go-errors.v1"
)

var ErrGetFromNullObj = errors.New("get attribute from null object")
var ErrKeyError = errKind.NewKind("key error: %s not found in object")


func JsonPathLookup(obj interface{}, jpath string) (interface{}, error) {
c, err := Compile(jpath)
Expand Down Expand Up @@ -45,6 +56,9 @@ func Compile(jpath string) (*Compiled, error) {
if err != nil {
return nil, err
}
if len(tokens) == 0 {
return nil, fmt.Errorf("empty path")
}
if tokens[0] != "@" && tokens[0] != "$" {
return nil, fmt.Errorf("$ or @ should in front of path")
}
Expand All @@ -70,7 +84,7 @@ func (c *Compiled) String() string {
func (c *Compiled) Lookup(obj interface{}) (interface{}, error) {
var err error
for _, s := range c.steps {
// "key", "idx"
// "key", "idx", "range", "filter", "scan"
switch s.op {
case "key":
obj, err = get_key(obj, s.key)
Expand Down Expand Up @@ -132,8 +146,20 @@ func (c *Compiled) Lookup(obj interface{}) (interface{}, error) {
if err != nil {
return nil, err
}
case "scan":
obj, err = get_scan(obj)
if err != nil {
return nil, err
}
if obj == nil {
continue
}
// empty scan is NULL
if len(obj.([]interface{})) == 0 {
obj = nil
}
default:
return nil, fmt.Errorf("expression don't support in filter")
return nil, fmt.Errorf("unsupported jsonpath operation: %s", s.op)
}
}
return obj, nil
Expand All @@ -144,9 +170,27 @@ func tokenize(query string) ([]string, error) {
// token_start := false
// token_end := false
token := ""
quoteChar := rune(0)

// fmt.Println("-------------------------------------------------- start")
for idx, x := range query {
if quoteChar != 0 {
if x == quoteChar {
quoteChar = 0
} else {
token += string(x)
}

continue
} else if x == '"' {
if token == "." {
token = ""
}

quoteChar = x
continue
}

token += string(x)
// //fmt.Printf("idx: %d, x: %s, token: %s, tokens: %v\n", idx, string(x), token, tokens)
if idx == 0 {
Expand Down Expand Up @@ -193,6 +237,11 @@ func tokenize(query string) ([]string, error) {
}
}
}

if quoteChar != 0 {
token = string(quoteChar) + token
}

if len(token) > 0 {
if token[0] == '.' {
token = token[1:]
Expand Down Expand Up @@ -325,7 +374,7 @@ func filter_get_from_explicit_path(obj interface{}, path string) (interface{}, e
return nil, err
}
default:
return nil, fmt.Errorf("expression don't support in filter")
return nil, fmt.Errorf("unsupported jsonpath operation %s in filter", op)
}
}
return xobj, nil
Expand All @@ -343,7 +392,7 @@ func get_key(obj interface{}, key string) (interface{}, error) {
if jsonMap, ok := obj.(map[string]interface{}); ok {
val, exists := jsonMap[key]
if !exists {
return nil, fmt.Errorf("key error: %s not found in object", key)
return nil, ErrKeyError.New(key)
}
return val, nil
}
Expand All @@ -353,7 +402,7 @@ func get_key(obj interface{}, key string) (interface{}, error) {
return reflect.ValueOf(obj).MapIndex(kv).Interface(), nil
}
}
return nil, fmt.Errorf("key error: %s not found in object", key)
return nil, ErrKeyError.New(key)
case reflect.Slice:
// slice we should get from all objects in it.
res := []interface{}{}
Expand Down Expand Up @@ -521,6 +570,59 @@ func get_filtered(obj, root interface{}, filter string) ([]interface{}, error) {
return res, nil
}

func get_scan(obj interface{}) (interface{}, error) {
if reflect.TypeOf(obj) == nil {
return nil, nil
}
switch reflect.TypeOf(obj).Kind() {
case reflect.Map:
// iterate over keys in sorted by length, then alphabetically
var res []interface{}
if jsonMap, ok := obj.(map[string]interface{}); ok {
var sortedKeys []string
for k := range jsonMap {
sortedKeys = append(sortedKeys, k)
}
sort.Slice(sortedKeys, func(i, j int) bool {
if len(sortedKeys[i]) != len(sortedKeys[j]) {
return len(sortedKeys[i]) < len(sortedKeys[j])
}
return sortedKeys[i] < sortedKeys[j]
})
for _, k := range sortedKeys {
res = append(res, jsonMap[k])
}
return res, nil
}
keys := reflect.ValueOf(obj).MapKeys()
sort.Slice(keys, func(i, j int) bool {
ki, kj := keys[i].String(), keys[j].String()
if len(ki) != len(kj) {
return len(ki) < len(kj)
}
return ki < kj
})
for _, k := range keys {
res = append(res, reflect.ValueOf(obj).MapIndex(k).Interface())
}
return res, nil
case reflect.Slice:
// slice we should get from all objects in it.
var res []interface{}
for i := 0; i < reflect.ValueOf(obj).Len(); i++ {
tmp := reflect.ValueOf(obj).Index(i).Interface()
newObj, err := get_scan(tmp)
if err != nil {
return nil, err
}
res = append(res, newObj.([]interface{})...)
}
return res, nil
default:
return nil, fmt.Errorf("object is not scannable: %v", reflect.TypeOf(obj).Kind())
}
}

// @.isbn => @.isbn, exists, nil
// @.price < 10 => @.price, <, 10
// @.price <= $.expensive => @.price, <=, $.expensive
Expand Down
Loading