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 to specify tag value that indicates squash #18

Merged
merged 1 commit into from
Aug 11, 2024

Conversation

triarius
Copy link

@triarius triarius commented Apr 3, 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, viper 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.SquashTagOption = "inline"
})

Note: I originally made this PR on upstream, but I soon realised that this is the blessed successor. It's somewhat of a co-incidence that the main use case is in viper itself!

Copy link
Member

@sagikazarmark sagikazarmark left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @triarius!

I'm a little bit reluctant to accept this change, because it makes it more easy to misconfigure the library.

I'm also a bit worried that JSON's ,inline and mapstructure's ,squash may not always work 100% the same way in which case we might just sent people on a wild goose chase with this change.

However, you make a strong case about being able to change the tag name, but not the option and how that may be a limitation, so I think we are better off with this change after all.

Would you mind taking a look at my comment?

Thanks!

mapstructure.go Outdated
@@ -265,6 +265,10 @@ type DecoderConfig struct {
// defaults to "mapstructure"
TagName string

// The name of the value in the tag that indicates a field should
// be squashed. This defaults to "squash".
SquashName string
Copy link
Member

Choose a reason for hiding this comment

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

AFAIK these are called options or tag options, so something like the following would be a better name IMO:

  • CustomSquashTagOption
  • SquashTagOptionValue
  • SquashTagOptionName
  • SquashTagOption

SquashTagOption may be the better one as it's short.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good, I've updated to use SquashTagOption

@sagikazarmark sagikazarmark merged commit 2842dc8 into go-viper:main Aug 11, 2024
8 checks passed
@sagikazarmark sagikazarmark added this to the v2.1.0 milestone Aug 11, 2024
@triarius triarius deleted the viper-squash-name branch August 11, 2024 22:15
codeboten added a commit to open-telemetry/opentelemetry-collector that referenced this pull request Aug 13, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[github.com/go-viper/mapstructure/v2](https://togithub.com/go-viper/mapstructure)
| `v2.0.0` -> `v2.1.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fgo-viper%2fmapstructure%2fv2/v2.1.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fgo-viper%2fmapstructure%2fv2/v2.1.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fgo-viper%2fmapstructure%2fv2/v2.0.0/v2.1.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fgo-viper%2fmapstructure%2fv2/v2.0.0/v2.1.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>go-viper/mapstructure
(github.com/go-viper/mapstructure/v2)</summary>

###
[`v2.1.0`](https://togithub.com/go-viper/mapstructure/releases/tag/v2.1.0)

[Compare
Source](https://togithub.com/go-viper/mapstructure/compare/v2.0.0...v2.1.0)

#### What's Changed

- build(deps): bump actions/setup-go from 5.0.0 to 5.0.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[go-viper/mapstructure#28
- build(deps): bump actions/checkout from 4.1.1 to 4.1.6 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[go-viper/mapstructure#27
- build(deps): bump golangci/golangci-lint-action from 3.7.0 to 6.0.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[go-viper/mapstructure#26
- build(deps): bump actions/setup-go from 5.0.1 to 5.0.2 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[go-viper/mapstructure#34
- build(deps): bump actions/checkout from 4.1.6 to 4.1.7 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[go-viper/mapstructure#29
- Port Succo's fix to cache decodeHooks by
[@&#8203;cedric-cordenier](https://togithub.com/cedric-cordenier) in
[go-viper/mapstructure#33
- build(deps): bump golangci/golangci-lint-action from 6.0.1 to 6.1.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[go-viper/mapstructure#38
- Add configuration option to specify tag value that indicates squash by
[@&#8203;triarius](https://togithub.com/triarius) in
[go-viper/mapstructure#18
- Adding support for squash: interface. by
[@&#8203;m1k1o](https://togithub.com/m1k1o) in
[go-viper/mapstructure#17

#### New Contributors

- [@&#8203;dependabot](https://togithub.com/dependabot) made their first
contribution in
[go-viper/mapstructure#28
- [@&#8203;cedric-cordenier](https://togithub.com/cedric-cordenier) made
their first contribution in
[go-viper/mapstructure#33
- [@&#8203;triarius](https://togithub.com/triarius) made their first
contribution in
[go-viper/mapstructure#18
- [@&#8203;m1k1o](https://togithub.com/m1k1o) made their first
contribution in
[go-viper/mapstructure#17

**Full Changelog**:
go-viper/mapstructure@v2.0.0...v2.1.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (UTC), Automerge - At any
time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/open-telemetry/opentelemetry-collector).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yMC4xIiwidXBkYXRlZEluVmVyIjoiMzguMjAuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIiwicmVub3ZhdGVib3QiXX0=-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
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

2 participants