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

feat: if err occur, dst will recover to init #199

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
38 changes: 13 additions & 25 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
return ErrInvalidCopyFrom
}

cacheToValue := indirect(reflect.New(to.Type()))
cacheToValue.Set(to)
defer func() {
// if err occur, toValue needs to recover to init state.
if err != nil {
to.Set(cacheToValue)
}
}()

fromType, isPtrFrom := indirectType(from.Type())
toType, _ := indirectType(to.Type())

Expand Down Expand Up @@ -241,13 +250,6 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
return
}

if len(converters) > 0 {
if ok, e := set(to, from, opt.DeepCopy, converters); e == nil && ok {
// converter supported
return
}
}

if from.Kind() == reflect.Slice || to.Kind() == reflect.Slice {
isSlice = true
if from.Kind() == reflect.Slice {
Expand All @@ -272,24 +274,10 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
dest = indirect(to)
}

isSet := false
if len(converters) > 0 {
if ok, e := set(dest, source, opt.DeepCopy, converters); e == nil && ok {
if isSlice {
// FIXME: maybe should check the other types?
if to.Type().Elem().Kind() == reflect.Ptr {
to.Index(i).Set(dest.Addr())
} else {
if to.Len() < i+1 {
reflect.Append(to, dest)
} else {
to.Index(i).Set(dest)
}
}
} else {
to.Set(dest)
}

continue
if isSet, err = set(dest, source, opt.DeepCopy, converters); err != nil {
return err
}
}

Expand All @@ -307,7 +295,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
}

// check source
if source.IsValid() {
if source.IsValid() && !isSet {
copyUnexportedStructFields(dest, source)

// Copy from source field to dest field or method
Expand Down
3 changes: 2 additions & 1 deletion copier_issue170_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package copier_test

import (
"github.com/jinzhu/copier"
"reflect"
"testing"

"github.com/jinzhu/copier"
)

type A struct {
Expand Down
68 changes: 67 additions & 1 deletion copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,6 @@ func TestDeepCopyAnonymousFieldTime(t *testing.T) {
}

func TestSqlNullFiled(t *testing.T) {

type sqlStruct struct {
MkId sql.NullInt64
MkExpiryDateType sql.NullInt32
Expand Down Expand Up @@ -1762,3 +1761,70 @@ func TestNestedNilPointerStruct(t *testing.T) {
t.Errorf("to (%v) value should equal from (%v) value", to.Title, from.Title)
}
}

func TestOccurErr(t *testing.T) {
t.Run("CopyWithOption err occur", func(t *testing.T) {
type srcTags struct {
Field string
Index int
}
type destTags struct {
Field string
Index string
}

dst := &destTags{
Field: "init",
Index: "0",
}
src := &srcTags{
Field: "copied",
Index: 1,
}
err := copier.CopyWithOption(dst, src, copier.Option{
Converters: []copier.TypeConverter{
{
SrcType: 1,
DstType: "",
Fn: func(src interface{}) (dst interface{}, err error) {
return nil, fmt.Errorf("return err")
},
},
},
})
if err == nil {
t.Errorf("should return err")
}
if dst.Field != "init" || dst.Index != "0" {
t.Error("when err occur, the dst should be init")
}

})
t.Run("copy err occur", func(t *testing.T) {
type srcTags struct {
field string
Field2 string
}

type destTags struct {
Field string `copier:"field"`
Field2 string `copier:"Field2"`
}

dst := &destTags{
Field: "init",
Field2: "init2",
}
src := &srcTags{
field: "Field1->Field1",
Field2: "Field2->Field2",
}
err := copier.Copy(dst, src)
if err == nil {
t.Errorf("should return err")
}
if dst.Field != "init" || dst.Field2 != "init2" {
t.Error("when err occur, the dst should be init")
}
})
}
Loading