PaesslerAG/gval

unable to get value

Gigaclank opened this issue · 4 comments

i have run the following with jsonPath

package main

func main() {
	v := interface{}(nil)
	json.Unmarshal([]byte(`{
		"foo": {
			"bar": {
				"val": 3
			}
		}
	}`), &v)

	value, err := jsonpath.Get("$.*.*.val", v)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	for _, value := range value.([]interface{}) {
		fmt.Println(value)
	}
}

and this is my output
$.*.*.valmap[foo:map[bar:map[val:3]]]3

however if i run

v := interface{}(nil)
	json.Unmarshal([]byte(`{
		"foo": {
			"bar": {
				"val": 3
			}
		}
	}`), &v)
value, err := gval.Evaluate("$.*.*.val", v, jsonpath.Language())

it returns

[]
am i using this wrong?

i would expect it to return

3
and if i run

v := interface{}(nil)
	json.Unmarshal([]byte(`{
		"foo": {
			"bar": {
				"val": 3
			}
		}
	}`), &v)
value, err := gval.Evaluate("$.*.*.val == 3", v, jsonpath.Language())

i would expect

true 

Hi @Gigaclank ,

I've copied your code and your first 2 examples returned the expected 3. The last example does not work, because $.*.*.val is an array that contains ´3´

Which version do you use?

Hi @Gigaclank ,

I've copied your code and your first 2 examples returned the expected 3. The last example does not work, because $.*.*.val is an array that contains ´3´

Which version do you use?

I am using the latest version of gval and jsonpath with golang 1.12.6 in a docker container.

i have found a work around which is to in my evaluation rule change it from:
$.*.*.val == 3
to
$.*.*.val == [3]
its a workaround but is not particularly user friendly whenever i am allowing my users to create the rule from a UI.

You could use something like

	value, err := gval.Evaluate("$.*.*.val == 3", v, jsonpath.Language(), gval.InfixOperator("==", func(a, b interface{}) (interface{}, error) {
		aa, ok := a.([]interface{})
		if !ok {
			return reflect.DeepEqual(a, b), nil
		}
		for _, x := range aa {
			if reflect.DeepEqual(x, b) {
				return true, nil
			}
		}
		return false, nil
	}))

in order to scale up the equals operator to multiple values.