TextFormatDecoder.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. // Sources/SwiftProtobuf/TextFormatDecoder.swift - Text format decoding
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Test format decoding engine.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. import Foundation
  15. ///
  16. /// Provides a higher-level interface to the token stream coming
  17. /// from a TextFormatScanner. In particular, this provides
  18. /// single-token pushback and convenience functions for iterating
  19. /// over complex structures.
  20. ///
  21. internal struct TextFormatDecoder: Decoder {
  22. internal var scanner: TextFormatScanner
  23. private var fieldCount = 0
  24. private var terminator: UInt8?
  25. private var fieldNameMap: _NameMap?
  26. private var messageType: any Message.Type
  27. internal var options: TextFormatDecodingOptions {
  28. scanner.options
  29. }
  30. internal var complete: Bool {
  31. mutating get {
  32. scanner.complete
  33. }
  34. }
  35. internal init(
  36. messageType: any Message.Type,
  37. utf8Pointer: UnsafeRawPointer,
  38. count: Int,
  39. options: TextFormatDecodingOptions,
  40. extensions: (any ExtensionMap)?
  41. ) throws {
  42. scanner = TextFormatScanner(utf8Pointer: utf8Pointer, count: count, options: options, extensions: extensions)
  43. guard let nameProviding = (messageType as? any _ProtoNameProviding.Type) else {
  44. throw TextFormatDecodingError.missingFieldNames
  45. }
  46. fieldNameMap = nameProviding._protobuf_nameMap
  47. self.messageType = messageType
  48. }
  49. internal init(messageType: any Message.Type, scanner: TextFormatScanner, terminator: UInt8?) throws {
  50. self.scanner = scanner
  51. self.terminator = terminator
  52. guard let nameProviding = (messageType as? any _ProtoNameProviding.Type) else {
  53. throw TextFormatDecodingError.missingFieldNames
  54. }
  55. fieldNameMap = nameProviding._protobuf_nameMap
  56. self.messageType = messageType
  57. }
  58. mutating func handleConflictingOneOf() throws {
  59. throw TextFormatDecodingError.conflictingOneOf
  60. }
  61. mutating func nextFieldNumber() throws -> Int? {
  62. if fieldCount > 0 {
  63. scanner.skipOptionalSeparator()
  64. }
  65. if let fieldNumber = try scanner.nextFieldNumber(
  66. names: fieldNameMap!,
  67. messageType: messageType,
  68. terminator: terminator
  69. ) {
  70. fieldCount += 1
  71. return fieldNumber
  72. } else {
  73. return nil
  74. }
  75. }
  76. mutating func decodeSingularFloatField(value: inout Float) throws {
  77. try scanner.skipRequiredColon()
  78. value = try scanner.nextFloat()
  79. }
  80. mutating func decodeSingularFloatField(value: inout Float?) throws {
  81. try scanner.skipRequiredColon()
  82. value = try scanner.nextFloat()
  83. }
  84. mutating func decodeRepeatedFloatField(value: inout [Float]) throws {
  85. try scanner.skipRequiredColon()
  86. if scanner.skipOptionalBeginArray() {
  87. var firstItem = true
  88. while true {
  89. if scanner.skipOptionalEndArray() {
  90. return
  91. }
  92. if firstItem {
  93. firstItem = false
  94. } else {
  95. try scanner.skipRequiredComma()
  96. }
  97. let n = try scanner.nextFloat()
  98. value.append(n)
  99. }
  100. } else {
  101. let n = try scanner.nextFloat()
  102. value.append(n)
  103. }
  104. }
  105. mutating func decodeSingularDoubleField(value: inout Double) throws {
  106. try scanner.skipRequiredColon()
  107. value = try scanner.nextDouble()
  108. }
  109. mutating func decodeSingularDoubleField(value: inout Double?) throws {
  110. try scanner.skipRequiredColon()
  111. value = try scanner.nextDouble()
  112. }
  113. mutating func decodeRepeatedDoubleField(value: inout [Double]) throws {
  114. try scanner.skipRequiredColon()
  115. if scanner.skipOptionalBeginArray() {
  116. var firstItem = true
  117. while true {
  118. if scanner.skipOptionalEndArray() {
  119. return
  120. }
  121. if firstItem {
  122. firstItem = false
  123. } else {
  124. try scanner.skipRequiredComma()
  125. }
  126. let n = try scanner.nextDouble()
  127. value.append(n)
  128. }
  129. } else {
  130. let n = try scanner.nextDouble()
  131. value.append(n)
  132. }
  133. }
  134. mutating func decodeSingularInt32Field(value: inout Int32) throws {
  135. try scanner.skipRequiredColon()
  136. let n = try scanner.nextSInt()
  137. if n > Int64(Int32.max) || n < Int64(Int32.min) {
  138. throw TextFormatDecodingError.malformedNumber
  139. }
  140. value = Int32(truncatingIfNeeded: n)
  141. }
  142. mutating func decodeSingularInt32Field(value: inout Int32?) throws {
  143. try scanner.skipRequiredColon()
  144. let n = try scanner.nextSInt()
  145. if n > Int64(Int32.max) || n < Int64(Int32.min) {
  146. throw TextFormatDecodingError.malformedNumber
  147. }
  148. value = Int32(truncatingIfNeeded: n)
  149. }
  150. mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
  151. try scanner.skipRequiredColon()
  152. if scanner.skipOptionalBeginArray() {
  153. var firstItem = true
  154. while true {
  155. if scanner.skipOptionalEndArray() {
  156. return
  157. }
  158. if firstItem {
  159. firstItem = false
  160. } else {
  161. try scanner.skipRequiredComma()
  162. }
  163. let n = try scanner.nextSInt()
  164. if n > Int64(Int32.max) || n < Int64(Int32.min) {
  165. throw TextFormatDecodingError.malformedNumber
  166. }
  167. value.append(Int32(truncatingIfNeeded: n))
  168. }
  169. } else {
  170. let n = try scanner.nextSInt()
  171. if n > Int64(Int32.max) || n < Int64(Int32.min) {
  172. throw TextFormatDecodingError.malformedNumber
  173. }
  174. value.append(Int32(truncatingIfNeeded: n))
  175. }
  176. }
  177. mutating func decodeSingularInt64Field(value: inout Int64) throws {
  178. try scanner.skipRequiredColon()
  179. value = try scanner.nextSInt()
  180. }
  181. mutating func decodeSingularInt64Field(value: inout Int64?) throws {
  182. try scanner.skipRequiredColon()
  183. value = try scanner.nextSInt()
  184. }
  185. mutating func decodeRepeatedInt64Field(value: inout [Int64]) throws {
  186. try scanner.skipRequiredColon()
  187. if scanner.skipOptionalBeginArray() {
  188. var firstItem = true
  189. while true {
  190. if scanner.skipOptionalEndArray() {
  191. return
  192. }
  193. if firstItem {
  194. firstItem = false
  195. } else {
  196. try scanner.skipRequiredComma()
  197. }
  198. let n = try scanner.nextSInt()
  199. value.append(n)
  200. }
  201. } else {
  202. let n = try scanner.nextSInt()
  203. value.append(n)
  204. }
  205. }
  206. mutating func decodeSingularUInt32Field(value: inout UInt32) throws {
  207. try scanner.skipRequiredColon()
  208. let n = try scanner.nextUInt()
  209. if n > UInt64(UInt32.max) {
  210. throw TextFormatDecodingError.malformedNumber
  211. }
  212. value = UInt32(truncatingIfNeeded: n)
  213. }
  214. mutating func decodeSingularUInt32Field(value: inout UInt32?) throws {
  215. try scanner.skipRequiredColon()
  216. let n = try scanner.nextUInt()
  217. if n > UInt64(UInt32.max) {
  218. throw TextFormatDecodingError.malformedNumber
  219. }
  220. value = UInt32(truncatingIfNeeded: n)
  221. }
  222. mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
  223. try scanner.skipRequiredColon()
  224. if scanner.skipOptionalBeginArray() {
  225. var firstItem = true
  226. while true {
  227. if scanner.skipOptionalEndArray() {
  228. return
  229. }
  230. if firstItem {
  231. firstItem = false
  232. } else {
  233. try scanner.skipRequiredComma()
  234. }
  235. let n = try scanner.nextUInt()
  236. if n > UInt64(UInt32.max) {
  237. throw TextFormatDecodingError.malformedNumber
  238. }
  239. value.append(UInt32(truncatingIfNeeded: n))
  240. }
  241. } else {
  242. let n = try scanner.nextUInt()
  243. if n > UInt64(UInt32.max) {
  244. throw TextFormatDecodingError.malformedNumber
  245. }
  246. value.append(UInt32(truncatingIfNeeded: n))
  247. }
  248. }
  249. mutating func decodeSingularUInt64Field(value: inout UInt64) throws {
  250. try scanner.skipRequiredColon()
  251. value = try scanner.nextUInt()
  252. }
  253. mutating func decodeSingularUInt64Field(value: inout UInt64?) throws {
  254. try scanner.skipRequiredColon()
  255. value = try scanner.nextUInt()
  256. }
  257. mutating func decodeRepeatedUInt64Field(value: inout [UInt64]) throws {
  258. try scanner.skipRequiredColon()
  259. if scanner.skipOptionalBeginArray() {
  260. var firstItem = true
  261. while true {
  262. if scanner.skipOptionalEndArray() {
  263. return
  264. }
  265. if firstItem {
  266. firstItem = false
  267. } else {
  268. try scanner.skipRequiredComma()
  269. }
  270. let n = try scanner.nextUInt()
  271. value.append(n)
  272. }
  273. } else {
  274. let n = try scanner.nextUInt()
  275. value.append(n)
  276. }
  277. }
  278. mutating func decodeSingularSInt32Field(value: inout Int32) throws {
  279. try decodeSingularInt32Field(value: &value)
  280. }
  281. mutating func decodeSingularSInt32Field(value: inout Int32?) throws {
  282. try decodeSingularInt32Field(value: &value)
  283. }
  284. mutating func decodeRepeatedSInt32Field(value: inout [Int32]) throws {
  285. try decodeRepeatedInt32Field(value: &value)
  286. }
  287. mutating func decodeSingularSInt64Field(value: inout Int64) throws {
  288. try decodeSingularInt64Field(value: &value)
  289. }
  290. mutating func decodeSingularSInt64Field(value: inout Int64?) throws {
  291. try decodeSingularInt64Field(value: &value)
  292. }
  293. mutating func decodeRepeatedSInt64Field(value: inout [Int64]) throws {
  294. try decodeRepeatedInt64Field(value: &value)
  295. }
  296. mutating func decodeSingularFixed32Field(value: inout UInt32) throws {
  297. try decodeSingularUInt32Field(value: &value)
  298. }
  299. mutating func decodeSingularFixed32Field(value: inout UInt32?) throws {
  300. try decodeSingularUInt32Field(value: &value)
  301. }
  302. mutating func decodeRepeatedFixed32Field(value: inout [UInt32]) throws {
  303. try decodeRepeatedUInt32Field(value: &value)
  304. }
  305. mutating func decodeSingularFixed64Field(value: inout UInt64) throws {
  306. try decodeSingularUInt64Field(value: &value)
  307. }
  308. mutating func decodeSingularFixed64Field(value: inout UInt64?) throws {
  309. try decodeSingularUInt64Field(value: &value)
  310. }
  311. mutating func decodeRepeatedFixed64Field(value: inout [UInt64]) throws {
  312. try decodeRepeatedUInt64Field(value: &value)
  313. }
  314. mutating func decodeSingularSFixed32Field(value: inout Int32) throws {
  315. try decodeSingularInt32Field(value: &value)
  316. }
  317. mutating func decodeSingularSFixed32Field(value: inout Int32?) throws {
  318. try decodeSingularInt32Field(value: &value)
  319. }
  320. mutating func decodeRepeatedSFixed32Field(value: inout [Int32]) throws {
  321. try decodeRepeatedInt32Field(value: &value)
  322. }
  323. mutating func decodeSingularSFixed64Field(value: inout Int64) throws {
  324. try decodeSingularInt64Field(value: &value)
  325. }
  326. mutating func decodeSingularSFixed64Field(value: inout Int64?) throws {
  327. try decodeSingularInt64Field(value: &value)
  328. }
  329. mutating func decodeRepeatedSFixed64Field(value: inout [Int64]) throws {
  330. try decodeRepeatedInt64Field(value: &value)
  331. }
  332. mutating func decodeSingularBoolField(value: inout Bool) throws {
  333. try scanner.skipRequiredColon()
  334. value = try scanner.nextBool()
  335. }
  336. mutating func decodeSingularBoolField(value: inout Bool?) throws {
  337. try scanner.skipRequiredColon()
  338. value = try scanner.nextBool()
  339. }
  340. mutating func decodeRepeatedBoolField(value: inout [Bool]) throws {
  341. try scanner.skipRequiredColon()
  342. if scanner.skipOptionalBeginArray() {
  343. var firstItem = true
  344. while true {
  345. if scanner.skipOptionalEndArray() {
  346. return
  347. }
  348. if firstItem {
  349. firstItem = false
  350. } else {
  351. try scanner.skipRequiredComma()
  352. }
  353. let n = try scanner.nextBool()
  354. value.append(n)
  355. }
  356. } else {
  357. let n = try scanner.nextBool()
  358. value.append(n)
  359. }
  360. }
  361. mutating func decodeSingularStringField(value: inout String) throws {
  362. try scanner.skipRequiredColon()
  363. value = try scanner.nextStringValue()
  364. }
  365. mutating func decodeSingularStringField(value: inout String?) throws {
  366. try scanner.skipRequiredColon()
  367. value = try scanner.nextStringValue()
  368. }
  369. mutating func decodeRepeatedStringField(value: inout [String]) throws {
  370. try scanner.skipRequiredColon()
  371. if scanner.skipOptionalBeginArray() {
  372. var firstItem = true
  373. while true {
  374. if scanner.skipOptionalEndArray() {
  375. return
  376. }
  377. if firstItem {
  378. firstItem = false
  379. } else {
  380. try scanner.skipRequiredComma()
  381. }
  382. let n = try scanner.nextStringValue()
  383. value.append(n)
  384. }
  385. } else {
  386. let n = try scanner.nextStringValue()
  387. value.append(n)
  388. }
  389. }
  390. mutating func decodeSingularBytesField(value: inout Data) throws {
  391. try scanner.skipRequiredColon()
  392. value = try scanner.nextBytesValue()
  393. }
  394. mutating func decodeSingularBytesField(value: inout Data?) throws {
  395. try scanner.skipRequiredColon()
  396. value = try scanner.nextBytesValue()
  397. }
  398. mutating func decodeRepeatedBytesField(value: inout [Data]) throws {
  399. try scanner.skipRequiredColon()
  400. if scanner.skipOptionalBeginArray() {
  401. var firstItem = true
  402. while true {
  403. if scanner.skipOptionalEndArray() {
  404. return
  405. }
  406. if firstItem {
  407. firstItem = false
  408. } else {
  409. try scanner.skipRequiredComma()
  410. }
  411. let n = try scanner.nextBytesValue()
  412. value.append(n)
  413. }
  414. } else {
  415. let n = try scanner.nextBytesValue()
  416. value.append(n)
  417. }
  418. }
  419. private mutating func decodeEnum<E: Enum>() throws -> E where E.RawValue == Int {
  420. if let name = try scanner.nextOptionalEnumName() {
  421. if let b = E(rawUTF8: name) {
  422. return b
  423. } else {
  424. throw TextFormatDecodingError.unrecognizedEnumValue
  425. }
  426. }
  427. let number = try scanner.nextSInt()
  428. if number >= Int64(Int32.min) && number <= Int64(Int32.max) {
  429. let n = Int32(truncatingIfNeeded: number)
  430. if let e = E(rawValue: Int(n)) {
  431. return e
  432. } else {
  433. throw TextFormatDecodingError.unrecognizedEnumValue
  434. }
  435. }
  436. throw TextFormatDecodingError.malformedText
  437. }
  438. mutating func decodeSingularEnumField<E: Enum>(value: inout E?) throws where E.RawValue == Int {
  439. try scanner.skipRequiredColon()
  440. let e: E = try decodeEnum()
  441. value = e
  442. }
  443. mutating func decodeSingularEnumField<E: Enum>(value: inout E) throws where E.RawValue == Int {
  444. try scanner.skipRequiredColon()
  445. let e: E = try decodeEnum()
  446. value = e
  447. }
  448. mutating func decodeRepeatedEnumField<E: Enum>(value: inout [E]) throws where E.RawValue == Int {
  449. try scanner.skipRequiredColon()
  450. if scanner.skipOptionalBeginArray() {
  451. var firstItem = true
  452. while true {
  453. if scanner.skipOptionalEndArray() {
  454. return
  455. }
  456. if firstItem {
  457. firstItem = false
  458. } else {
  459. try scanner.skipRequiredComma()
  460. }
  461. let e: E = try decodeEnum()
  462. value.append(e)
  463. }
  464. } else {
  465. let e: E = try decodeEnum()
  466. value.append(e)
  467. }
  468. }
  469. mutating func decodeSingularMessageField<M: Message>(value: inout M?) throws {
  470. _ = scanner.skipOptionalColon()
  471. if value == nil {
  472. value = M()
  473. }
  474. let terminator = try scanner.skipObjectStart()
  475. var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
  476. if M.self == Google_Protobuf_Any.self {
  477. var any = value as! Google_Protobuf_Any?
  478. try any!.decodeTextFormat(decoder: &subDecoder)
  479. value = any as! M?
  480. } else {
  481. try value!.decodeMessage(decoder: &subDecoder)
  482. }
  483. assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
  484. scanner = subDecoder.scanner
  485. }
  486. mutating func decodeRepeatedMessageField<M: Message>(value: inout [M]) throws {
  487. _ = scanner.skipOptionalColon()
  488. if scanner.skipOptionalBeginArray() {
  489. var firstItem = true
  490. while true {
  491. if scanner.skipOptionalEndArray() {
  492. return
  493. }
  494. if firstItem {
  495. firstItem = false
  496. } else {
  497. try scanner.skipRequiredComma()
  498. }
  499. let terminator = try scanner.skipObjectStart()
  500. var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
  501. if M.self == Google_Protobuf_Any.self {
  502. var message = Google_Protobuf_Any()
  503. try message.decodeTextFormat(decoder: &subDecoder)
  504. value.append(message as! M)
  505. } else {
  506. var message = M()
  507. try message.decodeMessage(decoder: &subDecoder)
  508. value.append(message)
  509. }
  510. assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
  511. scanner = subDecoder.scanner
  512. }
  513. } else {
  514. let terminator = try scanner.skipObjectStart()
  515. var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
  516. if M.self == Google_Protobuf_Any.self {
  517. var message = Google_Protobuf_Any()
  518. try message.decodeTextFormat(decoder: &subDecoder)
  519. value.append(message as! M)
  520. } else {
  521. var message = M()
  522. try message.decodeMessage(decoder: &subDecoder)
  523. value.append(message)
  524. }
  525. assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
  526. scanner = subDecoder.scanner
  527. }
  528. }
  529. mutating func decodeSingularGroupField<G: Message>(value: inout G?) throws {
  530. try decodeSingularMessageField(value: &value)
  531. }
  532. mutating func decodeRepeatedGroupField<G: Message>(value: inout [G]) throws {
  533. try decodeRepeatedMessageField(value: &value)
  534. }
  535. private mutating func decodeMapEntry<KeyType, ValueType: MapValueType>(
  536. mapType: _ProtobufMap<KeyType, ValueType>.Type,
  537. value: inout _ProtobufMap<KeyType, ValueType>.BaseType
  538. ) throws {
  539. var keyField: KeyType.BaseType?
  540. var valueField: ValueType.BaseType?
  541. let terminator = try scanner.skipObjectStart()
  542. let ignoreExtensionFields = options.ignoreUnknownExtensionFields
  543. while true {
  544. if scanner.skipOptionalObjectEnd(terminator) {
  545. if let keyField = keyField, let valueField = valueField {
  546. value[keyField] = valueField
  547. return
  548. } else {
  549. throw TextFormatDecodingError.malformedText
  550. }
  551. }
  552. if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
  553. switch key {
  554. case "key", "1":
  555. try KeyType.decodeSingular(value: &keyField, from: &self)
  556. case "value", "2":
  557. try ValueType.decodeSingular(value: &valueField, from: &self)
  558. default:
  559. if ignoreExtensionFields && key.hasPrefix("[") {
  560. try scanner.skipUnknownFieldValue()
  561. } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
  562. try scanner.skipUnknownFieldValue()
  563. } else {
  564. throw TextFormatDecodingError.unknownField
  565. }
  566. }
  567. scanner.skipOptionalSeparator()
  568. } else {
  569. throw TextFormatDecodingError.malformedText
  570. }
  571. }
  572. }
  573. mutating func decodeMapField<KeyType, ValueType: MapValueType>(
  574. fieldType: _ProtobufMap<KeyType, ValueType>.Type,
  575. value: inout _ProtobufMap<KeyType, ValueType>.BaseType
  576. ) throws {
  577. _ = scanner.skipOptionalColon()
  578. if scanner.skipOptionalBeginArray() {
  579. var firstItem = true
  580. while true {
  581. if scanner.skipOptionalEndArray() {
  582. return
  583. }
  584. if firstItem {
  585. firstItem = false
  586. } else {
  587. try scanner.skipRequiredComma()
  588. }
  589. try decodeMapEntry(mapType: fieldType, value: &value)
  590. }
  591. } else {
  592. try decodeMapEntry(mapType: fieldType, value: &value)
  593. }
  594. }
  595. private mutating func decodeMapEntry<KeyType, ValueType>(
  596. mapType: _ProtobufEnumMap<KeyType, ValueType>.Type,
  597. value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
  598. ) throws where ValueType.RawValue == Int {
  599. var keyField: KeyType.BaseType?
  600. var valueField: ValueType?
  601. let terminator = try scanner.skipObjectStart()
  602. let ignoreExtensionFields = options.ignoreUnknownExtensionFields
  603. while true {
  604. if scanner.skipOptionalObjectEnd(terminator) {
  605. if let keyField = keyField, let valueField = valueField {
  606. value[keyField] = valueField
  607. return
  608. } else {
  609. throw TextFormatDecodingError.malformedText
  610. }
  611. }
  612. if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
  613. switch key {
  614. case "key", "1":
  615. try KeyType.decodeSingular(value: &keyField, from: &self)
  616. case "value", "2":
  617. try decodeSingularEnumField(value: &valueField)
  618. default:
  619. if ignoreExtensionFields && key.hasPrefix("[") {
  620. try scanner.skipUnknownFieldValue()
  621. } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
  622. try scanner.skipUnknownFieldValue()
  623. } else {
  624. throw TextFormatDecodingError.unknownField
  625. }
  626. }
  627. scanner.skipOptionalSeparator()
  628. } else {
  629. throw TextFormatDecodingError.malformedText
  630. }
  631. }
  632. }
  633. mutating func decodeMapField<KeyType, ValueType>(
  634. fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
  635. value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
  636. ) throws where ValueType.RawValue == Int {
  637. _ = scanner.skipOptionalColon()
  638. if scanner.skipOptionalBeginArray() {
  639. var firstItem = true
  640. while true {
  641. if scanner.skipOptionalEndArray() {
  642. return
  643. }
  644. if firstItem {
  645. firstItem = false
  646. } else {
  647. try scanner.skipRequiredComma()
  648. }
  649. try decodeMapEntry(mapType: fieldType, value: &value)
  650. }
  651. } else {
  652. try decodeMapEntry(mapType: fieldType, value: &value)
  653. }
  654. }
  655. private mutating func decodeMapEntry<KeyType, ValueType>(
  656. mapType: _ProtobufMessageMap<KeyType, ValueType>.Type,
  657. value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
  658. ) throws {
  659. var keyField: KeyType.BaseType?
  660. var valueField: ValueType?
  661. let terminator = try scanner.skipObjectStart()
  662. let ignoreExtensionFields = options.ignoreUnknownExtensionFields
  663. while true {
  664. if scanner.skipOptionalObjectEnd(terminator) {
  665. if let keyField = keyField, let valueField = valueField {
  666. value[keyField] = valueField
  667. return
  668. } else {
  669. throw TextFormatDecodingError.malformedText
  670. }
  671. }
  672. if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
  673. switch key {
  674. case "key", "1":
  675. try KeyType.decodeSingular(value: &keyField, from: &self)
  676. case "value", "2":
  677. try decodeSingularMessageField(value: &valueField)
  678. default:
  679. if ignoreExtensionFields && key.hasPrefix("[") {
  680. try scanner.skipUnknownFieldValue()
  681. } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
  682. try scanner.skipUnknownFieldValue()
  683. } else {
  684. throw TextFormatDecodingError.unknownField
  685. }
  686. }
  687. scanner.skipOptionalSeparator()
  688. } else {
  689. throw TextFormatDecodingError.malformedText
  690. }
  691. }
  692. }
  693. mutating func decodeMapField<KeyType, ValueType>(
  694. fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
  695. value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
  696. ) throws {
  697. _ = scanner.skipOptionalColon()
  698. if scanner.skipOptionalBeginArray() {
  699. var firstItem = true
  700. while true {
  701. if scanner.skipOptionalEndArray() {
  702. return
  703. }
  704. if firstItem {
  705. firstItem = false
  706. } else {
  707. try scanner.skipRequiredComma()
  708. }
  709. try decodeMapEntry(mapType: fieldType, value: &value)
  710. }
  711. } else {
  712. try decodeMapEntry(mapType: fieldType, value: &value)
  713. }
  714. }
  715. mutating func decodeExtensionField(
  716. values: inout ExtensionFieldValueSet,
  717. messageType: any Message.Type,
  718. fieldNumber: Int
  719. ) throws {
  720. if let ext = scanner.extensions?[messageType, fieldNumber] {
  721. try values.modify(index: fieldNumber) { fieldValue in
  722. if fieldValue != nil {
  723. try fieldValue!.decodeExtensionField(decoder: &self)
  724. } else {
  725. fieldValue = try ext._protobuf_newField(decoder: &self)
  726. }
  727. if fieldValue == nil {
  728. // Really things should never get here, for TextFormat, decoding
  729. // the value should always work or throw an error. This specific
  730. // error result is to allow this to be more detectable.
  731. throw TextFormatDecodingError.internalExtensionError
  732. }
  733. }
  734. }
  735. }
  736. }