Skip to content

Commit b63a4a2

Browse files
committed
#12 allow dst not to have all src fields
1 parent 97f533f commit b63a4a2

File tree

3 files changed

+31
-43
lines changed

3 files changed

+31
-43
lines changed

model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,9 @@ func doCopy(dv, sv reflect.Value) []error {
680680
// validate field - exists in dst, kind and type
681681
err := validateCopyField(f, sfv, dfv)
682682
if err != nil {
683-
errs = append(errs, err)
683+
if err != errFieldNotExists {
684+
errs = append(errs, err)
685+
}
684686

685687
continue
686688
}

model_test.go

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -737,24 +737,6 @@ func TestCopyStructTypeDiffOnLevel1Interface(t *testing.T) {
737737
assertEqual(t, src.Level1.Name, dst.Level1.(SampleLevelSrc).Name)
738738
}
739739

740-
func TestCopyStructElementIsNotValidInDst(t *testing.T) {
741-
type Source struct {
742-
Name string
743-
Year int
744-
}
745-
746-
type Destination struct {
747-
Name string
748-
}
749-
750-
src := Source{Year: 2016}
751-
dst := Destination{Name: "Value is gonna disappear"}
752-
753-
errs := Copy(&dst, src)
754-
755-
assertEqual(t, "Field: 'Year', does not exists in dst", errs[0].Error())
756-
}
757-
758740
func TestCopyStructZeroValToDst(t *testing.T) {
759741
type Source struct {
760742
Name string
@@ -1896,29 +1878,6 @@ func TestKind(t *testing.T) {
18961878
assertEqual(t, true, reflect.Invalid == kind6)
18971879
}
18981880

1899-
func TestMissingDestField(t *testing.T) {
1900-
1901-
type C struct {
1902-
}
1903-
1904-
type A struct {
1905-
X string
1906-
Y string
1907-
Z C
1908-
}
1909-
1910-
type B struct {
1911-
X string
1912-
}
1913-
1914-
a := A{X: "X", Y: "Y", Z: C{}}
1915-
b := B{}
1916-
errs := Copy(&b, &a)
1917-
assertEqual(t, 2, len(errs))
1918-
assertEqual(t, "Field: 'Y', does not exists in dst", errs[0].Error())
1919-
assertEqual(t, "Field: 'Z', does not exists in dst", errs[1].Error())
1920-
}
1921-
19221881
func TestNestedStructToStructMapping(t *testing.T) {
19231882
type C struct {
19241883
X string
@@ -2155,6 +2114,30 @@ func TestSetField(t *testing.T) {
21552114
assertEqual(t, "Field: String, type/kind did not match", err.Error())
21562115
}
21572116

2117+
func TestImprovedCopy(t *testing.T) {
2118+
type DomainObject struct {
2119+
Name string
2120+
Address string
2121+
Phone string
2122+
}
2123+
2124+
type LoginGreeterDTO struct {
2125+
Name string
2126+
}
2127+
2128+
src := DomainObject{
2129+
Name: "go-model",
2130+
Address: "123 sample street",
2131+
Phone: "000-000-0000",
2132+
}
2133+
2134+
dst := LoginGreeterDTO{}
2135+
2136+
errs := Copy(&dst, src)
2137+
assertEqual(t, 0, len(errs))
2138+
assertEqual(t, "go-model", dst.Name)
2139+
}
2140+
21582141
//
21592142
// helper test methods
21602143
//

util.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"reflect"
1111
)
1212

13+
var errFieldNotExists = errors.New("Field does not exists")
14+
1315
func isFieldZero(f reflect.Value) bool {
1416
// zero value of the given field
1517
// For example: reflect.Zero(reflect.TypeOf(42)) returns a Value with Kind Int and value 0
@@ -35,7 +37,8 @@ func isNoTraverseType(v reflect.Value) bool {
3537
func validateCopyField(f reflect.StructField, sfv, dfv reflect.Value) error {
3638
// check dst field is exists, if not valid move on
3739
if !dfv.IsValid() {
38-
return fmt.Errorf("Field: '%v', does not exists in dst", f.Name)
40+
return errFieldNotExists
41+
//return fmt.Errorf("Field does not exists in dst", f.Name)
3942
}
4043

4144
if conversionExists(sfv.Type(), dfv.Type()) {

0 commit comments

Comments
 (0)