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

Enable skipping unexported fields #223

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Option struct {
// Custom field name mappings to copy values with different names in `fromValue` and `toValue` types.
// Examples can be found in `copier_field_name_mapping_test.go`.
FieldNameMapping []FieldNameMapping

// SkipUnexported will skip private fields: they will not be copied
SkipUnexported bool
}

func (opt Option) converters() map[converterPair]TypeConverter {
Expand Down Expand Up @@ -320,7 +323,9 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)

// check source
if source.IsValid() {
copyUnexportedStructFields(dest, source)
if !opt.SkipUnexported {
copyUnexportedStructFields(dest, source)
}

// Copy from source field to dest field or method
fromTypeFields := deepFields(fromType)
Expand Down
50 changes: 50 additions & 0 deletions copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ func TestStructField(t *testing.T) {
DeepCopy: true,
}

optionsSkipUnexported := copier.Option{
SkipUnexported: true,
}

checkDetail := func(t *testing.T, source Detail, target Detail) {
if source.Info1 != target.Info1 {
t.Errorf("info1 is diff: source: %v, target: %v", source.Info1, target.Info1)
Expand Down Expand Up @@ -704,6 +708,52 @@ func TestStructField(t *testing.T) {
}
})
})

t.Run("should handle unexported fields", func(t *testing.T) {
t.Run("should copy unexported fields", func(t *testing.T) {
from := &struct {
unexportedField string
}{
unexportedField: "foo",
}

to := &struct {
unexportedField string
}{}

err := copier.Copy(to, from)
if err != nil {
t.Errorf("should not return an error")
return
}

if to.unexportedField != from.unexportedField {
t.Errorf("should be equal")
}
})

t.Run("should not copy unexported fields with disallowed by the option", func(t *testing.T) {
from := &struct {
unexportedField string
}{
unexportedField: "foo",
}

to := &struct {
unexportedField string
}{}

err := copier.CopyWithOption(to, from, optionsSkipUnexported)
if err != nil {
t.Errorf("should not return an error")
return
}

if to.unexportedField == from.unexportedField {
t.Errorf("should not be equal")
}
})
})
}

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