From 20cd7c5abdab28d875f2cfeb15736059dc07d13b Mon Sep 17 00:00:00 2001 From: Nick Muller Date: Tue, 30 Jun 2020 11:38:31 +0200 Subject: [PATCH] Try to convert if possible, instead of just panicing with "Currently not supported". Convert will panic anyway if the conversion is not possible, and this way we support mapping of types that _can_ be converted. --- automapper.go | 2 +- automapper_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/automapper.go b/automapper.go index 1ef7f4e..05796ee 100644 --- a/automapper.go +++ b/automapper.go @@ -81,7 +81,7 @@ func mapValues(sourceVal, destVal reflect.Value, loose bool) { } else if destType.Kind() == reflect.Slice { mapSlice(sourceVal, destVal, loose) } else { - panic("Currently not supported") + destVal.Set(sourceVal.Convert(destType)) } } diff --git a/automapper_test.go b/automapper_test.go index bd8b93e..70c9bd9 100644 --- a/automapper_test.go +++ b/automapper_test.go @@ -256,6 +256,19 @@ func TestWithLooseOption(t *testing.T) { assert.Equal(t, dest.Bar, 0) } +func TestNamedType(t *testing.T) { + type SourceType string + type DestType string + source := struct { + Foo SourceType + }{"abc"} + dest := struct { + Foo DestType + }{} + Map(&source, &dest) + assert.Equal(t, string(source.Foo), string(dest.Foo)) +} + type SourceParent struct { Children []SourceTypeA }