darccio/mergo

Override merging of pointers

bitcoin-coder-bob opened this issue · 2 comments

The behavior of merging with override enabled on pointers does not seem to be respected the same way as other types.

I have highlighted the behavior in this go playground: https://go.dev/play/p/AaVowu8Mvef

the *string does not seem to override.

Do I have to implement a transformer to accomplish this or is this a bug?

I have the same problem, hope it will be fixed soon
thx

@bitcoin-coder-bob @ybbiubiubiu This is fixable using the option mergo.WithoutDereference:

package main

import (
	"fmt"

	"dario.cat/mergo"
)

type Foo struct {
	A *string
	B int64
}

func main() {
	first := "first"
	second := "second"
	src := Foo{
		A: &first,
		B: 2,
	}

	dest := Foo{
		A: &second,
		B: 1,
	}

	mergo.Merge(&dest, src, mergo.WithOverride, mergo.WithoutDereference)
	fmt.Printf("expected dest.A to be: %v  got: %v\n", *src.A, *dest.A)
	fmt.Printf("expected dest.A to be: %v  got: %v\n", src.A, dest.A)
}