copy

package
v0.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (

	// Some default converter types for a nicer syntax
	String  string  = ""
	Bool    bool    = false
	Int     int     = 0
	Float32 float32 = 0
	Float64 float64 = 0
)

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

func CopyWithOption(toValue interface{}, fromValue interface{}, opts ...Opt) (err error)

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

type FieldNameMapping struct {
	SrcType interface{}
	DstType interface{}
	Mapping map[string]string
}

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 WithDeepCopy

func WithDeepCopy() Opt

WithDeepCopy will copy the fields deeply.

func WithFieldNameMapping

func WithFieldNameMapping(fieldNameMapping ...FieldNameMapping) Opt

WithFieldNameMapping are the field name mappings.

func WithIgnoreEmpty

func WithIgnoreEmpty() Opt

WithIgnoreEmpty will ignore empty fields.

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.

func DefaultOpts

func DefaultOpts() *Opts

DefaultOpts are the default options for the copy.

func (*Opts) Configure

func (o *Opts) Configure(opts ...Opt)

Configure the copy.

type TypeConverter

type TypeConverter struct {
	SrcType interface{}
	DstType interface{}
	Fn      func(src interface{}) (dst interface{}, err error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL