Skip to content

Commit

Permalink
Merge pull request #1 from fjogeleit/not-found-error
Browse files Browse the repository at this point in the history
Implement NotFoundError
  • Loading branch information
fjogeleit authored Aug 7, 2024
2 parents d2e288e + aa9fe97 commit 45cb222
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ htmlc:
go test -cover -coverpkg ./... -coverprofile="/tmp/jpcov" ./... && go tool cover -html="/tmp/jpcov" && unlink /tmp/jpcov

buildfuzz:
go-fuzz-build github.com/jmespath-community/go-jmespath/fuzz
go-fuzz-build github.com/kyverno/go-community-jmespath/fuzz

fuzz: buildfuzz
go-fuzz -bin=./jmespath-fuzz.zip -workdir=fuzz/testdata
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# go-jmespath - A JMESPath implementation in Go

[![GoDoc](https://godoc.org/github.com/jmespath-community/go-jmespath?status.svg)](https://godoc.org/github.com/jmespath-community/go-jmespath)
[![GoDoc](https://godoc.org/github.com/kyverno/go-community-jmespath?status.svg)](https://godoc.org/github.com/kyverno/go-community-jmespath)
[![codecov](https://codecov.io/gh/jmespath-community/go-jmespath/branch/main/graph/badge.svg)](https://app.codecov.io/gh/jmespath-community/go-jmespath/branch/main)
[![Go Report Card](https://goreportcard.com/badge/github.com/jmespath-community/go-jmespath)](https://goreportcard.com/report/github.com/jmespath-community/go-jmespath)
[![Go Report Card](https://goreportcard.com/badge/github.com/kyverno/go-community-jmespath)](https://goreportcard.com/report/github.com/kyverno/go-community-jmespath)
![License: Apache-2.0](https://img.shields.io/github/license/jmespath-community/go-jmespath?color=blue)

go-jmespath is a GO implementation of JMESPath,
Expand All @@ -15,7 +15,7 @@ you use, `jmespath.Search`:


```go
> import "github.com/jmespath-community/go-jmespath"
> import "github.com/kyverno/go-community-jmespath"
>
> var jsondata = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) // your data
> var data interface{}
Expand Down
4 changes: 2 additions & 2 deletions cmd/jpgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"io"
"os"

"github.com/jmespath-community/go-jmespath/pkg/api"
"github.com/jmespath-community/go-jmespath/pkg/parsing"
"github.com/kyverno/go-community-jmespath/pkg/api"
"github.com/kyverno/go-community-jmespath/pkg/parsing"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/jmespath.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jmespath

import "github.com/jmespath-community/go-jmespath/pkg/parsing"
import "github.com/kyverno/go-community-jmespath/pkg/parsing"

// Fuzz will fuzz test the JMESPath parser.
func Fuzz(data []byte) int {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/jmespath-community/go-jmespath
module github.com/kyverno/go-community-jmespath

go 1.18

Expand Down
13 changes: 9 additions & 4 deletions jp.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package jmespath

import (
"github.com/jmespath-community/go-jmespath/pkg/api"
"github.com/jmespath-community/go-jmespath/pkg/functions"
"github.com/jmespath-community/go-jmespath/pkg/interpreter"
"github.com/jmespath-community/go-jmespath/pkg/parsing"
"github.com/kyverno/go-community-jmespath/pkg/api"
jperror "github.com/kyverno/go-community-jmespath/pkg/error"
"github.com/kyverno/go-community-jmespath/pkg/functions"
"github.com/kyverno/go-community-jmespath/pkg/interpreter"
"github.com/kyverno/go-community-jmespath/pkg/parsing"
)

// api types
Expand Down Expand Up @@ -50,3 +51,7 @@ const (
JpExpref = functions.JpExpref
JpAny = functions.JpAny
)

// error types

type NotFoundError = jperror.NotFoundError
11 changes: 10 additions & 1 deletion jp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"testing"

"github.com/jmespath-community/go-jmespath/pkg/parsing"
"github.com/kyverno/go-community-jmespath/pkg/parsing"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -85,6 +85,10 @@ func runSyntaxTestCase(assert *assert.Assertions, given interface{}, testcase Te
// an error when we try to evaluate the expression.
// fmt.Println(fmt.Sprintf("%s: %s", filename, testcase.Expression))
_, err := Search(testcase.Expression, given)
if _, ok := err.(NotFoundError); ok {
err = nil
}

assert.NotNil(err, fmt.Sprintf("Expression: %s", testcase.Expression))
}

Expand All @@ -105,6 +109,11 @@ func runTestCase(assert *assert.Assertions, given interface{}, testcase TestCase
return
}
actual, err := Search(testcase.Expression, given)
if _, ok := err.(NotFoundError); ok {
err = nil
actual = nil
}

if assert.Nil(err, fmt.Sprintf("Expression: %s", testcase.Expression)) {
assert.Equal(testcase.Result, actual, fmt.Sprintf("Expression: %s", testcase.Expression))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package api
import (
"strconv"

"github.com/jmespath-community/go-jmespath/pkg/interpreter"
"github.com/jmespath-community/go-jmespath/pkg/parsing"
"github.com/kyverno/go-community-jmespath/pkg/interpreter"
"github.com/kyverno/go-community-jmespath/pkg/parsing"
)

// JMESPath is the representation of a compiled JMES path query. A JMESPath is
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"testing"

"github.com/jmespath-community/go-jmespath/pkg/functions"
"github.com/jmespath-community/go-jmespath/pkg/interpreter"
"github.com/kyverno/go-community-jmespath/pkg/functions"
"github.com/kyverno/go-community-jmespath/pkg/interpreter"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -125,7 +125,8 @@ func TestSearch(t *testing.T) {
"a": 42.0,
},
},
want: nil,
want: nil,
wantErr: true,
}, {
args: args{
expression: "`null` | {foo: @}",
Expand Down
13 changes: 13 additions & 0 deletions pkg/error/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import (
"fmt"
)

// NotFoundError is returned when it is impossible to resolve the AstField.
type NotFoundError struct {
key string
}

func (n NotFoundError) Error() string {
return fmt.Sprintf("Unknown key \"%s\" in path", n.key)
}

func NotFound(key string) NotFoundError {
return NotFoundError{key}
}

func NotAnInteger(name string, arg string) error {
return errors.New(formatNotAnInteger(name, arg))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"unicode"
"unicode/utf8"

jperror "github.com/jmespath-community/go-jmespath/pkg/error"
"github.com/jmespath-community/go-jmespath/pkg/util"
jperror "github.com/kyverno/go-community-jmespath/pkg/error"
"github.com/kyverno/go-community-jmespath/pkg/util"
)

type (
Expand Down
6 changes: 3 additions & 3 deletions pkg/interpreter/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
"fmt"

jperror "github.com/jmespath-community/go-jmespath/pkg/error"
"github.com/jmespath-community/go-jmespath/pkg/functions"
"github.com/jmespath-community/go-jmespath/pkg/util"
jperror "github.com/kyverno/go-community-jmespath/pkg/error"
"github.com/kyverno/go-community-jmespath/pkg/functions"
"github.com/kyverno/go-community-jmespath/pkg/util"
)

type FunctionCaller interface {
Expand Down
Loading

0 comments on commit 45cb222

Please sign in to comment.