Skip to content

feat: add DecodeSquash functional option helper for Unmarshal#2133

Open
HarshalPatel1972 wants to merge 1 commit into
spf13:masterfrom
HarshalPatel1972:fix/unmarshal-embedded-structs
Open

feat: add DecodeSquash functional option helper for Unmarshal#2133
HarshalPatel1972 wants to merge 1 commit into
spf13:masterfrom
HarshalPatel1972:fix/unmarshal-embedded-structs

Conversation

@HarshalPatel1972

Copy link
Copy Markdown

Fixes #1624

Summary

Introduces an idiomatic, functional DecoderConfigOption helper named DecodeSquash to allow developers to programmatically opt-in to squashing embedded/anonymous structures during the Unmarshal decoding pipeline.

Changes

  • Added DecodeSquash(squash bool) DecoderConfigOption helper inside viper.go to mutate the underlying mapstructure.DecoderConfig.Squash field context.
  • Implemented a comprehensive regression test suite TestUnmarshalEmbeddedUnexportedStruct inside viper_test.go to validate successful promoted field traversal without relying on manual mapstructure:",squash" struct tags.

Motivation

By default, the underlying reflection engine (mitchellh/mapstructure) completely ignores fields inside nested unexported embedded structures unless explicit global squashing is toggled. Setting global squashing inside the core configuration would be a breaking change for developers relying on deeply nested, non-flattened keys.

Providing DecodeSquash gives developers a clean, surgical, opt-in mechanism right inside individual Unmarshal parameters without altering global configuration semantics.

Tested on

  • Verified via full repository testing matrix (go test ./... -v passes cleanly with zero regressions).

Copilot AI review requested due to automatic review settings June 5, 2026 17:01
@CLAassistant

CLAassistant commented Jun 5, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds an explicit decoder option to control mapstructure “squash” behavior during Unmarshal, and validates it with a regression test for embedded unexported structs.

Changes:

  • Introduces DecodeSquash(bool) as a DecoderConfigOption to toggle mapstructure.DecoderConfig.Squash.
  • Adds a unit test covering unmarshalling into a struct that embeds an unexported struct with squash enabled.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
viper.go Adds DecodeSquash option to enable/disable squashing embedded fields during decode.
viper_test.go Adds a regression test ensuring squashed decoding works with an embedded unexported struct.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

3 participants