Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ func TestInvalidMustCompilePanics(t *testing.T) {
}()
MustCompile("not a valid expression")
}

func TestToEntries(t *testing.T) {
assert := assert.New(t)
data := make(map[string]interface{})
data["foo"] = "bar"
data["baz"] = 42
result, err := Search("to_entries(@)", data)
assert.Nil(err)

entries, ok := result.([]interface{})
assert.True(ok)
assert.Equal(2, len(entries))

for _, entry := range entries {
entryMap, ok := entry.(map[string]interface{})
assert.True(ok)
_, hasKey := entryMap["key"]
_, hasValue := entryMap["value"]
assert.True(hasKey)
assert.True(hasValue)
}
}
12 changes: 12 additions & 0 deletions compliance/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@
"expression": "values(foo)",
"error": "invalid-type"
},
{
"expression": "to_entries(objects)",
"result": [{"key": "bar", "value": "baz"}, {"key": "foo", "value": "bar"}]
},
{
"expression": "to_entries(empty_hash)",
"result": []
},
{
"expression": "to_entries(foo)",
"error": "invalid-type"
},
{
"expression": "join(', ', strings)",
"result": "a, b, c"
Expand Down
25 changes: 25 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ func newFunctionCaller() *functionCaller {
},
handler: jpfValues,
},
"to_entries": {
name: "to_entries",
arguments: []argSpec{
{types: []jpType{jpObject}},
},
handler: jpfToEntries,
},
"sort": {
name: "sort",
arguments: []argSpec{
Expand Down Expand Up @@ -711,6 +718,24 @@ func jpfValues(arguments []interface{}) (interface{}, error) {
}
return collected, nil
}
func jpfToEntries(arguments []interface{}) (interface{}, error) {
arg := arguments[0].(map[string]interface{})
keys := make([]string, 0, len(arg))
for key := range arg {
keys = append(keys, key)
}
sort.Strings(keys)

collected := make([]interface{}, 0, len(arg))
for _, key := range keys {
entry := map[string]interface{}{
"key": key,
"value": arg[key],
}
collected = append(collected, entry)
}
return collected, nil
}
func jpfSort(arguments []interface{}) (interface{}, error) {
if items, ok := toArrayNum(arguments[0]); ok {
d := sort.Float64Slice(items)
Expand Down
14 changes: 14 additions & 0 deletions fuzz/jmespath_native_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package jmespath

import (
"testing"

"github.com/jmespath/go-jmespath"
)

func FuzzParse(f *testing.F) {
f.Fuzz(func(t *testing.T, data string) {
p := jmespath.NewParser()
_, _ = p.Parse(data)
})
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/jmespath/go-jmespath

go 1.14
go 1.23

require github.com/jmespath/go-jmespath/internal/testify v1.5.1

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)
4 changes: 2 additions & 2 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (lexer *Lexer) tokenize(expression string) ([]token, error) {
loop:
for {
r := lexer.next()
if identifierStartBits&(1<<(uint64(r)-64)) > 0 {
if r >= 64 && r < 128 && identifierStartBits&(1<<(uint64(r)-64)) > 0 {
t := lexer.consumeUnquotedIdentifier()
tokens = append(tokens, t)
} else if val, ok := basicTokens[r]; ok {
Expand Down Expand Up @@ -386,7 +386,7 @@ func (lexer *Lexer) consumeUnquotedIdentifier() token {
start := lexer.currentPos - lexer.lastWidth
for {
r := lexer.next()
if r < 0 || r > 128 || identifierTrailingBits[uint64(r)/64]&(1<<(uint64(r)%64)) == 0 {
if r < 0 || r >= 128 || identifierTrailingBits[uint64(r)/64]&(1<<(uint64(r)%64)) == 0 {
lexer.back()
break
}
Expand Down