Documentation
¶
Index ¶
- Constants
- Variables
- func AddStepKind(dstIfSrcElseSrc reflect.Kind, args ...any)
- func AddStepType(dstIfSrcElseSrc reflect.Type, args ...any)
- func CheckDisallowIface[TYPE any](t reflect.Type) bool
- func CheckDisallowKind[TYPE any](t reflect.Type) bool
- func CheckDisallowType[TYPE any](t reflect.Type) bool
- func Copy(ctx context.Context, dst any, src any, opts ...func(*State)) (err error)
- func CopyT[TYP any, PTR any](ctx context.Context, dst *PTR, src TYP, opts ...func(*State)) (err error)
- func Flag(flag CloneFlag, reset ...bool) func(s *State)
- func SharedStateContext(ctx context.Context, opts ...func(s *State)) context.Context
- func StructFieldsForClone(s *State, typ reflect.Type) iter.Seq[reflect.StructField]
- func WrapStep(fn func(*State, Step) Step) func(s *State)
- type AllowList
- type BaseStep
- type ClonableCheckFunc
- type ClonableCheckTypeFunc
- type ClonableCheckValueFunc
- type CloneFlag
- type FuncStep
- type InitStep
- type InterfaceStep
- type MapStep
- type PointerStep
- type Registry
- type ResettableRegistry
- type SliceStep
- type State
- func (s *State) Cache() ResettableRegistry
- func (s *State) MakeMap(oldPtr reflect.Value, dstVal reflect.Value, newTyp reflect.Type, _len int) (newOrCached reflect.Value, wasCached bool)
- func (s *State) MakeSlice(oldPtr reflect.Value, dstVal reflect.Value, newTyp reflect.Type, l int) (n reflect.Value, wasCached bool)
- func (s *State) New(keyPtr reflect.Value, dstVal reflect.Value, newTyp reflect.Type) (newOrCached reflect.Value, cached bool)
- func (s *State) Step(dstIfSrcElseSrc reflect.Type, src reflect.Type) (Step, bool)
- func (s *State) StepCopy(ctx context.Context, step Step, dst, src reflect.Value) error
- func (s *State) StepInit(ctx context.Context, dst, src reflect.Type) (step Step, err error)
- type Step
- type StructStep
- type StructToMapStep
- type ToArrayStep
- type UUIDStep
Constants ¶
const ( CodePointerError errors.GoCode = "PointerError" CodeNoSteps errors.GoCode = "NoSteps" CodeInvalid errors.GoCode = "Invalid" )
const STRUCT_TAG = "clone"
const (
UNKNOWN_FLAG_FALLBACK = true
)
Variables ¶
var ( ErrNotPointer = errors.New(CodePointerError, "value is not a pointer") ErrNoSteps = errors.New(CodeNoSteps, "no step found") ErrInvalid = errors.New(CodeInvalid, "invalid value") )
var OK = NewAllowList()
Functions ¶
func AddStepKind ¶
func AddStepType ¶
func CheckDisallowIface ¶
disallowed if provided reflect.Type implements [T]
func CheckDisallowKind ¶
disallowed if provided reflect.Type is the same kind as [TYPE]
func CheckDisallowType ¶
disallowed if provided reflect.Type is the same kind as [TYPE]
func Copy ¶
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 ¶
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 ¶
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 ¶
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)
type ClonableCheckFunc ¶
type ClonableCheckFunc interface {
ClonableCheckValueFunc | ClonableCheckTypeFunc
}
type ClonableCheckTypeFunc ¶
type CloneFlag ¶
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 InterfaceStep ¶
type InterfaceStep struct {
// contains filtered or unexported fields
}
type MapStep ¶
type MapStep struct {
// contains filtered or unexported fields
}
map => map/interface
type PointerStep ¶
type PointerStep struct {
// contains filtered or unexported fields
}
type ResettableRegistry ¶
var NopRegistry ResettableRegistry = nopRegistry{}
type SliceStep ¶
type SliceStep struct {
// contains filtered or unexported fields
}
type State ¶
type State struct {
Flags CloneFlag
// contains filtered or unexported fields
}
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].
type StructStep ¶
type StructStep struct {
// contains filtered or unexported fields
}
type StructToMapStep ¶
type StructToMapStep struct {
// contains filtered or unexported fields
}
struct => map