Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion automapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions automapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package automapper

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -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
}
Expand Down