Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 11 days 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 (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-09T01:40:23.000Z (7 months ago)
- Last Synced: 2024-06-21T17:03:06.210Z (5 months 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 mainimport (
"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 mainimport (
"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