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

Mapstructure tags are ignored on slice struct items #19

Open
Thiht opened this issue Apr 20, 2024 · 3 comments
Open

Mapstructure tags are ignored on slice struct items #19

Thiht opened this issue Apr 20, 2024 · 3 comments

Comments

@Thiht
Copy link

Thiht commented Apr 20, 2024

I'm trying to convert a struct to a map, and can't figure out how to make it work.

Basically I have these structs:

type SQLStepResult struct {
	Queries []QueryResult `mapstructure:"queries"`
}

type QueryResult struct {
	Rows []map[string]any `mapstructure:"rows"`
}

and I want to convert SQLStepResult to:

{
  "queries": [
    {
      "rows": [
        // ...
      ]
    }
  ]
}

But I always end up with Rows instead of rows.

I tried using the undocumented mapstructure.RecursiveStructToMapHookFunc but it doesn't appear to do anything

Is there a solution?

Go playground for reference: https://go.dev/play/p/ZlTOw1mqnH5

@Thiht Thiht changed the title Mapstructure tags are ignored on nested Mapstructure tags are ignored on nested structs Apr 20, 2024
@Thiht
Copy link
Author

Thiht commented Apr 20, 2024

I think something needs to be done to handle slices/arrays here: https://github.com/go-viper/mapstructure/blob/main/mapstructure.go#L1008

I'll try to look into it but will probably need some help, the code is hard

@Thiht Thiht changed the title Mapstructure tags are ignored on nested structs Mapstructure tags are ignored on slice struct items Apr 20, 2024
@sagikazarmark
Copy link
Member

Hm, that looks like a bug. I don't have a solution from the top of my head. I'll try to look into it when I find the time.

@wazir-ahmed
Copy link

This is related to mitchellh#249. PR mitchellh#314 has a solution. It would be great if the solution in the PR is ported to this repo and merged.

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

3 participants