-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embedded structs cause decoding issues. #25
Comments
I wouldn't get to this one soon. Just so you are not stuck, you can change the const recoverPanicToErr to true (line 46 of helper.go). This will propagate the panic and crash the program with a stack trace when an error happens, and can get some insight into where the error is originating from. |
Can you work on getting this issue to a self-contained one, so I can fix. Something short and straight to the point, like all your other issues. Thanks. |
Yes I can. I actually have no idea what's going wrong with it, so that's why I couldn't condense it further. I'll dig in more and try to get a smaller reproduce. |
Okay, here's a new reproduce. The error should be easier to see since I've made the number of structs smaller, and the msgpack much simpler. This snippet fails to decode if the Astruct inside Bstruct is embedded, if it is named (not embedded) then I receive no error and it decodes correctly. The error is as follows:
Now, the error message is different but I believe it's a symptom of the same underlying problem. If we can get this one fixed, I'll verify that it fixes the original snippet as well. package main
import (
"github.com/ugorji/go/codec"
"bytes"
"log"
"github.com/davecgh/go-spew/spew"
)
type Astruct struct {
Abyte1 []byte
}
type Bstruct struct {
*Astruct
Bint1 int32
}
var test1 = []byte{
0x92, //Bstruct{
0x91, //Astruct{
0xc4, 0x5, 0x1, 0x2, 0x3, 0x4, 0x5, // []byte{1, 2, 3, 4, 5}
0x6, // Bint1 = 6
}
func main() {
var m codec.MsgpackHandle
m.StructToArray = true
m.WriteExt = true
b := Bstruct{}
decoder := codec.NewDecoder(bytes.NewBuffer(test1), &m)
if err := decoder.Decode(&b); err != nil {
log.Println("Failed to decode to struct:", err)
} else {
spew.Dump(b)
}
var i interface{}
decoder = codec.NewDecoder(bytes.NewBuffer(test1), &m)
if err := decoder.Decode(&i); err != nil {
log.Println("Failed to decode to interface:", err)
} else {
spew.Dump(i)
}
} Output when Bstruct's Astruct is not named:
Output when Bstruct's Astruct is named:
|
K, this is a user error. https://github.com/ugorji/go/blob/master/codec/encode.go#L663
So the encoding will be like this was encoded: However, you are passing something that looks like It wouldn't work. Your struct is not aligned with the encoded data (the encoded data says that an array is inside an array). If you want it to work, you have 2 options:
With that, things work, and error goes away. To see how this works, you should set the const recoverPanicToErr to true (line 46 of helper.go). This way, when an error occurs, we don't recover the panic and you see where in the code it occurred, and you can follow it. It may help. |
This encoding was actually done by the C library wrapped in Python not by me. The original big scary example has data directly from that encoder. I simply reduced the problem to be this small bit. I haven't used reflect enough to know if there's a way we can find out if the struct was embedded to detect and handle this case. However, if you're satisfied with this resolution then I am. Being able to embed the struct and encode/decode properly with a struct field literal tag is an acceptable workaround. Thanks. |
its actually not a work around per se. and the problem wasn't with the (on go ... typing from phone ... enjoy)
|
Either way, let's consider this one buried. I think with all these issues out of the way, I can start coding haha. Thanks again! |
I'm sorry I couldn't get a smaller test case for this, I'm short on time this morning. This is trying to decode a payload from Python (hence the ugly string constant).
If you name the Bstruct's *Astruct field instead of embedding it, it decodes as expected. If you don't you get the error message:
Output:
The text was updated successfully, but these errors were encountered: