Skip to content

Commit d657a81

Browse files
committed
Fix bug of extra paths
1 parent 08270b5 commit d657a81

File tree

3 files changed

+80
-66
lines changed

3 files changed

+80
-66
lines changed

Sources/SwiftProtobuf/GetPathDecoder.swift

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ struct GetPathDecoder<T: Message>: Decoder {
3030
_hasPath
3131
}
3232

33-
internal init(path: [String]) {
34-
if let firstComponent = path.first {
35-
self.number = T.number(for: firstComponent)
33+
internal init(path: [String]) throws {
34+
guard let firstComponent = path.first,
35+
let number = T.number(for: firstComponent) else {
36+
throw PathDecodingError.pathNotFound
3637
}
38+
self.number = number
3739
self.nextPath = .init(path.dropFirst())
3840
}
3941

@@ -44,211 +46,214 @@ struct GetPathDecoder<T: Message>: Decoder {
4446
return number
4547
}
4648

47-
private mutating func captureValue(_ value: Any?) {
49+
private mutating func captureValue(_ value: Any?) throws {
50+
if !nextPath.isEmpty {
51+
throw PathDecodingError.pathNotFound
52+
}
4853
self._value = value
4954
self._hasPath = true
5055
}
5156

5257
mutating func decodeSingularFloatField(value: inout Float) throws {
53-
captureValue(value)
58+
try captureValue(value)
5459
}
5560

5661
mutating func decodeSingularFloatField(value: inout Float?) throws {
57-
captureValue(value)
62+
try captureValue(value)
5863
}
5964

6065
mutating func decodeRepeatedFloatField(value: inout [Float]) throws {
61-
captureValue(value)
66+
try captureValue(value)
6267
}
6368

6469
mutating func decodeSingularDoubleField(value: inout Double) throws {
65-
captureValue(value)
70+
try captureValue(value)
6671
}
6772

6873
mutating func decodeSingularDoubleField(value: inout Double?) throws {
69-
captureValue(value)
74+
try captureValue(value)
7075
}
7176

7277
mutating func decodeRepeatedDoubleField(value: inout [Double]) throws {
73-
captureValue(value)
78+
try captureValue(value)
7479
}
7580

7681
mutating func decodeSingularInt32Field(value: inout Int32) throws {
77-
captureValue(value)
82+
try captureValue(value)
7883
}
7984

8085
mutating func decodeSingularInt32Field(value: inout Int32?) throws {
81-
captureValue(value)
86+
try captureValue(value)
8287
}
8388

8489
mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
85-
captureValue(value)
90+
try captureValue(value)
8691
}
8792

8893
mutating func decodeSingularInt64Field(value: inout Int64) throws {
89-
captureValue(value)
94+
try captureValue(value)
9095
}
9196

9297
mutating func decodeSingularInt64Field(value: inout Int64?) throws {
93-
captureValue(value)
98+
try captureValue(value)
9499
}
95100

96101
mutating func decodeRepeatedInt64Field(value: inout [Int64]) throws {
97-
captureValue(value)
102+
try captureValue(value)
98103
}
99104

100105
mutating func decodeSingularUInt32Field(value: inout UInt32) throws {
101-
captureValue(value)
106+
try captureValue(value)
102107
}
103108

104109
mutating func decodeSingularUInt32Field(value: inout UInt32?) throws {
105-
captureValue(value)
110+
try captureValue(value)
106111
}
107112

108113
mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
109-
captureValue(value)
114+
try captureValue(value)
110115
}
111116

112117
mutating func decodeSingularUInt64Field(value: inout UInt64) throws {
113-
captureValue(value)
118+
try captureValue(value)
114119
}
115120

116121
mutating func decodeSingularUInt64Field(value: inout UInt64?) throws {
117-
captureValue(value)
122+
try captureValue(value)
118123
}
119124

120125
mutating func decodeRepeatedUInt64Field(value: inout [UInt64]) throws {
121-
captureValue(value)
126+
try captureValue(value)
122127
}
123128

124129
mutating func decodeSingularSInt32Field(value: inout Int32) throws {
125-
captureValue(value)
130+
try captureValue(value)
126131
}
127132

128133
mutating func decodeSingularSInt32Field(value: inout Int32?) throws {
129-
captureValue(value)
134+
try captureValue(value)
130135
}
131136

132137
mutating func decodeRepeatedSInt32Field(value: inout [Int32]) throws {
133-
captureValue(value)
138+
try captureValue(value)
134139
}
135140

136141
mutating func decodeSingularSInt64Field(value: inout Int64) throws {
137-
captureValue(value)
142+
try captureValue(value)
138143
}
139144

140145
mutating func decodeSingularSInt64Field(value: inout Int64?) throws {
141-
captureValue(value)
146+
try captureValue(value)
142147
}
143148

144149
mutating func decodeRepeatedSInt64Field(value: inout [Int64]) throws {
145-
captureValue(value)
150+
try captureValue(value)
146151
}
147152

148153
mutating func decodeSingularFixed32Field(value: inout UInt32) throws {
149-
captureValue(value)
154+
try captureValue(value)
150155
}
151156

152157
mutating func decodeSingularFixed32Field(value: inout UInt32?) throws {
153-
captureValue(value)
158+
try captureValue(value)
154159
}
155160

156161
mutating func decodeRepeatedFixed32Field(value: inout [UInt32]) throws {
157-
captureValue(value)
162+
try captureValue(value)
158163
}
159164

160165
mutating func decodeSingularFixed64Field(value: inout UInt64) throws {
161-
captureValue(value)
166+
try captureValue(value)
162167
}
163168

164169
mutating func decodeSingularFixed64Field(value: inout UInt64?) throws {
165-
captureValue(value)
170+
try captureValue(value)
166171
}
167172

168173
mutating func decodeRepeatedFixed64Field(value: inout [UInt64]) throws {
169-
captureValue(value)
174+
try captureValue(value)
170175
}
171176

172177
mutating func decodeSingularSFixed32Field(value: inout Int32) throws {
173-
captureValue(value)
178+
try captureValue(value)
174179
}
175180

176181
mutating func decodeSingularSFixed32Field(value: inout Int32?) throws {
177-
captureValue(value)
182+
try captureValue(value)
178183
}
179184

180185
mutating func decodeRepeatedSFixed32Field(value: inout [Int32]) throws {
181-
captureValue(value)
186+
try captureValue(value)
182187
}
183188

184189
mutating func decodeSingularSFixed64Field(value: inout Int64) throws {
185-
captureValue(value)
190+
try captureValue(value)
186191
}
187192

188193
mutating func decodeSingularSFixed64Field(value: inout Int64?) throws {
189-
captureValue(value)
194+
try captureValue(value)
190195
}
191196

192197
mutating func decodeRepeatedSFixed64Field(value: inout [Int64]) throws {
193-
captureValue(value)
198+
try captureValue(value)
194199
}
195200

196201
mutating func decodeSingularBoolField(value: inout Bool) throws {
197-
captureValue(value)
202+
try captureValue(value)
198203
}
199204

200205
mutating func decodeSingularBoolField(value: inout Bool?) throws {
201-
captureValue(value)
206+
try captureValue(value)
202207
}
203208

204209
mutating func decodeRepeatedBoolField(value: inout [Bool]) throws {
205-
captureValue(value)
210+
try captureValue(value)
206211
}
207212

208213
mutating func decodeSingularStringField(value: inout String) throws {
209-
captureValue(value)
214+
try captureValue(value)
210215
}
211216

212217
mutating func decodeSingularStringField(value: inout String?) throws {
213-
captureValue(value)
218+
try captureValue(value)
214219
}
215220

216221
mutating func decodeRepeatedStringField(value: inout [String]) throws {
217-
captureValue(value)
222+
try captureValue(value)
218223
}
219224

220225
mutating func decodeSingularBytesField(value: inout Data) throws {
221-
captureValue(value)
226+
try captureValue(value)
222227
}
223228

224229
mutating func decodeSingularBytesField(value: inout Data?) throws {
225-
captureValue(value)
230+
try captureValue(value)
226231
}
227232

228233
mutating func decodeRepeatedBytesField(value: inout [Data]) throws {
229-
captureValue(value)
234+
try captureValue(value)
230235
}
231236

232237
mutating func decodeSingularEnumField<E>(value: inout E) throws {
233-
captureValue(value)
238+
try captureValue(value)
234239
}
235240

236241
mutating func decodeSingularEnumField<E>(value: inout E?) throws {
237-
captureValue(value)
242+
try captureValue(value)
238243
}
239244

240245
mutating func decodeRepeatedEnumField<E>(value: inout [E]) throws {
241-
captureValue(value)
246+
try captureValue(value)
242247
}
243248

244249
mutating func decodeSingularMessageField<M>(
245250
value: inout M?
246251
) throws where M : Message {
247252
if nextPath.isEmpty {
248-
captureValue(value)
253+
try captureValue(value)
249254
return
250255
}
251-
var decoder = GetPathDecoder<M>(path: nextPath)
256+
var decoder = try GetPathDecoder<M>(path: nextPath)
252257
if value != nil {
253258
try value?.decodeMessage(decoder: &decoder)
254259
} else {
@@ -260,36 +265,36 @@ struct GetPathDecoder<T: Message>: Decoder {
260265
}
261266

262267
mutating func decodeRepeatedMessageField<M>(value: inout [M]) throws {
263-
captureValue(value)
268+
try captureValue(value)
264269
}
265270

266271
mutating func decodeSingularGroupField<G>(value: inout G?) throws {
267-
captureValue(value)
272+
try captureValue(value)
268273
}
269274

270275
mutating func decodeRepeatedGroupField<G>(value: inout [G]) throws {
271-
captureValue(value)
276+
try captureValue(value)
272277
}
273278

274279
mutating func decodeMapField<KeyType, ValueType>(
275280
fieldType: _ProtobufMap<KeyType, ValueType>.Type,
276281
value: inout _ProtobufMap<KeyType, ValueType>.BaseType
277282
) throws {
278-
captureValue(value)
283+
try captureValue(value)
279284
}
280285

281286
mutating func decodeMapField<KeyType, ValueType>(
282287
fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
283288
value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
284289
) throws {
285-
captureValue(value)
290+
try captureValue(value)
286291
}
287292

288293
mutating func decodeMapField<KeyType, ValueType>(
289294
fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
290295
value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
291296
) throws {
292-
captureValue(value)
297+
try captureValue(value)
293298
}
294299

295300
mutating func decodeExtensionField(
@@ -303,14 +308,16 @@ struct GetPathDecoder<T: Message>: Decoder {
303308
extension Message {
304309
mutating func `get`(path: String) throws -> Any? {
305310
let _path = path.components(separatedBy: ".")
306-
var decoder = GetPathDecoder<Self>(path: _path)
311+
var decoder = try GetPathDecoder<Self>(path: _path)
307312
try decodeMessage(decoder: &decoder)
308313
return decoder.value
309314
}
310315

311316
mutating func hasPath(path: String) -> Bool {
312317
let _path = path.components(separatedBy: ".")
313-
var decoder = GetPathDecoder<Self>(path: _path)
318+
guard var decoder = try? GetPathDecoder<Self>(path: _path) else {
319+
return false
320+
}
314321
try? decodeMessage(decoder: &decoder)
315322
return decoder.hasPath
316323
}

Sources/SwiftProtobuf/SetPathDecoder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public enum PathDecodingError: Error {
2222
/// If a value of type A is applied to a path with type B.
2323
/// this error will be thrown.
2424
case typeMismatch
25+
26+
/// Describes path is not found in message type.
27+
///
28+
/// If a message has no property with path this error
29+
/// will be thrown.
30+
case pathNotFound
2531
}
2632

2733
extension Message {

Tests/SwiftProtobufTests/Test_FieldMask.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,16 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
184184
}
185185

186186
// Checks `isPathValid` func
187-
// 1. Valid primitive path should be valid.
188-
// 2. Valid nested path should be valid.
189-
// 3. Invalid primitive path should be valid.
190-
// 4. Invalid nested path should be valid.
187+
// 1. Valid primitive path.
188+
// 2. Valid nested path.
189+
// 3. Invalid primitive path.
190+
// 4, 5. Invalid nested path.
191191
func testIsPathValid() {
192192
XCTAssertTrue(SwiftProtoTesting_TestAllTypes.isPathValid("optional_int32"))
193193
XCTAssertTrue(SwiftProtoTesting_TestAllTypes.isPathValid("optional_nested_message.bb"))
194194
XCTAssertFalse(SwiftProtoTesting_TestAllTypes.isPathValid("optional_int"))
195195
XCTAssertFalse(SwiftProtoTesting_TestAllTypes.isPathValid("optional_nested_message.bc"))
196+
XCTAssertFalse(SwiftProtoTesting_TestAllTypes.isPathValid("optional_nested_message.bb.a"))
196197
}
197198

198199
// Checks `isValid` func of FieldMask.

0 commit comments

Comments
 (0)