Skip to content

Conversation

vsemichev
Copy link
Contributor

Hi, this PR will hopefully fix the issue #187 with map destinations for Map method. It's one simple change.

@mallardduck
Copy link

Based on #246 - I'm not sure this will ever be accepted.

@darccio
Copy link
Owner

darccio commented Aug 1, 2024

@mallardduck This PR is missing tests. I asked the original contributor to add them but I got no answer.

@vsemichev
Copy link
Contributor Author

Hi @darccio, sorry for the delay. I've added a couple of test to verify the fix. Also, a slight statement change to take into account the case where you want to override map values even when the source structure fields are empty.

@darccio
Copy link
Owner

darccio commented Aug 17, 2024

@vsemichev Looks it works and all tests are green. Let's release another v1 version.

@darccio darccio merged commit 96f24af into darccio:master Aug 17, 2024
3 checks passed
@@ -58,7 +58,7 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, conf
}
fieldName := field.Name
fieldName = changeInitialCase(fieldName, unicode.ToLower)
if v, ok := dstMap[fieldName]; !ok || (isEmptyValue(reflect.ValueOf(v), !config.ShouldNotDereference) || overwrite) {
if _, ok := dstMap[fieldName]; !ok || (!isEmptyValue(reflect.ValueOf(src.Field(i).Interface()), !config.ShouldNotDereference) && overwrite) || config.overwriteWithEmptyValue {

Choose a reason for hiding this comment

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

Unfortunately, this change would break our use case, consider the following scenario

	dst := map[string]interface{}{
		"foo":   nil,
		"bar":   1,
	}
	src := struct {
		Foo   *string
		Bar   int
	}{
		Foo: &"hello",
		Bar: 42,
	}
	if err := mergo.Map(&dst, src); err != nil {
		t.Error(err)
	}

we expect *dst["foo"] == "hello" and dst["bar"] == 1

Copy link
Owner

Choose a reason for hiding this comment

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

@frankdvd Your use case seems to be an unintended consequence. Mergo shouldn't flatten structures while mapping.

project-mirrors-bot-tu bot pushed a commit to project-mirrors/forgejo-runner-as-gitea-act-runner-fork that referenced this pull request Aug 11, 2025
This PR contains the following updates:

| Package | Change | Age | Confidence |
|---|---|---|---|
| [github.com/imdario/mergo](https://github.com/imdario/mergo) | `v0.3.16` -> `v1.0.2` | [![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fimdario%2fmergo/v1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fimdario%2fmergo/v0.3.16/v1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>imdario/mergo (github.com/imdario/mergo)</summary>

### [`v1.0.2`](https://github.com/darccio/mergo/releases/tag/v1.0.2)

[Compare Source](darccio/mergo@v1.0.1...v1.0.2)

#### What's Changed

- Drops `gopkg.in/yaml.v3`, only used for loading fixtures. Thanks [@&#8203;trim21](https://github.com/trim21) for bringing to my attention ([#&#8203;262](darccio/mergo#262)) that this library is no longer maintained.

**Full Changelog**: darccio/mergo@v1.0.1...v1.0.2

### [`v1.0.1`](https://github.com/darccio/mergo/releases/tag/v1.0.1)

[Compare Source](darccio/mergo@v1.0.0...v1.0.1)

#### What's Changed

- fixes issue [#&#8203;187](darccio/mergo#187) by [@&#8203;vsemichev](https://github.com/vsemichev) in darccio/mergo#253
- fix: `WithoutDereference` should respect non-nil struct pointers by [@&#8203;joshkaplinsky](https://github.com/joshkaplinsky) in darccio/mergo#251

#### New Contributors

- [@&#8203;vsemichev](https://github.com/vsemichev) made their first contribution in darccio/mergo#253
- [@&#8203;joshkaplinsky](https://github.com/joshkaplinsky) made their first contribution in darccio/mergo#251

**Full Changelog**: darccio/mergo@v1.0.0...v1.0.1

### [`v1.0.0`](https://github.com/darccio/mergo/releases/tag/v1.0.0): 1.0.0 released with new module URL: dario.cat/mergo

[Compare Source](darccio/mergo@v0.3.16...v1.0.0)

This PR is a release containing [0.3.15](https://github.com/imdario/mergo/releases/tag/v0.3.15) features but changing the module URL to `dario.cat/mergo`.

#### What's Changed

- feat: v1.0.0 released by [@&#8203;imdario](https://github.com/imdario) in darccio/mergo#238

**Full Changelog**: darccio/mergo@v0.3.16...v1.0.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC).

🚦 **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 has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS41MS4xIiwidXBkYXRlZEluVmVyIjoiNDEuNTEuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- other
  - [PR](https://code.forgejo.org/forgejo/runner/pulls/840): <!--number 840 --><!--line 0 --><!--description VXBkYXRlIG1vZHVsZSBnaXRodWIuY29tL2ltZGFyaW8vbWVyZ28gdG8gdjE=-->Update module github.com/imdario/mergo to v1<!--description-->
<!--end release-notes-assistant-->

Co-authored-by: Earl Warren <contact@earl-warren.org>
Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/840
Co-authored-by: Renovate Bot <bot@kriese.eu>
Co-committed-by: Renovate Bot <bot@kriese.eu>
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.

4 participants