copy

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2020 License: Apache-2.0 Imports: 6 Imported by: 3

README

Package for fast copying structs of different types

GoDev Go Report Card Mentioned in Awesome Go

This package is meant to make copying of structs to/from others structs a bit easier.

Nested structures, embedded types, pointers, sql null types are supported.

Installation

go get -u github.com/gotidy/copy

Example

type Person struct {
    Name       string
    MiddleName *string
    Surname    string
}

type User struct {
    Person
    Email   string
    Age     int8
    Married bool
}

type Employee struct {
    Name       string
    MiddleName string
    Surname    string
    Email      string
    Age        int
}

src := User{
    Person: Person{
        Name:       "John",
        MiddleName: nil,
        Surname:    "Smith",
    },
    Email:   "john.smith@joy.me",
    Age:     33,
    Married: false,
}
dst := Employee{}

copiers := copy.New() // New("json")
copiers.Copy(&dst, &src)

// Or more fast use case is to create the type specific copier.

copier := copiers.Get(&Employee{}, &User{}) // Created once for a pair of types.
copier.Copy(&dst, &src)

Alternative projects

Benchmark

Benchmarks source code can be found here

go test -bench=. -benchmem ./...
goos: darwin
goarch: amd64
pkg: github.com/gotidy/copy-bench
BenchmarkManualCopy-12         177310519         6.92 ns/op          0 B/op        0 allocs/op
BenchmarkCopiers-12             13476417         84.1 ns/op          0 B/op        0 allocs/op
BenchmarkCopier-12              40226689         27.5 ns/op          0 B/op        0 allocs/op
BenchmarkJinzhuCopier-12          407480         2711 ns/op       2480 B/op       34 allocs/op
BenchmarkDeepcopier-12            262836         4346 ns/op       4032 B/op       73 allocs/op
PASS
ok      github.com/gotidy/copy-bench    6.922s

See the documentation for more information.

License

Apache 2.0

Documentation

Overview

Package copy provides a copy library to make copying of structs to/from others structs a bit easier.

Package copy provides a copy library to make copying of structs to/from others structs a bit easier.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(dst, src interface{})

Copy copies the contents of src into dst. Dst and src each must be a pointer to a struct.

func Prepare added in v0.2.0

func Prepare(dst, src interface{})

Prepare caches structures of src and dst. Dst and src each must be a pointer to struct. contents is not copied. It can be used for checking ability of copying.

copy.Prepare(&dst, &src)

func PtrOf added in v0.5.0

func PtrOf(i interface{}) unsafe.Pointer

Types

type BaseCopier added in v0.5.1

type BaseCopier struct {
	*Copiers
	// contains filtered or unexported fields
}

func NewBaseCopier added in v0.5.1

func NewBaseCopier(c *Copiers) BaseCopier

type Copier

type Copier interface {
	Copy(dst interface{}, src interface{})
}

StructCopier fills a destination from source.

func Get added in v0.2.3

func Get(dst, src interface{}) Copier

Get Copier for a specific destination and source.

type Copiers added in v0.2.0

type Copiers struct {
	// contains filtered or unexported fields
}

Copiers is a structs copier.

func New

func New(options ...Option) *Copiers

New create new internalCopier.

func (*Copiers) Copy added in v0.2.0

func (c *Copiers) Copy(dst, src interface{})

Copy copies the contents of src into dst. Dst and src each must be a pointer to struct.

func (*Copiers) Get added in v0.2.0

func (c *Copiers) Get(dst, src interface{}) Copier

Get Copier for a specific destination and source.

func (*Copiers) Prepare added in v0.2.0

func (c *Copiers) Prepare(dst, src interface{})

Prepare caches structures of src and dst. Dst and src each must be a pointer to struct. contents is not copied. It can be used for checking ability of copying.

c := copy.New()
c.Prepare(&dst, &src)

type Option added in v0.2.0

type Option func(c *Options)

Option changes default Copiers parameters.

func Skip added in v0.2.2

func Skip() Option

Skip nonassignable types else cause panic.

func Tag added in v0.2.2

func Tag(tag string) Option

Tag set tag name.

type Options added in v0.2.0

type Options struct {
	Tag  string
	Skip bool
}

Options is Copiers parameters.

type PValueToPValueCopier added in v0.6.0

type PValueToPValueCopier struct {
	BaseCopier
	// contains filtered or unexported fields
}

func NewPValueToPValueCopier added in v0.6.0

func NewPValueToPValueCopier(c *Copiers) *PValueToPValueCopier

func (*PValueToPValueCopier) Copy added in v0.6.0

func (c *PValueToPValueCopier) Copy(dst, src interface{})

type PValueToValueCopier added in v0.6.0

type PValueToValueCopier struct {
	BaseCopier
	// contains filtered or unexported fields
}

func NewPValueToValueCopier added in v0.6.0

func NewPValueToValueCopier(c *Copiers) *PValueToValueCopier

func (*PValueToValueCopier) Copy added in v0.6.0

func (c *PValueToValueCopier) Copy(dst, src interface{})

type SliceCopier added in v0.6.0

type SliceCopier struct {
	BaseCopier
	// contains filtered or unexported fields
}

func NewSliceCopier added in v0.6.0

func NewSliceCopier(c *Copiers) *SliceCopier

func (*SliceCopier) Copy added in v0.6.0

func (c *SliceCopier) Copy(dst, src interface{})

Copy copies the contents of src into dst. Dst and src each must be a pointer to struct.

type StructCopier added in v0.5.0

type StructCopier struct {
	BaseCopier
	// contains filtered or unexported fields
}

StructCopier fills a destination from source.

func NewStructCopier added in v0.5.0

func NewStructCopier(c *Copiers) *StructCopier

func (*StructCopier) Copy added in v0.5.0

func (c *StructCopier) Copy(dst, src interface{})

Copy copies the contents of src into dst. Dst and src each must be a pointer to struct.

type Type added in v0.5.0

type Type = unsafe.Pointer

func DataOf added in v0.5.0

func DataOf(i interface{}) (typ Type, data unsafe.Pointer)

func TypeOf added in v0.5.0

func TypeOf(i interface{}) (typ Type)

type TypeInfo added in v0.5.1

type TypeInfo struct {
	Type Type
	Name string
}

func NewTypeInfo added in v0.5.1

func NewTypeInfo(typ reflect.Type) TypeInfo

func (TypeInfo) Check added in v0.5.1

func (t TypeInfo) Check(typ Type) bool

type ValueKind added in v0.5.0

type ValueKind int
const (
	UnknownKind    ValueKind = 0
	StructValue    ValueKind = 1
	SliceValue     ValueKind = 2
	MapValue       ValueKind = 3
	PtrValue       ValueKind = 0b10
	StructPtrValue ValueKind = StructValue + PtrValue
	SlicePtrValue  ValueKind = SliceValue + PtrValue
	MapPtrValue    ValueKind = MapValue + PtrValue
)

type ValueToPValueCopier added in v0.6.0

type ValueToPValueCopier struct {
	BaseCopier
	// contains filtered or unexported fields
}

func NewValueToPValueCopier added in v0.6.0

func NewValueToPValueCopier(c *Copiers) *ValueToPValueCopier

func (*ValueToPValueCopier) Copy added in v0.6.0

func (c *ValueToPValueCopier) Copy(dst, src interface{})

Directories

Path Synopsis
examples
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
internal

Jump to

Keyboard shortcuts

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