Google_Protobuf_Struct.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. // ProtobufRuntime/Sources/Protobuf/Google_Protobuf_Struct.swift - Well-known Struct 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. /// Struct, Value, ListValue are well-known message types that can be used
  14. /// to parse or encode arbitrary JSON without a predefined schema.
  15. ///
  16. // -----------------------------------------------------------------------------
  17. import Swift
  18. /*
  19. * Hand-built implementation.
  20. */
  21. public enum Google_Protobuf_NullValue: ProtobufEnum {
  22. // TODO: This is awkward, see the references to .NullValue(.NullValue) below.
  23. // TODO: The .nullValue property on Google_Protobuf_Value has to have
  24. // a type; but this is a little weird.
  25. public typealias RawValue = Int
  26. /// Null value.
  27. case nullValue
  28. public init?(rawValue: Int) {self = .nullValue}
  29. public init?(name: String) {self = .nullValue}
  30. public init?(jsonName: String) {self = .nullValue}
  31. public init?(protoName: String) {self = .nullValue}
  32. public init() {self = .nullValue}
  33. public var rawValue: Int {return 0}
  34. public var json: String {return "null"}
  35. public var hashValue: Int {return 0}
  36. public var debugDescription: String {return "NullValue"}
  37. }
  38. /// `Struct` represents a structured data value, consisting of fields
  39. /// which map to dynamically typed values. In some languages, `Struct`
  40. /// might be supported by a native representation. For example, in
  41. /// scripting languages like JS a struct is represented as an
  42. /// object. The details of that representation are described together
  43. /// with the proto support for the language.
  44. ///
  45. /// The JSON representation for `Struct` is JSON object.
  46. // Should Google_Protobuf_Struct be a synonym for [String: Any]?
  47. // TODO: Implement CollectionType
  48. public struct Google_Protobuf_Struct: ProtobufGeneratedMessage, Hashable, CustomReflectable, ExpressibleByDictionaryLiteral {
  49. public var swiftClassName: String {return "Google_Protobuf_Struct"}
  50. public var protoMessageName: String {return "Struct"}
  51. public var protoPackageName: String {return "google.protobuf"}
  52. public var jsonFieldNames: [String:Int] {return ["fields":1]}
  53. public var protoFieldNames: [String:Int] {return ["fields":1]}
  54. public typealias Key = String
  55. public typealias Value = Google_Protobuf_Value
  56. /// Unordered map of dynamically typed values.
  57. public var fields: Dictionary<String,Google_Protobuf_Value> = [:]
  58. public init() {}
  59. public init(fields: [String: Google_Protobuf_Value]) {
  60. self.fields = fields
  61. }
  62. public init(dictionaryLiteral: (String, Google_Protobuf_Value)...) {
  63. fields = [:]
  64. for (k,v) in dictionaryLiteral {
  65. fields[k] = v
  66. }
  67. }
  68. public subscript(index: String) -> Google_Protobuf_Value? {
  69. get {return fields[index]}
  70. set(newValue) {fields[index] = newValue}
  71. }
  72. public mutating func decodeFromJSONObject(jsonDecoder: inout ProtobufJSONDecoder) throws {
  73. var key = ""
  74. var state = ProtobufJSONDecoder.ObjectParseState.expectFirstKey
  75. while let token = try jsonDecoder.nextToken() {
  76. switch token {
  77. case .string(let s): // This is a key
  78. if state != .expectKey && state != .expectFirstKey {
  79. throw ProtobufDecodingError.malformedJSON
  80. }
  81. key = s
  82. state = .expectColon
  83. case .colon:
  84. if state != .expectColon {
  85. throw ProtobufDecodingError.malformedJSON
  86. }
  87. guard let valueToken = try jsonDecoder.nextToken() else {
  88. throw ProtobufDecodingError.malformedJSON
  89. }
  90. var value = Google_Protobuf_Value()
  91. switch valueToken {
  92. case .number(_), .string(_), .boolean(_):
  93. try value.decodeFromJSONToken(token: valueToken)
  94. case .null:
  95. break
  96. case .beginArray:
  97. try value.decodeFromJSONArray(jsonDecoder: &jsonDecoder)
  98. case .beginObject:
  99. try value.decodeFromJSONObject(jsonDecoder: &jsonDecoder)
  100. default:
  101. throw ProtobufDecodingError.malformedJSON
  102. }
  103. fields[key] = value
  104. state = .expectComma
  105. case .comma:
  106. if state != .expectComma {
  107. throw ProtobufDecodingError.malformedJSON
  108. }
  109. state = .expectKey
  110. case .endObject:
  111. if state != .expectFirstKey && state != .expectComma {
  112. throw ProtobufDecodingError.malformedJSON
  113. }
  114. return
  115. default:
  116. throw ProtobufDecodingError.malformedJSON
  117. }
  118. }
  119. throw ProtobufDecodingError.truncatedInput
  120. }
  121. public func serializeJSON() throws -> String {
  122. var jsonEncoder = ProtobufJSONEncoder()
  123. jsonEncoder.startObject()
  124. for (k,v) in fields {
  125. jsonEncoder.startField(name: k)
  126. try v.serializeJSONValue(jsonEncoder: &jsonEncoder)
  127. }
  128. jsonEncoder.endObject()
  129. return jsonEncoder.result
  130. }
  131. public func serializeAnyJSON() throws -> String {
  132. let value = try serializeJSON()
  133. return "{\"@type\":\"\(anyTypeURL)\",\"value\":\(value)}"
  134. }
  135. public mutating func _protoc_generated_decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
  136. var handled = false
  137. switch protoFieldNumber {
  138. case 1: handled = try setter.decodeMapField(fieldType: ProtobufMap<ProtobufString,Google_Protobuf_Value>.self, value: &fields)
  139. default:
  140. break
  141. }
  142. return handled
  143. }
  144. public func _protoc_generated_traverse(visitor: inout ProtobufVisitor) throws {
  145. if !fields.isEmpty {
  146. try visitor.visitMapField(fieldType: ProtobufMap<ProtobufString,Google_Protobuf_Value>.self, value: fields, protoFieldNumber: 1, protoFieldName: "fields", jsonFieldName: "fields", swiftFieldName: "fields")
  147. }
  148. }
  149. public var _protoc_generated_isEmpty: Bool {
  150. if !fields.isEmpty {return false}
  151. return true
  152. }
  153. public func _protoc_generated_isEqualTo(other: Google_Protobuf_Struct) -> Bool {
  154. if fields != other.fields {return false}
  155. return true
  156. }
  157. }
  158. /// `Value` represents a dynamically typed value which can be either
  159. /// null, a number, a string, a boolean, a recursive struct value, or a
  160. /// list of values. A producer of value is expected to set one of that
  161. /// variants, absence of any variant indicates an error.
  162. ///
  163. /// The JSON representation for `Value` is JSON value.
  164. public struct Google_Protobuf_Value: ProtobufAbstractMessage, Hashable, CustomReflectable, ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByStringLiteral, ExpressibleByBooleanLiteral, ExpressibleByNilLiteral {
  165. public var swiftClassName: String {return "Google_Protobuf_Value"}
  166. public var protoMessageName: String {return "Value"}
  167. public var protoPackageName: String {return "google.protobuf"}
  168. public var jsonFieldNames: [String: Int] {return [
  169. "nullValue": 1,
  170. "numberValue": 2,
  171. "stringValue": 3,
  172. "boolValue": 4,
  173. "structValue": 5,
  174. "listValue": 6,
  175. ]}
  176. public var protoFieldNames: [String: Int] {return [
  177. "null_value": 1,
  178. "number_value": 2,
  179. "string_value": 3,
  180. "bool_value": 4,
  181. "struct_value": 5,
  182. "list_value": 6,
  183. ]}
  184. // TODO: Would it make sense to collapse the implementation here and
  185. // make Google_Protobuf_Value be the enum directly?
  186. public typealias FloatLiteralType = Double
  187. public typealias IntegerLiteralType = Int64
  188. public typealias StringLiteralType = String
  189. public typealias ExtendedGraphemeClusterLiteralType = String
  190. public typealias UnicodeScalarLiteralType = String
  191. public typealias BooleanLiteralType = Bool
  192. public init() {
  193. kind = .nullValue(.nullValue)
  194. }
  195. public init(nullValue: ()) {
  196. kind = .nullValue(.nullValue)
  197. }
  198. public init(nilLiteral: ()) {
  199. kind = .nullValue(.nullValue)
  200. }
  201. public init(numberValue: Double) {
  202. kind = .numberValue(numberValue)
  203. }
  204. public init(integerLiteral value: Int64) {
  205. kind = .numberValue(Double(value))
  206. }
  207. public init(floatLiteral value: Double) {
  208. kind = .numberValue(value)
  209. }
  210. public init(stringValue: String) {
  211. kind = .stringValue(stringValue)
  212. }
  213. public init(stringLiteral value: String) {
  214. kind = .stringValue(value)
  215. }
  216. public init(unicodeScalarLiteral value: String) {
  217. kind = .stringValue(value)
  218. }
  219. public init(extendedGraphemeClusterLiteral value: String) {
  220. kind = .stringValue(value)
  221. }
  222. public init(boolValue: Bool) {
  223. kind = .boolValue(boolValue)
  224. }
  225. public init(booleanLiteral value: Bool) {
  226. kind = .boolValue(value)
  227. }
  228. public init(structValue: Google_Protobuf_Struct) {
  229. kind = .structValue(structValue)
  230. }
  231. public init(listValue: Google_Protobuf_ListValue) {
  232. kind = .listValue(listValue)
  233. }
  234. public init<T>(array: [T]) {
  235. let anyList = array.map {$0 as Any}
  236. kind = .listValue(Google_Protobuf_ListValue(any: anyList))
  237. }
  238. public init(anyArray: [Any]) {
  239. kind = .listValue(Google_Protobuf_ListValue(any: anyArray))
  240. }
  241. public init(any: Any) {
  242. switch any {
  243. case let i as Int:
  244. self.init(numberValue: Double(i))
  245. case let d as Double:
  246. self.init(numberValue: d)
  247. case let f as Float:
  248. self.init(numberValue: Double(f))
  249. case let b as Bool:
  250. self.init(boolValue: b)
  251. case let s as String:
  252. self.init(stringValue: s)
  253. default:
  254. self.init()
  255. }
  256. }
  257. mutating public func decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
  258. switch protoFieldNumber {
  259. case 1, 2, 3, 4, 5, 6:
  260. return try kind.decodeField(setter: &setter, protoFieldNumber: protoFieldNumber)
  261. default: return false
  262. }
  263. }
  264. public static func decodeFromJSONNull() throws -> Google_Protobuf_Value? {
  265. return Google_Protobuf_Value()
  266. }
  267. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  268. try kind.decodeFromJSONToken(token: token)
  269. }
  270. public mutating func decodeFromJSONObject(jsonDecoder: inout ProtobufJSONDecoder) throws {
  271. var s = Google_Protobuf_Struct()
  272. try s.decodeFromJSONObject(jsonDecoder: &jsonDecoder)
  273. kind = .structValue(s)
  274. }
  275. public mutating func decodeFromJSONArray(jsonDecoder: inout ProtobufJSONDecoder) throws {
  276. var s = Google_Protobuf_ListValue()
  277. try s.decodeFromJSONArray(jsonDecoder: &jsonDecoder)
  278. kind = .listValue(s)
  279. }
  280. public func serializeJSON() throws -> String {
  281. var jsonEncoder = ProtobufJSONEncoder()
  282. try serializeJSONValue(jsonEncoder: &jsonEncoder)
  283. return jsonEncoder.result
  284. }
  285. public func serializeAnyJSON() throws -> String {
  286. let value = try serializeJSON()
  287. return "{\"@type\":\"\(anyTypeURL)\",\"value\":\(value)}"
  288. }
  289. fileprivate func serializeJSONValue(jsonEncoder: inout ProtobufJSONEncoder) throws {
  290. try kind.serializeJSONField(encoder: &jsonEncoder)
  291. }
  292. public var isEmpty: Bool {return kind == .None}
  293. public func isEqualTo(other: Google_Protobuf_Value) -> Bool {
  294. return kind == other.kind
  295. }
  296. public init(any: Google_Protobuf_Any) throws {
  297. try any.unpackTo(target: &self)
  298. }
  299. public var debugDescription: String {
  300. get {
  301. do {
  302. let json = try serializeJSON()
  303. switch kind {
  304. case .nullValue(_): return "\(swiftClassName)(null)"
  305. case .numberValue(_): return "\(swiftClassName)(numberValue:\(json))"
  306. case .stringValue(_): return"\(swiftClassName)(stringValue:\(json))"
  307. case .boolValue(_): return"\(swiftClassName)(boolValue:\(json))"
  308. case .structValue(_): return"\(swiftClassName)(structValue:\(json))"
  309. case .listValue(_): return"\(swiftClassName)(listValue:\(json))"
  310. case .None: return "\(swiftClassName)()"
  311. }
  312. } catch let e {
  313. return "\(swiftClassName)(FAILURE: \(e))"
  314. }
  315. }
  316. }
  317. public func traverse(visitor: inout ProtobufVisitor) throws {
  318. try kind.traverse(visitor: &visitor, start:1, end: 7)
  319. }
  320. // Storage ivars
  321. private var kind = Google_Protobuf_Value.OneOf_Kind()
  322. /// Represents a null value.
  323. public var nullValue: Google_Protobuf_NullValue? {
  324. get {
  325. if case .nullValue(let v) = kind {
  326. return v
  327. }
  328. return nil
  329. }
  330. set {
  331. if let newValue = newValue {
  332. kind = .nullValue(newValue)
  333. } else {
  334. kind = .None
  335. }
  336. }
  337. }
  338. /// Represents a double value.
  339. public var numberValue: Double? {
  340. get {
  341. if case .numberValue(let v) = kind {
  342. return v
  343. }
  344. return nil
  345. }
  346. set {
  347. if let newValue = newValue {
  348. kind = .numberValue(newValue)
  349. } else {
  350. kind = .None
  351. }
  352. }
  353. }
  354. /// Represents a string value.
  355. public var stringValue: String? {
  356. get {
  357. if case .stringValue(let v) = kind {
  358. return v
  359. }
  360. return nil
  361. }
  362. set {
  363. if let newValue = newValue {
  364. kind = .stringValue(newValue)
  365. } else {
  366. kind = .None
  367. }
  368. }
  369. }
  370. /// Represents a boolean value.
  371. public var boolValue: Bool? {
  372. get {
  373. if case .boolValue(let v) = kind {
  374. return v
  375. }
  376. return nil
  377. }
  378. set {
  379. if let newValue = newValue {
  380. kind = .boolValue(newValue)
  381. } else {
  382. kind = .None
  383. }
  384. }
  385. }
  386. /// Represents a structured value.
  387. public var structValue: Google_Protobuf_Struct? {
  388. get {
  389. if case .structValue(let v) = kind {
  390. return v
  391. }
  392. return nil
  393. }
  394. set {
  395. if let newValue = newValue {
  396. kind = .structValue(newValue)
  397. } else {
  398. kind = .None
  399. }
  400. }
  401. }
  402. /// Represents a repeated `Value`.
  403. public var listValue: Google_Protobuf_ListValue? {
  404. get {
  405. if case .listValue(let v) = kind {
  406. return v
  407. }
  408. return nil
  409. }
  410. set {
  411. if let newValue = newValue {
  412. kind = .listValue(newValue)
  413. } else {
  414. kind = .None
  415. }
  416. }
  417. }
  418. public enum OneOf_Kind: ExpressibleByNilLiteral, ProtobufOneofEnum {
  419. case nullValue(Google_Protobuf_NullValue)
  420. case numberValue(Double)
  421. case stringValue(String)
  422. case boolValue(Bool)
  423. case structValue(Google_Protobuf_Struct)
  424. case listValue(Google_Protobuf_ListValue)
  425. case None
  426. public init(nilLiteral: ()) {
  427. self = .None
  428. }
  429. public init() {
  430. self = .None
  431. }
  432. public mutating func decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
  433. let handled: Bool
  434. switch protoFieldNumber {
  435. case 1:
  436. var value: Google_Protobuf_NullValue?
  437. handled = try setter.decodeOptionalField(fieldType: Google_Protobuf_NullValue.self, value: &value)
  438. if let value = value, handled {
  439. self = .nullValue(value)
  440. }
  441. case 2:
  442. var value: Double?
  443. handled = try setter.decodeOptionalField(fieldType: ProtobufDouble.self, value: &value)
  444. if let value = value, handled {
  445. self = .numberValue(value)
  446. }
  447. case 3:
  448. var value: String?
  449. handled = try setter.decodeOptionalField(fieldType: ProtobufString.self, value: &value)
  450. if let value = value, handled {
  451. self = .stringValue(value)
  452. }
  453. case 4:
  454. var value: Bool?
  455. handled = try setter.decodeOptionalField(fieldType: ProtobufBool.self, value: &value)
  456. if let value = value, handled {
  457. self = .boolValue(value)
  458. }
  459. case 5:
  460. var value: Google_Protobuf_Struct?
  461. handled = try setter.decodeOptionalMessageField(fieldType: Google_Protobuf_Struct.self, value: &value)
  462. if let value = value, handled {
  463. self = .structValue(value)
  464. }
  465. case 6:
  466. var value: Google_Protobuf_ListValue?
  467. handled = try setter.decodeOptionalMessageField(fieldType: Google_Protobuf_ListValue.self, value: &value)
  468. if let value = value, handled {
  469. self = .listValue(value)
  470. }
  471. default:
  472. throw ProtobufDecodingError.schemaMismatch
  473. }
  474. return true
  475. }
  476. fileprivate func serializeJSONField(encoder: inout ProtobufJSONEncoder) throws {
  477. switch self {
  478. case .nullValue(_): encoder.putNullValue()
  479. case .numberValue(let v): encoder.putDoubleValue(value: v, quote: false)
  480. case .stringValue(let v): encoder.putStringValue(value: v)
  481. case .boolValue(let v): encoder.putBoolValue(value: v, quote: false)
  482. case .structValue(let v): encoder.append(text: try v.serializeJSON())
  483. case .listValue(let v): encoder.append(text: try v.serializeJSON())
  484. case .None:
  485. break
  486. }
  487. }
  488. public mutating func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  489. switch token {
  490. case .null:
  491. self = .nullValue(.nullValue)
  492. case .number(_):
  493. if let value = token.asDouble {
  494. self = .numberValue(value)
  495. } else {
  496. throw ProtobufDecodingError.malformedJSONNumber
  497. }
  498. case .string(let s):
  499. self = .stringValue(s)
  500. case .boolean(let b):
  501. self = .boolValue(b)
  502. default:
  503. throw ProtobufDecodingError.schemaMismatch
  504. }
  505. }
  506. public func traverse(visitor: inout ProtobufVisitor, start: Int, end: Int) throws {
  507. switch self {
  508. case .nullValue(let v):
  509. if start <= 1 && 1 < end {
  510. try visitor.visitSingularField(fieldType: Google_Protobuf_NullValue.self, value: v, protoFieldNumber: 1, protoFieldName: "null_value", jsonFieldName: "value", swiftFieldName: "nullValue")
  511. }
  512. case .numberValue(let v):
  513. if start <= 2 && 2 < end {
  514. try visitor.visitSingularField(fieldType: ProtobufDouble.self, value: v, protoFieldNumber: 2, protoFieldName: "number_value", jsonFieldName: "value", swiftFieldName: "numberValue")
  515. }
  516. case .stringValue(let v):
  517. if start <= 3 && 3 < end {
  518. try visitor.visitSingularField(fieldType: ProtobufString.self, value: v, protoFieldNumber: 3, protoFieldName: "string_value", jsonFieldName: "value", swiftFieldName: "stringValue")
  519. }
  520. case .boolValue(let v):
  521. if start <= 4 && 4 < end {
  522. try visitor.visitSingularField(fieldType: ProtobufBool.self, value: v, protoFieldNumber: 4, protoFieldName: "bool_value", jsonFieldName: "value", swiftFieldName: "boolValue")
  523. }
  524. case .structValue(let v):
  525. if start <= 5 && 5 < end {
  526. try visitor.visitSingularMessageField(value: v, protoFieldNumber: 5, protoFieldName: "struct_value", jsonFieldName: "value", swiftFieldName: "structValue")
  527. }
  528. case .listValue(let v):
  529. if start <= 6 && 6 < end {
  530. try visitor.visitSingularMessageField(value: v, protoFieldNumber: 6, protoFieldName: "list_value", jsonFieldName: "value", swiftFieldName: "listValue")
  531. }
  532. case .None:
  533. break
  534. }
  535. }
  536. public var hashValue: Int {
  537. switch self {
  538. case .nullValue(_): return 1
  539. case .numberValue(let v): return v.hashValue
  540. case .stringValue(let v): return v.hashValue
  541. case .boolValue(let v): return v.hashValue
  542. case .structValue(let v): return v.hashValue
  543. case .listValue(let v): return v.hashValue
  544. case .None: return 0
  545. }
  546. }
  547. public var isEmpty: Bool {return self == .None}
  548. }
  549. }
  550. /// `ListValue` is a wrapper around a repeated field of values.
  551. ///
  552. /// The JSON representation for `ListValue` is JSON array.
  553. public struct Google_Protobuf_ListValue: ProtobufAbstractMessage, Hashable, CustomReflectable, ExpressibleByArrayLiteral {
  554. public var swiftClassName: String {return "Google_Protobuf_ListValue"}
  555. public var protoMessageName: String {return "ListValue"}
  556. public var protoPackageName: String {return "google.protobuf"}
  557. public var jsonFieldNames: [String:Int] {return ["values":1]}
  558. public var protoFieldNames: [String:Int] {return ["values":1]}
  559. // TODO: Give this a direct array interface by proxying the interesting
  560. // bits down to values
  561. public typealias Element = Google_Protobuf_Value
  562. /// Repeated field of dynamically typed values.
  563. public var values: [Google_Protobuf_Value] = []
  564. public init() {}
  565. public init(values: [Google_Protobuf_Value]) {
  566. self.values = values
  567. }
  568. public init(arrayLiteral elements: Google_Protobuf_ListValue.Element...) {
  569. values = elements
  570. }
  571. public init(any: [Any]) {
  572. values = any.map {Google_Protobuf_Value(any: $0)}
  573. }
  574. public subscript(index: Int) -> Google_Protobuf_Value {
  575. get {return values[index]}
  576. set(newValue) {values[index] = newValue}
  577. }
  578. public mutating func decodeFromJSONObject(jsonDecoder: inout ProtobufJSONDecoder) throws {
  579. throw ProtobufDecodingError.schemaMismatch
  580. }
  581. public mutating func decodeFromJSONArray(jsonDecoder: inout ProtobufJSONDecoder) throws {
  582. var firstItem = true
  583. while true {
  584. guard let token = try jsonDecoder.nextToken() else {
  585. throw ProtobufDecodingError.truncatedInput
  586. }
  587. switch token {
  588. case .endArray:
  589. if !firstItem {
  590. throw ProtobufDecodingError.malformedJSON
  591. }
  592. return
  593. case .null:
  594. values.append(Google_Protobuf_Value())
  595. case .beginObject:
  596. var message = Google_Protobuf_Value()
  597. try message.decodeFromJSONObject(jsonDecoder: &jsonDecoder)
  598. values.append(message)
  599. case .beginArray:
  600. var message = Google_Protobuf_Value()
  601. try message.decodeFromJSONArray(jsonDecoder: &jsonDecoder)
  602. values.append(message)
  603. case .boolean(_), .string(_), .number(_):
  604. var message = Google_Protobuf_Value()
  605. try message.decodeFromJSONToken(token: token)
  606. values.append(message)
  607. default:
  608. throw ProtobufDecodingError.malformedJSON
  609. }
  610. firstItem = false
  611. if let separatorToken = try jsonDecoder.nextToken() {
  612. switch separatorToken {
  613. case .comma: break
  614. case .endArray: return
  615. default: throw ProtobufDecodingError.malformedJSON
  616. }
  617. }
  618. }
  619. }
  620. public func serializeJSON() throws -> String {
  621. var jsonEncoder = ProtobufJSONEncoder()
  622. jsonEncoder.append(text: "[")
  623. var separator = ""
  624. for v in values {
  625. jsonEncoder.append(text: separator)
  626. try v.serializeJSONValue(jsonEncoder: &jsonEncoder)
  627. separator = ","
  628. }
  629. jsonEncoder.append(text: "]")
  630. return jsonEncoder.result
  631. }
  632. public func serializeAnyJSON() throws -> String {
  633. let value = try serializeJSON()
  634. return "{\"@type\":\"\(anyTypeURL)\",\"value\":\(value)}"
  635. }
  636. public init(any: Google_Protobuf_Any) throws {
  637. try any.unpackTo(target: &self)
  638. }
  639. public var debugDescription: String {
  640. get {
  641. do {
  642. let json = try serializeJSON()
  643. return "\(swiftClassName)(\(json))"
  644. } catch let e {
  645. return "\(swiftClassName)(FAILURE: \(e))"
  646. }
  647. }
  648. }
  649. mutating public func decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
  650. switch protoFieldNumber {
  651. case 1: return try setter.decodeRepeatedMessageField(fieldType: Google_Protobuf_Value.self, value: &values)
  652. default: return false
  653. }
  654. }
  655. public func traverse(visitor: inout ProtobufVisitor) throws {
  656. if !values.isEmpty {
  657. try visitor.visitRepeatedMessageField(value: values, protoFieldNumber: 1, protoFieldName: "values", jsonFieldName: "values", swiftFieldName: "values")
  658. }
  659. }
  660. public var isEmpty: Bool {return (values.isEmpty)}
  661. public func isEqualTo(other: Google_Protobuf_ListValue) -> Bool {
  662. return values == other.values
  663. }
  664. }
  665. public func ==(lhs: Google_Protobuf_Value.OneOf_Kind, rhs: Google_Protobuf_Value.OneOf_Kind) -> Bool {
  666. switch (lhs, rhs) {
  667. case (.nullValue(_), .nullValue(_)): return true
  668. case (.numberValue(let l), .numberValue(let r)): return l == r
  669. case (.stringValue(let l), .stringValue(let r)): return l == r
  670. case (.boolValue(let l), .boolValue(let r)): return l == r
  671. case (.structValue(let l), .structValue(let r)): return l == r
  672. case (.listValue(let l), .listValue(let r)): return l == r
  673. case (.None, .None): return true
  674. default: return false
  675. }
  676. }