dyad

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 7 Imported by: 1

README

Dyad

A Go module for making deep copies of arbitrary values.

Documentation Latest Version Build Status Code Coverage

Dyad makes clones of Go values. It attempts to make all types clonable, or fallback to predictable (and configurable) behavior when non-clonable types (such as channels) are encountered.

Documentation

Overview

Package dyad makes deep copies of arbitrary values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone[T any](src T, options ...Option) (dst T)

Clone returns a deep copy of src.

Example
package main

import (
	"fmt"

	"github.com/dogmatiq/dyad"
)

func main() {
	type Value struct {
		Data map[any]any
	}

	src := Value{
		Data: map[any]any{
			"key": "original value",
		},
	}

	dst := dyad.Clone(src)
	dst.Data["key"] = "altered value"

	fmt.Println(src.Data["key"])
	fmt.Println(dst.Data["key"])

}
Output:

original value
altered value

Types

type ChannelStrategy

type ChannelStrategy int

ChannelStrategy is an enumeration of strategies that can be used by Clone() when a channel is encountered.

const (
	// PanicOnChannel causes Clone() to panic when it encounters a
	// channel.
	//
	// This is the default behavior.
	PanicOnChannel ChannelStrategy = iota

	// ShareChannels causes Clone() to share the same channel between the
	// original and cloned values.
	ShareChannels

	// IgnoreChannels causes Clone() to use a nil value when a channel is
	// encountered.
	IgnoreChannels
)

type Option

type Option func(*cloneOptions)

An Option changes the behavior of a clone operation.

The signature of this function is not part of the public API and may change at any time without warning.

func WithChannelStrategy

func WithChannelStrategy(s ChannelStrategy) Option

WithChannelStrategy is an option that controls how Clone() behaves when it encounters a channel.

func WithUnexportedFieldStrategy

func WithUnexportedFieldStrategy(s UnexportedFieldStrategy) Option

WithUnexportedFieldStrategy is an option that controls how Clone() behaves when it encounters an unexported field.

type UnexportedFieldStrategy

type UnexportedFieldStrategy int

UnexportedFieldStrategy is an enumeration of strategies that can be used by Clone() when an unexported struct field is encountered.

const (
	// PanicOnUnexportedField causes Clone() to panic when it encounters an
	// unexported struct field.
	//
	// This is the default behavior.
	PanicOnUnexportedField UnexportedFieldStrategy = iota

	// CloneUnexportedFields causes Clone() to clone unexported struct fields
	// just as it would any other value.
	CloneUnexportedFields

	// IgnoreUnexportedFields causes Clone() to skip cloning of unexported
	// struct fields, leaving them as their zero value.
	IgnoreUnexportedFields
)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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