-
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
Decode array/list types to array instead of slice (optionally?) #170
Comments
I worked out how to do this for the Msgpack case, at least, in a branch for my own use -- here's a PoC PR, which maybe could be extended to handle all cases correctly and make the array decoding optional: #171. |
It's not clear to me how to solve this. To read a value from an interface, you must know the exact underlying type. In theory, a map[interface{}]X cannot have some keys being arrays unless the user knows the full list of array types which may exist here. I don't think we can solve this issue here. The better solution, is to have a map[[7]int]string, etc where you say this is a map type, and the keys are arrays of 7 values each. For this, go-codec has a great solution. In addition, your PR just assumes that valueTypeArray is for map keys. however, valueTypeArray from kInterfaceNaked is for any value, which may exist anywhere. So, in summary, the way to solve this is to use an explicit array, or expect that the value will be one of the known types (primitive or slice or map of basic values). |
No response from OP, so closing. |
Hi, sorry for the delayed response; I'm on vacation this week. I agree it's a tricky problem, and the best course of action may be to not support arbitrary keys. But I do think the current implementation means that certain valid msgpack messages can't be decoded. It's true that you need to know what the keys might be, but that's also true for Maybe there's a solution using a "boxed" As for |
Good points. Thought about it more, and fixed some days back. I don't have all my notes, but just realized that I didn't update yet. |
Msgpack, for example, allows arbitrary values as map keys, but slices can't be map keys since they're not hashable. Arrays can be, and for formats like msgpack where the length of the array is specified at the beginning of the encoded byte stream, using a fixed-length array seems sensible.
I only use this library for Msgpack encoding/decoding, so I'm not familiar enough with the other use cases to know which this would be inappropriate for. Also, it seems like the only way to construct arbitrary type and size arrays at run time is to use
reflect
heavily, which presumably has a performance cost. So I would think this feature would have to be optional.The text was updated successfully, but these errors were encountered: