|
| 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 | +} |
0 commit comments