Skip to content

Commit 6aa97ce

Browse files
committed
API Break: Use Data type from Foundation for binary data
This changes the current API and will break existing clients. The protobuf binary serialization methods have changed to use Data: ProtobufMessage.init(protobuf:) now takes Data instead of [UInt8] ProtobufMessage.serializeProtobuf() now returns Data instead of [UInt8] The [UInt8] support is still available under a different name: ProtobufMessage.init(protobufBytes: [UInt8]) ProtobufMessage.serializeProtobufBytes() throws -> [UInt8] Similarly, proto `bytes` fields that were previously rendered as [UInt8] properties in Swift are now rendered to Data properties. This change was made to better align Swift Protobuf conventions with Swift 3 Foundation.
1 parent 159f205 commit 6aa97ce

File tree

92 files changed

+453
-386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+453
-386
lines changed

CollectTests.awk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ BEGIN {
3232
TESTCASES=""
3333
TESTCASE_separator=""
3434
printf("import XCTest\n")
35-
printf("@testable import ProtobufTestSuite\n")
35+
printf("@testable import ProtobufTests\n")
3636
printf("\n")
3737
printf("private func run_test(test:() -> ()) throws {\n")
3838
printf(" test()\n")

Sources/Protobuf/Google_Protobuf_Any.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
///
1818
// -----------------------------------------------------------------------------
1919

20+
import Foundation
2021
import Swift
2122

2223
fileprivate func typeName(fromURL s: String) -> String {
@@ -140,13 +141,13 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
140141
public var typeURL: String?
141142

142143
/// Must be valid serialized data of the above specified type.
143-
public var value: [UInt8]? {
144+
public var value: Data? {
144145
get {
145146
if let value = _value {
146147
return value
147148
} else if let message = _message {
148149
do {
149-
return try message.serializeProtobufBytes()
150+
return try message.serializeProtobuf()
150151
} catch {
151152
return nil
152153
}
@@ -157,7 +158,7 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
157158
if let messageType = Google_Protobuf_Any.wellKnownTypes[encodedTypeName] {
158159
do {
159160
let m = try messageType.init(any: self)
160-
return try m.serializeProtobufBytes()
161+
return try m.serializeProtobuf()
161162
} catch {
162163
return nil
163164
}
@@ -166,15 +167,15 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
166167
if let messageType = Google_Protobuf_Any.knownTypes[encodedTypeName] {
167168
do {
168169
let m = try messageType.init(any: self)
169-
return try m.serializeProtobufBytes()
170+
return try m.serializeProtobuf()
170171
} catch {
171172
return nil
172173
}
173174
}
174175
// TODO: Google spec requires a lot more work in the general case:
175176
// let encodedType = ... fetch google.protobuf.Type based on typeURL ...
176177
// let type = Google_Protobuf_Type(protobuf: encodedType)
177-
// return ProtobufDynamic(type: type, any: self)?.serializeProtobufBytes()
178+
// return ProtobufDynamic(type: type, any: self)?.serializeProtobuf()
178179

179180
// See the comments in serializeJSON() above for more discussion of what would be needed to fully implement this.
180181
return nil
@@ -188,7 +189,7 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
188189
_jsonFields = nil
189190
}
190191
}
191-
private var _value: [UInt8]?
192+
private var _value: Data?
192193

193194
private var _message: ProtobufMessage?
194195
private var _jsonFields: [String:[ProtobufJSONToken]]?
@@ -218,7 +219,7 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
218219
let messageTypeName = typeName(fromMessage: m)
219220
knownTypes[messageTypeName] = messageType
220221
}
221-
222+
222223
public init() {}
223224

224225
public init(message: ProtobufMessage) {
@@ -233,7 +234,7 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
233234
default: return false
234235
}
235236
}
236-
237+
237238
public mutating func decodeFromJSONObject(jsonDecoder: inout ProtobufJSONDecoder) throws {
238239
var key = ""
239240
var state = ProtobufJSONDecoder.ObjectParseState.expectFirstKey
@@ -292,15 +293,16 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
292293
if encodedType != messageType {
293294
throw ProtobufDecodingError.malformedAnyField
294295
}
295-
var protobuf: [UInt8]?
296+
var protobuf: Data?
296297
if let message = _message {
297-
protobuf = try message.serializeProtobufBytes()
298+
protobuf = try message.serializeProtobuf()
298299
} else if let value = _value {
299300
protobuf = value
300301
}
301302
if let protobuf = protobuf {
302303
// Decode protobuf from the stored bytes
303-
try protobuf.withUnsafeBufferPointer { (bp) in
304+
try protobuf.withUnsafeBytes { (p: UnsafePointer<UInt8>) in
305+
let bp = UnsafeBufferPointer(start: p, count: protobuf.count)
304306
var protobufDecoder = ProtobufBinaryDecoder(protobufPointer: bp)
305307
try protobufDecoder.decodeFullObject(message: &target)
306308
}
@@ -368,7 +370,7 @@ public struct Google_Protobuf_Any: ProtobufAbstractMessage, Hashable, Equatable,
368370
hash = (hash &* 16777619) ^ t.hashValue
369371
}
370372
if let v = _value {
371-
hash = (hash &* 16777619) ^ ProtobufHash(bytes: v)
373+
hash = (hash &* 16777619) ^ v.hashValue
372374
}
373375
if let m = _message {
374376
hash = (hash &* 16777619) ^ m.hashValue

Sources/Protobuf/ProtobufBinaryTypes.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -613,21 +613,18 @@ public extension ProtobufString {
613613
///
614614
public extension ProtobufBytes {
615615
public static func protobufWireType() -> Int { return 2 }
616-
public static func serializeProtobufValue(encoder: inout ProtobufBinaryEncoder, value: [UInt8]) {
617-
encoder.putBytesValue(value: value)
618-
}
619616

620617
public static func serializeProtobufValue(encoder: inout ProtobufBinaryEncoder, value: Data) {
621618
encoder.putBytesValue(value: [UInt8](value))
622619
}
623620

624-
public static func setFromProtobufBuffer(buffer: UnsafeBufferPointer<UInt8>, value: inout [UInt8]?) throws -> Bool {
625-
value = [UInt8](buffer)
621+
public static func setFromProtobufBuffer(buffer: UnsafeBufferPointer<UInt8>, value: inout Data?) throws -> Bool {
622+
value = Data(bytes: [UInt8](buffer))
626623
return true
627624
}
628625

629-
public static func setFromProtobufBuffer(buffer: UnsafeBufferPointer<UInt8>, value: inout [[UInt8]]) throws -> Bool {
630-
value.append([UInt8](buffer))
626+
public static func setFromProtobufBuffer(buffer: UnsafeBufferPointer<UInt8>, value: inout [Data]) throws -> Bool {
627+
value.append(Data(bytes: [UInt8](buffer)))
631628
return true
632629
}
633630
}

Sources/Protobuf/ProtobufDebugDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public struct ProtobufDebugDescriptionVisitor: ProtobufVisitor {
5959
separator = ","
6060
description.append(swiftFieldName + ":" + String(reflecting: value))
6161
}
62-
62+
6363
mutating public func visitRepeatedField<S: ProtobufTypeProperties>(fieldType: S.Type, value: [S.BaseType], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
6464
description.append(separator)
6565
description.append(swiftFieldName)

Sources/Protobuf/ProtobufJSONDecoding.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
///
2727
// -----------------------------------------------------------------------------
2828

29+
import Foundation
2930
import Swift
3031

3132
private func fromHexDigit(_ c: Character?) -> UInt32? {
@@ -858,7 +859,7 @@ private func normalizeIntString(_ s: String) -> [Character]? {
858859
return number
859860
}
860861

861-
private func decodeBytes(_ s: String) -> [UInt8]? {
862+
private func decodeBytes(_ s: String) -> Data? {
862863
var out = [UInt8]()
863864
let digits = s.utf8
864865
var n = 0
@@ -889,7 +890,7 @@ private func decodeBytes(_ s: String) -> [UInt8]? {
889890
if bits != 0 {
890891
return nil
891892
}
892-
return out
893+
return Data(bytes: out)
893894
}
894895

895896

@@ -1000,7 +1001,7 @@ public enum ProtobufJSONToken: Equatable, ProtobufFieldDecoder {
10001001
}
10011002
}
10021003

1003-
var asBytes: [UInt8]? {
1004+
var asBytes: Data? {
10041005
switch self {
10051006
case .string(let s): return decodeBytes(s)
10061007
default: return nil

Sources/Protobuf/ProtobufJSONEncoding.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
///
1515
// -----------------------------------------------------------------------------
1616

17+
import Foundation
1718
import Swift
1819

1920
public struct ProtobufJSONEncodingVisitor: ProtobufVisitor {
@@ -248,7 +249,7 @@ public struct ProtobufJSONEncoder {
248249
}
249250
append(text: "\"")
250251
}
251-
mutating func putBytesValue(value: [UInt8]) {
252+
mutating func putBytesValue(value: Data) {
252253
var out: String = ""
253254
if value.count > 0 {
254255
let digits: [Character] = ["A", "B", "C", "D", "E", "F",

Sources/Protobuf/ProtobufJSONTypes.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
///
1616
// -----------------------------------------------------------------------------
1717

18+
import Foundation
1819
import Swift
1920

2021
public protocol ProtobufJSONCodableType: ProtobufTypePropertiesBase {
@@ -680,11 +681,11 @@ public extension ProtobufBytes {
680681
}
681682
}
682683

683-
public static func serializeJSONValue(encoder: inout ProtobufJSONEncoder, value: [UInt8]) {
684+
public static func serializeJSONValue(encoder: inout ProtobufJSONEncoder, value: Data) {
684685
encoder.putBytesValue(value: value)
685686
}
686687

687-
public static func decodeJSONMapFieldValue(jsonDecoder: inout ProtobufJSONDecoder) throws -> [UInt8]? {
688+
public static func decodeJSONMapFieldValue(jsonDecoder: inout ProtobufJSONDecoder) throws -> Data? {
688689
if let token = try jsonDecoder.nextToken() {
689690
switch token {
690691
case .string(_):

Sources/Protobuf/ProtobufTypes.swift

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
///
1818
// -----------------------------------------------------------------------------
1919

20+
import Foundation
2021
import Swift
2122

2223
///
@@ -32,18 +33,21 @@ import Swift
3233
/// various coding/decoding functions to provide type-specific
3334
/// information.
3435
///
36+
/// The types defined here are extended in ProtobufBinaryTypes.swift
37+
/// with serialization support for binary protobuf encoding, and in
38+
/// ProtobufJSONTypes.swift with serialization support for JSON encoding.
39+
///
3540
public protocol ProtobufTypePropertiesBase {
3641
// Default here is appropriate for enums and messages
3742
// Other types will override this
3843
associatedtype BaseType = Self
3944

4045
/// Hash the provided value
41-
/// In particular, [UInt8] is not Hashable, so we can't just
42-
/// use .hashValue everywhere.
46+
/// TODO: Can we just replace this with .hashValue everywhere?
4347
static func hash(value: BaseType) -> Int
4448

45-
/// In Swift 3, [UInt8] isn't Equatable, so I've added this method
46-
/// to provide a consistent way to compute equality.
49+
/// Test if two values are equal
50+
/// TODO: Can we just replace this with == everywhere?
4751
static func isEqual(_ lhs: BaseType, _ rhs: BaseType) -> Bool
4852
}
4953

@@ -77,23 +81,20 @@ public protocol ProtobufMapValueType: ProtobufTypeProperties {
7781
///
7882
public struct ProtobufFloat: ProtobufTypeProperties, ProtobufMapValueType {
7983
public typealias BaseType = Float
80-
public static func describe(value: BaseType) -> String {return value.description}
8184
}
8285

8386
///
8487
/// Double
8588
///
8689
public struct ProtobufDouble: ProtobufTypeProperties, ProtobufMapValueType {
8790
public typealias BaseType = Double
88-
public static func describe(value: BaseType) -> String {return value.description}
8991
}
9092

9193
///
9294
/// Int32
9395
///
9496
public struct ProtobufInt32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
9597
public typealias BaseType = Int32
96-
public static func describe(value: BaseType) -> String {return value.description}
9798
}
9899

99100
///
@@ -102,15 +103,13 @@ public struct ProtobufInt32: ProtobufTypeProperties, ProtobufMapKeyType, Protobu
102103

103104
public struct ProtobufInt64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
104105
public typealias BaseType = Int64
105-
public static func describe(value: BaseType) -> String {return value.description}
106106
}
107107

108108
///
109109
/// UInt32
110110
///
111111
public struct ProtobufUInt32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
112112
public typealias BaseType = UInt32
113-
public static func describe(value: BaseType) -> String {return value.description}
114113
}
115114

116115
///
@@ -119,15 +118,13 @@ public struct ProtobufUInt32: ProtobufTypeProperties, ProtobufMapKeyType, Protob
119118

120119
public struct ProtobufUInt64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
121120
public typealias BaseType = UInt64
122-
public static func describe(value: BaseType) -> String {return value.description}
123121
}
124122

125123
///
126124
/// SInt32
127125
///
128126
public struct ProtobufSInt32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
129127
public typealias BaseType = Int32
130-
public static func describe(value: BaseType) -> String {return value.description}
131128
}
132129

133130
///
@@ -136,76 +133,61 @@ public struct ProtobufSInt32: ProtobufTypeProperties, ProtobufMapKeyType, Protob
136133

137134
public struct ProtobufSInt64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
138135
public typealias BaseType = Int64
139-
public static func describe(value: BaseType) -> String {return value.description}
140136
}
141137

142138
///
143139
/// Fixed32
144140
///
145141
public struct ProtobufFixed32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
146142
public typealias BaseType = UInt32
147-
public static func describe(value: BaseType) -> String {return value.description}
148143
}
149144

150145
///
151146
/// Fixed64
152147
///
153148
public struct ProtobufFixed64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
154149
public typealias BaseType = UInt64
155-
public static func describe(value: BaseType) -> String {return value.description}
156150
}
157151

158152
///
159153
/// SFixed32
160154
///
161155
public struct ProtobufSFixed32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
162156
public typealias BaseType = Int32
163-
public static func describe(value: BaseType) -> String {return value.description}
164157
}
165158

166159
///
167160
/// SFixed64
168161
///
169162
public struct ProtobufSFixed64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
170163
public typealias BaseType = Int64
171-
public static func describe(value: BaseType) -> String {return value.description}
172164
}
173165

174166
//
175167
// ========= Bool =========
176168
//
177169
public struct ProtobufBool: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
178170
public typealias BaseType = Bool
179-
public static func describe(value: BaseType) -> String {return value.description}
180171
}
181172

182173
//
183174
// ========== String ==========
184175
//
185176
public struct ProtobufString: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
186177
public typealias BaseType = String
187-
public static func describe(value: BaseType) -> String {return value.debugDescription}
188178
}
189179

190180
//
191181
// ========== Bytes ==========
192182
//
193183
public struct ProtobufBytes: ProtobufTypeProperties, ProtobufMapValueType {
194-
public typealias BaseType = [UInt8]
195-
196-
public static func hash(value: BaseType) -> Int {return ProtobufHash(bytes: value)}
197-
public static func describe(value: BaseType) -> String {return value.debugDescription}
198-
199-
// Note: [UInt8] isn't Equatable, so we can't rely on the default implementation above
200-
// But there is an == overload, so this same definition works here.
201-
public static func isEqual(_ lhs: BaseType, _ rhs: BaseType) -> Bool {return lhs == rhs}
184+
public typealias BaseType = Data
202185
}
203186

204187
//
205188
// ========== Enum ==========
206189
//
207190
extension ProtobufEnum where RawValue == Int {
208-
public static func describe(value: Self) -> String {return String(reflecting: value)}
209191
}
210192

211193
//

0 commit comments

Comments
 (0)