Skip to content

Commit 08150c8

Browse files
authored
chore: prefer int64 to int (#10)
1 parent 9e15632 commit 08150c8

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

pkg/jsonpath/parser.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99
)
1010

11-
const MaxSafeFloat = 9007199254740991
11+
const MaxSafeFloat int64 = 9007199254740991
1212

1313
type mode int
1414

@@ -209,14 +209,14 @@ func (p *JSONPath) parseSelector() (retSelector *selector, err error) {
209209
if err != nil {
210210
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
211211
}
212-
err = p.checkSafeInteger(int(i), lit)
212+
err = p.checkSafeInteger(i, lit)
213213
if err != nil {
214214
return nil, err
215215
}
216216

217217
p.current++
218218

219-
return &selector{kind: selectorSubKindArrayIndex, index: int(i)}, nil
219+
return &selector{kind: selectorSubKindArrayIndex, index: i}, nil
220220
} else if p.tokens[p.current].Token == token.ARRAY_SLICE {
221221
slice, err := p.parseSliceSelector()
222222
if err != nil {
@@ -232,12 +232,12 @@ func (p *JSONPath) parseSelector() (retSelector *selector, err error) {
232232

233233
func (p *JSONPath) parseSliceSelector() (*slice, error) {
234234
// slice-selector = [start S] ":" S [end S] [":" [S step]]
235-
var start, end, step *int
235+
var start, end, step *int64
236236

237237
// parse the start index
238238
if p.tokens[p.current].Token == token.INTEGER {
239239
literal := p.tokens[p.current].Literal
240-
i, err := strconv.Atoi(literal)
240+
i, err := strconv.ParseInt(literal, 10, 64)
241241
if err != nil {
242242
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
243243
}
@@ -259,7 +259,7 @@ func (p *JSONPath) parseSliceSelector() (*slice, error) {
259259
// parse the end index
260260
if p.tokens[p.current].Token == token.INTEGER {
261261
literal := p.tokens[p.current].Literal
262-
i, err := strconv.Atoi(literal)
262+
i, err := strconv.ParseInt(literal, 10, 64)
263263
if err != nil {
264264
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
265265
}
@@ -277,7 +277,7 @@ func (p *JSONPath) parseSliceSelector() (*slice, error) {
277277
p.current++
278278
if p.tokens[p.current].Token == token.INTEGER {
279279
literal := p.tokens[p.current].Literal
280-
i, err := strconv.Atoi(literal)
280+
i, err := strconv.ParseInt(literal, 10, 64)
281281
if err != nil {
282282
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
283283
}
@@ -297,7 +297,7 @@ func (p *JSONPath) parseSliceSelector() (*slice, error) {
297297
return &slice{start: start, end: end, step: step}, nil
298298
}
299299

300-
func (p *JSONPath) checkSafeInteger(i int, literal string) error {
300+
func (p *JSONPath) checkSafeInteger(i int64, literal string) error {
301301
if i > MaxSafeFloat || i < -MaxSafeFloat {
302302
return p.parseFailure(&p.tokens[p.current], "outside bounds for safe integers")
303303
}

pkg/jsonpath/selector.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ const (
1717
)
1818

1919
type slice struct {
20-
start *int
21-
end *int
22-
step *int
20+
start *int64
21+
end *int64
22+
step *int64
2323
}
2424

2525
type selector struct {
2626
kind selectorSubKind
2727
name string
28-
index int
28+
index int64
2929
slice *slice
3030
filter *filterSelector
3131
}
@@ -36,24 +36,24 @@ func (s selector) ToString() string {
3636
return "'" + escapeString(s.name) + "'"
3737
case selectorSubKindArrayIndex:
3838
// int to string
39-
return strconv.Itoa(s.index)
39+
return strconv.FormatInt(s.index, 10)
4040
case selectorSubKindFilter:
4141
return "?" + s.filter.ToString()
4242
case selectorSubKindWildcard:
4343
return "*"
4444
case selectorSubKindArraySlice:
4545
builder := strings.Builder{}
4646
if s.slice.start != nil {
47-
builder.WriteString(strconv.Itoa(*s.slice.start))
47+
builder.WriteString(strconv.FormatInt(*s.slice.start, 10))
4848
}
4949
builder.WriteString(":")
5050
if s.slice.end != nil {
51-
builder.WriteString(strconv.Itoa(*s.slice.end))
51+
builder.WriteString(strconv.FormatInt(*s.slice.end, 10))
5252
}
5353

5454
if s.slice.step != nil {
5555
builder.WriteString(":")
56-
builder.WriteString(strconv.Itoa(*s.slice.step))
56+
builder.WriteString(strconv.FormatInt(*s.slice.step, 10))
5757
}
5858
return builder.String()
5959
default:

pkg/jsonpath/yaml_query.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
132132
return nil
133133
}
134134
// if out of bounds, return nothing
135-
if s.index >= len(value.Content) || s.index < -len(value.Content) {
135+
if s.index >= int64(len(value.Content)) || s.index < -int64(len(value.Content)) {
136136
return nil
137137
}
138138
// if index is negative, go backwards
139139
if s.index < 0 {
140-
return []*yaml.Node{value.Content[len(value.Content)+s.index]}
140+
return []*yaml.Node{value.Content[int64(len(value.Content))+s.index]}
141141
}
142142
return []*yaml.Node{value.Content[s.index]}
143143
case selectorSubKindWildcard:
@@ -160,7 +160,7 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
160160
if len(value.Content) == 0 {
161161
return nil
162162
}
163-
step := 1
163+
step := int64(1)
164164
if s.slice.step != nil {
165165
step = *s.slice.step
166166
}
@@ -169,7 +169,7 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
169169
}
170170

171171
start, end := s.slice.start, s.slice.end
172-
lower, upper := bounds(start, end, step, len(value.Content))
172+
lower, upper := bounds(start, end, step, int64(len(value.Content)))
173173

174174
var result []*yaml.Node
175175
if step > 0 {
@@ -204,15 +204,15 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
204204
return nil
205205
}
206206

207-
func normalize(i, length int) int {
207+
func normalize(i, length int64) int64 {
208208
if i >= 0 {
209209
return i
210210
}
211211
return length + i
212212
}
213213

214-
func bounds(start, end *int, step, length int) (int, int) {
215-
var nStart, nEnd int
214+
func bounds(start, end *int64, step, length int64) (int64, int64) {
215+
var nStart, nEnd int64
216216
if start != nil {
217217
nStart = normalize(*start, length)
218218
} else if step > 0 {
@@ -228,7 +228,7 @@ func bounds(start, end *int, step, length int) (int, int) {
228228
nEnd = -1
229229
}
230230

231-
var lower, upper int
231+
var lower, upper int64
232232
if step >= 0 {
233233
lower = max(min(nStart, length), 0)
234234
upper = min(max(nEnd, 0), length)

web/src/assets/wasm/lib.wasm

82 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)