Unexpected Behavior When Merging Structs Without Override #260
-
|
When using mergo.Merge with override set to false, non-empty fields in the destination struct are being overwritten by values from the source struct. This behavior contradicts the expected functionality where only empty fields in the destination should be updated. Expected Behavior: Actual Behavior: This behavior is unexpected, as mergo.Merge should only populate empty fields in dest when WithOverride is not used. Could you clarify if this is intended behavior or provide a solution? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Sorry, I just saw this. I'll check it this week, @sourabh-khot65. |
Beta Was this translation helpful? Give feedback.
-
|
@sourabh-khot65 I'm afraid your code doesn't reproduce the issue: https://go.dev/play/p/U5rnLXo5YlI package main
import (
"fmt"
"dario.cat/mergo"
)
type Candidate struct {
FirstName *string
LastName *string
Skills *string
}
func ptr(s string) *string {
return &s
}
func main() {
dest := &Candidate{
FirstName: ptr("John"),
LastName: ptr("Doe"),
Skills: nil, // Empty destination field
}
src := &Candidate{
LastName: ptr("Smith"),
Skills: ptr("Go"), // Source has a value
}
// Merge without override
err := mergo.Merge(dest, src)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("FirstName: %q; LastName: %q; Skills: %q\n", *dest.FirstName, *dest.LastName, *dest.Skills)
}I only modified the |
Beta Was this translation helpful? Give feedback.
-
|
@darccio Thank you for reaching out. It seems there was an implementation issue on my end, which was later resolved. |
Beta Was this translation helpful? Give feedback.
@sourabh-khot65 I'm afraid your code doesn't reproduce the issue: https://go.dev/play/p/U5rnLXo5YlI