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

Valuer interface #191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ type FieldNameMapping struct {
Mapping map[string]string
}

// Valuer lets custom types implement a function returning the actual value to copy.
// For example if your type is a wrapper, or if it doesn't have to implement `sql/driver.Valuer`,
// you can implement this interface so the returned value will be used instead. It can also be used
// to format your type or convert it to another one before being copied.
// This also enables conversion for types using generics, as you cannot use them with `TypeConverter`.
type Valuer interface {
CopyValue() interface{}
}

// Tag Flags
type flags struct {
BitFlags map[string]uint8
Expand All @@ -118,6 +127,11 @@ func CopyWithOption(toValue interface{}, fromValue interface{}, opt Option) (err
}

func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unnecessary break line

if fromCopyValuer, ok := fromValue.(Valuer); ok {
fromValue = fromCopyValuer.CopyValue()
}

var (
isSlice bool
amount = 1
Expand Down Expand Up @@ -558,6 +572,11 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ
if !from.IsValid() {
return true, nil
}

if fromCopyValuer, ok := copyValuer(from); ok {
from = reflect.ValueOf(fromCopyValuer.CopyValue())
}

if ok, err := lookupAndCopyWithConverter(to, from, converters); err != nil {
return false, err
} else if ok {
Expand Down Expand Up @@ -819,6 +838,16 @@ func driverValuer(v reflect.Value) (i driver.Valuer, ok bool) {
return
}

func copyValuer(v reflect.Value) (i Valuer, ok bool) {
if !v.CanAddr() {
i, ok = v.Interface().(Valuer)
return
}

i, ok = v.Addr().Interface().(Valuer)
return
}

func fieldByName(v reflect.Value, name string, caseSensitive bool) reflect.Value {
if caseSensitive {
return v.FieldByName(name)
Expand Down
32 changes: 32 additions & 0 deletions copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1762,3 +1762,35 @@ func TestNestedNilPointerStruct(t *testing.T) {
t.Errorf("to (%v) value should equal from (%v) value", to.Title, from.Title)
}
}

type testValuer struct {
Value interface{}
}

func (v testValuer) CopyValue() interface{} {
return v.Value
}

func TestCopyValuer(t *testing.T) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unnecessary break line

to := struct {
Value string
}{
Value: "initial",
}

from := struct {
Value testValuer
}{
Value: testValuer{Value: "override"},
}

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

if to.Value != from.Value.Value {
t.Errorf("to (%v) value should equal to from (%v) value", to.Value, from.Value.Value)
}
}