Skip to content

Commit a438568

Browse files
committed
init
1 parent 5e289fa commit a438568

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/insei/gomapper
2+
3+
go 1.18
4+
5+
require github.com/stretchr/testify v1.8.2
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

mapper.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package gomapper
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
var routes = map[reflect.Type]map[reflect.Type]func(source interface{}, dest interface{}) error{}
9+
10+
func AddRoute[TSource, TDest interface{}](mapFunc func(source TSource, dest TDest) error) error {
11+
source := *new(TSource)
12+
dest := *new(TDest)
13+
destValueOf := reflect.ValueOf(dest)
14+
if destValueOf.Kind() != reflect.Ptr {
15+
return fmt.Errorf("destination object must be of reference type")
16+
}
17+
sourceValueOf := reflect.ValueOf(source)
18+
if sourceValueOf.Kind() == reflect.Ptr {
19+
return fmt.Errorf("source object must not be of reference type")
20+
}
21+
var route map[reflect.Type]func(source interface{}, dest interface{}) error
22+
route, ok := routes[reflect.TypeOf(source)]
23+
if !ok {
24+
route = map[reflect.Type]func(source interface{}, dest interface{}) error{}
25+
routes[reflect.TypeOf(source)] = route
26+
}
27+
funcConverted := func(source any, dest any) error {
28+
sourceForMap := source
29+
sourceValueOf := reflect.ValueOf(source)
30+
if sourceValueOf.Kind() == reflect.Ptr {
31+
sourceForMap = sourceValueOf.Elem().Interface()
32+
}
33+
return mapFunc(sourceForMap.(TSource), dest.(TDest))
34+
}
35+
route[reflect.TypeOf(dest)] = funcConverted
36+
return nil
37+
}
38+
39+
//Map source to dest
40+
func Map(source interface{}, dest interface{}) error {
41+
if source == nil || dest == nil {
42+
return fmt.Errorf("")
43+
}
44+
destValueOf := reflect.ValueOf(dest)
45+
if destValueOf.Kind() != reflect.Ptr {
46+
return fmt.Errorf("")
47+
}
48+
49+
var sourceToMap any
50+
sourceToMap = source
51+
sourceValueOf := reflect.ValueOf(source)
52+
if sourceValueOf.Kind() == reflect.Ptr {
53+
sourceToMap = sourceValueOf.Elem().Interface()
54+
}
55+
route, ok := routes[reflect.TypeOf(sourceToMap)]
56+
if !ok {
57+
return fmt.Errorf("route not found")
58+
}
59+
mapFunc, ok := route[reflect.TypeOf(dest)]
60+
if !ok {
61+
return fmt.Errorf("route not found")
62+
}
63+
return mapFunc(source, dest)
64+
}
65+
66+
// MapTo Map source to the new dest object
67+
func MapTo[TDest interface{}](source interface{}) (TDest, error) {
68+
dest := new(TDest)
69+
if source == nil {
70+
return *dest, nil
71+
}
72+
var sourceToMap any
73+
sourceToMap = source
74+
sourceValueOf := reflect.ValueOf(source)
75+
if sourceValueOf.Kind() == reflect.Ptr {
76+
sourceToMap = sourceValueOf.Elem().Interface()
77+
}
78+
route, ok := routes[reflect.TypeOf(sourceToMap)]
79+
if !ok {
80+
return *dest, nil
81+
}
82+
mapFunc, ok := route[reflect.TypeOf(dest)]
83+
if !ok {
84+
return *dest, nil
85+
}
86+
err := mapFunc(source, dest)
87+
return *dest, err
88+
}

mapper_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package gomapper
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type TestingStruct struct {
10+
Name string
11+
}
12+
13+
type TestingStruct2 struct {
14+
Name string
15+
}
16+
17+
func Test(t *testing.T) {
18+
err := AddRoute[TestingStruct, *TestingStruct2](func(source TestingStruct, dest *TestingStruct2) error {
19+
dest.Name = source.Name
20+
return nil
21+
})
22+
assert.NoError(t, err)
23+
24+
source1 := &TestingStruct{Name: "Test1"}
25+
dest1, err := MapTo[TestingStruct2](source1)
26+
assert.NoError(t, err)
27+
assert.Equal(t, source1.Name, dest1.Name)
28+
29+
source2 := TestingStruct{Name: "Test2"}
30+
dest2, err := MapTo[TestingStruct2](source2)
31+
assert.NoError(t, err)
32+
assert.Equal(t, source2.Name, dest2.Name)
33+
34+
source3 := &TestingStruct{Name: "Test3"}
35+
dest3 := &TestingStruct2{}
36+
err = Map(source3, dest3)
37+
assert.NoError(t, err)
38+
assert.Equal(t, source3.Name, dest3.Name)
39+
40+
source4 := TestingStruct{Name: "Test4"}
41+
dest4 := &TestingStruct2{}
42+
err = Map(source4, dest4)
43+
assert.NoError(t, err)
44+
assert.Equal(t, source4.Name, dest4.Name)
45+
}

0 commit comments

Comments
 (0)