ProtobufTypes.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // ProtobufRuntime/Sources/Protobuf/ProtobufTypes.swift - Proto data types
  2. //
  3. // This source file is part of the Swift.org open source project
  4. //
  5. // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See http://swift.org/LICENSE.txt for license information
  9. // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  10. //
  11. // -----------------------------------------------------------------------------
  12. ///
  13. /// Since proto types do not 1:1 map to Swift types, we need a way to
  14. /// specify encoding, hash, equality, and other common abilities.
  15. /// These types are extended in the Binary and JSON portions of the runtime
  16. /// with additional coding and decoding details.
  17. ///
  18. // -----------------------------------------------------------------------------
  19. import Swift
  20. ///
  21. /// Core support for each proto data type.
  22. ///
  23. /// Note that we cannot just extend the standard Int32, etc, types
  24. /// with serialization information since proto language supports
  25. /// distinct types (with different codings) that use the same
  26. /// in-memory representation. For example, proto "sint32" and
  27. /// "sfixed32" both are represented in-memory as Int32.
  28. ///
  29. /// These types are used generically and also passed into
  30. /// various coding/decoding functions to provide type-specific
  31. /// information.
  32. ///
  33. public protocol ProtobufTypePropertiesBase {
  34. // Default here is appropriate for enums and messages
  35. // Other types will override this
  36. associatedtype BaseType = Self
  37. /// Hash the provided value
  38. /// In particular, [UInt8] is not Hashable, so we can't just
  39. /// use .hashValue everywhere.
  40. static func hash(value: BaseType) -> Int
  41. /// In Swift 3, [UInt8] isn't Equatable, so I've added this method
  42. /// to provide a consistent way to compute equality.
  43. static func isEqual(_ lhs: BaseType, _ rhs: BaseType) -> Bool
  44. }
  45. public extension ProtobufTypePropertiesBase where BaseType: Hashable {
  46. public static func hash(value: BaseType) -> Int {return value.hashValue}
  47. public static func isEqual(_ lhs: BaseType, _ rhs: BaseType) -> Bool {return lhs == rhs}
  48. }
  49. public protocol ProtobufTypeProperties: ProtobufJSONCodableType, ProtobufBinaryCodableType {
  50. }
  51. ///
  52. /// Protocol for types that can be used as map keys
  53. ///
  54. public protocol ProtobufMapKeyType: ProtobufJSONCodableMapKeyType, ProtobufBinaryCodableMapKeyType {
  55. }
  56. ///
  57. /// Marker protocol for types that can be used as map values.
  58. ///
  59. public protocol ProtobufMapValueType: ProtobufTypeProperties {
  60. }
  61. //
  62. // We have a protocol for every wire type defining the base serialization for that type.
  63. //
  64. ///
  65. /// Float traits
  66. ///
  67. public struct ProtobufFloat: ProtobufTypeProperties, ProtobufMapValueType {
  68. public typealias BaseType = Float
  69. public static func describe(value: BaseType) -> String {return value.description}
  70. }
  71. ///
  72. /// Double
  73. ///
  74. public struct ProtobufDouble: ProtobufTypeProperties, ProtobufMapValueType {
  75. public typealias BaseType = Double
  76. public static func describe(value: BaseType) -> String {return value.description}
  77. }
  78. ///
  79. /// Int32
  80. ///
  81. public struct ProtobufInt32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  82. public typealias BaseType = Int32
  83. public static func describe(value: BaseType) -> String {return value.description}
  84. }
  85. ///
  86. /// Int64
  87. ///
  88. public struct ProtobufInt64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  89. public typealias BaseType = Int64
  90. public static func describe(value: BaseType) -> String {return value.description}
  91. }
  92. ///
  93. /// UInt32
  94. ///
  95. public struct ProtobufUInt32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  96. public typealias BaseType = UInt32
  97. public static func describe(value: BaseType) -> String {return value.description}
  98. }
  99. ///
  100. /// UInt64
  101. ///
  102. public struct ProtobufUInt64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  103. public typealias BaseType = UInt64
  104. public static func describe(value: BaseType) -> String {return value.description}
  105. }
  106. ///
  107. /// SInt32
  108. ///
  109. public struct ProtobufSInt32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  110. public typealias BaseType = Int32
  111. public static func describe(value: BaseType) -> String {return value.description}
  112. }
  113. ///
  114. /// SInt64
  115. ///
  116. public struct ProtobufSInt64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  117. public typealias BaseType = Int64
  118. public static func describe(value: BaseType) -> String {return value.description}
  119. }
  120. ///
  121. /// Fixed32
  122. ///
  123. public struct ProtobufFixed32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  124. public typealias BaseType = UInt32
  125. public static func describe(value: BaseType) -> String {return value.description}
  126. }
  127. ///
  128. /// Fixed64
  129. ///
  130. public struct ProtobufFixed64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  131. public typealias BaseType = UInt64
  132. public static func describe(value: BaseType) -> String {return value.description}
  133. }
  134. ///
  135. /// SFixed32
  136. ///
  137. public struct ProtobufSFixed32: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  138. public typealias BaseType = Int32
  139. public static func describe(value: BaseType) -> String {return value.description}
  140. }
  141. ///
  142. /// SFixed64
  143. ///
  144. public struct ProtobufSFixed64: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  145. public typealias BaseType = Int64
  146. public static func describe(value: BaseType) -> String {return value.description}
  147. }
  148. //
  149. // ========= Bool =========
  150. //
  151. public struct ProtobufBool: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  152. public typealias BaseType = Bool
  153. public static func describe(value: BaseType) -> String {return value.description}
  154. }
  155. //
  156. // ========== String ==========
  157. //
  158. public struct ProtobufString: ProtobufTypeProperties, ProtobufMapKeyType, ProtobufMapValueType {
  159. public typealias BaseType = String
  160. public static func describe(value: BaseType) -> String {return value.debugDescription}
  161. }
  162. //
  163. // ========== Bytes ==========
  164. //
  165. public struct ProtobufBytes: ProtobufTypeProperties, ProtobufMapValueType {
  166. public typealias BaseType = [UInt8]
  167. public static func hash(value: BaseType) -> Int {return ProtobufHash(bytes: value)}
  168. public static func describe(value: BaseType) -> String {return value.debugDescription}
  169. // Note: [UInt8] isn't Equatable, so we can't rely on the default implementation above
  170. // But there is an == overload, so this same definition works here.
  171. public static func isEqual(_ lhs: BaseType, _ rhs: BaseType) -> Bool {return lhs == rhs}
  172. }
  173. //
  174. // ========== Enum ==========
  175. //
  176. extension ProtobufEnum where RawValue == Int {
  177. public static func describe(value: Self) -> String {return String(reflecting: value)}
  178. }
  179. //
  180. // ========== Maps ===========
  181. //
  182. // Not a full ProtobufTypeProperties, just a generic marker
  183. // for propagating the key/value types.
  184. //
  185. public struct ProtobufMap<MapKeyType: ProtobufMapKeyType, MapValueType: ProtobufMapValueType>
  186. where MapKeyType.BaseType: Hashable
  187. {
  188. typealias Key = MapKeyType.BaseType
  189. typealias Value = MapValueType.BaseType
  190. public typealias BaseType = Dictionary<Key, Value>
  191. }