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

Basic struct tags support: skip field, assign field #15

Open
wants to merge 3 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,18 @@ DeepCopy makes deep copies of things: unexported field values are not copied.

## Usage
cpy := deepcopy.Copy(orig)

## Tags

The following struct tags is supported:

```
type A struct {
Field1 SomeType `deepcopy:"-"` // skip, will have zero-value in copy
Field2 *SomeType `deepcopy:"="` // treat like with "=" assignment operator
}
```

Specifically the `=` tag is usable when you want to copy a struct containing
something like `*sync.Mutex` or `*os.File` and don't want it to be deeply copied
but simply assigned.
12 changes: 11 additions & 1 deletion deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,22 @@ func copyRecursive(original, cpy reflect.Value) {
}
// Go through each field of the struct and copy it.
for i := 0; i < original.NumField(); i++ {
field := original.Type().Field(i)
// The Type's StructField for a given field is checked to see if StructField.PkgPath
// is set to determine if the field is exported or not because CanSet() returns false
// for settable fields. I'm not sure why. -mohae
if original.Type().Field(i).PkgPath != "" {
if field.PkgPath != "" {
continue
}

switch field.Tag.Get("deepcopy") {
case "-":
continue
case "=":
cpy.Field(i).Set(original.Field(i))
continue
}

copyRecursive(original.Field(i), cpy.Field(i))
}

Expand Down
39 changes: 39 additions & 0 deletions deepcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,45 @@ func TestPointerToStruct(t *testing.T) {
}
}

func TestFieldSkipByTag(t *testing.T) {
type Foo struct {
Bar int `deepcopy:"-"`
Thing int
}

f := Foo{Bar: 42, Thing: 44}
cpy := Copy(f).(Foo)

if !reflect.DeepEqual(cpy, Foo{Thing: 44}) { // Bar is skipped
t.Errorf("expected the copy to be equal to the original (except for memory location); it wasn't: got %#v; want %#v", f, cpy)
}
}

func TestFieldAssignByTag(t *testing.T) {
type Inner struct {
Field int
}
type Foo struct {
Bar *Inner `deepcopy:"="`
Thing int
}

inner := &Inner{Field: 100}
f := Foo{Bar: inner, Thing: 44}
cpy := Copy(f).(Foo)

if !reflect.DeepEqual(cpy, Foo{Bar: inner, Thing: 44}) { // Bar is skipped
t.Errorf("expected the copy to be equal to the original (except for memory location); it wasn't: got %#v; want %#v", f, cpy)
}

inner.Field = 101
if inner.Field != f.Bar.Field || f.Bar.Field != cpy.Bar.Field {
t.Errorf("expected that pointer-assigned value will change in both original and copy: %d != %d != %d",
inner.Field, f.Bar.Field, cpy.Bar.Field)
}

}

func TestIssue9(t *testing.T) {
// simple pointer copy
x := 42
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mtfelian/deepcopy

go 1.12