summaryrefslogtreecommitdiff
path: root/vendor/github.com/exponent-io/jsonpath/path.go
blob: d7db2ad336efd546c032d1a24e625e9f9405d1af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Extends the Go runtime's json.Decoder enabling navigation of a stream of json tokens.
package jsonpath

import "fmt"

type jsonContext int

const (
	none jsonContext = iota
	objKey
	objValue
	arrValue
)

// AnyIndex can be used in a pattern to match any array index.
const AnyIndex = -2

// JsonPath is a slice of strings and/or integers. Each string specifies an JSON object key, and
// each integer specifies an index into a JSON array.
type JsonPath []interface{}

func (p *JsonPath) push(n interface{}) { *p = append(*p, n) }
func (p *JsonPath) pop()               { *p = (*p)[:len(*p)-1] }

// increment the index at the top of the stack (must be an array index)
func (p *JsonPath) incTop() { (*p)[len(*p)-1] = (*p)[len(*p)-1].(int) + 1 }

// name the key at the top of the stack (must be an object key)
func (p *JsonPath) nameTop(n string) { (*p)[len(*p)-1] = n }

// infer the context from the item at the top of the stack
func (p *JsonPath) inferContext() jsonContext {
	if len(*p) == 0 {
		return none
	}
	t := (*p)[len(*p)-1]
	switch t.(type) {
	case string:
		return objKey
	case int:
		return arrValue
	default:
		panic(fmt.Sprintf("Invalid stack type %T", t))
	}
}

// Equal tests for equality between two JsonPath types.
func (p *JsonPath) Equal(o JsonPath) bool {
	if len(*p) != len(o) {
		return false
	}
	for i, v := range *p {
		if v != o[i] {
			return false
		}
	}
	return true
}

func (p *JsonPath) HasPrefix(o JsonPath) bool {
	for i, v := range o {
		if v != (*p)[i] {
			return false
		}
	}
	return true
}