https://github.com/acj/scrub
Recursively walk a Go struct and zero out specific fields
https://github.com/acj/scrub
go recursion sanitize struct testing walk
Last synced: over 1 year ago
JSON representation
Recursively walk a Go struct and zero out specific fields
- Host: GitHub
- URL: https://github.com/acj/scrub
- Owner: acj
- License: mit
- Created: 2024-01-27T17:05:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-09T01:40:23.000Z (about 2 years ago)
- Last Synced: 2025-01-04T09:08:47.698Z (over 1 year ago)
- Topics: go, recursion, sanitize, struct, testing, walk
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# scrub
Recursively set specific struct fields to their zero values
Possible use cases:
- scrubbing sensitive data from structs before logging
- comparing structs with noisy fields (timestamps, random values, etc) for testing or diagnostic purposes
## Examples
### Using struct tags
```go
package main
import (
"fmt"
"github.com/acj/scrub"
)
type User struct {
Name string
Age int `scrub:"true"`
}
func main() {
user := User{
Name: "Wall-E",
Age: 22,
}
scrub.TaggedFields(&user)
fmt.Printf("%+v\n", user) // {Name:Wall-E Age:0}
}
```
### Using named fields (blocklist)
```go
package main
import (
"fmt"
"github.com/acj/scrub"
)
type User struct {
Name string
Age int
}
func main() {
user := User{
Name: "Wall-E",
Age: 22,
}
scrub.NamedFields(&user, "Age")
fmt.Printf("%+v\n", user) // {Name:Wall-E Age:0}
}
```
## License
MIT