pii

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 3 Imported by: 0

README

CI cov

pii — PII Masking & Data Sanitization Library for Go

Mask sensitive fields in Go structs at runtime or compile time. Supports names, emails, phones, and PANs with custom masker registration.


Pii generates Sanitize() methods for Go structs. Any field with a Sanitize() method gets sanitized automatically; everything else is copied as-is. The result is a clean, non-mutating sanitizer you can call before logging or serializing sensitive data.

Install

go install github.com/alifengineer/pii@latest

Quick Start

Define a struct using sanitize types:

package user

import "github.com/alifengineer/pii/sanitize"

//go:generate pii

type User struct {
    ID    int
    Name  sanitize.Name
    Phone sanitize.Phone
    Email sanitize.Email
}

Run go generate:

go generate ./...

Pii creates input_sanitize.go with:

func (v User) Sanitize() User {
    v.Name = v.Name.Sanitize()
    v.Phone = v.Phone.Sanitize()
    v.Email = v.Email.Sanitize()
    return v
}

Use it:

sanitized := user.Sanitize()
log.Println(sanitized) // {42 A**** S**** ***7890 a***@e***.com}

Built-in Sanitize Types

Type Example Input Sanitized Output
sanitize.Phone +1234567890 ***7890
sanitize.Email alice@example.com a***@e***.com
sanitize.Name Alice Smith A**** S****
sanitize.PAN 4111111111111111 411111******1111

Custom Sanitize Types

Any type with a Sanitize() method that returns itself works:

type SSN string

func (s SSN) Sanitize() SSN {
    if len(s) < 4 {
        return "****"
    }
    return SSN("***-**-" + string(s[len(s)-4:]))
}

Pii detects Sanitize() on struct fields and calls it in the generated Sanitize() method.

Selective Generation

Use -type to generate for specific structs only:

//go:generate pii -type=User,Order

When -type is set:

  • Only named structs get Sanitize() methods
  • Transitive dependencies are auto-included (if User has a ContactInfo field that needs sanitizing, ContactInfo.Sanitize() is generated too)
  • Structs not named and not reachable as dependencies are skipped
  • Unknown type names produce a clear error

When -type is omitted, all qualifying structs in the package are generated (default behavior).

Nested Structs

Pii handles struct fields that themselves contain sanitizable fields:

type ContactInfo struct {
    Phone sanitize.Phone
    Email sanitize.Email
}

type User struct {
    Name    string
    Contact ContactInfo
}

Both ContactInfo.Sanitize() and User.Sanitize() are generated. User.Sanitize() calls Contact.Sanitize() internally.

Pointer fields (*ContactInfo) and slices are also handled.

Flags

Flag Description
-type=Name1,Name2 Generate only for named structs and their dependencies
-destination=path Output file path (default: <source>_sanitize.go)
-check Compare output to existing file, exit non-zero if different
-v Print per-field reasoning and auto-included dependencies to stderr
-debug Dump parsed model to stdout without generating

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mask

func Mask(i any) any

func MaskEmail

func MaskEmail[T ~string](v T) T

MaskEmail keeps the first char of the local part and the full domain. "john.doe@acme.com" -> "j*******@acme.com".

func MaskName

func MaskName[T ~string](v T) T

MaskName keeps the first rune, stars the rest. "Jonathan" -> "J*******".

func MaskPAN

func MaskPAN[T ~string](v T) T

MaskPAN keeps the first 6 (BIN) and last 4 digits — PCI-DSS safe. "4111 1111 1111 1111" -> "411111******1111".

func MaskPhone

func MaskPhone[T ~string](v T) T

MaskPhone keeps the last 4 digits of the numeric portion. "+998 (90) 123-45-67" -> "********4567".

func Register

func Register(tag TagValue, m Masker)

Types

type Masker

type Masker func(value string) string

type TagValue

type TagValue string
const (
	Name  TagValue = "name"
	Phone TagValue = "phone"
	Email TagValue = "email"
	PAN   TagValue = "pan"
)

Directories

Path Synopsis
cmd
sanitizer command
internal
Package sanitize provides built-in sanitizable types for use with the pii code generator.
Package sanitize provides built-in sanitizable types for use with the pii code generator.

Jump to

Keyboard shortcuts

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