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

Times are being overwritten even when src is empty #52

Closed
diego-aslz opened this issue Jan 11, 2018 · 3 comments
Closed

Times are being overwritten even when src is empty #52

diego-aslz opened this issue Jan 11, 2018 · 3 comments

Comments

@diego-aslz
Copy link

diego-aslz commented Jan 11, 2018

I'm using mergo with structs that can have empty times. I expected it not to overwrite times in destination when the value in source is empty (time.IsEmpty() returns true).

Sample failing test:

type structWithTime struct {
	Birth time.Time
}

func TestEmptyTime(t *testing.T) {
	now := time.Now()
	dst := structWithTime{now}
	src := structWithTime{}
	if err := MergeWithOverwrite(&dst, src); err != nil {
		t.FailNow()
	}
	if dst.Birth.IsZero() {
		t.Fatalf("time.Time should not have been overwritten: dst.Birth(%v) != now(%v)", dst.Birth, now)
	}
}

Output:

--- FAIL: TestEmptyTime (0.00s)
	mergo_test.go:630: time.Time should not have been overwritten: dst.Birth(0001-01-01 00:00:00 +0000 UTC) != now(2018-01-11 14:00:18.433651 -0200 -02 m=+0.002753105)

Is this expected behavior or can I go ahead fork & change it?

@darccio
Copy link
Owner

darccio commented Jan 11, 2018

Updated: I understood wrongly your issue and my assumptions were also wrong. I'm checking this.

This is expected behavior. Mergo checks for zero values of basic types. time.Time isn't a basic type but a struct. Although semantically time.Time{} is empty, it isn't an empty (zero value) struct because it has non-zero value fields.

Changing this behavior would mean to support every possible way to check if a struct is semantically empty, calling specific functions. It could work for stdlib's non-basic types but it won't scale.

So, there is no hope? Of course there is :) I just merged PR #49 that adds transformers. Transformers allow to merge specific types differently that the default. I'm just getting used to it (it was implemented by @vdemeester from Docker) and I think it fits right in your case.

@darccio
Copy link
Owner

darccio commented Jan 11, 2018

Ok, I understand what is happening. As per MergeWithOverwrite's documentation:

MergeWithOverwrite will do the same as Merge except that non-empty dst attributes will be overriden by non-empty src attribute values.

As I said in my previous comment, time.Time can be semantically empty (as in IsZero() returns true) but it is a struct. Structs in Go don't have a zero value by themselves, as described in the official documentation about zero value:

When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value is set to the zero value for its type: false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps. This initialization is done recursively, so for instance each element of an array of structs will have its fields zeroed if no value is specified.

So the overwriting behavior works as expected. It overrides one struct by another one.

The only way to solve this is using transformers, as I also said in my previous comment. I wrote some tests showing how to use transformers in your case, with Merge and MergeWithOverwrite:

type structWithTime struct {
	Birth time.Time
}

type timeTransfomer struct {
	overwrite bool
}

func (t timeTransfomer) Transformer(typ reflect.Type) func (dst, src reflect.Value) error {
	if typ == reflect.TypeOf(time.Time{}) {
		return func (dst, src reflect.Value) error {
			if dst.CanSet() {
				if t.overwrite {
					isZero := src.MethodByName("IsZero")
					result := isZero.Call([]reflect.Value{})
					if !result[0].Bool() {
						dst.Set(src)
					}
				} else {
					isZero := dst.MethodByName("IsZero")
					result := isZero.Call([]reflect.Value{})
					if result[0].Bool() {
						dst.Set(src)
					}
				}
			}
			return nil
		}
	}
	return nil
}

// Expected standard behavior: ruthless overwrite.
func TestOverwriteZeroSrcTime(t *testing.T) {
	now := time.Now()
	dst := structWithTime{now}
	src := structWithTime{}
	if err := MergeWithOverwrite(&dst, src); err != nil {
		t.FailNow()
	}
	if !dst.Birth.IsZero() {
		t.Fatalf("dst should have been overwritten: dst.Birth(%v) != now(%v)", dst.Birth, now)
	}
}

// Non standard behavior for MergeWithOverwrite: src.IsZero() == true then we don't overwrite dst.
func TestOverwriteZeroSrcTimeWithTransformer(t *testing.T) {
	now := time.Now()
	dst := structWithTime{now}
	src := structWithTime{}
	if err := MergeWithOverwrite(&dst, src, WithTransformers(timeTransfomer{true})); err != nil {
		t.FailNow()
	}
	if dst.Birth.IsZero() {
		t.Fatalf("dst should not have been overwritten: dst.Birth(%v) != now(%v)", dst.Birth, now)
	}
}

// Expected standard behavior: ruthless overwrite.
func TestOverwriteZeroDstTime(t *testing.T) {
	now := time.Now()
	dst := structWithTime{}
	src := structWithTime{now}
	if err := MergeWithOverwrite(&dst, src); err != nil {
		t.FailNow()
	}
	if dst.Birth.IsZero() {
		t.Fatalf("dst should have been overwritten: dst.Birth(%v) != zero(%v)", dst.Birth, time.Time{})
	}
}

// Expected standard behavior: dst is a struct and structs doesn't have a zero value, so we don't overwrite it.
func TestZeroDstTime(t *testing.T) {
	now := time.Now()
	dst := structWithTime{}
	src := structWithTime{now}
	if err := Merge(&dst, src); err != nil {
		t.FailNow()
	}
	if !dst.Birth.IsZero() {
		t.Fatalf("dst should not have been overwritten: dst.Birth(%v) != zero(%v)", dst.Birth, time.Time{})
	}
}

// Non standard behavior for Merge: dst.IsZero() == true then we overwrite dst.
func TestZeroDstTimeWithTransformer(t *testing.T) {
	now := time.Now()
	dst := structWithTime{}
	src := structWithTime{now}
	if err := Merge(&dst, src, WithTransformers(timeTransfomer{})); err != nil {
		t.FailNow()
	}
	if dst.Birth.IsZero() {
		t.Fatalf("dst should have been overwritten: dst.Birth(%v) != now(%v)", dst.Birth, now)
	}
}

darccio added a commit that referenced this issue Jan 11, 2018
@darccio darccio closed this as completed Jan 11, 2018
@diego-aslz
Copy link
Author

Thank you so much @imdario for the explanation. I'm gonna use the Transformer approach.

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

No branches or pull requests

2 participants