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

Add configuration option for tag value that indicates squash #354

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

triarius
Copy link

@triarius triarius commented Apr 2, 2024

When decoding, a nested struct could be squashed with the mapstructure:",squash" struct tag. A similar functionality appears when marshalling JSON into structs, but it is called inline instead of squash.

Given that a DecoderConfig field allows specifying a different name than mapstructure for the struct tag, I think users would want to be able to specify a different value to indicate the "squashing" of nested structs.

Indeed, such functionality would allow a more satisfactory resolution to spf13/viper #1050. While it remains true that users may specify a struct tag of `mapstructure:",squash", it is sometimes not possible to do this because the structs are not owned by the current package. If this is merged, users will be able to correctly parse configuration that contains foreign types that have nested structs that need to be inlined with something like:

cfg := SomeStructWithNestedStructs{}
viper.Unmarshal(&cfg, func(c *mapstructure.DecoderConfig) {
	c.TagName = "json"
	c.SquashName = "inline"
})

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

Successfully merging this pull request may close these issues.

None yet

1 participant