Advanced Configuration Examples
Here are some advanced examples of configuring and using Censor in more complex scenarios.
Custom Masking Patterns
package main
import (
"fmt"
"github.com/vpakhuchyi/censor"
)
func main() {
c := censor.New(
censor.WithFields([]censor.Field{
{
Name: "credit_card",
Mask: "XXXX-XXXX-XXXX-$last4",
},
{
Name: "email",
Mask: "$first3****@$domain",
},
}),
)
data := map[string]interface{}{
"credit_card": "4111-1111-1111-1234",
"email": "john.doe@example.com",
}
sanitized := c.Process(data)
fmt.Printf("%+v\n", sanitized)
}
Complex Nested Structures
package main
import (
"fmt"
"github.com/vpakhuchyi/censor"
)
type Address struct {
Street string
City string
Country string
}
type Payment struct {
CardNumber string
CVV string
}
type User struct {
Username string
Password string
Address Address
Payment Payment
Metadata map[string]interface{}
}
func main() {
c := censor.New(
censor.WithOptions(censor.Options{
Recursive: true,
PartialMatch: true,
}),
)
user := User{
Username: "john_doe",
Password: "secret123",
Address: Address{
Street: "123 Main St",
City: "New York",
Country: "USA",
},
Payment: Payment{
CardNumber: "4111-1111-1111-1234",
CVV: "123",
},
Metadata: map[string]interface{}{
"secret_key": "api_key_123",
"email": "john@example.com",
},
}
sanitized := c.Process(user)
fmt.Printf("%+v\n", sanitized)
}
Custom Field Matching
package main
import (
"fmt"
"github.com/vpakhuchyi/censor"
)
func main() {
c := censor.New(
censor.WithOptions(censor.Options{
CaseSensitive: true,
PartialMatch: false,
}),
censor.WithFields([]censor.Field{
{Name: "Password", Mask: "***"}, // Will only match exact "Password"
{Name: "SECRET", Mask: "###"}, // Will only match exact "SECRET"
}),
)
data := map[string]interface{}{
"Password": "secret123", // Will be masked
"password": "secret456", // Won't be masked (case sensitive)
"SECRET": "key123", // Will be masked
"secret_key": "key456", // Won't be masked (no partial match)
}
sanitized := c.Process(data)
fmt.Printf("%+v\n", sanitized)
}
Loading Configuration from File
package main
import (
"fmt"
"github.com/vpakhuchyi/censor"
)
func main() {
// Load configuration from file
config, err := censor.LoadConfig("censor.yml")
if err != nil {
panic(err)
}
// Create censor instance with loaded configuration
c := censor.New(
censor.WithConfig(config),
)
data := map[string]interface{}{
"username": "john_doe",
"password": "secret123",
"api_key": "key123",
}
sanitized := c.Process(data)
fmt.Printf("%+v\n", sanitized)
}
Example censor.yml
:
fields:
- name: password
mask: "***"
- name: api_key
mask: "key_****"
- name: credit_card
mask: "XXXX-XXXX-XXXX-$last4"
options:
case_sensitive: true
partial_match: false
recursive: true
These examples demonstrate more advanced usage patterns of Censor. For more information about specific features and options, refer to the Configuration guide.