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).
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}
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 }
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}