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

Swift: encode & decode size-delimited messages #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lizard-boy
Copy link

Adds the ability to encode and decode a size-delimited message collection in Swift.

The intended use case is to decode a stream of messages recorded to disk (e.g., Bazel's Build Event Protocol). Similar implementations exist in Java (writeDelimitedTo and parseDelimitedFrom) and other languages in protocolbuffers/protobuf#10229.

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR introduces size-delimited message encoding and decoding functionality to the Swift runtime for Wire, enhancing compatibility with protocols like Bazel's Build Event Protocol.

  • Added decodeSizeDelimited method in ProtoDecoder.swift to handle varint-prefixed message collections
  • Implemented encodeSizeDelimited in ProtoEncoder.swift for efficient encoding of message arrays with size delimiters
  • Enhanced WriteBuffer.swift with empty data checks for improved efficiency
  • Added comprehensive test cases in ProtoDecoderTests.swift, ProtoEncoderTests.swift, RoundTripTests.swift, and WriteBufferTests.swift to verify new functionality

7 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings

Comment on lines +180 to +181
while fullBuffer.isDataRemaining, let size = try? fullBuffer.readVarint64() {
if size == 0 { break }
Copy link

Choose a reason for hiding this comment

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

logic: Breaking on size 0 might skip valid empty messages. Consider handling this case explicitly

@@ -74,13 +78,17 @@ final class WriteBuffer {

func append(_ value: WriteBuffer) {
precondition(value !== self)
guard value.count > 0 else { return }
Copy link

Choose a reason for hiding this comment

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

style: Consider using isEmpty instead of count > 0 for consistency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants