Skip to content
Open
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
2 changes: 1 addition & 1 deletion automapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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 && sourceVal.Type() != destVal.Type() {
if sourceVal.Type().Kind() == reflect.Ptr {
if sourceVal.IsNil() {
// If source is nil, it maps to an empty struct
Expand Down
18 changes: 16 additions & 2 deletions automapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package automapper

import (
"testing"

"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestPanicWhenDestIsNotPointer(t *testing.T) {
Expand Down Expand Up @@ -256,6 +256,20 @@ func TestWithLooseOption(t *testing.T) {
assert.Equal(t, dest.Bar, 0)
}

func TestSetStructOfSameTypeDirectly(t *testing.T) {
type FooType struct {
time.Time
}
source := struct {
Foo FooType
}{FooType{Time: time.Now().UTC()}}
dest := struct {
Foo FooType
}{}
Map(&source, &dest)
assert.Equal(t, source.Foo.String(), dest.Foo.String())
}

type SourceParent struct {
Children []SourceTypeA
}
Expand Down