Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Encoding does not marshal the fields as expected #342

Open
Laubi opened this issue Oct 30, 2023 · 1 comment
Open

Encoding does not marshal the fields as expected #342

Laubi opened this issue Oct 30, 2023 · 1 comment

Comments

@Laubi
Copy link

Laubi commented Oct 30, 2023

According to this comment, encoding should be the arguments reversed. However, the mapstructure annotations are lost and it's not correctly encoded.

Example: https://go.dev/play/p/m1pPpDYYMED

The expected output should have been

{myName:something}

instead of

{Name:something}
@Laubi Laubi changed the title Encoding is not working as expected Encoding does not marshal the fields as expected Oct 30, 2023
@dplepage-dd
Copy link

var output any
mapstructure.Decode(foo, &output)

will interpret output as a struct and so when you're done output will be a Foo.

You want:

var output map[string]any
mapstructure.Decode(foo, &output)

Arguably, it would be better if there were two separate functions, Decode[T any](map[string]any, *T) and Encode[T any](*T, map[string]any) so that getting this wrong would be a compile-time error, but that wouldn't be backwards-compatible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants