Giter Club home page Giter Club logo

jsonpath's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jsonpath's Issues

Panic on filter query

A panic happens when using a literal compare in a boolean logical operation

To Reproduce
version: df1774f
Att the following test cases to TestRetrieve_filterExist:

                     {
				jsonpath:     `$[?(@.a>1 && (1==2))]`,
				inputJSON:    `[{"a":1},{"a":2},{"a":3}]`,
				expectedJSON: `[]`,
			},  
			  {
				jsonpath:     `$[?(@.a>1 && (1==1))]`,
				inputJSON:    `[{"a":1},{"a":2},{"a":3}]`,
				expectedJSON: `[{"a":2},{"a":3}]`,
			},  

then run
go test . -run TestRetrieve_filterExist

--- FAIL: TestRetrieve_filterExist (0.00s)
--- FAIL: TestRetrieve_filterExist/child_<$[?(@.a>1_&&(1==2))]><[{"a":1},{"a":2},{"a":3}]> (0.00s)
panic: runtime error: index out of range [1] with length 1 [recovered]
panic: runtime error: index out of range [1] with length 1

goroutine 567 [running]:
testing.tRunner.func1.2({0x11919a0, 0xc000182630})
/usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
/usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1392 +0x39f
panic({0x11919a0, 0xc000182630})
/usr/local/Cellar/go/1.18/libexec/src/runtime/panic.go:838 +0x207
github.com/AsaiYusuke/jsonpath.(*syntaxLogicalAnd).compute(0xc0001ef8c0, {0x1171e40, 0xc00060eee8}, {0xc0001c75c0, 0x3, 0x4}, 0x12f66e0?)
/go/src/jsonpath/syntax_query_logical_and.go:14 +0x130
github.com/AsaiYusuke/jsonpath.(*syntaxFilterQualifier).retrieveList(0xc00060efd8, {0x1171e40, 0xc00060eee8}, {0xc0001c75c0, 0x3, 0x4}, 0xc00060f008)
/go/src/jsonpath/syntax_node_qualifier_filter.go:96 +0x62
github.com/AsaiYusuke/jsonpath.(*syntaxFilterQualifier).retrieve(0x97?, {0x1171e40?, 0xc00060eee8?}, {0x1171e40?, 0xc00060eee8?}, 0x0?)
/go/src/jsonpath/syntax_node_qualifier_filter.go:19 +0x9e
github.com/AsaiYusuke/jsonpath.Parse.func2({0x1171e40, 0xc00060eee8})
/go/src/jsonpath/jsonpath.go:57 +0x77
github.com/AsaiYusuke/jsonpath.Retrieve({0x11a7a90?, 0x1171e40?}, {0x1171e40, 0xc00060eee8}, {0x0?, 0x1178a20?, 0xc0006036d0?})
/go/src/jsonpath/jsonpath.go:18 +0x64
github.com/AsaiYusuke/jsonpath.execTestRetrieve(0xc0005ba820, {0x1171e40, 0xc00060eee8}, {{0x11a7a90, 0x15}, {0x11a95d6, 0x19}, {0x119fd44, 0x2}, {0x0, ...}, ...})
/go/src/jsonpath/test_jsonpath_test.go:85 +0x2db
github.com/AsaiYusuke/jsonpath.execTestRetrieveTestGroups.func1(0xc0005ba820)
/go/src/jsonpath/test_jsonpath_test.go:129 +0x1a5
testing.tRunner(0xc0005ba820, 0xc0005c2000)
/usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
/usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1486 +0x35f
FAIL github.com/AsaiYusuke/jsonpath 0.376s

Expected behavior
Test should not panic and pass

Question: FilterFunction with extra arguments

I am trying to filter based on an array containing a given string.

Working Test

package targetsubscriptions

import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"

	"github.com/AsaiYusuke/jsonpath"
)

type events struct {
}

func TestContains(t *testing.T) {
	srcJSON := `{
		"metadata": {
			"mfg": "seimens",
			"type": "sensor",
			"sub_type": "temperature",
			"model": "K-32-abcd",
			"tags": ["a", "b", "c"]
		},
		"data": {
			"a":"b"
		}
	}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	var events []interface{}
	events = append(events, src)

	config := jsonpath.Config{}
	config.SetFilterFunction(`contains`, func(param interface{}) (interface{}, error) {
		if stringParam, ok := param.(string); ok {
			if stringParam == "a" {
				return stringParam, nil
			}
			return nil, fmt.Errorf(`not found`)
		}
		return nil, fmt.Errorf(`type error`)
	})

	jsonPath := `$[?(@.metadata.tags[*].contains())]`

	output, err := jsonpath.Retrieve(jsonPath, events, config)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}

I have my contains func hard coded to look for "a" for the test.

is there anyway that I can write my jsonpath as follows;

jsonPath := `$[?(@.metadata.tags[*].contains(a))]`

or better yet as described as described here => jsonpath-examples-expression-cheetsheet ;

jsonPath := `$[?(@.metadata.tags[*]=='a')]`

where the implementation might look like this;

func contains(param interface{}, extra... interface{}) (interface{}, error) {
 
}

Does it not support the following methods

Describe the bug
Does it not support the following methods,"$..book[(@.length-1)]"

To Reproduce
Steps to reproduce the behavior:
jsonData:

{
	"store": {
		"book": [{
				"category": "reference",
				"author": "Nigel Rees",
				"title": "Sayings of the Century",
				"price": 8.95
			}, {
				"category": "fiction",
				"author": "Evelyn Waugh",
				"title": "Sword of Honour",
				"price": 12.99
			}, {
				"category": "fiction",
				"author": "Herman Melville",
				"title": "Moby Dick",
				"isbn": "0-553-21311-3",
				"price": 8.99
			}, {
				"category": "fiction",
				"author": "J. R. R. Tolkien",
				"title": "The Lord of the Rings",
				"isbn": "0-395-19395-8",
				"price": 22.99
			}
		],
		"bicycle": {
			"color": "red",
			"price": 19.95
		}
	}
}
i4, _ := jsonpath.Retrieve("$..book[(@.length-1)]", src)

2、result:[]

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Array filter fails if zero results pass the predicate

Describe the bug
I want to assert that a particular jsonpath evaluates to an empty array. This instead returns jsonpath.ErrorMemberNotExist.

To Reproduce

package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$[?(@.id == 2)].name`, `[ { "id": 1, "name": "One" } ]`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}

This returns: jsonpath.ErrorMemberNotExist, member did not exist (path=[?(@.id == 2)])

Expected behavior
I expected it to return an empty array/slice (e.g. any[]{}).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.