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

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant