ohler55 / ojg

Optimized JSON for Go
MIT License
857 stars 49 forks source link

recursive descent on object, unexpected results #140

Closed sschulz-t closed 11 months ago

sschulz-t commented 1 year ago

I am facing an issue with matching a json path with a recursive descent (e.g., $..city) against a go object. See the following minimal example code (or use this go playground link: https://go.dev/play/p/mtIWc1-iNz3 )

package main
import (
    "fmt"
    "github.com/ohler55/ojg/jp"
)

func main() {
    type obj struct {
        Some string
        Data map[string]interface{}
    }

    o := obj{
        Some: "a_string",
        Data: map[string]interface{}{
            "test": map[string]interface{}{
                "city": "bielefeld",
            },
        },
    }

    path := "$..city"

    x, err := jp.ParseString(path)
    if err != nil {
        fmt.Println(err)
    }

    // this does not work
    fmt.Printf("(%s) GOT %v\n", path, x.Get(o))

    // however, this does
    fmt.Printf("(%s) GOT %v\n", path, x.Get(o.Data))
}

I expected x.Get(o) to resolve to the city as well. However, it does not.

Is this a bug or is it expected that it does not recurse into the struct's keys (e.g., o.Data) on its own?

ohler55 commented 1 year ago

Looks like a bug. I suspect it has something to do with the transition from reflection on the struct to the map data. I'll take a look and get it fixed.

ohler55 commented 1 year ago

It looks like the descent .. expression does not traverse struct fields. That will take a bit longer to implement but I will put it in the queue to be done.

sschulz-t commented 1 year ago

Thanks for the quick response! As a temporary workaround, I marshal and unmarshal the object and feed jsonpath with that :)

Will the fixed descent use the struct key name or the json decorator (e.g., type obj struct { Some string json:"city" }) as well?

ohler55 commented 1 year ago

If there is a decorator it will be used otherwise the field name as lower case or what ever the options specify.

ohler55 commented 1 year ago

The struct-path has a fix. Still needs some testing but should work.

ohler55 commented 1 year ago

v1.20.0 released with the fix.

ohler55 commented 1 year ago

Can this be closed?