util

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BodyKindJSON = "json"
	BodyKindXML  = "xml"
	BodyKindHTML = "html"
	BodyKindText = "text"
)

Body kind constants used by egress and the response UI.

View Source
const MaxPrettyBytes = 32 << 20

MaxPrettyBytes is the hard ceiling on formatting a body.

A formatted copy is a second full representation of the body, and producing it needs a working buffer on top of that: formatting an 86 MB JSON response cost ~200 MB in copies and buffers. Past this size the raw body is displayed as received, which also lets the viewer read the response's own buffer instead of copying it.

Variables

View Source
var ErrBodyNotFormatted = errors.New("body left unformatted")

ErrBodyNotFormatted reports that a body was deliberately left unformatted — it is already laid out in lines, or too large to reformat. Callers should display the raw body.

Functions

func ApplyBodyFormat added in v0.7.0

func ApplyBodyFormat(contentType string, body []byte) (kind, pretty, jsonStr string, isJSON bool)

ApplyBodyFormat sets Pretty/BodyKind/IsJSON/JSON on a response-like target.

When the body is left unformatted (already line-broken, or over MaxPrettyBytes) pretty is returned empty so the caller displays the raw body without copying it. jsonStr is still filled for bodies within MaxPrettyBytes, since jsonpath extraction needs a string; past that it is left empty rather than holding another copy of a very large body.

func DetectBodyKind added in v0.7.0

func DetectBodyKind(contentType string, body []byte) string

DetectBodyKind chooses a display format from Content-Type, then body sniffing.

func EncodeJSON added in v0.4.6

func EncodeJSON(data any) ([]byte, error)

func IsJSON added in v0.4.6

func IsJSON(s string) bool

func IsJSONBytes added in v0.7.0

func IsJSONBytes(data []byte) bool

IsJSONBytes reports whether data is valid JSON without copying it into a string or building a value tree.

func IsXML added in v0.7.0

func IsXML(s string) bool

IsXML reports whether s looks like well-formed XML (or HTML-as-XML).

func IsXMLBytes added in v0.7.0

func IsXMLBytes(data []byte) bool

IsXMLBytes reports whether data looks like well-formed XML (or HTML-as-XML) without copying it into a string.

func MakeDir

func MakeDir(dir string) error

func ParseJSON added in v0.4.6

func ParseJSON(text string) (map[string]any, error)

func PrettyBody added in v0.7.0

func PrettyBody(body []byte, kind string) (string, error)

PrettyBody returns indented text for known structured kinds; otherwise the raw body.

A body that is already laid out in lines, or larger than MaxPrettyBytes, returns ErrBodyNotFormatted with no text.

func PrettyHTML added in v0.7.0

func PrettyHTML(data []byte) (string, error)

PrettyHTML applies a lightweight indent to HTML-ish markup. Failures return raw text.

func PrettyJSON added in v0.4.6

func PrettyJSON(data []byte) (string, error)

PrettyJSON indents data for display without decoding it.

This shows the response as the server sent it: object keys keep their original order, numbers keep their original text, and escape sequences are left as-is. The previous implementation decoded into interface{} and marshalled back, which reordered keys alphabetically (Go map marshalling) and round-tripped every number through float64 — so an int64 id like 9007199254740993 displayed as ...992. It also cost many times the body size in live objects: a 10 MB response pushed the Go heap from 45 MB to 195 MB, and the process never gave that back.

The buffer is pre-sized so it does not grow by doubling: an undersized guess on a 20 MB result churns through tens of MB of intermediate arrays before it settles. Four-space indenting of compact JSON roughly doubles it, so budget for that up front — overshooting costs one oversized array, undershooting costs a full realloc and copy.

func PrettyXML added in v0.7.0

func PrettyXML(data []byte) (string, error)

PrettyXML indents XML. On failure it returns the original string with a non-nil error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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