package helper

import (
	"reflect"

	"github.com/imdario/mergo"
)

type ptrTransformer struct{}

func (t ptrTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
	if typ.Kind() == reflect.Ptr {
		return func(dst, src reflect.Value) error {
			if dst.CanSet() {
				if dst.IsNil() {
					dst.Set(src)
				}
			}
			return nil
		}
	}
	return nil
}

// Merge merges 2 objects or maps
func Merge(dst, src interface{}) error {
	return mergo.Merge(dst, src, mergo.WithTransformers(ptrTransformer{}))
}
