core

package
v5.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareValues

func CompareValues(v1, v2 reflect.Value, op string, ignoreCase bool) (bool, error)

func ConvertValue

func ConvertValue(v reflect.Value, targetType reflect.Type) reflect.Value

func ConvertValueChecked added in v5.5.0

func ConvertValueChecked(v reflect.Value, targetType reflect.Type) (reflect.Value, error)

ConvertValueChecked is ConvertValue with a verdict: it reports an error instead of handing back a value the caller would panic on when setting. Patch values routinely come from untrusted input (a JSON document from a peer), so a type mismatch has to be an error, never a panic.

func Copy

func Copy[T any](src T, opts ...CopyOption) (T, error)

Copy creates a deep copy of src. It returns the copy and a nil error in case of success and the zero value for the type and a non-nil error on failure.

It correctly handles cyclic references and unexported fields.

func CopyShared added in v5.11.0

func CopyShared(src any, pointers PointersMap) (any, error)

CopyShared deep-copies src recording into (and honouring) the caller's pointers map instead of a private one. Passing the same map across several copies makes them one copy for sharing purposes: a value any of them has already copied is reused, not copied again. Generated Clone code threads its memo through here for the fields it cannot copy itself.

func DeepCopyValue

func DeepCopyValue(v reflect.Value) reflect.Value

func Dereference

func Dereference(v reflect.Value) (reflect.Value, error)

func Equal

func Equal[T any](a, b T, opts ...EqualOption) bool

Equal performs a deep equality check between a and b.

func EscapeKey

func EscapeKey(key string) string

func ExtractKey

func ExtractKey(v reflect.Value, fieldIdx int) any

func GetKeyField

func GetKeyField(typ reflect.Type) (int, bool)

func JoinPath

func JoinPath(parent, child string) string

JoinPath joins two JSON Pointer paths with a slash.

func MustCopy

func MustCopy[T any](src T, opts ...CopyOption) T

MustCopy creates a deep copy of src. It returns the copy on success or panics in case of any failure.

It correctly handles cyclic references and unexported fields.

func NormalizePath

func NormalizePath(path string) string

NormalizePath converts a dot-notation or JSON Pointer path to a standard JSON Pointer.

func RegisterCustomCopy

func RegisterCustomCopy(typ reflect.Type, fn reflect.Value)

RegisterCustomCopy registers a custom copy function for a specific type. The function must be of type func(src T) (T, error).

func RegisterCustomEqual

func RegisterCustomEqual(typ reflect.Type, fn reflect.Value)

RegisterCustomEqual registers a custom equality function for a specific type. The function must be of type func(a, b T) bool.

func SetValue

func SetValue(v, newVal reflect.Value)

func UnescapeKey added in v5.4.0

func UnescapeKey(token string) string

UnescapeKey reverses EscapeKey, turning the RFC 6901 escape sequences "~1" and "~0" back into "/" and "~". The order (slash first) matters: unescaping "~0" first would turn "~01" into "~1" and then wrongly into "/".

func ValueEqual

func ValueEqual(a, b reflect.Value, config *equalConfig) bool

ValueEqual performs a deep equality check between two reflect.Values.

func ValueToInterface

func ValueToInterface(v reflect.Value) any

Types

type Copier

type Copier[T any] interface {
	// Copy returns a deep copy of the receiver.
	Copy() (T, error)
}

Copier is an interface that types can implement to provide their own custom deep copy logic. The type T in Copy() (T, error) must be the same concrete type as the receiver that implements this interface.

type CopyOption

type CopyOption interface {
	// contains filtered or unexported methods
}

CopyOption allows configuring the behavior of the Copy function.

func CopyIgnorePath

func CopyIgnorePath(path string) CopyOption

CopyIgnorePath returns an option that tells Copy to ignore the specified path. The ignored path will have the zero value for its type in the resulting copy.

func SkipUnsupported

func SkipUnsupported() CopyOption

SkipUnsupported returns an option that tells Copy to skip unsupported types (like non-nil functions or channels) instead of returning an error.

type DeepPath

type DeepPath string

DeepPath represents a path to a field or element within a structure. It supports JSON Pointers (RFC 6901) syntax like "/Field/SubField".

func (DeepPath) Delete

func (p DeepPath) Delete(v reflect.Value) error

func (DeepPath) Navigate

func (p DeepPath) Navigate(v reflect.Value, parts []PathPart) (reflect.Value, PathPart, error)

func (DeepPath) Resolve

func (p DeepPath) Resolve(v reflect.Value) (reflect.Value, error)

Resolve traverses v using the path and returns the reflect.Value found.

func (DeepPath) ResolveMember added in v5.11.0

func (p DeepPath) ResolveMember(v reflect.Value) (reflect.Value, error)

ResolveMember resolves the path to the member the last segment names — the struct field, map value, or slice element itself — without dereferencing it. Resolve returns the value a pointer member points at; alias operations need the pointer, because their whole purpose is to install that same reference somewhere else.

func (DeepPath) ResolveParent

func (p DeepPath) ResolveParent(v reflect.Value) (reflect.Value, PathPart, error)

func (DeepPath) ResolveParentPath

func (p DeepPath) ResolveParentPath() (DeepPath, PathPart, error)

ResolveParentPath splits the path into parent path and the last part.

func (DeepPath) Set

func (p DeepPath) Set(v reflect.Value, val reflect.Value) error

type EqualOption

type EqualOption interface {
	// contains filtered or unexported methods
}

EqualOption allows configuring the behavior of the Equal function.

func EqualIgnorePath

func EqualIgnorePath(path string) EqualOption

EqualIgnorePath returns an option that tells Equal to ignore the specified path.

type FieldInfo

type FieldInfo struct {
	Index   int
	Name    string
	JSONTag string
	Tag     StructTag
}

type PathPart

type PathPart struct {
	Key     string
	Index   int
	IsIndex bool
}

func ParseJSONPointer

func ParseJSONPointer(path string) []PathPart

func ParsePath

func ParsePath(path string) []PathPart

ParsePath parses a JSON Pointer path (RFC 6901).

type PointersMap added in v5.11.0

type PointersMap map[PointersMapKey]reflect.Value

PointersMap records the copy made for each value during one deep copy. It is exported so a copy can be run against a caller-provided map: generated Clone code and the reflection engine share one map, which is what keeps a value referenced from both sides copied exactly once.

type PointersMapKey added in v5.11.0

type PointersMapKey struct {
	Ptr uintptr
	Typ reflect.Type
}

PointersMapKey identifies one already-copied value: the address it lives at and the type it was reached as. The type matters because pointers of different types can share an address — a struct and its first field — and must not be mistaken for one another.

type StructTag

type StructTag struct {
	Ignore   bool
	ReadOnly bool
	Atomic   bool
	Key      bool
}

func ParseTag

func ParseTag(field reflect.StructField) StructTag

type TypeInfo

type TypeInfo struct {
	Fields        []FieldInfo
	KeyFieldIndex int
}

func GetTypeInfo

func GetTypeInfo(typ reflect.Type) *TypeInfo

type VisitKey

type VisitKey struct {
	A, B uintptr
	Typ  reflect.Type
}

Jump to

Keyboard shortcuts

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