Skip to content

Yet a one more deep-copy&traverse package for Go, but this time with capability of custom processing (like removing secrets from your data)

License

Notifications You must be signed in to change notification settings

xaionaro-go/object

Repository files navigation

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}

About

Yet a one more deep-copy&traverse package for Go, but this time with capability of custom processing (like removing secrets from your data)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages