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

Making ByteBuffer's description more useful #2864

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 11 additions & 18 deletions Sources/NIOCore/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -957,37 +957,30 @@ public struct ByteBuffer {
}

extension ByteBuffer: CustomStringConvertible, CustomDebugStringConvertible {
/// A `String` describing this `ByteBuffer`. Example:
/// A `String` describing this `ByteBuffer`. For a `ByteBuffer` initialised with `hello world`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A `String` describing this `ByteBuffer`. For a `ByteBuffer` initialised with `hello world`
/// A `String` describing this `ByteBuffer` including length and the bytes it contains (partially).
///
/// For a `ByteBuffer` initialised with `hello world`

Nit: The first line in the doc comments is special, it's the summary. That summary gets displayed by itself in many scenarios so we should aim for it to make sense without the rest of the doc comment.

/// the description would be the following:
///
/// ByteBuffer { readerIndex: 0, writerIndex: 4, readableBytes: 4, capacity: 512, storageCapacity: 1024, slice: 256..<768, storage: 0x0000000103001000 (1024 bytes)}
/// [68656c6c6f20776f726c64](11 bytes)
///
/// Buffers larger that 64 bytes will get truncated when printing out.
/// The format of the description is not API.
///
/// - returns: A description of this `ByteBuffer`.
public var description: String {
"""
ByteBuffer { \
readerIndex: \(self.readerIndex), \
writerIndex: \(self.writerIndex), \
readableBytes: \(self.readableBytes), \
capacity: \(self.capacity), \
storageCapacity: \(self.storageCapacity), \
slice: \(self._slice), \
storage: \(self._storage.bytes) (\(self._storage.capacity) bytes) \
}
"""
"[\(self.hexDump(format: .compact(maxBytes: 64)))](\(self.readableBytes) bytes)"
}

/// A `String` describing this `ByteBuffer` with some portion of the readable bytes dumped too. Example:
/// A `String` describing this `ByteBuffer`. For a `ByteBuffer` initialised with `hello world`
/// the description would be the following:
///
/// ByteBuffer { readerIndex: 0, writerIndex: 4, readableBytes: 4, capacity: 512, slice: 256..<768, storage: 0x0000000103001000 (1024 bytes)}
/// readable bytes (max 1k): [ 00 01 02 03 ]
/// [68656c6c6f20776f726c64](11 bytes)
///
/// Buffers larger that 64 bytes will get truncated when printing out.
/// The format of the description is not API.
///
/// - returns: A description of this `ByteBuffer` useful for debugging.
/// - returns: A description of this `ByteBuffer`.
public var debugDescription: String {
"\(self.description)\nreadable bytes (max 1k): \(self._storage.dumpBytes(slice: self._slice, offset: self.readerIndex, length: min(1024, self.readableBytes)))"
"[\(self.hexDump(format: .compact(maxBytes: 64)))](\(self.readableBytes) bytes)"
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOCore/ByteBuffer-hexdump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extension ByteBuffer {
private func _hexDump(maxBytes: Int, separateWithWhitespace: Bool) -> String {
// If the buffer length fits in the max bytes limit in the hex dump, just dump the whole thing.
if self.readableBytes <= maxBytes {
return self.hexDump(format: .plain)
return self._hexDump(separateWithWhitespace: separateWithWhitespace)
}

var buffer = self
Expand Down
14 changes: 13 additions & 1 deletion Tests/NIOCoreTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,11 @@ class ByteBufferTest: XCTestCase {
let buf = ByteBuffer(string: "Hello")
XCTAssertEqual("48656c6c6f", buf.hexDump(format: .compact))
}

func testHexDumpCompactReadableBytesLessThenMaxBytes() {
let buf = ByteBuffer(string: "hello world")
XCTAssertEqual("68656c6c6f20776f726c64", buf.hexDump(format: .compact(maxBytes: 100)))
}

func testHexDumpCompactEmptyBuffer() {
let buf = ByteBuffer(string: "")
Expand Down Expand Up @@ -3602,5 +3607,12 @@ extension ByteBufferTest {
XCTAssertEqual(error as? Base64Error, .invalidCharacter)
}
}


func testByteBufferDescription() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think it would be nice to add two other test cases:

  • the empty ByteBuffer
  • one where the hex bytes get truncated (i.e. more than 128 bytes or whatever the limit is)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added!

let buffer = ByteBuffer(string: "hello world")

XCTAssertEqual(buffer.description, "[68656c6c6f20776f726c64](11 bytes)")

XCTAssertEqual(buffer.description, buffer.debugDescription)
}
}
2 changes: 1 addition & 1 deletion Tests/NIOCoreTests/NIOAnyDebugTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NIOAnyDebugTest: XCTestCase {
let bb = ByteBuffer(string: "byte buffer string")
XCTAssertTrue(
wrappedInNIOAnyBlock(bb).contains(
"NIOAny { ByteBuffer { readerIndex: 0, writerIndex: 18, readableBytes: 18, capacity: 32, storageCapacity: 32, slice: _ByteBufferSlice { 0..<32 }, storage: "
"NIOAny { [627974652062756666657220737472696e67](18 bytes) }"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I almost think with this change we should change NIOAny's description to include ByteBuffer: when it's the case .byteBuffer. Previously that wasn't necessary because ByteBuffer.description contained the word ByteBuffer but that's now changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at NIOAny I would improve its description altogether to include information about the wrapped type. But I would do that in a separate issue/PR.

)
)
XCTAssertTrue(wrappedInNIOAnyBlock(bb).hasSuffix(" }"))
Expand Down
Loading