diff --git a/automapper.go b/automapper.go index 1ef7f4e..bc28923 100644 --- a/automapper.go +++ b/automapper.go @@ -13,6 +13,7 @@ package automapper import ( "fmt" "reflect" + "time" ) // Map fills out the fields in dest with values from source. All fields in the @@ -58,7 +59,7 @@ func MapLoose(source, dest interface{}) { func mapValues(sourceVal, destVal reflect.Value, loose bool) { destType := destVal.Type() - if destType.Kind() == reflect.Struct { + if destType.Kind() == reflect.Struct && !structCanBeSet(sourceVal, destVal) { if sourceVal.Type().Kind() == reflect.Ptr { if sourceVal.IsNil() { // If source is nil, it maps to an empty struct @@ -161,3 +162,8 @@ func valueIsContainedInNilEmbeddedType(source reflect.Value, fieldName string) b } return false } + +func structCanBeSet(sourceVal, destVal reflect.Value) bool { + timeType := reflect.TypeOf(time.Time{}) + return sourceVal.Type() == timeType && destVal.Type() == timeType +} diff --git a/automapper_test.go b/automapper_test.go index bd8b93e..8a6a43b 100644 --- a/automapper_test.go +++ b/automapper_test.go @@ -4,6 +4,7 @@ package automapper import ( "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -256,6 +257,17 @@ func TestWithLooseOption(t *testing.T) { assert.Equal(t, dest.Bar, 0) } +func TestStructCanBeSet(t *testing.T) { + source := struct { + Foo time.Time + }{time.Now()} + dest := struct { + Foo time.Time + }{} + Map(&source, &dest) + assert.Equal(t, source.Foo, dest.Foo) +} + type SourceParent struct { Children []SourceTypeA }