object

package module
v0.0.0-...-753ce10 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: CC0-1.0 Imports: 13 Imported by: 3

README

About

Go Reference Go Report Card

This package provides functions for deep copying an arbitrary object in Go. The difference of this deepcopier from others is that this allows to use a custom function that modifies the copied data. Personally I use it to erase all the secrets from my data while doing a copy (that in turn is used for logging).

How to use

JUST DEEP COPY
package main

import (
	"fmt"

	"github.com/xaionaro-go/object"
)

type myStruct struct {
	PublicData string
	SecretData string `secret:""`
}

func main() {
	value := myStruct{
		PublicData: "true == true",
		SecretData: "but there is a nuance",
	}

	censoredValue := object.DeepCopy(value)
	fmt.Println(censoredValue)
}

$ go run ./examples/object/
{true == true but there is a nuance}
REMOVE MY SECRETS
package main

import (
	"fmt"

	"github.com/xaionaro-go/object"
)

type myStruct struct {
	PublicData string
	SecretData string `secret:""`
}

func main() {
	value := myStruct{
		PublicData: "true == true",
		SecretData: "but there is a nuance",
	}

	censoredValue := object.DeepCopyWithoutSecrets(value)
	fmt.Println(censoredValue)
}
$ go run ./examples/censoredvalue/
{true == true }
CUSTOM PROCESSING
package main

import (
	"fmt"
	"reflect"

	"github.com/xaionaro-go/object"
)

type myStruct struct {
	PublicData string
	SecretData string `secret:""`
}

func main() {
	value := myStruct{
		PublicData: "true == true",
		SecretData: "but there is a nuance",
	}

	censoredValue := object.DeepCopy(value, object.OptionWithVisitorFunc(func(_ *object.ProcContext, v reflect.Value, sf *reflect.StructField) (reflect.Value, bool, error) {
		if sf == nil {
			return v, true, nil
		}
		switch sf.Name {
		case "PublicData":
			return reflect.ValueOf("true == false"), true, nil
		case "SecretData":
			return reflect.ValueOf("this is the nuance, sometimes"), true, nil
		}
		return v, true, nil
	}))
	fmt.Println(censoredValue)
}
$ go run ./examples/customprocessing/
{true == false this is the nuance, sometimes}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepCopy

func DeepCopy[T any](
	obj T,
	opts ...Option,
) T

DeepCopy returns a deep copy of the object.

Keep in mind, by default it does not copy unexported data (unless option `WithUnexported(true)` is provided).

func DeepCopyWithoutSecrets

func DeepCopyWithoutSecrets[T any](
	obj T,
	opts ...Option,
) T

DeepCopyWithoutSecrets returns a deep copy of the object, but with all fields tagged as `secret:""` reset to their zero values.

Keep in mind, this function does not censor: * the internals of: channels, function values, uintptr-s and unsafe.Pointer-s; * the keys of maps.

Also, it does not copy unexported data.

func RemoveSecrets

func RemoveSecrets[T any, PTR Pointer[T]](obj PTR)

RemoveSecrets returns zero-s all fields tagged as `secret:""`.

Keep in mind, this function does not zero: * the internals of: channels, function values, uintptr-s and unsafe.Pointer-s; * the keys of maps.

Also, it does not copy unexported data!

func Traverse

func Traverse(
	obj any,
	visitorFunc VisitorFunc,
) error

Traverse recursively traverses the object `obj`.

Types

type Hash

type Hash []byte

Hash is a hash of an value/object.

func CalcCryptoHash

func CalcCryptoHash(args ...any) (Hash, error)

CalcCryptoHash returns a cryptographically secure hash of an arbitrary set of values.

func (Hash) Equals

func (h Hash) Equals(b Hash) bool

Equals returns true if the hash is equal to the provided hash

func (Hash) Less

func (h Hash) Less(b Hash) bool

Less returns true if the hash is ordered earlier than the provided hash (could be useful for implementing sort.Interface).

type HashBuilder

type HashBuilder struct {
	HashValue     hash.Hash
	StableHashing bool
	// contains filtered or unexported fields
}

HashBuilder is the handler which converts a set of variables to a Hash.

func NewHashBuilderStable

func NewHashBuilderStable(hash hash.Hash) *HashBuilder

NewBuilderStable returns a new instance of HashBuilder that builds stable hashes (that do not change for the same object after each restart of the program).

func NewHashBuilderUnstable

func NewHashBuilderUnstable(hash hash.Hash) *HashBuilder

NewBuilderUnstable returns a new instance of HashBuilder that builds unstable hashes (that change for the same object after each restart of the program).

func (*HashBuilder) Reset

func (b *HashBuilder) Reset()

Reset resets the state of the hash.

func (*HashBuilder) ResetAndHash

func (b *HashBuilder) ResetAndHash(args ...any) (Hash, error)

ResetAndHash resets the state of the hash, calculates a new hash using given arguments, and returns it's value.

func (*HashBuilder) Result

func (b *HashBuilder) Result() []byte

Reset returns current hash.

func (*HashBuilder) Write

func (b *HashBuilder) Write(args ...any) error

Write adds more measurements to the current hash.

type Option

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

type OptionWithUnexported

type OptionWithUnexported bool

type OptionWithVisitorFunc

type OptionWithVisitorFunc VisitorFunc

type Options

type Options []Option

type Pointer

type Pointer[T any] interface {
	*T
}

Pointer is a constraint for pointers only.

type ProcContext

type ProcContext struct {

	// CustomData is overwritable and all the children in the tree
	// will receive this provided value.
	CustomData any
	// contains filtered or unexported fields
}

ProcContext is a structure provided to a callback on every call.

func (*ProcContext) Depth

func (ctx *ProcContext) Depth() uint

func (*ProcContext) Next

func (ctx *ProcContext) Next(pathPart string) *ProcContext

func (*ProcContext) Parent

func (ctx *ProcContext) Parent() *ProcContext

func (*ProcContext) Path

func (ctx *ProcContext) Path() string

type VisitorFunc

VisitorFunc is called on every node during a traversal.

Directories

Path Synopsis
examples
censoredvalue command
deepcopy command

Jump to

Keyboard shortcuts

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