Documentation
¶
Overview ¶
Package print provides utilities for printing and saving JSON data with support for masking sensitive information.
Basic Usage:
data := map[string]string{
"user": "admin",
"pass": "secret"
}
// Pretty print JSON
str, err := print.PrettyJSON(data)
// Print JSON, returns error message if fails
str := print.MaybePrettyJSON(data)
// Save to file
err := print.SaveJSONFile("data.json", data)
Secure JSON (with masked sensitive data):
type User struct {
Username string `json:"username"`
Password string `json:"password" mask:"filled4"` // Will be masked
APIKey string `json:"api_key" mask:"filled32"` // Will be masked
}
user := User{
Username: "admin",
Password: "secret",
APIKey: "1234567890",
}
// Print masked JSON
str, err := print.SecureJSON(user)
// Save masked JSON to file
err := print.SaveSecureJSONFile("user.json", user)
HTTP Request/Response printing:
// Print HTTP request as JSON
req, _ := http.NewRequest("POST", "https://api.example.com", nil)
str := print.PrintHTTPRequest(req)
// Print HTTP response as JSON
resp, _ := http.Get("https://api.example.com")
str := print.PrintHTTPResponse(resp)
Default Masked Fields:
- Password/password
- SigningKey/signing_key
- Authorization/authorization
Features:
- Pretty prints JSON with proper indentation
- Masks sensitive data (passwords, tokens, keys)
- Saves JSON to files
- Prints HTTP requests and responses as JSON
- Thread safe
- Handles errors gracefully
Index ¶
- func HighlightJSON(data any) (string, error)
- func IsInterfaceNil(i any) bool
- func MaybeHighlightJSON(data any) string
- func MaybePrettyJSON(data any) string
- func MaybeSecureHighlightJSON(data any) string
- func MaybeSecureJSON(data any) string
- func NotInterfaceNil(i any) bool
- func PrettyJSON(data any) (string, error)
- func PrintHTTPRequest(req *http.Request) string
- func PrintHTTPResponse(resp *http.Response) string
- func SaveJSONFile(name string, data any) error
- func SaveSecureJSONFile(name string, data any) error
- func SecureHighlightJSON(data any) (string, error)
- func SecureJSON(data any) (string, error)
- type Masker
- type RequestJSON
- type ResponseJSON
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HighlightJSON ¶ added in v0.2.0
func IsInterfaceNil ¶
IsInterfaceNil will check if an interface is nil
func MaybeHighlightJSON ¶ added in v0.2.0
func MaybePrettyJSON ¶
MaybePrettyJSON will return a JSON string, in case of a decofing error happening it will return the mesasge: error printing
func MaybeSecureHighlightJSON ¶ added in v0.3.0
func MaybeSecureJSON ¶
func PrettyJSON ¶
PrettyJSON will pretty print as a JSON string
func PrintHTTPRequest ¶
PrintHTTPRequest will print an http.Request instance as a JSON struct. Note, if we read the body using io.ReadAll, the body will be drained. So, we either call PrintHTTPRequest before we read the body (since we reset it), or we get an error message
func PrintHTTPResponse ¶
func SaveJSONFile ¶
SaveJSONFile will create a new file with content
func SaveSecureJSONFile ¶
SaveSecureJSONFile will create a new file with content
func SecureHighlightJSON ¶ added in v0.3.0
func SecureJSON ¶
Types ¶
type Masker ¶
type Masker interface {
// Mask takes any type T and returns a masked version of the same type
// along with any error that occurred during masking
Mask(target any) (ret any, err error)
}
Masker defines the interface for masking data
var PrintMasker Masker = defaultMasker{/* contains filtered or unexported fields */}