Skip to content

Commit

Permalink
updated fmap dependency to version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Тимур Искандаров authored and Insei committed Jul 19, 2024
1 parent eea42bf commit c6d21a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
36 changes: 22 additions & 14 deletions auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ package gomapper
import (
"reflect"

"github.com/insei/fmap"
"github.com/insei/fmap/v3"
)

var manualFieldRoutes = map[reflect.Type]map[string]string{}
var (
manualFieldRoutes = map[reflect.Type]map[string]string{}
)

func AutoRoute[TSource, TDest any | []any](options ...AutoMapperOption) error {
s := new(TSource)
d := new(TDest)
sourceFields := fmap.GetFrom(s)
destFields := fmap.GetFrom(d)
sourceStorage, _ := fmap.GetFrom(s)
destStorage, _ := fmap.GetFrom(d)
sourceType := reflect.TypeOf(s)

parseOptions(options, sourceType)

mapFunc := func(source TSource, dest *TDest) error {
for key, sourceFld := range sourceFields {
destFld, ok := destFields[getDestFieldName(sourceType, key)]
for _, sourcePath := range sourceStorage.GetAllPaths() {
destFld, ok := destStorage.Find(getDestFieldName(sourceType, sourcePath))
if !ok {
continue
}
if err := setFieldRecursive(sourceFld, destFld, source, dest); err != nil {

srcFld := sourceStorage.MustFind(sourcePath)
if err := setFieldRecursive(srcFld, destFld, source, dest); err != nil {
return err
}
}
Expand All @@ -41,6 +45,7 @@ func parseOptions(options []AutoMapperOption, sourceType reflect.Type) {
}
manualFieldRoutes[sourceType][autoMapperOption.source] = autoMapperOption.dest
}

}
}

Expand All @@ -49,7 +54,7 @@ func setFieldRecursive(sourceFld, destFld fmap.Field, source, dest any) error {
return r(sourceFld.Get(source), destFld.GetPtr(dest))
}

if sourceFld.Type.Kind() != reflect.Struct {
if sourceFld.GetType().Kind() != reflect.Struct {
sourceVal := sourceFld.Get(source)
if sourceVal != nil {
destFld.Set(dest, sourceVal)
Expand All @@ -58,12 +63,15 @@ func setFieldRecursive(sourceFld, destFld fmap.Field, source, dest any) error {
}

sourceStructField := sourceFld.GetPtr(source)
sourceFields := fmap.GetFrom(sourceStructField)
sourceStorage, _ := fmap.GetFrom(sourceStructField)

destStructField := destFld.GetPtr(dest)
destFields := fmap.GetFrom(destStructField)
destStorage, _ := fmap.GetFrom(destStructField)

for fieldName, sField := range sourceFields {
dField, ok := destFields[getDestFieldName(sField.Type, fieldName)]
for _, sPath := range sourceStorage.GetAllPaths() {
sField := sourceStorage.MustFind(sPath)
dPath := getDestFieldName(sField.GetType(), sPath)
dField, ok := destStorage.Find(dPath)
if !ok {
continue
}
Expand All @@ -76,8 +84,8 @@ func setFieldRecursive(sourceFld, destFld fmap.Field, source, dest any) error {
}

func getRouteIfExists(sourceFld, destFld fmap.Field) (func(source interface{}, dest interface{}) error, bool) {
destType := destFld.Type
sourceType := sourceFld.Type
destType := destFld.GetType()
sourceType := sourceFld.GetType()
for sourceType.Kind() == reflect.Ptr {
sourceType = sourceType.Elem()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/google/uuid v1.6.0
github.com/insei/fmap v1.0.2
github.com/insei/fmap/v3 v3.0.0
github.com/stretchr/testify v1.9.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/insei/fmap v1.0.2 h1:QBq2iO0B2jcY6HOyNzij3kX2YjNj/pUB9G5mkm4EZR8=
github.com/insei/fmap v1.0.2/go.mod h1:dcTq2SHdtlqmKuxuBHHa6zL+oFPpMbN64HtAuq2PcUU=
github.com/insei/fmap/v3 v3.0.0 h1:BpRsFgQ2nt5tl/8tzp6y+MWIAJhqCTeKBHC56L/Um30=
github.com/insei/fmap/v3 v3.0.0/go.mod h1:Kk0gs7nKb4E/JycKJFnrsX5hlyBBe0yetGKFCJG0vzk=
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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down

0 comments on commit c6d21a5

Please sign in to comment.