Skip to main content

Copier

The copier package provides struct-to-struct field copying with built-in type converters for common VEF types.

Quick Start

import "github.com/coldsmirk/vef-framework-go/copier"

type UserParams struct {
Username string
Email string
}

type User struct {
Username string
Email string
IsActive bool
}

params := UserParams{Username: "alice", Email: "alice@example.com"}
user := User{}

err := copier.Copy(params, &user)
// user.Username = "alice", user.Email = "alice@example.com"

Options

WithIgnoreEmpty

Skips copying fields with zero values. This is how the framework's Update CRUD operation merges partial updates:

err := copier.Copy(params, &user, copier.WithIgnoreEmpty())

WithDeepCopy

Enables deep copying of nested structures:

err := copier.Copy(src, &dst, copier.WithDeepCopy())

WithCaseInsensitive

Enables case-insensitive field name matching:

err := copier.Copy(src, &dst, copier.WithCaseInsensitive())

WithFieldNameMapping

Adds custom field name mappings for fields with different names:

err := copier.Copy(src, &dst, copier.WithFieldNameMapping(
copier.FieldNameMapping{
SrcType: UserParams{},
DstType: User{},
Mapping: map[string]string{
"Name": "Username",
},
},
))

WithTypeConverters

Adds custom type converters:

err := copier.Copy(src, &dst, copier.WithTypeConverters(
copier.TypeConverter{
SrcType: MyCustomType{},
DstType: copier.String,
Fn: func(src interface{}) (interface{}, error) {
return src.(MyCustomType).String(), nil
},
},
))

Built-in Type Converters

The copier includes automatic converters for value ↔ pointer conversions of all common types:

Type PairDirection
string*stringBoth ways
bool*boolBoth ways
int, int8...int64*int...*int64Both ways
uint, uint8...uint64*uint...*uint64Both ways
float32, float64*float32, *float64Both ways
decimal.Decimal*decimal.DecimalBoth ways
time.Time*time.TimeBoth ways
timex.DateTime*timex.DateTimeBoth ways
timex.Date*timex.DateBoth ways
timex.Time*timex.TimeBoth ways

This means you can freely use pointer types in params structs for optional fields without worrying about type conversion.

Framework Integration

The copier package is used internally by the CRUD Create and Update builders to copy TParams fields into TModel instances. The Update builder specifically uses WithIgnoreEmpty() to support partial updates.