| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- // Sources/SwiftProtobuf/TextFormatDecoder.swift - Text format decoding
- //
- // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
- // Licensed under Apache License v2.0 with Runtime Library Exception
- //
- // See LICENSE.txt for license information:
- // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
- //
- // -----------------------------------------------------------------------------
- ///
- /// Test format decoding engine.
- ///
- // -----------------------------------------------------------------------------
- import Foundation
- ///
- /// Provides a higher-level interface to the token stream coming
- /// from a TextFormatScanner. In particular, this provides
- /// single-token pushback and convenience functions for iterating
- /// over complex structures.
- ///
- internal struct TextFormatDecoder: Decoder {
- internal var scanner: TextFormatScanner
- private var fieldCount = 0
- private let terminator: UInt8?
- private let fieldNameMap: _NameMap
- private let messageType: any Message.Type
- internal var options: TextFormatDecodingOptions {
- scanner.options
- }
- internal var complete: Bool { scanner.complete }
- internal init(
- messageType: any Message.Type,
- utf8Pointer: UnsafeRawPointer,
- count: Int,
- options: TextFormatDecodingOptions,
- extensions: (any ExtensionMap)?
- ) throws {
- scanner = TextFormatScanner(utf8Pointer: utf8Pointer, count: count, options: options, extensions: extensions)
- guard let nameProviding = (messageType as? any _ProtoNameProviding.Type) else {
- throw TextFormatDecodingError.missingFieldNames
- }
- fieldNameMap = nameProviding._protobuf_nameMap
- self.messageType = messageType
- self.terminator = nil
- }
- internal init(messageType: any Message.Type, scanner: TextFormatScanner, terminator: UInt8?) throws {
- self.scanner = scanner
- self.terminator = terminator
- guard let nameProviding = (messageType as? any _ProtoNameProviding.Type) else {
- throw TextFormatDecodingError.missingFieldNames
- }
- fieldNameMap = nameProviding._protobuf_nameMap
- self.messageType = messageType
- }
- mutating func handleConflictingOneOf() throws {
- throw TextFormatDecodingError.conflictingOneOf
- }
- mutating func nextFieldNumber() throws -> Int? {
- if fieldCount > 0 {
- scanner.skipOptionalSeparator()
- }
- if let fieldNumber = try scanner.nextFieldNumber(
- names: fieldNameMap,
- messageType: messageType,
- terminator: terminator
- ) {
- fieldCount += 1
- return fieldNumber
- } else {
- return nil
- }
- }
- mutating func decodeSingularFloatField(value: inout Float) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextFloat()
- }
- mutating func decodeSingularFloatField(value: inout Float?) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextFloat()
- }
- mutating func decodeRepeatedFloatField(value: inout [Float]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextFloat()
- value.append(n)
- }
- } else {
- let n = try scanner.nextFloat()
- value.append(n)
- }
- }
- mutating func decodeSingularDoubleField(value: inout Double) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextDouble()
- }
- mutating func decodeSingularDoubleField(value: inout Double?) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextDouble()
- }
- mutating func decodeRepeatedDoubleField(value: inout [Double]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextDouble()
- value.append(n)
- }
- } else {
- let n = try scanner.nextDouble()
- value.append(n)
- }
- }
- mutating func decodeSingularInt32Field(value: inout Int32) throws {
- try scanner.skipRequiredColon()
- let n = try scanner.nextSInt()
- if n > Int64(Int32.max) || n < Int64(Int32.min) {
- throw TextFormatDecodingError.malformedNumber
- }
- value = Int32(truncatingIfNeeded: n)
- }
- mutating func decodeSingularInt32Field(value: inout Int32?) throws {
- try scanner.skipRequiredColon()
- let n = try scanner.nextSInt()
- if n > Int64(Int32.max) || n < Int64(Int32.min) {
- throw TextFormatDecodingError.malformedNumber
- }
- value = Int32(truncatingIfNeeded: n)
- }
- mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextSInt()
- if n > Int64(Int32.max) || n < Int64(Int32.min) {
- throw TextFormatDecodingError.malformedNumber
- }
- value.append(Int32(truncatingIfNeeded: n))
- }
- } else {
- let n = try scanner.nextSInt()
- if n > Int64(Int32.max) || n < Int64(Int32.min) {
- throw TextFormatDecodingError.malformedNumber
- }
- value.append(Int32(truncatingIfNeeded: n))
- }
- }
- mutating func decodeSingularInt64Field(value: inout Int64) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextSInt()
- }
- mutating func decodeSingularInt64Field(value: inout Int64?) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextSInt()
- }
- mutating func decodeRepeatedInt64Field(value: inout [Int64]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextSInt()
- value.append(n)
- }
- } else {
- let n = try scanner.nextSInt()
- value.append(n)
- }
- }
- mutating func decodeSingularUInt32Field(value: inout UInt32) throws {
- try scanner.skipRequiredColon()
- let n = try scanner.nextUInt()
- if n > UInt64(UInt32.max) {
- throw TextFormatDecodingError.malformedNumber
- }
- value = UInt32(truncatingIfNeeded: n)
- }
- mutating func decodeSingularUInt32Field(value: inout UInt32?) throws {
- try scanner.skipRequiredColon()
- let n = try scanner.nextUInt()
- if n > UInt64(UInt32.max) {
- throw TextFormatDecodingError.malformedNumber
- }
- value = UInt32(truncatingIfNeeded: n)
- }
- mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextUInt()
- if n > UInt64(UInt32.max) {
- throw TextFormatDecodingError.malformedNumber
- }
- value.append(UInt32(truncatingIfNeeded: n))
- }
- } else {
- let n = try scanner.nextUInt()
- if n > UInt64(UInt32.max) {
- throw TextFormatDecodingError.malformedNumber
- }
- value.append(UInt32(truncatingIfNeeded: n))
- }
- }
- mutating func decodeSingularUInt64Field(value: inout UInt64) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextUInt()
- }
- mutating func decodeSingularUInt64Field(value: inout UInt64?) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextUInt()
- }
- mutating func decodeRepeatedUInt64Field(value: inout [UInt64]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextUInt()
- value.append(n)
- }
- } else {
- let n = try scanner.nextUInt()
- value.append(n)
- }
- }
- mutating func decodeSingularSInt32Field(value: inout Int32) throws {
- try decodeSingularInt32Field(value: &value)
- }
- mutating func decodeSingularSInt32Field(value: inout Int32?) throws {
- try decodeSingularInt32Field(value: &value)
- }
- mutating func decodeRepeatedSInt32Field(value: inout [Int32]) throws {
- try decodeRepeatedInt32Field(value: &value)
- }
- mutating func decodeSingularSInt64Field(value: inout Int64) throws {
- try decodeSingularInt64Field(value: &value)
- }
- mutating func decodeSingularSInt64Field(value: inout Int64?) throws {
- try decodeSingularInt64Field(value: &value)
- }
- mutating func decodeRepeatedSInt64Field(value: inout [Int64]) throws {
- try decodeRepeatedInt64Field(value: &value)
- }
- mutating func decodeSingularFixed32Field(value: inout UInt32) throws {
- try decodeSingularUInt32Field(value: &value)
- }
- mutating func decodeSingularFixed32Field(value: inout UInt32?) throws {
- try decodeSingularUInt32Field(value: &value)
- }
- mutating func decodeRepeatedFixed32Field(value: inout [UInt32]) throws {
- try decodeRepeatedUInt32Field(value: &value)
- }
- mutating func decodeSingularFixed64Field(value: inout UInt64) throws {
- try decodeSingularUInt64Field(value: &value)
- }
- mutating func decodeSingularFixed64Field(value: inout UInt64?) throws {
- try decodeSingularUInt64Field(value: &value)
- }
- mutating func decodeRepeatedFixed64Field(value: inout [UInt64]) throws {
- try decodeRepeatedUInt64Field(value: &value)
- }
- mutating func decodeSingularSFixed32Field(value: inout Int32) throws {
- try decodeSingularInt32Field(value: &value)
- }
- mutating func decodeSingularSFixed32Field(value: inout Int32?) throws {
- try decodeSingularInt32Field(value: &value)
- }
- mutating func decodeRepeatedSFixed32Field(value: inout [Int32]) throws {
- try decodeRepeatedInt32Field(value: &value)
- }
- mutating func decodeSingularSFixed64Field(value: inout Int64) throws {
- try decodeSingularInt64Field(value: &value)
- }
- mutating func decodeSingularSFixed64Field(value: inout Int64?) throws {
- try decodeSingularInt64Field(value: &value)
- }
- mutating func decodeRepeatedSFixed64Field(value: inout [Int64]) throws {
- try decodeRepeatedInt64Field(value: &value)
- }
- mutating func decodeSingularBoolField(value: inout Bool) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextBool()
- }
- mutating func decodeSingularBoolField(value: inout Bool?) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextBool()
- }
- mutating func decodeRepeatedBoolField(value: inout [Bool]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextBool()
- value.append(n)
- }
- } else {
- let n = try scanner.nextBool()
- value.append(n)
- }
- }
- mutating func decodeSingularStringField(value: inout String) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextStringValue()
- }
- mutating func decodeSingularStringField(value: inout String?) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextStringValue()
- }
- mutating func decodeRepeatedStringField(value: inout [String]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextStringValue()
- value.append(n)
- }
- } else {
- let n = try scanner.nextStringValue()
- value.append(n)
- }
- }
- mutating func decodeSingularBytesField(value: inout Data) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextBytesValue()
- }
- mutating func decodeSingularBytesField(value: inout Data?) throws {
- try scanner.skipRequiredColon()
- value = try scanner.nextBytesValue()
- }
- mutating func decodeRepeatedBytesField(value: inout [Data]) throws {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let n = try scanner.nextBytesValue()
- value.append(n)
- }
- } else {
- let n = try scanner.nextBytesValue()
- value.append(n)
- }
- }
- private mutating func decodeEnum<E: Enum>() throws -> E where E.RawValue == Int {
- if let name = try scanner.nextOptionalEnumName() {
- if let b = E(rawUTF8: name) {
- return b
- } else {
- throw TextFormatDecodingError.unrecognizedEnumValue
- }
- }
- let number = try scanner.nextSInt()
- if number >= Int64(Int32.min) && number <= Int64(Int32.max) {
- let n = Int32(truncatingIfNeeded: number)
- if let e = E(rawValue: Int(n)) {
- return e
- } else {
- throw TextFormatDecodingError.unrecognizedEnumValue
- }
- }
- throw TextFormatDecodingError.malformedText
- }
- mutating func decodeSingularEnumField<E: Enum>(value: inout E?) throws where E.RawValue == Int {
- try scanner.skipRequiredColon()
- let e: E = try decodeEnum()
- value = e
- }
- mutating func decodeSingularEnumField<E: Enum>(value: inout E) throws where E.RawValue == Int {
- try scanner.skipRequiredColon()
- let e: E = try decodeEnum()
- value = e
- }
- mutating func decodeRepeatedEnumField<E: Enum>(value: inout [E]) throws where E.RawValue == Int {
- try scanner.skipRequiredColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let e: E = try decodeEnum()
- value.append(e)
- }
- } else {
- let e: E = try decodeEnum()
- value.append(e)
- }
- }
- mutating func decodeSingularMessageField<M: Message>(value: inout M?) throws {
- _ = scanner.skipOptionalColon()
- if value == nil {
- value = M()
- }
- let terminator = try scanner.skipObjectStart()
- var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
- if M.self == Google_Protobuf_Any.self {
- var any = value as! Google_Protobuf_Any?
- try any!.decodeTextFormat(decoder: &subDecoder)
- value = any as! M?
- } else {
- try value!.decodeMessage(decoder: &subDecoder)
- }
- assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
- scanner = subDecoder.scanner
- }
- mutating func decodeRepeatedMessageField<M: Message>(value: inout [M]) throws {
- _ = scanner.skipOptionalColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- let terminator = try scanner.skipObjectStart()
- var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
- if M.self == Google_Protobuf_Any.self {
- var message = Google_Protobuf_Any()
- try message.decodeTextFormat(decoder: &subDecoder)
- value.append(message as! M)
- } else {
- var message = M()
- try message.decodeMessage(decoder: &subDecoder)
- value.append(message)
- }
- assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
- scanner = subDecoder.scanner
- }
- } else {
- let terminator = try scanner.skipObjectStart()
- var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
- if M.self == Google_Protobuf_Any.self {
- var message = Google_Protobuf_Any()
- try message.decodeTextFormat(decoder: &subDecoder)
- value.append(message as! M)
- } else {
- var message = M()
- try message.decodeMessage(decoder: &subDecoder)
- value.append(message)
- }
- assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
- scanner = subDecoder.scanner
- }
- }
- mutating func decodeSingularGroupField<G: Message>(value: inout G?) throws {
- try decodeSingularMessageField(value: &value)
- }
- mutating func decodeRepeatedGroupField<G: Message>(value: inout [G]) throws {
- try decodeRepeatedMessageField(value: &value)
- }
- private mutating func decodeMapEntry<KeyType, ValueType: MapValueType>(
- mapType: _ProtobufMap<KeyType, ValueType>.Type,
- value: inout _ProtobufMap<KeyType, ValueType>.BaseType
- ) throws {
- var keyField: KeyType.BaseType?
- var valueField: ValueType.BaseType?
- let terminator = try scanner.skipObjectStart()
- let ignoreExtensionFields = options.ignoreUnknownExtensionFields
- while true {
- if scanner.skipOptionalObjectEnd(terminator) {
- if let keyField = keyField, let valueField = valueField {
- value[keyField] = valueField
- return
- } else {
- throw TextFormatDecodingError.malformedText
- }
- }
- if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
- switch key {
- case "key", "1":
- try KeyType.decodeSingular(value: &keyField, from: &self)
- case "value", "2":
- try ValueType.decodeSingular(value: &valueField, from: &self)
- default:
- if ignoreExtensionFields && key.hasPrefix("[") {
- try scanner.skipUnknownFieldValue()
- } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
- try scanner.skipUnknownFieldValue()
- } else {
- throw TextFormatDecodingError.unknownField
- }
- }
- scanner.skipOptionalSeparator()
- } else {
- throw TextFormatDecodingError.malformedText
- }
- }
- }
- mutating func decodeMapField<KeyType, ValueType: MapValueType>(
- fieldType: _ProtobufMap<KeyType, ValueType>.Type,
- value: inout _ProtobufMap<KeyType, ValueType>.BaseType
- ) throws {
- _ = scanner.skipOptionalColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- try decodeMapEntry(mapType: fieldType, value: &value)
- }
- } else {
- try decodeMapEntry(mapType: fieldType, value: &value)
- }
- }
- private mutating func decodeMapEntry<KeyType, ValueType>(
- mapType: _ProtobufEnumMap<KeyType, ValueType>.Type,
- value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
- ) throws where ValueType.RawValue == Int {
- var keyField: KeyType.BaseType?
- var valueField: ValueType?
- let terminator = try scanner.skipObjectStart()
- let ignoreExtensionFields = options.ignoreUnknownExtensionFields
- while true {
- if scanner.skipOptionalObjectEnd(terminator) {
- if let keyField = keyField, let valueField = valueField {
- value[keyField] = valueField
- return
- } else {
- throw TextFormatDecodingError.malformedText
- }
- }
- if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
- switch key {
- case "key", "1":
- try KeyType.decodeSingular(value: &keyField, from: &self)
- case "value", "2":
- try decodeSingularEnumField(value: &valueField)
- default:
- if ignoreExtensionFields && key.hasPrefix("[") {
- try scanner.skipUnknownFieldValue()
- } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
- try scanner.skipUnknownFieldValue()
- } else {
- throw TextFormatDecodingError.unknownField
- }
- }
- scanner.skipOptionalSeparator()
- } else {
- throw TextFormatDecodingError.malformedText
- }
- }
- }
- mutating func decodeMapField<KeyType, ValueType>(
- fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
- value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
- ) throws where ValueType.RawValue == Int {
- _ = scanner.skipOptionalColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- try decodeMapEntry(mapType: fieldType, value: &value)
- }
- } else {
- try decodeMapEntry(mapType: fieldType, value: &value)
- }
- }
- private mutating func decodeMapEntry<KeyType, ValueType>(
- mapType: _ProtobufMessageMap<KeyType, ValueType>.Type,
- value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
- ) throws {
- var keyField: KeyType.BaseType?
- var valueField: ValueType?
- let terminator = try scanner.skipObjectStart()
- let ignoreExtensionFields = options.ignoreUnknownExtensionFields
- while true {
- if scanner.skipOptionalObjectEnd(terminator) {
- if let keyField = keyField, let valueField = valueField {
- value[keyField] = valueField
- return
- } else {
- throw TextFormatDecodingError.malformedText
- }
- }
- if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
- switch key {
- case "key", "1":
- try KeyType.decodeSingular(value: &keyField, from: &self)
- case "value", "2":
- try decodeSingularMessageField(value: &valueField)
- default:
- if ignoreExtensionFields && key.hasPrefix("[") {
- try scanner.skipUnknownFieldValue()
- } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
- try scanner.skipUnknownFieldValue()
- } else {
- throw TextFormatDecodingError.unknownField
- }
- }
- scanner.skipOptionalSeparator()
- } else {
- throw TextFormatDecodingError.malformedText
- }
- }
- }
- mutating func decodeMapField<KeyType, ValueType>(
- fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
- value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
- ) throws {
- _ = scanner.skipOptionalColon()
- if scanner.skipOptionalBeginArray() {
- var firstItem = true
- while true {
- if scanner.skipOptionalEndArray() {
- return
- }
- if firstItem {
- firstItem = false
- } else {
- try scanner.skipRequiredComma()
- }
- try decodeMapEntry(mapType: fieldType, value: &value)
- }
- } else {
- try decodeMapEntry(mapType: fieldType, value: &value)
- }
- }
- mutating func decodeExtensionField(
- values: inout ExtensionFieldValueSet,
- messageType: any Message.Type,
- fieldNumber: Int
- ) throws {
- if let ext = scanner.extensions?[messageType, fieldNumber] {
- try values.modify(index: fieldNumber) { fieldValue in
- if fieldValue != nil {
- try fieldValue!.decodeExtensionField(decoder: &self)
- } else {
- fieldValue = try ext._protobuf_newField(decoder: &self)
- }
- if fieldValue == nil {
- // Really things should never get here, for TextFormat, decoding
- // the value should always work or throw an error. This specific
- // error result is to allow this to be more detectable.
- throw TextFormatDecodingError.internalExtensionError
- }
- }
- }
- }
- }
|