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

Embedded struct of pointer types will overwrite the whole destination struct #114

Closed
randallmlough opened this issue Apr 23, 2019 · 2 comments · Fixed by moby/moby#40045 or docker/cli#2277

Comments

@randallmlough
Copy link

If you create a struct that contains multiple pointer types and then attempt to merge two of them together where only one of those types has a value, then the whole destination struct will be overwritten.

Simple example

type Config struct {
	Foo string `json:"foo"`
	Bar string `json:"bar"`
	Params *Params `json:"params"`

}
type Params struct{
	Name string `json:"name"`
	Multi *MultiPtr `json:"multi"`
	Final *Final `json:"Final"`
}
type MultiPtr struct {
	Text string `json:"text"`
	Number int `json:"number"`
}
type Final struct {
	Msg1 string `json:"msg"`
	Msg2 string `json:"msg_2"`
}
func main() {
	a := &Config{
		Foo: "something",
		Params: &Params{
			Name: "should remain untouched", // will be removed/destroyed if `mergo.WithOverride` is enabled
			Multi: &MultiPtr{ // will be removed/destroyed if `mergo.WithOverride` is enabled
				Text: "should stay since b MultiPtr is nil",
				Number: 5,
			},
			Final: &Final{
				Msg1: "will only be overwritten when `mergo.WithOverride` is enabled",
				Msg2: "", // will always overwritten
			},
		},
	}
	b := &Config{
		Bar: "from b",
		Params: &Params{
			Final: &Final{
				Msg1: "I overwrote something",
				Msg2: "thanks for looking into this",
			},
		},
	}
	// with mergo.WithOverride disabled
	//if err := mergo.Merge(a, b); err != nil {
	//	log.Println(err)
	//}
	// outputs: {"foo":"something","bar":"from b","params":{"name":"should remain untouched","multi":{"text":"should stay since b MultiPtr is nil"},"Final":{"msg":"will only be overwritten when `mergo.WithOverride` is enabled","msg_2":"thanks for looking into this"}}}

	// with mergo.WithOverride enabled
	if err := mergo.Merge(a, b, mergo.WithOverride); err != nil {
		log.Println(err)
	}
        // Outputs: {"foo":"something","bar":"from b","params":{"name":null,"multi":null,"Final":{"msg":"I overwrote something","msg_2":"thanks for looking into this"}}}

	j, _ := json.Marshal(a)
	fmt.Println(string(j))
}

I saw issue #47 and the PR that followed it, but that doesn't seem to solve this issue

@thaJeztah
Copy link

@vdemeester looks like this can be closed (fixed by #120)

thaJeztah added a commit to thaJeztah/docker that referenced this issue Oct 6, 2019
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@vdemeester
Copy link
Collaborator

Indeed, thanks 👍

docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this issue Oct 7, 2019
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Upstream-commit: 9bd1b1a8eca97a2403bc7a2ed9a52427d8c27078
Component: engine
burnMyDread pushed a commit to burnMyDread/moby that referenced this issue Oct 21, 2019
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: zach <[email protected]>
thaJeztah added a commit to thaJeztah/cli that referenced this issue Oct 23, 2019
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
thaJeztah added a commit to thaJeztah/cli that referenced this issue Oct 23, 2019
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
zappy-shu pushed a commit to zappy-shu/cli that referenced this issue Jan 21, 2020
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this issue Jan 24, 2020
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Upstream-commit: 6cf7970cd397a77155aec077bd27755bc033b6f4
Component: cli
eiffel-fl pushed a commit to eiffel-fl/cli that referenced this issue Jul 28, 2020
full diff: darccio/mergo@v0.3.7...v0.3.8

includes:

- darccio/mergo#112 Add strict override
    - fixes darccio/mergo#111 WithOverride should be able to check types
- darccio/mergo#106 Fix merging of interface types with concrete values
- darccio/mergo#120 should not overwrite pointers directly, instead check embedded values
    - fixes darccio/mergo#114 Embedded struct of pointer types will overwrite the whole destination struct
- darccio/mergo#125 added WithOverrideEmptySlice config flag

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants