From d4e7a7b637ba73883a6c87c468633cba55f6c08d Mon Sep 17 00:00:00 2001 From: luoyy Date: Wed, 29 May 2024 22:15:12 +0800 Subject: [PATCH] (update): Add `fmt.GoStringer` support for Debug. --- types/buffer-ext.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/types/buffer-ext.go b/types/buffer-ext.go index 2d5cc24..8b4a591 100644 --- a/types/buffer-ext.go +++ b/types/buffer-ext.go @@ -20,6 +20,7 @@ type BufferInterface interface { Bytes() []byte AvailableBuffer() []byte fmt.Stringer + fmt.GoStringer Len() int Size() int64 Cap() int @@ -69,6 +70,14 @@ type BytesBuffer struct { *Buffer } +func (b *BytesBuffer) GoString() string { + if b == nil || b.Buffer == nil { + // Special case, useful in debugging. + return "" + } + return fmt.Sprintf("%v", b.Buffer.Bytes()) +} + func NewBytesBufferReader(r io.Reader) (BufferInterface, error) { b := NewBytesBuffer(nil) _, err := b.ReadFrom(r) @@ -88,6 +97,14 @@ type StringBuffer struct { *Buffer } +func (sb *StringBuffer) GoString() string { + if sb == nil || sb.Buffer == nil { + // Special case, useful in debugging. + return "" + } + return sb.Buffer.String() +} + // MarshalJSON returns sb as the JSON encoding of m. func (sb *StringBuffer) MarshalJSON() ([]byte, error) { if sb == nil || sb.Buffer == nil {