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

Support field separators on the last field of a message field. #1641

Merged
merged 1 commit into from
Apr 26, 2024
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
14 changes: 9 additions & 5 deletions Sources/SwiftProtobuf/TextFormatDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ internal struct TextFormatDecoder: Decoder {
}

mutating func nextFieldNumber() throws -> Int? {
if let terminator = terminator {
if scanner.skipOptionalObjectEnd(terminator) {
return nil
}
}
// Per https://protobuf.dev/reference/protobuf/textformat-spec/#fields, every field can be
// followed by a field separator, so if we've seen a field, remove the separator before
// checking for the terminator.
if fieldCount > 0 {
scanner.skipOptionalSeparator()
}
if let terminator = terminator,
scanner.skipOptionalObjectEnd(terminator) {
return nil
}
if let fieldNumber = try scanner.nextFieldNumber(names: fieldNameMap!, messageType: messageType) {
fieldCount += 1
return fieldNumber
} else if terminator == nil {
return nil
} else {
// If this decoder is looking for at a terminator, then if the scanner failed to
// find a field number, something went wrong (end of stream).
throw TextFormatDecodingError.truncated
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftProtobufTests/Test_TextFormat_proto3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,12 @@ final class Test_TextFormat_proto3: XCTestCase, PBTestHelpers {
assertTextFormatDecodeSucceeds("optional_int32:1;\n") {(o: MessageTestType) in
return o.optionalInt32 == 1
}
assertTextFormatDecodeSucceeds("optional_nested_message {bb:3,},") {(o: MessageTestType) in
return o.optionalNestedMessage.bb == 3
}
assertTextFormatDecodeSucceeds("optional_nested_message {bb:7;};") {(o: MessageTestType) in
return o.optionalNestedMessage.bb == 7
}
}

//
Expand Down
Loading