Categorygithub.com/xaionaro-go/object
repositorypackage
0.0.0-20241026212449-753ce10ec94c
Repository: https://github.com/xaionaro-go/object.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# 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}