Skip to main content

Type Support

Censor provides comprehensive support for Go's type system, allowing you to mask sensitive data across a wide range of data structures. This guide provides an overview of the types supported by Censor.

Supported Types

Censor supports the following Go types:

Basic Types

Type CategorySupported TypesDefault Behavior
StringstringMasked by default, unless tagged with censor:"display"
Numericint, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64Displayed as is
BooleanboolDisplayed as is
Timetime.TimeFormatted according to configuration
PointersPointers to any supported typeDereferenced and processed by underlying type rules

Learn more about Basic Types

Complex Types

Type CategorySupported TypesDefault Behavior
StructsAll struct typesFields processed according to their types
MapsMaps with any key and value typesKeys can be displayed or masked, values processed by type
SlicesSlices of any typeElements processed according to their type
ArraysArrays of any typeElements processed according to their type

Learn more about Complex Types

Special Types

Type CategorySupported TypesDefault Behavior
Custom TypesType aliases, custom structsProcessed according to underlying type
Interfacesinterface{}, anyProcessed based on concrete type at runtime
Custom HandlersAny registered typeProcessed by custom handler function

Learn more about Special Types

Unsupported Types

The following Go types are not supported by Censor:

TypeReason
chanChannels cannot be sanitized meaningfully
funcFunctions cannot be sanitized meaningfully
unsafe.PointerCannot safely process arbitrary memory
uintptrCannot safely process arbitrary memory addresses
complex64, complex128Not commonly used for sensitive data

Attempting to process these types will result in an error.

Format-Specific Behavior

Censor supports two main output formats:

FormatDescriptionUse Case
TEXTHuman-readable formatConsole output, debugging
JSONStructured formatAPIs, logging, data interchange

The same data may be presented differently depending on the chosen format.

Learn more about Format-Specific Behavior

Type-Specific Behavior

Strings

Strings are masked by default, but can be configured to be displayed using struct tags:

type User struct {
ID string `censor:"display"` // Will be displayed.
Email string // Will be masked.
Password string `censor:"mask"` // Will be masked (explicit).
}

Maps

Maps keys are processed according to a map key handler, and values are processed according to their types:

// Default behavior.
user := map[string]string{
"id": "123", // Key is displayed, value is displayed (number pattern).
"email": "user@example.com", // Key is displayed, value is masked.
"password": "secret123", // Key is displayed, value is masked.
}

// TEXT: map[id:123 email:[CENSORED] password:[CENSORED]]
// JSON: {"id":"123","email":"[CENSORED]","password":"[CENSORED]"}

Structs

Structs are processed recursively, with each field being processed according to its type:

type User struct {
ID string `censor:"display"`
Email string
Address Address
}

type Address struct {
Street string
City string
ZipCode string
}

// All string fields except ID will be masked

Complete Example

Here's a complete example showing how various types are processed:

package main

import (
"fmt"
"time"
"github.com/vpakhuchyi/censor"
)

type Address struct {
Street string
City string
ZipCode string
}

type User struct {
ID string `censor:"display"`
Email string
Password string
Age int
Active bool
CreatedAt time.Time
Address Address
Tags []string
Metadata map[string]string
}

func main() {
// Create a Censor instance.
c := censor.New()

// Create a user.
user := User{
ID: "123",
Email: "user@example.com",
Password: "secret123",
Age: 30,
Active: true,
CreatedAt: time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC),
Address: Address{
Street: "123 Main St",
City: "New York",
ZipCode: "10001",
},
Tags: []string{"premium", "active"},
Metadata: map[string]string{
"last_login": "2023-01-01",
"api_key": "sk_live_123456789",
},
}

// Process the data.
masked := c.Process(user)
fmt.Printf("%+v\n", masked)
}

// Output:
// {ID:123 Email:[CENSORED] Password:[CENSORED] Age:30 Active:true CreatedAt:2023-01-01 12:00:00 +0000 UTC Address:{Street:[CENSORED] City:[CENSORED] ZipCode:[CENSORED]} Tags:[[CENSORED] [CENSORED]] Metadata:map[api_key:[CENSORED] last_login:[CENSORED]]}

Next Steps

To learn more about each type category:

  1. Basic Types
  2. Complex Types
  3. Special Types
  4. Format-Specific behavior