From 23b62c429bc036ca50f5da1dbb3766098d52c0ac Mon Sep 17 00:00:00 2001 From: Konstantin Gamayunov Date: Tue, 23 Apr 2024 16:06:21 +0300 Subject: [PATCH] fix pointer to uuid type auto mapping and add test --- auto_test.go | 16 ++++++++++++++-- fields/fields.go | 8 ++++++++ routes.go | 4 ++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/auto_test.go b/auto_test.go index e54b623..f0ee0fe 100644 --- a/auto_test.go +++ b/auto_test.go @@ -1,6 +1,7 @@ package gomapper import ( + "github.com/google/uuid" "github.com/stretchr/testify/assert" "testing" "time" @@ -8,14 +9,20 @@ import ( type AutoMappingStructSource struct { Name string + Time time.Time + UUID uuid.UUID PtrTime *time.Time + PtrUUID *uuid.UUID NestedStruct NestedStructSource } type AutoMappingStructDest struct { Name string SecondName string + Time time.Time + UUID uuid.UUID PtrTime *time.Time + PtrUUID *uuid.UUID NestedStruct NestedStructDest } @@ -41,11 +48,16 @@ type DeepNestedStructDest struct { func TestAutoRoute(t *testing.T) { _ = AutoRoute[AutoMappingStructSource, AutoMappingStructDest]() t.Run("Auto route without options", func(t *testing.T) { - dt := time.Now() - source := &AutoMappingStructSource{Name: "Test1", PtrTime: &dt} + ptrTime := time.Now() + ptrUuid := uuid.New() + source := &AutoMappingStructSource{Name: "Test1", Time: time.Now(), UUID: uuid.New(), PtrTime: &ptrTime, PtrUUID: &ptrUuid} dest, err := MapTo[AutoMappingStructDest](source) assert.NoError(t, err) assert.Equal(t, source.Name, dest.Name) + assert.Equal(t, source.Time, dest.Time) + assert.Equal(t, source.UUID, dest.UUID) + assert.Equal(t, source.PtrUUID, dest.PtrUUID) + assert.Equal(t, source.PtrTime, dest.PtrTime) }) _ = AutoRoute[AutoMappingStructSource, AutoMappingStructDest](WithFieldRoute("Name", "SecondName")) t.Run("Auto route with options", func(t *testing.T) { diff --git a/fields/fields.go b/fields/fields.go index aaaa580..0f89ce6 100644 --- a/fields/fields.go +++ b/fields/fields.go @@ -46,6 +46,10 @@ func (f Field) Get(obj interface{}) interface{} { return getPtrValue[*bool](ptrToField) case reflect.Struct: return reflect.NewAt(f.Type, ptrToField).Interface() + case reflect.Slice: + return reflect.NewAt(f.Type, ptrToField).Interface() + case reflect.Array: + return reflect.NewAt(f.Type, ptrToField).Interface() default: panic("unhandled default case") } @@ -71,6 +75,10 @@ func (f Field) Get(obj interface{}) interface{} { return getPtrValue[bool](ptrToField) case reflect.Struct: return reflect.NewAt(f.Type, ptrToField).Interface() + case reflect.Slice: + return reflect.NewAt(f.Type, ptrToField).Interface() + case reflect.Array: + return reflect.NewAt(f.Type, ptrToField).Interface() default: panic("unhandled default case") } diff --git a/routes.go b/routes.go index d4d785e..94630f8 100644 --- a/routes.go +++ b/routes.go @@ -146,7 +146,7 @@ func addBaseTypesRoutes() { return nil }) _ = addRoute[time.Time, time.Time](func(source time.Time, dest *time.Time) error { - dest = &source + *dest = source return nil }) _ = addRoute[uuid.UUID, *uuid.UUID](func(source uuid.UUID, dest **uuid.UUID) error { @@ -154,7 +154,7 @@ func addBaseTypesRoutes() { return nil }) _ = addRoute[uuid.UUID, uuid.UUID](func(source uuid.UUID, dest *uuid.UUID) error { - dest = &source + *dest = source return nil }) }