Skip to content

Commit

Permalink
fix pointer to uuid type auto mapping and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lordspinach authored and Insei committed Apr 23, 2024
1 parent caf22e8 commit 23b62c4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 14 additions & 2 deletions auto_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package gomapper

import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

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
}

Expand All @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ 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 {
*dest = &source
return nil
})
_ = addRoute[uuid.UUID, uuid.UUID](func(source uuid.UUID, dest *uuid.UUID) error {
dest = &source
*dest = source
return nil
})
}
Expand Down

0 comments on commit 23b62c4

Please sign in to comment.