ohler55/ojg

panic when access map data which key is user defined type

Closed this issue · 5 comments

type String string
type X struct {
    Code String `json:"code,omitempty"`
}
x := map[String]*X{
    "abc": {
        Code: "123",
    },
}
var err error
var expr jp.Expr
if expr, err = jp.ParseString("$.abc.code"); err != nil {
    panic(err)
}
var values []interface{}
values = expr.Get(x) // panic here
fmt.Println("values:", values)

output:

    2 panic: reflect.Value.MapIndex: value of type string is not assignable to type main.String
    3
    4 goroutine 1 [running]:
    5 reflect.Value.assignTo({0x4efca0?, 0xc0000140b0?, 0x5e9de0?}, {0x50dcd7, 0x16}, 0x4efba0, 0x0)
    6         /home/linuxbrew/.linuxbrew/Cellar/go@1.22/1.22.6/libexec/src/reflect/value.go:3356 +0x299
    7 reflect.Value.MapIndex({0x4f4600?, 0xc0000b8210?, 0x7fd2ede54108?}, {0x4efca0, 0xc0000140b0, 0x98})
    8         /home/linuxbrew/.linuxbrew/Cellar/go@1.22/1.22.6/libexec/src/reflect/value.go:1821 +0xe8
    9 github.com/ohler55/ojg/jp.Expr.reflectGetChild({0x0?, 0x4f4600?, 0x0?}, {0x4f4600?, 0xc0000b8210?}, {0xc0000120d8, 0x3})
   10         /home/gonghuan/go/pkg/mod/github.com/ohler55/ojg@v1.24.0/jp/get.go:1657 +0x285
   11 github.com/ohler55/ojg/jp.Expr.Get({0xc0000ba040, 0x3, 0x4}, {0x4f4600, 0xc0000b8210})
   12         /home/gonghuan/go/pkg/mod/github.com/ohler55/ojg@v1.24.0/jp/get.go:73 +0x5ea8
   13 main.main()
   14         /home/gonghuan/bytedance/playground/main.go:25 +0xb6
   15 exit status 2

I'll look into it.

Took longer than expected to get to this. My apologies. Please try the "non-string-map-keys" branch.

thanks, again; seems fixed;
so, it will merge into main in next release tag ?

Released. Thanks for finding the issue.