https://github.com/xaionaro-go/object
Yet a one more deep-copy&traverse package for Go, but this time with capability of custom processing (like removing secrets from your data)
https://github.com/xaionaro-go/object
censor clean copy deep-copy deepcopy privacy remove-secrets
Last synced: about 1 year ago
JSON representation
Yet a one more deep-copy&traverse package for Go, but this time with capability of custom processing (like removing secrets from your data)
- Host: GitHub
- URL: https://github.com/xaionaro-go/object
- Owner: xaionaro-go
- License: cc0-1.0
- Created: 2024-10-22T16:11:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-26T21:24:51.000Z (over 1 year ago)
- Last Synced: 2024-12-18T08:40:15.193Z (over 1 year ago)
- Topics: censor, clean, copy, deep-copy, deepcopy, privacy, remove-secrets
- Language: Go
- Homepage:
- Size: 31.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# About
[](https://godoc.org/github.com/xaionaro-go/object)
[](https://goreportcard.com/report/github.com/xaionaro-go/object)
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
```go
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)
}
```
```sh
$ go run ./examples/object/
{true == true but there is a nuance}
```
### REMOVE MY SECRETS
```go
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)
}
```
```sh
$ go run ./examples/censoredvalue/
{true == true }
```
### CUSTOM PROCESSING
```go
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)
}
```
```sh
$ go run ./examples/customprocessing/
{true == false this is the nuance, sometimes}
```