Skip to content

Commit

Permalink
(update): Add fmt.GoStringer support for Debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
zishang520 committed May 29, 2024
1 parent 67a4539 commit d4e7a7b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions types/buffer-ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type BufferInterface interface {
Bytes() []byte
AvailableBuffer() []byte
fmt.Stringer
fmt.GoStringer
Len() int
Size() int64
Cap() int
Expand Down Expand Up @@ -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 "<nil>"
}
return fmt.Sprintf("%v", b.Buffer.Bytes())
}

func NewBytesBufferReader(r io.Reader) (BufferInterface, error) {
b := NewBytesBuffer(nil)
_, err := b.ReadFrom(r)
Expand All @@ -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 "<nil>"
}
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 {
Expand Down

0 comments on commit d4e7a7b

Please sign in to comment.