Skip to content

Commit

Permalink
feat: use any instead of interface (#106)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Sep 19, 2024
1 parent 76ba4b0 commit 9269200
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 206 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ you use, `jmespath.Search`:
> import "github.com/jmespath-community/go-jmespath"
>
> var jsondata = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) // your data
> var data interface{}
> var data any
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search("foo.bar.baz[2]", data)
result = 2
Expand All @@ -34,15 +34,15 @@ from a list. Here are a few more examples:

```go
> var jsondata = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) // your data
> var data interface{}
> var data any
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search("foo.bar", data)
result = { "baz": [ 0, 1, 2, 3, 4 ] }


> var jsondata = []byte(`{"foo": [{"first": "a", "last": "b"},
{"first": "c", "last": "d"}]}`) // your data
> var data interface{}
> var data any
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search({"foo[*].first", data)
result [ 'a', 'c' ]
Expand All @@ -51,7 +51,7 @@ result [ 'a', 'c' ]
> var jsondata = []byte(`{"foo": [{"age": 20}, {"age": 25},
{"age": 30}, {"age": 35},
{"age": 40}]}`) // your data
> var data interface{}
> var data any
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search("foo[?age > `30`]")
result = [ { age: 35 }, { age: 40 } ]
Expand All @@ -62,7 +62,7 @@ you are going to run multiple searches with it:
```go
> var jsondata = []byte(`{"foo": "bar"}`)
> var data interface{}
> var data any
> err := json.Unmarshal(jsondata, &data)
> precompiled, err := Compile("foo")
> if err != nil{
Expand Down
2 changes: 1 addition & 1 deletion cmd/jpgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c command) run(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error reading from stdin: %w", err)
}
}
var data interface{}
var data any
if err := json.Unmarshal(inputData, &data); err != nil {
return fmt.Errorf("invalid input JSON: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions jp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
)

type TestSuite struct {
Given interface{}
Given any
TestCases []TestCase `json:"cases"`
Comment string
}

type TestCase struct {
Comment string
Expression string
Result interface{}
Result any
Error string
}

Expand Down Expand Up @@ -80,15 +80,15 @@ func runTestSuite(assert *assert.Assertions, testsuite TestSuite, filename strin
}
}

func runSyntaxTestCase(assert *assert.Assertions, given interface{}, testcase TestCase, filename string) {
func runSyntaxTestCase(assert *assert.Assertions, given any, testcase TestCase, filename string) {
// Anything with an .Error means that we expect that JMESPath should return
// an error when we try to evaluate the expression.
// fmt.Println(fmt.Sprintf("%s: %s", filename, testcase.Expression))
_, err := Search(testcase.Expression, given)
assert.NotNil(err, fmt.Sprintf("Expression: %s", testcase.Expression))
}

func runTestCase(assert *assert.Assertions, given interface{}, testcase TestCase, filename string) {
func runTestCase(assert *assert.Assertions, given any, testcase TestCase, filename string) {
lexer := parsing.NewLexer()
var err error
_, err = lexer.Tokenize(testcase.Expression)
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// JMESPath is the representation of a compiled JMES path query. A JMESPath is
// safe for concurrent use by multiple goroutines.
type JMESPath interface {
Search(interface{}, ...interpreter.Option) (interface{}, error)
Search(any, ...interpreter.Option) (any, error)
}

type jmesPath struct {
Expand Down Expand Up @@ -46,13 +46,13 @@ func MustCompile(expression string) JMESPath {
}

// Search evaluates a JMESPath expression against input data and returns the result.
func (jp jmesPath) Search(data interface{}, opts ...interpreter.Option) (interface{}, error) {
func (jp jmesPath) Search(data any, opts ...interpreter.Option) (any, error) {
intr := interpreter.NewInterpreter(data, nil)
return intr.Execute(jp.node, data, opts...)
}

// Search evaluates a JMESPath expression against input data and returns the result.
func Search(expression string, data interface{}, opts ...interpreter.Option) (interface{}, error) {
func Search(expression string, data any, opts ...interpreter.Option) (any, error) {
compiled, err := Compile(expression)
if err != nil {
return nil, err
Expand Down
26 changes: 13 additions & 13 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestValidUncompiledExpressionSearches(t *testing.T) {
assert := assert.New(t)
j := []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`)
var d interface{}
var d any
err := json.Unmarshal(j, &d)
assert.Nil(err)
result, err := Search("foo.bar.baz[2]", d)
Expand All @@ -22,7 +22,7 @@ func TestValidUncompiledExpressionSearches(t *testing.T) {

func TestValidPrecompiledExpressionSearches(t *testing.T) {
assert := assert.New(t)
data := make(map[string]interface{})
data := make(map[string]any)
data["foo"] = "bar"
precompiled, err := Compile("foo")
assert.Nil(err)
Expand All @@ -45,7 +45,7 @@ func TestInvalidMustCompilePanics(t *testing.T) {
MustCompile("not a valid expression")
}

func jpfEcho(arguments []interface{}) (interface{}, error) {
func jpfEcho(arguments []any) (any, error) {
return arguments[0], nil
}

Expand All @@ -54,13 +54,13 @@ func TestSearch(t *testing.T) {

type args struct {
expression string
data interface{}
data any
funcs []functions.FunctionEntry
}
tests := []struct {
name string
args args
want interface{}
want any
wantErr bool
}{{
args: args{
Expand All @@ -70,13 +70,13 @@ func TestSearch(t *testing.T) {
}, {
args: args{
expression: "sort_by(@, &@ *`-1.0`)",
data: []interface{}{1.0, 2.0, 3.0, 4.0, 5.0},
data: []any{1.0, 2.0, 3.0, 4.0, 5.0},
},
want: []interface{}{5.0, 4.0, 3.0, 2.0, 1.0},
want: []any{5.0, 4.0, 3.0, 2.0, 1.0},
}, {
args: args{
expression: "echo(@)",
data: []interface{}{1.0, 2.0, 3.0, 4.0, 5.0},
data: []any{1.0, 2.0, 3.0, 4.0, 5.0},
funcs: []functions.FunctionEntry{{
Name: "echo",
Handler: jpfEcho,
Expand All @@ -85,7 +85,7 @@ func TestSearch(t *testing.T) {
},
}},
},
want: []interface{}{1.0, 2.0, 3.0, 4.0, 5.0},
want: []any{1.0, 2.0, 3.0, 4.0, 5.0},
}, {
args: args{
expression: "echo(@)",
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestSearch(t *testing.T) {
}, {
args: args{
expression: `@."$".a`,
data: map[string]interface{}{
data: map[string]any{
"a": 42.0,
},
},
Expand All @@ -130,21 +130,21 @@ func TestSearch(t *testing.T) {
args: args{
expression: "`null` | {foo: @}",
},
want: map[string]interface{}{
want: map[string]any{
"foo": nil,
},
}, {
args: args{
expression: "let $root = @ in $root.a",
data: map[string]interface{}{
data: map[string]any{
"a": 42.0,
},
},
want: 42.0,
}, {
args: args{
expression: "contains(@, { foo: 'bar' })",
data: []interface{}{map[string]any{}, nil, map[string]any{"foo": "bar"}},
data: []any{map[string]any{}, nil, map[string]any{"foo": "bar"}},
},
want: true,
}, {
Expand Down
8 changes: 4 additions & 4 deletions pkg/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ package binding
// You can get the value of the binding by calling the `Value` method.
type Binding interface {
// Get returns the value bound for a given name.
Value() (interface{}, error)
Value() (any, error)
}

type binding struct {
value interface{}
value any
}

func (b *binding) Value() (interface{}, error) {
func (b *binding) Value() (any, error) {
return b.value, nil
}

func NewBinding(value interface{}) Binding {
func NewBinding(value any) Binding {
return &binding{
value: value,
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestNewBinding(t *testing.T) {
tests := []struct {
name string
value interface{}
value any
want Binding
}{{
name: "nil",
Expand All @@ -24,8 +24,8 @@ func TestNewBinding(t *testing.T) {
want: &binding{"42"},
}, {
name: "array",
value: []interface{}{"42", 42},
want: &binding{[]interface{}{"42", 42}},
value: []any{"42", 42},
want: &binding{[]any{"42", 42}},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -39,8 +39,8 @@ func TestNewBinding(t *testing.T) {
func Test_binding_Value(t *testing.T) {
tests := []struct {
name string
value interface{}
want interface{}
value any
want any
wantErr bool
}{{
name: "nil",
Expand All @@ -59,8 +59,8 @@ func Test_binding_Value(t *testing.T) {
wantErr: false,
}, {
name: "array",
value: []interface{}{"42", 42},
want: []interface{}{"42", 42},
value: []any{"42", 42},
want: []any{"42", 42},
wantErr: false,
}}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/binding/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
)

func Resolve(name string, bindings Bindings) (interface{}, error) {
func Resolve(name string, bindings Bindings) (any, error) {
if bindings == nil {
return nil, errors.New("bindings must not be nil")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/binding/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestResolve(t *testing.T) {
tests := []struct {
name string
args args
want interface{}
want any
wantErr bool
}{{
name: "nil",
Expand Down
Loading

0 comments on commit 9269200

Please sign in to comment.