Google_Protobuf_Wrappers.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // ProtobufRuntime/Sources/Protobuf/Google_Protobuf_Wrappers.swift - Well-known Wrapper 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. /// This is a hand-built implementation of the well-known wrapper types.
  14. /// It exploits a common generic protocol to reduce duplicated code.
  15. ///
  16. // -----------------------------------------------------------------------------
  17. import Swift
  18. public protocol Google_Protobuf_Wrapper: ProtobufAbstractMessage, Hashable, CustomReflectable {
  19. associatedtype WrappedType: ProtobufTypeProperties
  20. var protoMessageName: String { get }
  21. var value: WrappedType.BaseType? { get set }
  22. }
  23. public extension Google_Protobuf_Wrapper {
  24. var protoPackageName: String {return "google.protobuf"}
  25. var swiftClassName: String {return "Google_Protobuf_" + protoMessageName}
  26. var isEmpty: Bool {return (value == nil)}
  27. var jsonFieldNames: [String:Int] {return ["value":1]}
  28. var protoFieldNames: [String:Int] {return ["value":1]}
  29. public init(_ value: WrappedType.BaseType) {
  30. self.init()
  31. self.value = value
  32. }
  33. public mutating func decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
  34. switch protoFieldNumber {
  35. case 1: return try setter.decodeOptionalField(fieldType: WrappedType.self, value: &value)
  36. default: return false
  37. }
  38. }
  39. public mutating func decodeField(setter: inout ProtobufFieldDecoder, protoFieldName: String) throws -> Bool {
  40. return try decodeField(setter: &setter, protoFieldNumber: 1)
  41. }
  42. public mutating func decodeField(setter: inout ProtobufFieldDecoder, jsonFieldName: String) throws -> Bool {
  43. return try decodeField(setter: &setter, protoFieldNumber: 1)
  44. }
  45. func serializeJSON() throws -> String {
  46. if let value = value {
  47. var encoder = ProtobufJSONEncoder()
  48. try WrappedType.serializeJSONValue(encoder: &encoder, value: value)
  49. return encoder.result
  50. } else {
  51. return "null"
  52. }
  53. }
  54. func serializeAnyJSON() throws -> String {
  55. let value = try serializeJSON()
  56. return "{\"@type\":\"\(anyTypeURL)\",\"value\":\(value)}"
  57. }
  58. public func traverse(visitor: inout ProtobufVisitor) throws {
  59. if let value = value {
  60. try visitor.visitSingularField(fieldType: WrappedType.self, value: value, protoFieldNumber: 1, protoFieldName: "value", jsonFieldName: "value", swiftFieldName: "value")
  61. }
  62. }
  63. }
  64. public extension Google_Protobuf_Wrapper where WrappedType.BaseType: Equatable {
  65. public func isEqualTo(other: Self) -> Bool {
  66. return value == other.value
  67. }
  68. }
  69. /// Wrapper message for `double`.
  70. ///
  71. /// The JSON representation for `DoubleValue` is JSON number.
  72. public struct Google_Protobuf_DoubleValue: Google_Protobuf_Wrapper, ExpressibleByFloatLiteral {
  73. public typealias WrappedType = ProtobufDouble
  74. public var protoMessageName: String {return "DoubleValue"}
  75. public var value: WrappedType.BaseType?
  76. public init() {}
  77. public typealias FloatLiteralType = Double
  78. public init(floatLiteral: FloatLiteralType) {value = floatLiteral}
  79. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  80. if let t = token.asDouble {
  81. value = t
  82. } else {
  83. throw ProtobufDecodingError.malformedJSONNumber
  84. }
  85. }
  86. }
  87. /// Wrapper message for `float`.
  88. ///
  89. /// The JSON representation for `FloatValue` is JSON number.
  90. public struct Google_Protobuf_FloatValue: Google_Protobuf_Wrapper, ExpressibleByFloatLiteral {
  91. public typealias WrappedType = ProtobufFloat
  92. public var protoMessageName: String {return "FloatValue"}
  93. public var value: WrappedType.BaseType?
  94. public init() {}
  95. public typealias FloatLiteralType = Float
  96. public init(floatLiteral: FloatLiteralType) {value = floatLiteral}
  97. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  98. if let t = token.asFloat {
  99. value = t
  100. } else {
  101. throw ProtobufDecodingError.malformedJSONNumber
  102. }
  103. }
  104. }
  105. /// Wrapper message for `int64`.
  106. ///
  107. /// The JSON representation for `Int64Value` is JSON string.
  108. public struct Google_Protobuf_Int64Value: Google_Protobuf_Wrapper {
  109. public typealias WrappedType = ProtobufInt64
  110. public var protoMessageName: String {return "Int64Value"}
  111. public var value: WrappedType.BaseType?
  112. public init() {}
  113. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  114. if let t = token.asInt64 {
  115. value = t
  116. } else {
  117. throw ProtobufDecodingError.malformedJSONNumber
  118. }
  119. }
  120. }
  121. /// Wrapper message for `uint64`.
  122. ///
  123. /// The JSON representation for `UInt64Value` is JSON string.
  124. public struct Google_Protobuf_UInt64Value: Google_Protobuf_Wrapper {
  125. public typealias WrappedType = ProtobufUInt64
  126. public var protoMessageName: String {return "UInt64Value"}
  127. public var value: WrappedType.BaseType?
  128. public init() {}
  129. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  130. if let t = token.asUInt64 {
  131. value = t
  132. } else {
  133. throw ProtobufDecodingError.malformedJSONNumber
  134. }
  135. }
  136. }
  137. /// Wrapper message for `int32`.
  138. ///
  139. /// The JSON representation for `Int32Value` is JSON number.
  140. public struct Google_Protobuf_Int32Value: Google_Protobuf_Wrapper {
  141. public typealias WrappedType = ProtobufInt32
  142. public var protoMessageName: String {return "Int32Value"}
  143. public var value: WrappedType.BaseType?
  144. public init() {}
  145. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  146. if let t = token.asInt32 {
  147. value = t
  148. } else {
  149. throw ProtobufDecodingError.malformedJSONNumber
  150. }
  151. }
  152. }
  153. /// Wrapper message for `uint32`.
  154. ///
  155. /// The JSON representation for `UInt32Value` is JSON number.
  156. public struct Google_Protobuf_UInt32Value: Google_Protobuf_Wrapper {
  157. public typealias WrappedType = ProtobufUInt32
  158. public var protoMessageName: String {return "UInt32Value"}
  159. public var value: WrappedType.BaseType?
  160. public init() {}
  161. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  162. if let t = token.asUInt32 {
  163. value = t
  164. } else {
  165. throw ProtobufDecodingError.malformedJSONNumber
  166. }
  167. }
  168. }
  169. /// Wrapper message for `bool`.
  170. ///
  171. /// The JSON representation for `BoolValue` is JSON `true` and `false`.
  172. public struct Google_Protobuf_BoolValue: Google_Protobuf_Wrapper, ExpressibleByBooleanLiteral {
  173. public typealias WrappedType = ProtobufBool
  174. public var protoMessageName: String {return "BoolValue"}
  175. public var value: WrappedType.BaseType?
  176. public init() {}
  177. public typealias BooleanLiteralType = Bool
  178. public init(booleanLiteral: Bool) {value = booleanLiteral}
  179. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  180. if let t = token.asBoolean {
  181. value = t
  182. } else {
  183. throw ProtobufDecodingError.schemaMismatch
  184. }
  185. }
  186. }
  187. /// Wrapper message for `string`.
  188. ///
  189. /// The JSON representation for `StringValue` is JSON string.
  190. public struct Google_Protobuf_StringValue: Google_Protobuf_Wrapper, ExpressibleByStringLiteral {
  191. public typealias WrappedType = ProtobufString
  192. public var protoMessageName: String {return "StringValue"}
  193. public var value: WrappedType.BaseType?
  194. public init() {}
  195. public typealias StringLiteralType = String
  196. public init(stringLiteral: String) {value = stringLiteral}
  197. public typealias ExtendedGraphemeClusterLiteralType = String
  198. public init(extendedGraphemeClusterLiteral: String) {value = extendedGraphemeClusterLiteral}
  199. public typealias UnicodeScalarLiteralType = String
  200. public init(unicodeScalarLiteral: String) {value = unicodeScalarLiteral}
  201. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  202. if case .string(let s) = token {
  203. value = s
  204. } else {
  205. throw ProtobufDecodingError.schemaMismatch
  206. }
  207. }
  208. }
  209. /// Wrapper message for `bytes`.
  210. ///
  211. /// The JSON representation for `BytesValue` is JSON string.
  212. public struct Google_Protobuf_BytesValue: Google_Protobuf_Wrapper {
  213. public typealias WrappedType = ProtobufBytes
  214. public var protoMessageName: String {return "BytesValue"}
  215. public var value: WrappedType.BaseType?
  216. public init() {}
  217. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  218. if let t = token.asBytes {
  219. value = t
  220. } else {
  221. throw ProtobufDecodingError.schemaMismatch
  222. }
  223. }
  224. public func isEqualTo(other: Google_Protobuf_BytesValue) -> Bool {
  225. if let l = value {
  226. if let r = other.value {
  227. return l == r
  228. }
  229. return l.isEmpty
  230. } else if let r = value {
  231. return r.isEmpty
  232. } else {
  233. return true
  234. }
  235. }
  236. }