Documentation
¶
Index ¶
Examples ¶
Constants ¶
Variables ¶
View Source
var ( // ErrInvalidCopyDestination is returned when the destination is not addressable. ErrInvalidCopyDestination = errors.New("copy: destination must be non-nil and addressable") // ErrInvalidCopyFrom is returned when the source is not addressable. ErrInvalidCopyFrom = errors.New("copy: from must be non-nil and addressable") // ErrMapKeyNotMatch is returned when the map key types do not match. ErrMapKeyNotMatch = errors.New("copy: map's key type doesn't match") // ErrNotSupported is returned when the type is not supported. ErrNotSupported = errors.New("copy: not supported") // ErrFieldNameTagStartNotUpperCase is returned when the field name tag does not start with an uppercase letter. ErrFieldNameTagStartNotUpperCase = errors.New("copy: field name tag must be start upper case") )
Functions ¶
func Copy ¶
func Copy(toValue interface{}, fromValue interface{}) (err error)
Copy is copying things
Example ¶
package main import ( "fmt" "github.com/zeiss/pkg/cast" ) func main() { type User struct { ID int Name *string Description string } type Employee struct { ID int Name string } user := User{ID: 1, Name: cast.Ptr("Jinzhu"), Description: "Gopher"} employee := Employee{} copy.Copy(&employee, &user) fmt.Printf("%v\n", employee) }
Output: {1 Jinzhu}
func CopyWithOption ¶
CopyWithOption copy with option
Example ¶
package main import ( "fmt" ) func main() { type User struct { ID int Name string } type Employee struct { ID int Name string `copy:"-"` } user := User{ID: 1, Name: "Jinzhu"} employee := Employee{} copy.Copy(&employee, &user) fmt.Printf("%v\n", employee) }
Output: {1 }
Types ¶
type FieldNameMapping ¶
FieldNameMapping is the field name mapping.
type Opt ¶
type Opt func(*Opts)
Opt is a function that configures the copy.
func WithCaseSensitive ¶
func WithCaseSensitive() Opt
WithCaseSensitive will ignore case sensitivity.
func WithConverters ¶
func WithConverters(converters ...TypeConverter) Opt
WithConverters are the type converters.
func WithFieldNameMapping ¶
func WithFieldNameMapping(fieldNameMapping ...FieldNameMapping) Opt
WithFieldNameMapping are the field name mappings.
type Opts ¶
type Opts struct { // IgnoreEmpty will ignore empty fields. IgnoreEmpty bool // CaseSensitive will ignore case sensitivity. CaseSensitive bool // DeepCopy will copy the fields deeply. DeepCopy bool // Converters are the type converters. Converters []TypeConverter // FieldNameMapping are the field name mappings. FieldNameMapping []FieldNameMapping }
Opts are the options for the copier.
type TypeConverter ¶
type TypeConverter struct { SrcType interface{} DstType interface{} Fn func(src interface{}) (dst interface{}, err error) }
Click to show internal directories.
Click to hide internal directories.