copier

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertField added in v0.0.8

func ConvertField[Src any, Dst any](field string, converter converter.Converter[Src, Dst]) option.Option[options]

func CopyTo

func CopyTo(src any, dst any) error

CopyTo 复制结构体, 纯递归实现. src 和 dst 都必须是结构体的指针

func IgnoreFields added in v0.0.8

func IgnoreFields(fields ...string) option.Option[options]

IgnoreFields 设置复制时要忽略的字段(option 设计模式)

Types

type Copier

type Copier[Src any, Dst any] interface {
	// CopyTo 将 src 中的数据复制到 dst 中
	CopyTo(src *Src, dst *Dst, opts ...option.Option[options]) error
	// Copy 将创建一个 Dst 的实例,并且将 Src 中的数据复制过去
	Copy(src *Src, opts ...option.Option[options]) (*Dst, error)
}

Copier 复制数据 1. 深拷贝亦或是浅拷贝,取决于具体的实现。每个实现都要声明清楚这一点; 2. Src 和 Dst 都必须是普通的结构体,支持组合 3. 只复制公共字段 这种设计设计,即使用 *Src 和 *Dst 可能加剧内存逃逸

type ReflectCopier

type ReflectCopier[Src any, Dst any] struct {
	// contains filtered or unexported fields
}

ReflectCopier 基于反射的实现 ReflectCopier 是浅拷贝

func NewReflectCopier

func NewReflectCopier[Src any, Dst any](opts ...option.Option[options]) (*ReflectCopier[Src, Dst], error)

NewReflectCopier 如果类型不匹配, 创建时直接检查报错.

func (*ReflectCopier[Src, Dst]) Copy

func (r *ReflectCopier[Src, Dst]) Copy(src *Src, opts ...option.Option[options]) (*Dst, error)

func (*ReflectCopier[Src, Dst]) CopyTo

func (r *ReflectCopier[Src, Dst]) CopyTo(src *Src, dst *Dst, opts ...option.Option[options]) error

CopyTo 执行复制 执行复制的逻辑是: 1. 按照字段的映射关系进行匹配 2. 如果 Src 和 Dst 中匹配的字段,其类型是基本类型(及其指针)或者内置类型(及其指针),并且类型一样,则直接用 Src 的值 3. 如果 Src 和 Dst 中匹配的字段,其类型都是结构体,或者都是结构体指针,则会深入复制 4. 否则,忽略字段

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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