deepcopy

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package deepcopy implements deep copy and deep translate of a value.

It is extended version of https://gist.github.com/hvoecking/10772475.

TranslateFn can be used to modify value on copying.

CustomDeepCopyMethod can be defined on a type, for example, to copy unexported fields. See "github.com/keboola/go-utils/pkg/orderedmap" package for example of CustomDeepCopyMethod.

Index

Examples

Constants

View Source
const CustomDeepCopyMethod = "HandleDeepCopy"

CustomDeepCopyMethod is name of the method that handles deep copy for the type.

Variables

This section is empty.

Functions

func Copy

func Copy(value any) any

Copy makes deep copy of the value.

Example
original := map[string]any{"foo": &Bar{Key1: "abc", Key2: "def", Key3: 123}}
clone := Copy(original).(map[string]any)

fmt.Printf("pointers are different:  original.foo ==  clone.foo -> %t\n", original["foo"] == clone["foo"])
fmt.Printf("values are same:        *original.foo == *clone.foo -> %t\n", *original["foo"].(*Bar) == *clone["foo"].(*Bar))
Output:

pointers are different:  original.foo ==  clone.foo -> false
values are same:        *original.foo == *clone.foo -> true

func CopyTranslate

func CopyTranslate(value any, callback TranslateFn) any

CopyTranslate makes deep copy of the value, each value is translated by TranslateFn.

Example
original := map[string]any{"foo": &Bar{Key1: "abc", Key2: "def", Key3: 123}}
clone := CopyTranslate(original, func(original, clone reflect.Value, path Path) {
	fmt.Printf("Copying: %s\n", path)
	// Uppercase each string
	if clone.Kind() == reflect.String {
		value := strings.ToUpper(clone.Interface().(string))
		clone.Set(reflect.ValueOf(value))
	}
}).(map[string]any)

// Dump clone
fmt.Println()
fmt.Println("Clone dump:")
s := spew.NewDefaultConfig()
s.DisablePointerAddresses = true
s.Dump(clone)
Output:

Copying: map[foo].<key>.string
Copying: map[foo].interface[*deepcopy_test.Bar].*deepcopy_test.Bar[Key1].string
Copying: map[foo].interface[*deepcopy_test.Bar].*deepcopy_test.Bar[Key2].string
Copying: map[foo].interface[*deepcopy_test.Bar].*deepcopy_test.Bar[Key3].interface[int].int
Copying: map[foo].interface[*deepcopy_test.Bar].*deepcopy_test.Bar[Key3].interface
Copying: map[foo].interface[*deepcopy_test.Bar].*struct
Copying: map[foo].interface[*deepcopy_test.Bar].ptr
Copying: map[foo].interface
Copying: map

Clone dump:
(map[string]interface {}) (len=1) {
 (string) (len=3) "FOO": (*deepcopy_test.Bar)({
  Key1: (string) (len=3) "ABC",
  Key2: (string) (len=3) "DEF",
  Key3: (int) 123
 })
}

func CopyTranslateSteps

func CopyTranslateSteps(value any, callback TranslateFn, path Path, visited VisitedPtrMap) any

CopyTranslateSteps makes deep copy of the value, each value is translated by TranslateFn. VisitedPtrMap allows you to connect copy to another copy operation and reuse pointers.

func DeepEqualNotSame

func DeepEqualNotSame(t *testing.T, a, b any, path string)

DeepEqualNotSame checks that a and b values are deep equally, but do not contain any pointer to same value.

Types

type CloneFn

type CloneFn func(clone reflect.Value)

CloneFn is custom implementation of deepcopy for a type, it is returned from CustomDeepCopyMethod.

type InterfaceStep

type InterfaceStep struct {
	TargetType reflect.Type
}

InterfaceStep - interface dereference.

func (InterfaceStep) String

func (v InterfaceStep) String() string

type MapKeyStep

type MapKeyStep struct {
	Key any
}

MapKeyStep - key in a map.

func (MapKeyStep) String

func (v MapKeyStep) String() string

type MapKeyValueStep

type MapKeyValueStep struct {
	Key any
}

MapKeyValueStep - value in a map.

func (MapKeyValueStep) String

func (v MapKeyValueStep) String() string

type Path

type Path []fmt.Stringer

Path to a nested value.

func (Path) Add

func (s Path) Add(step fmt.Stringer) Path

Add step.

func (Path) String

func (s Path) String() string

type PointerStep

type PointerStep struct{}

PointerStep - pointer dereference.

func (PointerStep) String

func (v PointerStep) String() string

type SliceIndexStep

type SliceIndexStep struct {
	Index int
}

SliceIndexStep - index in a slice.

func (SliceIndexStep) String

func (v SliceIndexStep) String() string

type StructFieldStep

type StructFieldStep struct {
	CurrentType reflect.Type
	Field       string
}

StructFieldStep - field in a struct.

func (StructFieldStep) String

func (v StructFieldStep) String() string

type TranslateFn

type TranslateFn func(original, clone reflect.Value, path Path)

TranslateFn is custom translate function to modify values on copying.

type TypeStep

type TypeStep struct {
	CurrentType string
}

TypeStep - type information.

func (TypeStep) String

func (v TypeStep) String() string

type VisitedPtrMap

type VisitedPtrMap map[uintptr]*reflect.Value

VisitedPtrMap maps pointer from original value to cloned value. Example: If, in original value A, is 3x a pointer that point to the value B. Then, in the cloned value AC, there will be 3x pointer to the cloned value BC.

Jump to

Keyboard shortcuts

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