TextFormatDecoder.swift 28 KB

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