Skip to content
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

reflect.Value.SetString using unaddressable value on concrete values contained within any structures #373

Open
element-of-surprise opened this issue Apr 21, 2024 · 0 comments

Comments

@element-of-surprise
Copy link

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/json way
  • json-experiment way (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.

@element-of-surprise element-of-surprise changed the title reflect.Value.SetString using unaddressable value on concrete values contained within any structures reflect.Value.SetString using unaddressable value on concrete values contained within any structures Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant