1717///
1818// -----------------------------------------------------------------------------
1919
20+ import Foundation
2021import 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+ ///
3540public 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///
7882public 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///
8689public 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///
9496public 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
103104public 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///
111111public 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
120119public 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///
128126public 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
137134public 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///
145141public 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///
153148public 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///
161155public 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///
169162public 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//
177169public 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//
185176public 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//
193183public 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//
207190extension ProtobufEnum where RawValue == Int {
208- public static func describe( value: Self ) -> String { return String ( reflecting: value) }
209191}
210192
211193//
0 commit comments