print

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 12 Imported by: 2

README

Go Print Utilities

Simple go package to print and save JSON data. It can print regular JSON and secure JSON by masking sensitive values like passwords and tokens.

Install

go get github.com/goliatone/go-print

Usage

Basic JSON printing:

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 (masks 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)

// Print masked JSON, returns error message if fails
str := print.MaybeSecureJSON(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)

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

Default Masked Fields

These fields are masked by default:

  • Password/password
  • SigningKey/signing_key
  • Authorization/authorization

License

MIT

Copyright (c) 2024 goliatone

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func HighlightJSON added in v0.2.0

func HighlightJSON(data any) (string, error)

func IsInterfaceNil

func IsInterfaceNil(i any) bool

IsInterfaceNil will check if an interface is nil

func MaybeHighlightJSON added in v0.2.0

func MaybeHighlightJSON(data any) string

func MaybePrettyJSON

func MaybePrettyJSON(data any) string

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 MaybeSecureHighlightJSON(data any) string

func MaybeSecureJSON

func MaybeSecureJSON(data any) string

func NotInterfaceNil

func NotInterfaceNil(i any) bool

NotInterfaceNil will negate check

func PrettyJSON

func PrettyJSON(data any) (string, error)

PrettyJSON will pretty print as a JSON string

func PrintHTTPRequest

func PrintHTTPRequest(req *http.Request) string

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 PrintHTTPResponse(resp *http.Response) string

func SaveJSONFile

func SaveJSONFile(name string, data any) error

SaveJSONFile will create a new file with content

func SaveSecureJSONFile

func SaveSecureJSONFile(name string, data any) error

SaveSecureJSONFile will create a new file with content

func SecureHighlightJSON added in v0.3.0

func SecureHighlightJSON(data any) (string, error)

func SecureJSON

func SecureJSON(data any) (string, error)

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 */}

type RequestJSON

type RequestJSON struct {
	Method        string      `json:"method"`
	URL           *url.URL    `json:"url"`
	Header        http.Header `json:"header"`
	Body          string      `json:"body,omitempty"`
	ContentLength int64       `json:"content_length"`
}

type ResponseJSON

type ResponseJSON struct {
	Status        string       `json:"status"`
	StatusCode    int          `json:"status_code"`
	Header        http.Header  `json:"header"`
	Body          string       `json:"body,omitempty"`
	ContentLength int64        `json:"content_length"`
	Request       *RequestJSON `json:"request"`
}

Jump to

Keyboard shortcuts

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