Skip to content

Commit

Permalink
no error on empty subfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
kolaworld committed Feb 17, 2025
1 parent 50a32d4 commit 59bbc7c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ func structToStruct(filter FieldFilter, src, dst *reflect.Value, userOptions *op

dstField := dst.FieldByName(dstName)
if !dstField.CanSet() {
return errors.Errorf("Can't set a value on a destination field %s", dstName)
if subFilter.IsEmpty() {
// skip field from source not found in destination given above subfilter is ok and empty.
continue

} else {
return errors.Errorf("Can't set a value on a destination field %s", dstName)
}
}

if err := structToStruct(subFilter, &srcField, &dstField, userOptions); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion copy_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ func TestStructToStruct_NonProtoFail(t *testing.T) {
userDst := &testproto.User{}
mask := fieldmask_utils.MaskFromString("")
err := fieldmask_utils.StructToStruct(mask, userSrc, userDst)
assert.NotNil(t, err)
assert.NoError(t, err)
assert.Equal(t, userSrc.Id, userDst.Id)
assert.Equal(t, userSrc.Deactivated, userDst.Deactivated)
}

func TestStructToStruct_UnknownAnyInSrcNoSubfieldMask(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,10 @@ func TestStructToStruct_SameInterfacesNonPtr_NonEmptyDst(t *testing.T) {

mask := fieldmask_utils.MaskFromString("Stringer")
err := fieldmask_utils.StructToStruct(mask, src, dst)
assert.Error(t, err)

assert.NoError(t, err)
assert.Equal(t, "Jessica", src.Stringer.String())
assert.Equal(t, "Adam", dst.Stringer.String())
}

func TestStructToStruct_NonPtrDst(t *testing.T) {
Expand Down

0 comments on commit 59bbc7c

Please sign in to comment.