Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SafeByte and SafeBytes #30

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ func (b *StringBuilder) SafeRune(s i.SafeRune) {
_ = b.Buffer.WriteRune(rune(s))
}

func (b *StringBuilder) SafeByte(s i.SafeByte) {
b.SetMode(ib.SafeEscaped)
_ = b.Buffer.WriteByte(byte(s))
}

func (b *StringBuilder) SafeBytes(s i.SafeBytes) {
b.SetMode(ib.SafeEscaped)
_, _ = b.Buffer.Write([]byte(s))
}

// UnsafeString is part of the SafeWriter interface.
func (b *StringBuilder) UnsafeString(s string) {
b.SetMode(ib.UnsafeEscaped)
Expand Down
6 changes: 3 additions & 3 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ func TestBuilder(t *testing.T) {
b.SafeRune('\n')

b.UnsafeByte('U')
b.SafeRune('\n')
b.SafeByte('\n')

b.UnsafeByte(m.StartS[0])
b.SafeRune('\n')
b.SafeByte('\n')

b.UnsafeBytes([]byte("UUU"))
b.SafeRune('\n')
b.SafeBytes([]byte("\n"))

actualR := b.RedactableString()
const expectedR = `‹unsafe›
Expand Down
18 changes: 18 additions & 0 deletions interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ type SafeWriter interface {
// SafeRune emits a safe rune.
SafeRune(SafeRune)

// SafeByte emits a safe byte.
SafeByte(SafeByte)

// SafeBytes emits a safe byte slice.
SafeBytes(SafeBytes)

// Print emits its arguments separated by spaces.
// For each argument it dynamically checks for the SafeFormatter or
// SafeValue interface and either use that, or mark the argument
Expand Down Expand Up @@ -119,6 +125,18 @@ type SafeRune rune
// SafeValue makes SafeRune a SafeValue.
func (SafeRune) SafeValue() {}

// SafeByte represents a byte that is not a sensitive value.
type SafeByte byte

// SafeValue makes SafeByte a SafeValue.
func (SafeByte) SafeValue() {}

// SafeBytes represents a byte slice that is not a sensitive value.
type SafeBytes []byte

// SafeValue makes SafeBytes a SafeValue.
func (SafeBytes) SafeValue() {}

// SafeValue is a marker interface to be implemented by types that
// alias base Go types and whose natural representation via Printf is
// always safe for reporting.
Expand Down
12 changes: 12 additions & 0 deletions internal/rfmt/printer_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func (p *pp) SafeRune(r i.SafeRune) {
p.buf.WriteRune(rune(r))
}

// SafeByte implements SafePrinter.
func (p *pp) SafeByte(r i.SafeByte) {
defer p.startSafeOverride().restore()
p.buf.WriteByte(byte(r))
}

// SafeBytes implements SafePrinter.
func (p *pp) SafeBytes(r i.SafeBytes) {
defer p.startSafeOverride().restore()
p.buf.Write(r)
}

func (p *pp) Print(args ...interface{}) {
defer p.buf.SetMode(p.buf.GetMode())
np := newPrinter()
Expand Down