scrub

package module
v0.0.0-...-1dca247 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2024 License: MIT Imports: 2 Imported by: 0

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
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)
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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NamedFields

func NamedFields(src any, names ...string)

NamedFields takes a struct and sets all fields with the given names to their zero value. This is useful when you want to scrub a struct type from a package that you don't control.

This function is a no-op for non-struct types.

func TaggedFields

func TaggedFields(src any)

TaggedFields takes a struct and recursively sets all fields annotated with a `scrub:"true"` struct tag to their zero value. This is useful when you control the struct definition.

This function is a no-op for non-struct types.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL