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

Add partial support to the binary decoding initializer. #290

Merged
merged 2 commits into from
Feb 22, 2017
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
4 changes: 4 additions & 0 deletions Sources/SwiftProtobuf/BinaryDecodingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public enum BinaryDecodingError: Error {
case malformedProtobuf
/// The data being parsed does not match the type specified in the proto file
case schemaMismatch
/// The message or nested messages definitions have required fields, and the
/// binary data did not include values for them. The `partial` support will
/// allow this incomplete data to be decoded.
case missingRequiredFields
}
18 changes: 17 additions & 1 deletion Sources/SwiftProtobuf/BinaryTypeAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,20 @@ public extension Message {
return visitor.serializedSize
}

init(serializedData data: Data, extensions: ExtensionSet? = nil) throws {
/// Initializes the message by decoding the Protocol Buffer binary serialization
/// format for this message.
///
/// - Parameters:
/// - serializedData: The binary serialization data to decode.
/// - extensions: An `ExtensionSet` to look up and decode any extensions in this
/// message or messages nested within this message's fields.
/// - partial: By default, the binary serialization format requires all `required`
/// fields be present; when `partial` is `false`,
/// `BinaryDecodingError.missingRequiredFields` is thrown if any were missing.
/// When `partial` is `true`, then partial messages are allowed, and
/// `Message.isInitialized` is not checked.
/// - Throws: An instance of `BinaryDecodingError` on failure.
init(serializedData data: Data, extensions: ExtensionSet? = nil, partial: Bool = false) throws {
self.init()
if !data.isEmpty {
try data.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) in
Expand All @@ -277,6 +290,9 @@ public extension Message {
extensions: extensions)
}
}
if !partial && !isInitialized {
throw BinaryDecodingError.missingRequiredFields
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -781,14 +781,16 @@ extension Test_Required {
("test_NestedInProto2_IsInitialized", {try run_test(test:($0 as! Test_Required).test_NestedInProto2_IsInitialized)}),
("test_NestedInProto3_IsInitialized", {try run_test(test:($0 as! Test_Required).test_NestedInProto3_IsInitialized)}),
("test_map_isInitialized", {try run_test(test:($0 as! Test_Required).test_map_isInitialized)}),
("test_Extensions_isInitialized", {try run_test(test:($0 as! Test_Required).test_Extensions_isInitialized)})
("test_Extensions_isInitialized", {try run_test(test:($0 as! Test_Required).test_Extensions_isInitialized)}),
("test_decodeRequired", {try run_test(test:($0 as! Test_Required).test_decodeRequired)})
]
}
}

extension Test_SmallRequired {
static var allTests: [(String, (XCTestCase) throws -> ())] {
return [
("test_decodeRequired", {try run_test(test:($0 as! Test_SmallRequired).test_decodeRequired)})
]
}
}
Expand Down
Loading