-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpath.go
168 lines (149 loc) · 3.84 KB
/
mpath.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package mpath
import (
"fmt"
"strings"
"sync"
sc "text/scanner"
"unicode"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
func Setup(jsonMarshalDecimalsWithoutQuotes bool) {
decimal.MarshalJSONWithoutQuotes = jsonMarshalDecimalsWithoutQuotes
}
var (
scannerPool = sync.Pool{
New: func() any {
s := newScanner()
s.sx.Mode = sc.ScanIdents | sc.ScanChars | sc.ScanStrings | sc.ScanRawStrings | sc.ScanComments | sc.SkipComments
s.sx.IsIdentRune = func(ch rune, i int) bool {
// if i == 0 && unicode.IsDigit(ch) {
// return false
// }
return ch != '\'' &&
ch != '"' &&
ch != '(' &&
ch != ')' &&
ch != '[' &&
ch != ']' &&
ch != '{' &&
ch != '}' &&
ch != '@' &&
ch != '$' &&
ch != '&' &&
ch != '.' &&
ch != ',' &&
ch != '=' &&
ch != '>' &&
ch != '<' &&
ch != '|' &&
ch != '!' &&
ch != ';' &&
ch != '/' &&
ch != '*' &&
// ch != '?' && // taken out to allow for null propagation
!unicode.IsSpace(ch) &&
unicode.IsPrint(ch)
}
s.sx.Error = func(es *sc.Scanner, msg string) {
//todo: find a way to pipe this out
}
return s
},
}
stringsReaderPool = sync.Pool{
New: func() any {
return &strings.Reader{}
},
}
)
func ParseString(ss string) (topOp Operation, err error) {
s := scannerPool.Get().(*scanner)
defer scannerPool.Put(s)
sr := stringsReaderPool.Get().(*strings.Reader)
defer stringsReaderPool.Put(sr)
s.Reset(sr, ss)
var r rune
r = s.Scan()
for {
if r == sc.EOF || r == 0 {
break
}
switch r {
case '{':
if topOp != nil {
return nil, erAt(s, "operation not terminated properly: found Logical Operation after top operation already defined")
}
// Curly braces are for logical operation groups (&& and ||)
topOp = &opLogicalOperation{}
r, err = topOp.Parse(s, r)
if err != nil {
return nil, err
}
case '@', '$':
if topOp != nil {
return nil, erAt(s, "operation not terminated properly: found Path after top operation already defined")
}
// @ and $ are Path starters and specify whether to use the original data, or the data at this point of the path
topOp = &opPath{}
r, err = topOp.Parse(s, r)
if err != nil {
return nil, err
}
default:
if topOp == nil {
return nil, errors.Wrap(erInvalid(s, '{', '@', '$'), "invalid query")
}
return nil, erAt(s, "operation not terminated properly: found '%s' (%d) after top operation already defined", s.TokenText(), r)
}
}
return
}
func erAt(s *scanner, str string, args ...any) (err error) {
args = append([]any{s.sx.Pos().Line, s.sx.Pos().Column}, args...)
err = fmt.Errorf("error at line %d col %d: "+str, args...)
return
}
func erInvalid(s *scanner, validRunes ...rune) error {
if len(validRunes) == 0 {
return erAt(s, "invalid next character '%s'", s.TokenText())
}
if len(validRunes) == 1 {
return erAt(s, "invalid next character '%s': must be '%s'", s.TokenText(), string(validRunes[0]))
}
validRunesAsStrings := make([]string, len(validRunes))
for idx, vr := range validRunes {
validRunesAsStrings[idx] = string(vr)
}
return erAt(s, "invalid next character '%s': must be one of '%s'", s.TokenText(), strings.Join(validRunesAsStrings, "', '"))
}
type scanner struct {
sx *sc.Scanner
}
func newScanner() *scanner {
return &scanner{
sx: &sc.Scanner{},
}
}
func (s *scanner) TokenText() (t string) {
return s.sx.TokenText()
}
func (s *scanner) Reset(sr *strings.Reader, ss string) {
sr.Reset(ss)
s.sx.Init(sr)
s.sx.Mode = sc.ScanIdents | sc.ScanChars | sc.ScanStrings | sc.ScanRawStrings | sc.ScanComments | sc.SkipComments
}
func (s *scanner) Scan() (r rune) {
for {
r = s.sx.Scan()
// todo: what is this for?
// if r == -4 {
// // fmt.Print(string(r))
// }
// fmt.Print(s.sx.TokenText())
if r < 0 || unicode.IsPrint(r) {
break
}
}
return
}