Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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