2019-02-26 13:05:15 +04:00
|
|
|
package jsonpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
opTypeIndex = iota
|
|
|
|
opTypeIndexRange
|
|
|
|
opTypeIndexWild
|
|
|
|
opTypeName
|
|
|
|
opTypeNameList
|
|
|
|
opTypeNameWild
|
|
|
|
)
|
|
|
|
|
|
|
|
type Path struct {
|
|
|
|
stringValue string
|
|
|
|
operators []*operator
|
|
|
|
captureEndValue bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type operator struct {
|
|
|
|
typ int
|
|
|
|
indexStart int
|
|
|
|
indexEnd int
|
|
|
|
hasIndexEnd bool
|
|
|
|
keyStrings map[string]struct{}
|
|
|
|
|
|
|
|
whereClauseBytes []byte
|
|
|
|
dependentPaths []*Path
|
|
|
|
whereClause []Item
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
// nolint:gocognit
|
2019-02-26 13:05:15 +04:00
|
|
|
func genIndexKey(tr tokenReader) (*operator, error) {
|
|
|
|
k := &operator{}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
var t *Item
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
var ok bool
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
if t, ok = tr.next(); !ok {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, errors.New("expected number, key, or *, but got none")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch t.typ {
|
|
|
|
case pathWildcard:
|
|
|
|
k.typ = opTypeIndexWild
|
|
|
|
k.indexStart = 0
|
2019-10-19 02:14:19 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
if t, ok = tr.next(); !ok {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, errors.New("expected ] after *, but got none")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 02:14:19 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
if t.typ != pathBracketRight {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, fmt.Errorf("expected ] after * instead of %q", t.val)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
case pathIndex:
|
|
|
|
v, err := strconv.Atoi(string(t.val))
|
|
|
|
if err != nil {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, fmt.Errorf("could not parse %q into int64", t.val)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
k.indexStart = v
|
|
|
|
k.indexEnd = v
|
|
|
|
k.hasIndexEnd = true
|
|
|
|
|
|
|
|
if t, ok = tr.next(); !ok {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, errors.New("expected number or *, but got none")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
switch t.typ {
|
|
|
|
case pathIndexRange:
|
|
|
|
if t, ok = tr.next(); !ok {
|
2019-10-19 01:17:00 +04:00
|
|
|
return nil, errors.New("expected number or *, but got none")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
switch t.typ {
|
|
|
|
case pathIndex:
|
|
|
|
v, err := strconv.Atoi(string(t.val))
|
|
|
|
if err != nil {
|
2019-10-19 01:17:00 +04:00
|
|
|
return nil, fmt.Errorf("could not parse %q into int64", t.val)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
k.indexEnd = v - 1
|
|
|
|
k.hasIndexEnd = true
|
|
|
|
|
|
|
|
if t, ok = tr.next(); !ok || t.typ != pathBracketRight {
|
2019-10-19 01:17:00 +04:00
|
|
|
return nil, errors.New("expected ], but got none")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
case pathBracketRight:
|
|
|
|
k.hasIndexEnd = false
|
|
|
|
default:
|
2019-10-19 01:17:00 +04:00
|
|
|
return nil, fmt.Errorf("unexpected value within brackets after index: %q", t.val)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
k.typ = opTypeIndexRange
|
|
|
|
case pathBracketRight:
|
|
|
|
k.typ = opTypeIndex
|
|
|
|
default:
|
2019-10-19 01:17:00 +04:00
|
|
|
return nil, fmt.Errorf("unexpected value within brackets after index: %q", t.val)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
case pathKey:
|
2019-10-19 00:39:21 +04:00
|
|
|
k.keyStrings = map[string]struct{}{
|
|
|
|
string(t.val[1 : len(t.val)-1]): {},
|
|
|
|
}
|
2019-02-26 13:05:15 +04:00
|
|
|
k.typ = opTypeName
|
|
|
|
|
|
|
|
if t, ok = tr.next(); !ok || t.typ != pathBracketRight {
|
2019-10-19 01:17:00 +04:00
|
|
|
return nil, errors.New("expected ], but got none")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
default:
|
2019-10-19 01:17:00 +04:00
|
|
|
return nil, fmt.Errorf("unexpected value within brackets: %q", t.val)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePath(pathString string) (*Path, error) {
|
|
|
|
lexer := NewSliceLexer([]byte(pathString), PATH)
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
p, err := tokensToOperators(lexer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.stringValue = pathString
|
|
|
|
|
|
|
|
//Generate dependent paths
|
|
|
|
for _, op := range p.operators {
|
|
|
|
if len(op.whereClauseBytes) > 0 {
|
|
|
|
var err error
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
trimmed := op.whereClauseBytes[1 : len(op.whereClauseBytes)-1]
|
|
|
|
whereLexer := NewSliceLexer(trimmed, EXPRESSION)
|
|
|
|
items := readerToArray(whereLexer)
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
if errItem, found := findErrors(items); found {
|
|
|
|
return nil, errors.New(string(errItem.val))
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform expression into postfix form
|
|
|
|
op.whereClause, err = infixToPostFix(items[:len(items)-1]) // trim EOF
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
op.dependentPaths = make([]*Path, 0)
|
|
|
|
// parse all paths in expression
|
|
|
|
for _, item := range op.whereClause {
|
|
|
|
if item.typ == exprPath {
|
|
|
|
p, err := parsePath(string(item.val))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
op.dependentPaths = append(op.dependentPaths, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tokensToOperators(tr tokenReader) (*Path, error) {
|
|
|
|
q := &Path{
|
|
|
|
stringValue: "",
|
|
|
|
captureEndValue: false,
|
|
|
|
operators: make([]*operator, 0),
|
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
for {
|
|
|
|
p, ok := tr.next()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
switch p.typ {
|
|
|
|
case pathRoot:
|
|
|
|
if len(q.operators) != 0 {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, errors.New("unexpected root node after start")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-10-19 01:41:57 +04:00
|
|
|
continue
|
2019-02-26 13:05:15 +04:00
|
|
|
case pathCurrent:
|
|
|
|
if len(q.operators) != 0 {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, errors.New("unexpected current node after start")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-10-19 01:41:57 +04:00
|
|
|
continue
|
2019-02-26 13:05:15 +04:00
|
|
|
case pathPeriod:
|
|
|
|
continue
|
|
|
|
case pathBracketLeft:
|
|
|
|
k, err := genIndexKey(tr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-10-19 02:14:19 +04:00
|
|
|
q.operators = append(q.operators, k)
|
2019-02-26 13:05:15 +04:00
|
|
|
case pathKey:
|
|
|
|
keyName := p.val
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
if len(p.val) == 0 {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, fmt.Errorf("key length is zero at %d", p.pos)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
if p.val[0] == '"' && p.val[len(p.val)-1] == '"' {
|
|
|
|
keyName = p.val[1 : len(p.val)-1]
|
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-10-19 00:39:21 +04:00
|
|
|
q.operators = append(
|
|
|
|
q.operators,
|
|
|
|
&operator{
|
|
|
|
typ: opTypeName,
|
|
|
|
keyStrings: map[string]struct{}{
|
|
|
|
string(keyName): {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2019-02-26 13:05:15 +04:00
|
|
|
case pathWildcard:
|
|
|
|
q.operators = append(q.operators, &operator{typ: opTypeNameWild})
|
|
|
|
case pathValue:
|
|
|
|
q.captureEndValue = true
|
|
|
|
case pathWhere:
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
case pathExpression:
|
|
|
|
if len(q.operators) == 0 {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, errors.New("cannot add where clause on last key")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
last := q.operators[len(q.operators)-1]
|
|
|
|
if last.whereClauseBytes != nil {
|
2019-10-19 01:41:57 +04:00
|
|
|
return nil, errors.New("expression on last key already set")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
2019-10-19 01:17:00 +04:00
|
|
|
|
2019-10-19 01:41:57 +04:00
|
|
|
last.whereClauseBytes = p.val
|
2019-02-26 13:05:15 +04:00
|
|
|
case pathError:
|
|
|
|
return q, errors.New(string(p.val))
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 01:41:57 +04:00
|
|
|
|
2019-02-26 13:05:15 +04:00
|
|
|
return q, nil
|
|
|
|
}
|