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 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
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ import (
"github.com/jinzhu/copier"
)

type Address struct{
City string
Street string
}

// copier.Valuer interface 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`.
func (a Address) CopyValue() interface{} {
return fmt.Sprintf("%s, %s", a.Street, a.City)
}

type User struct {
Name string
Role string
Expand All @@ -34,6 +48,8 @@ type User struct {

// Explicitly ignored in the destination struct.
Salary int

Address Address
}

func (user *User) DoubleAge() int32 {
Expand All @@ -55,6 +71,8 @@ type Employee struct {
DoubleAge int32
EmployeeId int64 `copier:"EmployeeNum"` // specify field name
SuperRole string

Address string
}

func (employee *Employee) Role(role string) {
Expand All @@ -63,8 +81,10 @@ func (employee *Employee) Role(role string) {

func main() {
var (
user = User{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 200000}
users = []User{{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 100000}, {Name: "jinzhu 2", Age: 30, Role: "Dev", Salary: 60000}}
user = User{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 200000, Address: Address{Street: "123 Main Street", City: "Somewhere"}}
users = []User{
{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 100000, Address: Address{Street: "124 Secondary Street", City: "SomewhereElse"}},
{Name: "jinzhu 2", Age: 30, Role: "Dev", Salary: 60000, Address: Address{Street: "125 Secondary Street", City: "SomewhereElse"}}}
employee = Employee{Salary: 150000}
employees = []Employee{}
)
Expand All @@ -73,20 +93,21 @@ func main() {

fmt.Printf("%#v \n", employee)
// Employee{
// Name: "Jinzhu", // Copy from field
// Age: 18, // Copy from field
// Salary:150000, // Copying explicitly ignored
// DoubleAge: 36, // Copy from method
// EmployeeId: 0, // Ignored
// SuperRole: "Super Admin", // Copy to method
// Name: "Jinzhu", // Copy from field
// Age: 18, // Copy from field
// Salary:150000, // Copying explicitly ignored
// DoubleAge: 36, // Copy from method
// EmployeeId: 0, // Ignored
// SuperRole: "Super Admin", // Copy to method
// Address: "123 Main Street, Somewhere", // Copy from value returned by CopyValue()
// }

// Copy struct to slice
copier.Copy(&employees, &user)

fmt.Printf("%#v \n", employees)
// []Employee{
// {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin"}
// {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin", Address: "123 Main Street, Somewhere"}
// }

// Copy slice to slice
Expand All @@ -95,8 +116,8 @@ func main() {

fmt.Printf("%#v \n", employees)
// []Employee{
// {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin"},
// {Name: "jinzhu 2", Age: 30, Salary:0, DoubleAge: 60, EmployeeId: 0, SuperRole: "Super Dev"},
// {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin", Address: "124 Secondary Street, SomewhereElse"},
// {Name: "jinzhu 2", Age: 30, Salary:0, DoubleAge: 60, EmployeeId: 0, SuperRole: "Super Dev", Address: "125 Secondary Street, SomewhereElse"},
// }

// Copy map to map
Expand Down
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)
}
}