package
1.1.6
Repository: https://github.com/cockroachdb/redact.git
Documentation: pkg.go.dev

# README

Overall code structure

This directory imports the "printf" logic as-is from the Go standard library and instruments it for redaction of sensitive data.

Overall, the original Go code is structured as follows:

  • the top-level API functions (Printf Fprintf etc) instantiate a "printer" struct called pp, then call the doPrint*() methods on it.

  • the pp contains a byte slice (pp.buf, type buffer) that accumulates the result of formatting regardless of the final output of the API - i.e. a buffer is used even when using Fprint to an io.Writer. Only after the doPrint() method finishes, is the buffer copied to the final io.Writer in the Fprint* variants.

  • each of the doPrint methods does some analysis on the argument list - quite simple for e.g. doPrintln, more intricate for doPrintf. As part of the analysis it emits "spacing" or no-op bytes directly on the pp.buf. For example doPrint emits spaces between arguments directly, doPrintf emits the non-formatting characters from the format string, as well as certain constant error strings (e.g. %(BADPREC)).

Refreshing the sources

The files in this directory have been imported from

$GOROOT/src/fmt/{format,print}.go

and

$GOROOT/src/internal/fmtsort

And patched using the included .diff files.

To upgrade to a newer Go implementation, import the files anew and re-apply the patches.

See the script refresh.sh for details.

# Packages

Package fmtsort provides a general stable ordering mechanism for maps, on behalf of the fmt and text/template packages.

# Functions

EscapeBytes escapes markers inside the given byte slice and encloses the entire byte slice between redaction markers.
Fprint formats using the default formats for its operands and writes to w.
Fprintf formats according to a format specifier and writes to w.
Fprintln formats using the default formats for its operands and writes to w.
HelperForErrorf is a helper to implement a redaction-aware fmt.Errorf-compatible function in a different package.
Print formats using the default formats for its operands and writes to standard output.
Printf formats according to a format specifier and writes to standard output.
Println formats using the default formats for its operands and writes to standard output.
RegisterRedactErrorFn registers an error redaction function for use during automatic redaction by this package.
RegisterSafeType registers a data type to always be considered safe during the production of redactable strings.
Sprint formats using the default formats for its operands and returns the resulting string.
Sprintf formats according to a format specifier and returns the resulting string.
Sprintfn produces a RedactableString using the provided SafeFormat-alike function.
Sprintln formats using the default formats for its operands and returns the resulting string.

# Interfaces

Formatter is implemented by any value that has a Format method.
GoStringer is implemented by any value that has a GoString method, which defines the Go syntax for that value.
State represents the printer state passed to custom formatters.
Stringer is implemented by any value that has a String method, which defines the ``native'' format for that value.