clone

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: GPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodePointerError errors.GoCode = "PointerError"
	CodeNoSteps      errors.GoCode = "NoSteps"
	CodeInvalid      errors.GoCode = "Invalid"
)
View Source
const STRUCT_TAG = "clone"
View Source
const (
	UNKNOWN_FLAG_FALLBACK = true
)

Variables

View Source
var (
	ErrNotPointer = errors.New(CodePointerError, "value is not a pointer")
	ErrNoSteps    = errors.New(CodeNoSteps, "no step found")
	ErrInvalid    = errors.New(CodeInvalid, "invalid value")
)

Functions

func AddStepKind

func AddStepKind(dstIfSrcElseSrc reflect.Kind, args ...any)

func AddStepType

func AddStepType(dstIfSrcElseSrc reflect.Type, args ...any)

func CheckDisallowIface

func CheckDisallowIface[TYPE any](t reflect.Type) bool

disallowed if provided reflect.Type implements [T]

func CheckDisallowKind

func CheckDisallowKind[TYPE any](t reflect.Type) bool

disallowed if provided reflect.Type is the same kind as [TYPE]

func CheckDisallowType

func CheckDisallowType[TYPE any](t reflect.Type) bool

disallowed if provided reflect.Type is the same kind as [TYPE]

func Copy

func Copy(ctx context.Context, dst any, src any, opts ...func(*State)) (err error)

Copy value src into pointer dst

Options can be provided to change the state and behaviour

func CopyT

func CopyT[TYP any, PTR any](ctx context.Context, dst *PTR, src TYP, opts ...func(*State)) (err error)

Copy value src into pointer dst

Options can be provided to change the state and behaviour

func Flag

func Flag(flag CloneFlag, reset ...bool) func(s *State)

Allow for enabling and disabling certain features

Setting some of these flags can have an impact on performance and/or functionality of the [Clone] function.

func SharedStateContext

func SharedStateContext(ctx context.Context, opts ...func(s *State)) context.Context

A shared state can be used to provide a shared step cache, optional shared pointer cache and any other options that provide functionality to the state.

The [Clone] function can also provide it's own options to mutate **A COPY OF** the shared state.

func StructFieldsForClone

func StructFieldsForClone(s *State, typ reflect.Type) iter.Seq[reflect.StructField]

func WrapStep

func WrapStep(fn func(*State, Step) Step) func(s *State)

Allow for wrapping of steps to perform actions pre/post init and pre/post copy.

Types

type AllowList

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

func NewAllowList

func NewAllowList() AllowList

func (*AllowList) Check

func (a *AllowList) Check[FUNC ClonableCheckFunc](fn FUNC)

func (AllowList) Type

func (a AllowList) Type(t reflect.Type) (allows bool)

func (AllowList) Value

func (a AllowList) Value(ctx context.Context, val reflect.Value) bool

type BaseStep

type BaseStep struct{}

func (BaseStep) Copy

func (f BaseStep) Copy(ctx context.Context, s *State, d, i reflect.Value) error

func (BaseStep) Init

func (f BaseStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (Step, error)

type ClonableCheckFunc

type ClonableCheckFunc interface {
	ClonableCheckValueFunc | ClonableCheckTypeFunc
}

type ClonableCheckTypeFunc

type ClonableCheckTypeFunc = func(reflect.Type) bool

type ClonableCheckValueFunc

type ClonableCheckValueFunc = func(reflect.Value, reflect.Type) bool

type CloneFlag

type CloneFlag = bitcheck.Flag
const (
	CF_INVALID       CloneFlag = iota
	CF_NOWRAP        CloneFlag = 1 << iota // don't allow wrapping, even if a wrap function is provided
	CF_NOVALIDATE                          // don't validate if dst is actually settable
	CF_KEEP_POINTERS                       // don't reset the pointer cache after before copy with a shared state
	CF_ONLY_PUB_FLDS                       // only public (non exported) struct fields can be cloned.
	CF_NO_CONVS                            // don't allow conversions from, int32 to int64 (singular example)
)

type FuncStep

type FuncStep func(s *State, dst, src reflect.Value) error

func (FuncStep) Copy

func (f FuncStep) Copy(ctx context.Context, s *State, d, i reflect.Value) error

type InitStep

type InitStep interface {
	Step
	Init(ctx context.Context, s *State, dst, src reflect.Type) (Step, error)
}

type InterfaceStep

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

func (InterfaceStep) Copy

func (f InterfaceStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) (err error)

func (InterfaceStep) Init

func (f InterfaceStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (_ Step, err error)

type MapStep

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

map => map/interface

func (MapStep) Copy

func (f MapStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) (err error)

func (MapStep) Init

func (m MapStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (step Step, err error)

type PointerStep

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

func (PointerStep) Copy

func (f PointerStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) error

func (PointerStep) Init

func (f PointerStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (_ Step, err error)

type Registry

type Registry interface {
	AddStepType(dstIfSrcElseSrc reflect.Type, src ...any)
	AddStepKind(dstIfSrcElseSrc reflect.Kind, src ...any)
	Step(dst reflect.Type, src reflect.Type) (Step, bool)
}

type ResettableRegistry

type ResettableRegistry interface {
	Registry
	Reset() int
}
var NopRegistry ResettableRegistry = nopRegistry{}

type SliceStep

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

func (SliceStep) Copy

func (f SliceStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) error

func (SliceStep) Init

func (f SliceStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (step Step, err error)

type State

type State struct {
	Flags CloneFlag
	// contains filtered or unexported fields
}

func StateFromContext

func StateFromContext(ctx context.Context) (*State, bool)

func (*State) Cache

func (s *State) Cache() ResettableRegistry

func (*State) MakeMap

func (s *State) MakeMap(oldPtr reflect.Value, dstVal reflect.Value, newTyp reflect.Type, _len int) (newOrCached reflect.Value, wasCached bool)

Initialize a new reflect.Value of kind reflect.Map with type [newTyp]

[keyPtr] is used to generate a cache key to properly clone any references that share the same pointer.

If dstVal is a pointer, and it's [Elem] method returns a value of type [newTyp] then State.New assumes it is safe to return (and cache) [dstVal].

func (*State) MakeSlice

func (s *State) MakeSlice(oldPtr reflect.Value, dstVal reflect.Value, newTyp reflect.Type, l int) (n reflect.Value, wasCached bool)

Initialize a new reflect.Value of kind reflect.Array or reflect.Slice with type [newTyp]

[keyPtr] is used to generate a cache key to properly clone any references that share the same pointer.

If dstVal is a pointer, and it's [Elem] method returns a value of type [newTyp] then State.New assumes it is safe to return (and cache) [dstVal].

If dstVal can be used, the slice is grown to length [l], with it's capacity and len set to [l].

func (*State) New

func (s *State) New(keyPtr reflect.Value, dstVal reflect.Value, newTyp reflect.Type) (newOrCached reflect.Value, cached bool)

Initialize a new reflect.Value of type [newTyp]

[keyPtr] is used to generate a cache key to properly clone any references that share the same pointer.

If dstVal is a pointer, and it's [Elem] method returns a value of type [newTyp] then State.New assumes it is safe to return (and cache) [dstVal].

func (*State) Step

func (s *State) Step(dstIfSrcElseSrc reflect.Type, src reflect.Type) (Step, bool)

func (*State) StepCopy

func (s *State) StepCopy(ctx context.Context, step Step, dst, src reflect.Value) error

func (*State) StepInit

func (s *State) StepInit(ctx context.Context, dst, src reflect.Type) (step Step, err error)

type Step

type Step interface {
	Copy(ctx context.Context, s *State, dst, src reflect.Value) error
}

type StructStep

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

func (StructStep) Copy

func (f StructStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) error

func (StructStep) Init

func (f StructStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (Step, error)

type StructToMapStep

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

struct => map

func (StructToMapStep) Copy

func (f StructToMapStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) error

func (StructToMapStep) Init

func (m StructToMapStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (step Step, err error)

type ToArrayStep

type ToArrayStep struct {
	SliceStep
}

func (ToArrayStep) Copy

func (f ToArrayStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) error

func (ToArrayStep) Init

func (f ToArrayStep) Init(ctx context.Context, s *State, dst, src reflect.Type) (step Step, err error)

type UUIDStep

type UUIDStep struct{}

func (UUIDStep) Copy

func (f UUIDStep) Copy(ctx context.Context, s *State, dst, src reflect.Value) error

Jump to

Keyboard shortcuts

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