-
-
Notifications
You must be signed in to change notification settings - Fork 245
Description
This is my second bug report around any, so I'm guessing that any support in version 5 has some challenges. This bug isn't as serious as the first one, as this can be worked around and my guess would require substantial work. But I wanted to note it for the authors.
Given a structure to encode and decode:
[]any{
"name",
[]any{
[]any{
0, // timestamp in seconds
Record{},
},
},
struct{TimeFormat string }{TimeFormat: "DateTime"},
}(Yeah, I know....)
You cannot decode back into the same structure, it gives an error. The error is different in version 4 and 5 but are likely related to the same problem with handling the reflection.
Expected Behavior
It should decode back into the structure. There are two ways that it could do this:
encoding/jsonwayjson-experimentway (better)
The original encoding/json would decode it, but replace Record with map[string]any. That is not ideal and unexpected. But it does kinda work.
The better approach is the json-experiment way. This package is likely to be incorporated as encoding/json/v2. It decodes it into the structure.
Current Behavior
v5: panic: reflect: reflect.Value.SetString using unaddressable value
v4: panic: Decode(nonsettable string)
Possible Solution
Adopt the decoding practices of json-experiment around any types.
Steps to Reproduce
https://go.dev/play/p/5vpaqDRrD5d
Here is JSON experiment doing the same type of encode/decode:
https://go.dev/play/p/Slea4ZbvjR7
Context (Environment)
Well, you can still decode into plainany with v4. Because of the other bug I left, you can't use version 5.
But this does mean you have to then manually convert from map[string]any to the concrete type. Not the end of the world.