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 ¶
- Constants
- func Copy(value any) any
- func CopyTranslate(value any, callback TranslateFn) any
- func CopyTranslateSteps(value any, callback TranslateFn, path Path, visited VisitedPtrMap) any
- func DeepEqualNotSame(t *testing.T, a, b any, path string)
- type CloneFn
- type InterfaceStep
- type MapKeyStep
- type MapKeyValueStep
- type Path
- type PointerStep
- type SliceIndexStep
- type StructFieldStep
- type TranslateFn
- type TypeStep
- type VisitedPtrMap
Examples ¶
Constants ¶
const CustomDeepCopyMethod = "HandleDeepCopy"
CustomDeepCopyMethod is name of the method that handles deep copy for the type.
Variables ¶
This section is empty.
Functions ¶
func Copy ¶
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.
Types ¶
type CloneFn ¶
CloneFn is custom implementation of deepcopy for a type, it is returned from CustomDeepCopyMethod.
type InterfaceStep ¶
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 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 ¶
StructFieldStep - field in a struct.
func (StructFieldStep) String ¶
func (v StructFieldStep) String() string
type TranslateFn ¶
TranslateFn is custom translate function to modify values on copying.
type VisitedPtrMap ¶
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.