Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A clear hint for NotSupported #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
converters map[converterPair]TypeConverter
)

// save convertes into map for faster lookup
// save converters into map for faster lookup
for i := range opt.Converters {
if converters == nil {
converters = make(map[converterPair]TypeConverter)
Expand Down Expand Up @@ -173,6 +173,18 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
return
}

if from.Kind() != reflect.Slice && fromType.Kind() == reflect.Map && toType.Kind() == reflect.Struct {
// only support map[string]interface{} to struct
for _, k := range from.MapKeys() {
if _, ok := toType.FieldByName(k.String()); ok {
if !set(to.FieldByName(s), from.MapIndex(k).Elem(), opt.DeepCopy, converters) {
return fmt.Errorf("%w map to struct, old key: %v, new filed: %v", ErrNotSupported, k.Type(), toType.Key())
}
}
}
return
}

if from.Kind() == reflect.Slice && to.Kind() == reflect.Slice && fromType.ConvertibleTo(toType) {
if to.IsNil() {
slice := reflect.MakeSlice(reflect.SliceOf(to.Type().Elem()), from.Len(), from.Cap())
Expand All @@ -196,8 +208,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
}

if fromType.Kind() != reflect.Struct || toType.Kind() != reflect.Struct {
// skip not supported type
return
return ErrNotSupported
}

if from.Kind() == reflect.Slice || to.Kind() == reflect.Slice {
Expand Down
58 changes: 47 additions & 11 deletions copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -1203,7 +1204,7 @@ func TestCopyMapOfInt(t *testing.T) {
func TestCopyMapOfSliceValue(t *testing.T) {
// case1: map's value is a simple slice
key, value := 2, 3
src := map[int][]int{key: []int{value} }
src := map[int][]int{key: []int{value}}
dst1 := map[int][]int{}
var dst2 map[int][]int
err := copier.Copy(&dst1, src)
Expand Down Expand Up @@ -1240,10 +1241,10 @@ func TestCopyMapOfSliceValue(t *testing.T) {
// case2: map's value is a slice whose element is map
key1, key2 := 2, 3
value = 4
s := map[int][]map[int]int{key1: []map[int]int{ {key2: value} } }
d1 := map[int][]map[int]int{key1: []map[int]int{ {key1: key2 } } }
d2 := map[int][]map[int]int{key1: []map[int]int{ } }
d3 := map[int][]map[int]int{key1: nil }
s := map[int][]map[int]int{key1: []map[int]int{{key2: value}}}
d1 := map[int][]map[int]int{key1: []map[int]int{{key1: key2}}}
d2 := map[int][]map[int]int{key1: []map[int]int{}}
d3 := map[int][]map[int]int{key1: nil}
d4 := map[int][]map[int]int{}
d5 := map[int][]map[int]int(nil)
ms := []map[int][]map[int]int{d1, d2, d3, d4, d5}
Expand All @@ -1267,7 +1268,7 @@ func TestCopyMapOfSliceValue(t *testing.T) {
for k, v := range m {
if k != key2 || v != value {
t.Errorf("Map's slice value should be copied recursively")
}
}
}
}
}
Expand All @@ -1276,7 +1277,7 @@ func TestCopyMapOfSliceValue(t *testing.T) {
func TestCopyMapOfPtrValue(t *testing.T) {
intV := 3
intv := intV
src := map[int]*int{2: &intv }
src := map[int]*int{2: &intv}
dst1 := map[int]*int{}
var dst2 map[int]*int
err := copier.Copy(&dst1, src)
Expand All @@ -1295,7 +1296,7 @@ func TestCopyMapOfPtrValue(t *testing.T) {
}

v3, ok := dst2[k]
if !ok || v3 == nil ||*v3 != *v1 || *v3 != intV {
if !ok || v3 == nil || *v3 != *v1 || *v3 != intV {
t.Errorf("Map should be copied")
}
}
Expand All @@ -1315,6 +1316,43 @@ func TestCopyWithOption(t *testing.T) {
}
}

type fromMapStruct struct {
A int
B string
}

func TestCopyMap2StructWithOption(t *testing.T) {
from := map[string]interface{}{
"A": "456",
"B": "sss",
}
to := &fromMapStruct{}
if err := copier.CopyWithOption(&to, from, copier.Option{
Converters: []copier.TypeConverter{
{
SrcType: copier.String,
DstType: copier.Int,
Fn: func(src interface{}) (interface{}, error) {
s, ok := src.(string)

if !ok {
return nil, errors.New("src type not matching")
}
return strconv.Atoi(s)
},
},
},
}); err != nil {
t.Error("Should not raise error")
}

if to.A != from["A"] {
t.Errorf("Field A should be copied")
} else if to.B != from["B"] {
t.Errorf("Field B should be copied")
}
}

type ScannerValue struct {
V int
}
Expand Down Expand Up @@ -1520,7 +1558,6 @@ func TestDeepCopyTime(t *testing.T) {
}
}


func TestNestedPrivateData(t *testing.T) {
type hasPrivate struct {
data int
Expand Down Expand Up @@ -1558,7 +1595,6 @@ func TestNestedPrivateData(t *testing.T) {
}
}


func TestDeepMapCopyTime(t *testing.T) {
t1 := time.Now()
t2 := t1.Add(time.Second)
Expand Down Expand Up @@ -1611,7 +1647,7 @@ func TestDeepCopySimpleTime(t *testing.T) {
}
}

type TimeWrapper struct{
type TimeWrapper struct {
time.Time
}

Expand Down