Skip to content

Commit

Permalink
chore: prefer int64 to int (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRooney authored Jan 11, 2025
1 parent 9e15632 commit 08150c8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
16 changes: 8 additions & 8 deletions pkg/jsonpath/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

const MaxSafeFloat = 9007199254740991
const MaxSafeFloat int64 = 9007199254740991

type mode int

Expand Down Expand Up @@ -209,14 +209,14 @@ func (p *JSONPath) parseSelector() (retSelector *selector, err error) {
if err != nil {
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
}
err = p.checkSafeInteger(int(i), lit)
err = p.checkSafeInteger(i, lit)
if err != nil {
return nil, err
}

p.current++

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

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

// parse the start index
if p.tokens[p.current].Token == token.INTEGER {
literal := p.tokens[p.current].Literal
i, err := strconv.Atoi(literal)
i, err := strconv.ParseInt(literal, 10, 64)
if err != nil {
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
}
Expand All @@ -259,7 +259,7 @@ func (p *JSONPath) parseSliceSelector() (*slice, error) {
// parse the end index
if p.tokens[p.current].Token == token.INTEGER {
literal := p.tokens[p.current].Literal
i, err := strconv.Atoi(literal)
i, err := strconv.ParseInt(literal, 10, 64)
if err != nil {
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
}
Expand All @@ -277,7 +277,7 @@ func (p *JSONPath) parseSliceSelector() (*slice, error) {
p.current++
if p.tokens[p.current].Token == token.INTEGER {
literal := p.tokens[p.current].Literal
i, err := strconv.Atoi(literal)
i, err := strconv.ParseInt(literal, 10, 64)
if err != nil {
return nil, p.parseFailure(&p.tokens[p.current], "expected an integer")
}
Expand All @@ -297,7 +297,7 @@ func (p *JSONPath) parseSliceSelector() (*slice, error) {
return &slice{start: start, end: end, step: step}, nil
}

func (p *JSONPath) checkSafeInteger(i int, literal string) error {
func (p *JSONPath) checkSafeInteger(i int64, literal string) error {
if i > MaxSafeFloat || i < -MaxSafeFloat {
return p.parseFailure(&p.tokens[p.current], "outside bounds for safe integers")
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/jsonpath/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const (
)

type slice struct {
start *int
end *int
step *int
start *int64
end *int64
step *int64
}

type selector struct {
kind selectorSubKind
name string
index int
index int64
slice *slice
filter *filterSelector
}
Expand All @@ -36,24 +36,24 @@ func (s selector) ToString() string {
return "'" + escapeString(s.name) + "'"
case selectorSubKindArrayIndex:
// int to string
return strconv.Itoa(s.index)
return strconv.FormatInt(s.index, 10)
case selectorSubKindFilter:
return "?" + s.filter.ToString()
case selectorSubKindWildcard:
return "*"
case selectorSubKindArraySlice:
builder := strings.Builder{}
if s.slice.start != nil {
builder.WriteString(strconv.Itoa(*s.slice.start))
builder.WriteString(strconv.FormatInt(*s.slice.start, 10))
}
builder.WriteString(":")
if s.slice.end != nil {
builder.WriteString(strconv.Itoa(*s.slice.end))
builder.WriteString(strconv.FormatInt(*s.slice.end, 10))
}

if s.slice.step != nil {
builder.WriteString(":")
builder.WriteString(strconv.Itoa(*s.slice.step))
builder.WriteString(strconv.FormatInt(*s.slice.step, 10))
}
return builder.String()
default:
Expand Down
16 changes: 8 additions & 8 deletions pkg/jsonpath/yaml_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
return nil
}
// if out of bounds, return nothing
if s.index >= len(value.Content) || s.index < -len(value.Content) {
if s.index >= int64(len(value.Content)) || s.index < -int64(len(value.Content)) {
return nil
}
// if index is negative, go backwards
if s.index < 0 {
return []*yaml.Node{value.Content[len(value.Content)+s.index]}
return []*yaml.Node{value.Content[int64(len(value.Content))+s.index]}
}
return []*yaml.Node{value.Content[s.index]}
case selectorSubKindWildcard:
Expand All @@ -160,7 +160,7 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
if len(value.Content) == 0 {
return nil
}
step := 1
step := int64(1)
if s.slice.step != nil {
step = *s.slice.step
}
Expand All @@ -169,7 +169,7 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
}

start, end := s.slice.start, s.slice.end
lower, upper := bounds(start, end, step, len(value.Content))
lower, upper := bounds(start, end, step, int64(len(value.Content)))

var result []*yaml.Node
if step > 0 {
Expand Down Expand Up @@ -204,15 +204,15 @@ func (s selector) Query(value *yaml.Node, root *yaml.Node) []*yaml.Node {
return nil
}

func normalize(i, length int) int {
func normalize(i, length int64) int64 {
if i >= 0 {
return i
}
return length + i
}

func bounds(start, end *int, step, length int) (int, int) {
var nStart, nEnd int
func bounds(start, end *int64, step, length int64) (int64, int64) {
var nStart, nEnd int64
if start != nil {
nStart = normalize(*start, length)
} else if step > 0 {
Expand All @@ -228,7 +228,7 @@ func bounds(start, end *int, step, length int) (int, int) {
nEnd = -1
}

var lower, upper int
var lower, upper int64
if step >= 0 {
lower = max(min(nStart, length), 0)
upper = min(max(nEnd, 0), length)
Expand Down
Binary file modified web/src/assets/wasm/lib.wasm
Binary file not shown.

0 comments on commit 08150c8

Please sign in to comment.