diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dc182f2 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/insei/gomapper + +go 1.18 + +require github.com/stretchr/testify v1.8.2 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6a56e69 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mapper.go b/mapper.go new file mode 100644 index 0000000..b34205e --- /dev/null +++ b/mapper.go @@ -0,0 +1,88 @@ +package gomapper + +import ( + "fmt" + "reflect" +) + +var routes = map[reflect.Type]map[reflect.Type]func(source interface{}, dest interface{}) error{} + +func AddRoute[TSource, TDest interface{}](mapFunc func(source TSource, dest TDest) error) error { + source := *new(TSource) + dest := *new(TDest) + destValueOf := reflect.ValueOf(dest) + if destValueOf.Kind() != reflect.Ptr { + return fmt.Errorf("destination object must be of reference type") + } + sourceValueOf := reflect.ValueOf(source) + if sourceValueOf.Kind() == reflect.Ptr { + return fmt.Errorf("source object must not be of reference type") + } + var route map[reflect.Type]func(source interface{}, dest interface{}) error + route, ok := routes[reflect.TypeOf(source)] + if !ok { + route = map[reflect.Type]func(source interface{}, dest interface{}) error{} + routes[reflect.TypeOf(source)] = route + } + funcConverted := func(source any, dest any) error { + sourceForMap := source + sourceValueOf := reflect.ValueOf(source) + if sourceValueOf.Kind() == reflect.Ptr { + sourceForMap = sourceValueOf.Elem().Interface() + } + return mapFunc(sourceForMap.(TSource), dest.(TDest)) + } + route[reflect.TypeOf(dest)] = funcConverted + return nil +} + +//Map source to dest +func Map(source interface{}, dest interface{}) error { + if source == nil || dest == nil { + return fmt.Errorf("") + } + destValueOf := reflect.ValueOf(dest) + if destValueOf.Kind() != reflect.Ptr { + return fmt.Errorf("") + } + + var sourceToMap any + sourceToMap = source + sourceValueOf := reflect.ValueOf(source) + if sourceValueOf.Kind() == reflect.Ptr { + sourceToMap = sourceValueOf.Elem().Interface() + } + route, ok := routes[reflect.TypeOf(sourceToMap)] + if !ok { + return fmt.Errorf("route not found") + } + mapFunc, ok := route[reflect.TypeOf(dest)] + if !ok { + return fmt.Errorf("route not found") + } + return mapFunc(source, dest) +} + +// MapTo Map source to the new dest object +func MapTo[TDest interface{}](source interface{}) (TDest, error) { + dest := new(TDest) + if source == nil { + return *dest, nil + } + var sourceToMap any + sourceToMap = source + sourceValueOf := reflect.ValueOf(source) + if sourceValueOf.Kind() == reflect.Ptr { + sourceToMap = sourceValueOf.Elem().Interface() + } + route, ok := routes[reflect.TypeOf(sourceToMap)] + if !ok { + return *dest, nil + } + mapFunc, ok := route[reflect.TypeOf(dest)] + if !ok { + return *dest, nil + } + err := mapFunc(source, dest) + return *dest, err +} diff --git a/mapper_test.go b/mapper_test.go new file mode 100644 index 0000000..f531782 --- /dev/null +++ b/mapper_test.go @@ -0,0 +1,45 @@ +package gomapper + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type TestingStruct struct { + Name string +} + +type TestingStruct2 struct { + Name string +} + +func Test(t *testing.T) { + err := AddRoute[TestingStruct, *TestingStruct2](func(source TestingStruct, dest *TestingStruct2) error { + dest.Name = source.Name + return nil + }) + assert.NoError(t, err) + + source1 := &TestingStruct{Name: "Test1"} + dest1, err := MapTo[TestingStruct2](source1) + assert.NoError(t, err) + assert.Equal(t, source1.Name, dest1.Name) + + source2 := TestingStruct{Name: "Test2"} + dest2, err := MapTo[TestingStruct2](source2) + assert.NoError(t, err) + assert.Equal(t, source2.Name, dest2.Name) + + source3 := &TestingStruct{Name: "Test3"} + dest3 := &TestingStruct2{} + err = Map(source3, dest3) + assert.NoError(t, err) + assert.Equal(t, source3.Name, dest3.Name) + + source4 := TestingStruct{Name: "Test4"} + dest4 := &TestingStruct2{} + err = Map(source4, dest4) + assert.NoError(t, err) + assert.Equal(t, source4.Name, dest4.Name) +}