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 Category | Supported Types | Default Behavior |
---|---|---|
String | string | Masked by default, unless tagged with censor:"display" |
Numeric | int , int8 , int16 , int32 , int64 , uint , uint8 , uint16 , uint32 , uint64 , float32 , float64 | Displayed as is |
Boolean | bool | Displayed as is |
Time | time.Time | Formatted according to configuration |
Pointers | Pointers to any supported type | Dereferenced and processed by underlying type rules |
Complex Types
Type Category | Supported Types | Default Behavior |
---|---|---|
Structs | All struct types | Fields processed according to their types |
Maps | Maps with any key and value types | Keys can be displayed or masked, values processed by type |
Slices | Slices of any type | Elements processed according to their type |
Arrays | Arrays of any type | Elements processed according to their type |
Learn more about Complex Types
Special Types
Type Category | Supported Types | Default Behavior |
---|---|---|
Custom Types | Type aliases, custom structs | Processed according to underlying type |
Interfaces | interface{} , any | Processed based on concrete type at runtime |
Custom Handlers | Any registered type | Processed by custom handler function |
Learn more about Special Types
Unsupported Types
The following Go types are not supported by Censor:
Type | Reason |
---|---|
chan | Channels cannot be sanitized meaningfully |
func | Functions cannot be sanitized meaningfully |
unsafe.Pointer | Cannot safely process arbitrary memory |
uintptr | Cannot safely process arbitrary memory addresses |
complex64 , complex128 | Not commonly used for sensitive data |
Attempting to process these types will result in an error.
Format-Specific Behavior
Censor supports two main output formats:
Format | Description | Use Case |
---|---|---|
TEXT | Human-readable format | Console output, debugging |
JSON | Structured format | APIs, 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: