@@ -27,6 +27,10 @@ internal struct TextFormatDecoder: Decoder {
27
27
private var fieldNameMap : _NameMap ?
28
28
private var messageType : any Message . Type
29
29
30
+ internal var options : TextFormatDecodingOptions {
31
+ return scanner. options
32
+ }
33
+
30
34
internal var complete : Bool {
31
35
mutating get {
32
36
return scanner. complete
@@ -63,27 +67,17 @@ internal struct TextFormatDecoder: Decoder {
63
67
}
64
68
65
69
mutating func nextFieldNumber( ) throws -> Int ? {
66
- // Per https://protobuf.dev/reference/protobuf/textformat-spec/#fields, every field can be
67
- // followed by a field separator, so if we've seen a field, remove the separator before
68
- // checking for the terminator.
69
70
if fieldCount > 0 {
70
71
scanner. skipOptionalSeparator ( )
71
72
}
72
- if let terminator = terminator,
73
- scanner. skipOptionalObjectEnd ( terminator) {
74
- return nil
75
- }
76
- if let fieldNumber = try scanner. nextFieldNumber ( names: fieldNameMap!, messageType: messageType) {
73
+ if let fieldNumber = try scanner. nextFieldNumber ( names: fieldNameMap!,
74
+ messageType: messageType,
75
+ terminator: terminator) {
77
76
fieldCount += 1
78
77
return fieldNumber
79
- } else if terminator == nil {
80
- return nil
81
78
} else {
82
- // If this decoder is looking for at a terminator, then if the scanner failed to
83
- // find a field number, something went wrong (end of stream).
84
- throw TextFormatDecodingError . truncated
79
+ return nil
85
80
}
86
-
87
81
}
88
82
89
83
mutating func decodeSingularFloatField( value: inout Float ) throws {
@@ -559,6 +553,7 @@ internal struct TextFormatDecoder: Decoder {
559
553
var keyField : KeyType . BaseType ?
560
554
var valueField : ValueType . BaseType ?
561
555
let terminator = try scanner. skipObjectStart ( )
556
+ let ignoreExtensionFields = options. ignoreUnknownExtensionFields
562
557
while true {
563
558
if scanner. skipOptionalObjectEnd ( terminator) {
564
559
if let keyField = keyField, let valueField = valueField {
@@ -568,14 +563,20 @@ internal struct TextFormatDecoder: Decoder {
568
563
throw TextFormatDecodingError . malformedText
569
564
}
570
565
}
571
- if let key = try scanner. nextKey ( ) {
566
+ if let key = try scanner. nextKey ( allowExtensions : ignoreExtensionFields ) {
572
567
switch key {
573
568
case " key " , " 1 " :
574
569
try KeyType . decodeSingular ( value: & keyField, from: & self )
575
570
case " value " , " 2 " :
576
571
try ValueType . decodeSingular ( value: & valueField, from: & self )
577
572
default :
578
- throw TextFormatDecodingError . unknownField
573
+ if ignoreExtensionFields && key. hasPrefix ( " [ " ) {
574
+ try scanner. skipUnknownFieldValue ( )
575
+ } else if options. ignoreUnknownFields && !key. hasPrefix ( " [ " ) {
576
+ try scanner. skipUnknownFieldValue ( )
577
+ } else {
578
+ throw TextFormatDecodingError . unknownField
579
+ }
579
580
}
580
581
scanner. skipOptionalSeparator ( )
581
582
} else {
@@ -608,6 +609,7 @@ internal struct TextFormatDecoder: Decoder {
608
609
var keyField : KeyType . BaseType ?
609
610
var valueField : ValueType ?
610
611
let terminator = try scanner. skipObjectStart ( )
612
+ let ignoreExtensionFields = options. ignoreUnknownExtensionFields
611
613
while true {
612
614
if scanner. skipOptionalObjectEnd ( terminator) {
613
615
if let keyField = keyField, let valueField = valueField {
@@ -617,14 +619,20 @@ internal struct TextFormatDecoder: Decoder {
617
619
throw TextFormatDecodingError . malformedText
618
620
}
619
621
}
620
- if let key = try scanner. nextKey ( ) {
622
+ if let key = try scanner. nextKey ( allowExtensions : ignoreExtensionFields ) {
621
623
switch key {
622
624
case " key " , " 1 " :
623
625
try KeyType . decodeSingular ( value: & keyField, from: & self )
624
626
case " value " , " 2 " :
625
627
try decodeSingularEnumField ( value: & valueField)
626
628
default :
627
- throw TextFormatDecodingError . unknownField
629
+ if ignoreExtensionFields && key. hasPrefix ( " [ " ) {
630
+ try scanner. skipUnknownFieldValue ( )
631
+ } else if options. ignoreUnknownFields && !key. hasPrefix ( " [ " ) {
632
+ try scanner. skipUnknownFieldValue ( )
633
+ } else {
634
+ throw TextFormatDecodingError . unknownField
635
+ }
628
636
}
629
637
scanner. skipOptionalSeparator ( )
630
638
} else {
@@ -657,6 +665,7 @@ internal struct TextFormatDecoder: Decoder {
657
665
var keyField : KeyType . BaseType ?
658
666
var valueField : ValueType ?
659
667
let terminator = try scanner. skipObjectStart ( )
668
+ let ignoreExtensionFields = options. ignoreUnknownExtensionFields
660
669
while true {
661
670
if scanner. skipOptionalObjectEnd ( terminator) {
662
671
if let keyField = keyField, let valueField = valueField {
@@ -666,14 +675,20 @@ internal struct TextFormatDecoder: Decoder {
666
675
throw TextFormatDecodingError . malformedText
667
676
}
668
677
}
669
- if let key = try scanner. nextKey ( ) {
678
+ if let key = try scanner. nextKey ( allowExtensions : ignoreExtensionFields ) {
670
679
switch key {
671
680
case " key " , " 1 " :
672
681
try KeyType . decodeSingular ( value: & keyField, from: & self )
673
682
case " value " , " 2 " :
674
683
try decodeSingularMessageField ( value: & valueField)
675
684
default :
676
- throw TextFormatDecodingError . unknownField
685
+ if ignoreExtensionFields && key. hasPrefix ( " [ " ) {
686
+ try scanner. skipUnknownFieldValue ( )
687
+ } else if options. ignoreUnknownFields && !key. hasPrefix ( " [ " ) {
688
+ try scanner. skipUnknownFieldValue ( )
689
+ } else {
690
+ throw TextFormatDecodingError . unknownField
691
+ }
677
692
}
678
693
scanner. skipOptionalSeparator ( )
679
694
} else {
0 commit comments