/src/swift-protobuf/Sources/SwiftProtobuf/ExtensionFields.swift
Line | Count | Source (jump to first uncovered line) |
1 | | // Sources/SwiftProtobuf/ExtensionFields.swift - Extension support |
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 | | /// Core protocols implemented by generated extensions. |
12 | | /// |
13 | | // ----------------------------------------------------------------------------- |
14 | | |
15 | | #if !swift(>=4.2) |
16 | | private let i_2166136261 = Int(bitPattern: 2166136261) |
17 | | private let i_16777619 = Int(16777619) |
18 | | #endif |
19 | | |
20 | | // |
21 | | // Type-erased Extension field implementation. |
22 | | // Note that it has no "self or associated type" references, so can |
23 | | // be used as a protocol type. (In particular, although it does have |
24 | | // a hashValue property, it cannot be Hashable.) |
25 | | // |
26 | | // This can encode, decode, return a hashValue and test for |
27 | | // equality with some other extension field; but it's type-sealed |
28 | | // so you can't actually access the contained value itself. |
29 | | // |
30 | | public protocol AnyExtensionField: CustomDebugStringConvertible { |
31 | | #if swift(>=4.2) |
32 | | func hash(into hasher: inout Hasher) |
33 | | #else |
34 | | var hashValue: Int { get } |
35 | | #endif |
36 | | var protobufExtension: AnyMessageExtension { get } |
37 | | func isEqual(other: AnyExtensionField) -> Bool |
38 | | |
39 | | /// Merging field decoding |
40 | | mutating func decodeExtensionField<T: Decoder>(decoder: inout T) throws |
41 | | |
42 | | /// Fields know their own type, so can dispatch to a visitor |
43 | | func traverse<V: Visitor>(visitor: inout V) throws |
44 | | |
45 | | /// Check if the field is initialized. |
46 | | var isInitialized: Bool { get } |
47 | | } |
48 | | |
49 | | extension AnyExtensionField { |
50 | | // Default implementation for extensions fields. The message types below provide |
51 | | // custom versions. |
52 | 28.3k | public var isInitialized: Bool { return true } |
53 | | } |
54 | | |
55 | | /// |
56 | | /// The regular ExtensionField type exposes the value directly. |
57 | | /// |
58 | | public protocol ExtensionField: AnyExtensionField, Hashable { |
59 | | associatedtype ValueType |
60 | | var value: ValueType { get set } |
61 | | init(protobufExtension: AnyMessageExtension, value: ValueType) |
62 | | init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws |
63 | | } |
64 | | |
65 | | /// |
66 | | /// Singular field |
67 | | /// |
68 | | public struct OptionalExtensionField<T: FieldType>: ExtensionField { |
69 | | public typealias BaseType = T.BaseType |
70 | | public typealias ValueType = BaseType |
71 | | public var value: ValueType |
72 | | public var protobufExtension: AnyMessageExtension |
73 | | |
74 | | public static func ==(lhs: OptionalExtensionField, |
75 | 0 | rhs: OptionalExtensionField) -> Bool { |
76 | 0 | return lhs.value == rhs.value |
77 | 0 | } |
78 | | |
79 | 9.10k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
80 | 9.10k | self.protobufExtension = protobufExtension |
81 | 9.10k | self.value = value |
82 | 9.10k | } |
83 | | |
84 | | public var debugDescription: String { |
85 | 0 | get { |
86 | 0 | return String(reflecting: value) |
87 | 0 | } |
88 | | } |
89 | | |
90 | | #if swift(>=4.2) |
91 | 0 | public func hash(into hasher: inout Hasher) { |
92 | 0 | hasher.combine(value) |
93 | 0 | } |
94 | | #else // swift(>=4.2) |
95 | | public var hashValue: Int { |
96 | | get { return value.hashValue } |
97 | | } |
98 | | #endif // swift(>=4.2) |
99 | | |
100 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
101 | 0 | let o = other as! OptionalExtensionField<T> |
102 | 0 | return self == o |
103 | 0 | } |
104 | | |
105 | 14.5k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
106 | 14.5k | var v: ValueType? |
107 | 14.5k | try T.decodeSingular(value: &v, from: &decoder) |
108 | 12.8k | if let v = v { |
109 | 12.8k | value = v |
110 | 14.5k | } |
111 | 14.5k | } |
112 | | |
113 | 16.6k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
114 | 16.6k | var v: ValueType? |
115 | 16.6k | try T.decodeSingular(value: &v, from: &decoder) |
116 | 9.10k | if let v = v { |
117 | 9.10k | self.init(protobufExtension: protobufExtension, value: v) |
118 | 16.6k | } else { |
119 | 7.49k | return nil |
120 | 9.10k | } |
121 | 9.10k | } |
122 | | |
123 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
124 | 0 | try T.visitSingular(value: value, fieldNumber: protobufExtension.fieldNumber, with: &visitor) |
125 | 0 | } |
126 | | } |
127 | | |
128 | | /// |
129 | | /// Repeated fields |
130 | | /// |
131 | | public struct RepeatedExtensionField<T: FieldType>: ExtensionField { |
132 | | public typealias BaseType = T.BaseType |
133 | | public typealias ValueType = [BaseType] |
134 | | public var value: ValueType |
135 | | public var protobufExtension: AnyMessageExtension |
136 | | |
137 | | public static func ==(lhs: RepeatedExtensionField, |
138 | 0 | rhs: RepeatedExtensionField) -> Bool { |
139 | 0 | return lhs.value == rhs.value |
140 | 0 | } |
141 | | |
142 | 52.9k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
143 | 52.9k | self.protobufExtension = protobufExtension |
144 | 52.9k | self.value = value |
145 | 52.9k | } |
146 | | |
147 | | #if swift(>=4.2) |
148 | 0 | public func hash(into hasher: inout Hasher) { |
149 | 0 | hasher.combine(value) |
150 | 0 | } |
151 | | #else // swift(>=4.2) |
152 | | public var hashValue: Int { |
153 | | get { |
154 | | var hash = i_2166136261 |
155 | | for e in value { |
156 | | hash = (hash &* i_16777619) ^ e.hashValue |
157 | | } |
158 | | return hash |
159 | | } |
160 | | } |
161 | | #endif // swift(>=4.2) |
162 | | |
163 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
164 | 0 | let o = other as! RepeatedExtensionField<T> |
165 | 0 | return self == o |
166 | 0 | } |
167 | | |
168 | 0 | public var debugDescription: String { |
169 | 0 | return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" |
170 | 0 | } |
171 | | |
172 | 781k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
173 | 781k | try T.decodeRepeated(value: &value, from: &decoder) |
174 | 781k | } |
175 | | |
176 | 54.6k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
177 | 54.6k | var v: ValueType = [] |
178 | 54.6k | try T.decodeRepeated(value: &v, from: &decoder) |
179 | 54.6k | self.init(protobufExtension: protobufExtension, value: v) |
180 | 54.6k | } |
181 | | |
182 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
183 | 0 | if value.count > 0 { |
184 | 0 | try T.visitRepeated(value: value, fieldNumber: protobufExtension.fieldNumber, with: &visitor) |
185 | 0 | } |
186 | 0 | } |
187 | | } |
188 | | |
189 | | /// |
190 | | /// Packed Repeated fields |
191 | | /// |
192 | | /// TODO: This is almost (but not quite) identical to RepeatedFields; |
193 | | /// find a way to collapse the implementations. |
194 | | /// |
195 | | public struct PackedExtensionField<T: FieldType>: ExtensionField { |
196 | | public typealias BaseType = T.BaseType |
197 | | public typealias ValueType = [BaseType] |
198 | | public var value: ValueType |
199 | | public var protobufExtension: AnyMessageExtension |
200 | | |
201 | | public static func ==(lhs: PackedExtensionField, |
202 | 0 | rhs: PackedExtensionField) -> Bool { |
203 | 0 | return lhs.value == rhs.value |
204 | 0 | } |
205 | | |
206 | 0 | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
207 | 0 | self.protobufExtension = protobufExtension |
208 | 0 | self.value = value |
209 | 0 | } |
210 | | |
211 | | #if swift(>=4.2) |
212 | 0 | public func hash(into hasher: inout Hasher) { |
213 | 0 | hasher.combine(value) |
214 | 0 | } |
215 | | #else // swift(>=4.2) |
216 | | public var hashValue: Int { |
217 | | get { |
218 | | var hash = i_2166136261 |
219 | | for e in value { |
220 | | hash = (hash &* i_16777619) ^ e.hashValue |
221 | | } |
222 | | return hash |
223 | | } |
224 | | } |
225 | | #endif // swift(>=4.2) |
226 | | |
227 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
228 | 0 | let o = other as! PackedExtensionField<T> |
229 | 0 | return self == o |
230 | 0 | } |
231 | | |
232 | 0 | public var debugDescription: String { |
233 | 0 | return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" |
234 | 0 | } |
235 | | |
236 | 0 | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
237 | 0 | try T.decodeRepeated(value: &value, from: &decoder) |
238 | 0 | } |
239 | | |
240 | 0 | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
241 | 0 | var v: ValueType = [] |
242 | 0 | try T.decodeRepeated(value: &v, from: &decoder) |
243 | 0 | self.init(protobufExtension: protobufExtension, value: v) |
244 | 0 | } |
245 | | |
246 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
247 | 0 | if value.count > 0 { |
248 | 0 | try T.visitPacked(value: value, fieldNumber: protobufExtension.fieldNumber, with: &visitor) |
249 | 0 | } |
250 | 0 | } |
251 | | } |
252 | | |
253 | | /// |
254 | | /// Enum extensions |
255 | | /// |
256 | | public struct OptionalEnumExtensionField<E: Enum>: ExtensionField where E.RawValue == Int { |
257 | | public typealias BaseType = E |
258 | | public typealias ValueType = E |
259 | | public var value: ValueType |
260 | | public var protobufExtension: AnyMessageExtension |
261 | | |
262 | | public static func ==(lhs: OptionalEnumExtensionField, |
263 | 0 | rhs: OptionalEnumExtensionField) -> Bool { |
264 | 0 | return lhs.value == rhs.value |
265 | 0 | } |
266 | | |
267 | 2.48k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
268 | 2.48k | self.protobufExtension = protobufExtension |
269 | 2.48k | self.value = value |
270 | 2.48k | } |
271 | | |
272 | | public var debugDescription: String { |
273 | 0 | get { |
274 | 0 | return String(reflecting: value) |
275 | 0 | } |
276 | | } |
277 | | |
278 | | #if swift(>=4.2) |
279 | 0 | public func hash(into hasher: inout Hasher) { |
280 | 0 | hasher.combine(value) |
281 | 0 | } |
282 | | #else // swift(>=4.2) |
283 | | public var hashValue: Int { |
284 | | get { return value.hashValue } |
285 | | } |
286 | | #endif // swift(>=4.2) |
287 | | |
288 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
289 | 0 | let o = other as! OptionalEnumExtensionField<E> |
290 | 0 | return self == o |
291 | 0 | } |
292 | | |
293 | 4.39k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
294 | 4.39k | var v: ValueType? |
295 | 4.39k | try decoder.decodeSingularEnumField(value: &v) |
296 | 3.47k | if let v = v { |
297 | 3.47k | value = v |
298 | 4.39k | } |
299 | 4.39k | } |
300 | | |
301 | 4.04k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
302 | 4.04k | var v: ValueType? |
303 | 4.04k | try decoder.decodeSingularEnumField(value: &v) |
304 | 2.48k | if let v = v { |
305 | 2.48k | self.init(protobufExtension: protobufExtension, value: v) |
306 | 4.04k | } else { |
307 | 1.55k | return nil |
308 | 2.48k | } |
309 | 2.48k | } |
310 | | |
311 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
312 | 0 | try visitor.visitSingularEnumField( |
313 | 0 | value: value, |
314 | 0 | fieldNumber: protobufExtension.fieldNumber) |
315 | 0 | } |
316 | | } |
317 | | |
318 | | /// |
319 | | /// Repeated Enum fields |
320 | | /// |
321 | | public struct RepeatedEnumExtensionField<E: Enum>: ExtensionField where E.RawValue == Int { |
322 | | public typealias BaseType = E |
323 | | public typealias ValueType = [E] |
324 | | public var value: ValueType |
325 | | public var protobufExtension: AnyMessageExtension |
326 | | |
327 | | public static func ==(lhs: RepeatedEnumExtensionField, |
328 | 0 | rhs: RepeatedEnumExtensionField) -> Bool { |
329 | 0 | return lhs.value == rhs.value |
330 | 0 | } |
331 | | |
332 | 8.28k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
333 | 8.28k | self.protobufExtension = protobufExtension |
334 | 8.28k | self.value = value |
335 | 8.28k | } |
336 | | |
337 | | #if swift(>=4.2) |
338 | 0 | public func hash(into hasher: inout Hasher) { |
339 | 0 | hasher.combine(value) |
340 | 0 | } |
341 | | #else // swift(>=4.2) |
342 | | public var hashValue: Int { |
343 | | get { |
344 | | var hash = i_2166136261 |
345 | | for e in value { |
346 | | hash = (hash &* i_16777619) ^ e.hashValue |
347 | | } |
348 | | return hash |
349 | | } |
350 | | } |
351 | | #endif // swift(>=4.2) |
352 | | |
353 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
354 | 0 | let o = other as! RepeatedEnumExtensionField<E> |
355 | 0 | return self == o |
356 | 0 | } |
357 | | |
358 | 0 | public var debugDescription: String { |
359 | 0 | return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" |
360 | 0 | } |
361 | | |
362 | 44.9k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
363 | 44.9k | try decoder.decodeRepeatedEnumField(value: &value) |
364 | 44.9k | } |
365 | | |
366 | 8.40k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
367 | 8.40k | var v: ValueType = [] |
368 | 8.40k | try decoder.decodeRepeatedEnumField(value: &v) |
369 | 8.40k | self.init(protobufExtension: protobufExtension, value: v) |
370 | 8.40k | } |
371 | | |
372 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
373 | 0 | if value.count > 0 { |
374 | 0 | try visitor.visitRepeatedEnumField( |
375 | 0 | value: value, |
376 | 0 | fieldNumber: protobufExtension.fieldNumber) |
377 | 0 | } |
378 | 0 | } |
379 | | } |
380 | | |
381 | | /// |
382 | | /// Packed Repeated Enum fields |
383 | | /// |
384 | | /// TODO: This is almost (but not quite) identical to RepeatedEnumFields; |
385 | | /// find a way to collapse the implementations. |
386 | | /// |
387 | | public struct PackedEnumExtensionField<E: Enum>: ExtensionField where E.RawValue == Int { |
388 | | public typealias BaseType = E |
389 | | public typealias ValueType = [E] |
390 | | public var value: ValueType |
391 | | public var protobufExtension: AnyMessageExtension |
392 | | |
393 | | public static func ==(lhs: PackedEnumExtensionField, |
394 | 0 | rhs: PackedEnumExtensionField) -> Bool { |
395 | 0 | return lhs.value == rhs.value |
396 | 0 | } |
397 | | |
398 | 0 | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
399 | 0 | self.protobufExtension = protobufExtension |
400 | 0 | self.value = value |
401 | 0 | } |
402 | | |
403 | | #if swift(>=4.2) |
404 | 0 | public func hash(into hasher: inout Hasher) { |
405 | 0 | hasher.combine(value) |
406 | 0 | } |
407 | | #else // swift(>=4.2) |
408 | | public var hashValue: Int { |
409 | | get { |
410 | | var hash = i_2166136261 |
411 | | for e in value { |
412 | | hash = (hash &* i_16777619) ^ e.hashValue |
413 | | } |
414 | | return hash |
415 | | } |
416 | | } |
417 | | #endif // swift(>=4.2) |
418 | | |
419 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
420 | 0 | let o = other as! PackedEnumExtensionField<E> |
421 | 0 | return self == o |
422 | 0 | } |
423 | | |
424 | 0 | public var debugDescription: String { |
425 | 0 | return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" |
426 | 0 | } |
427 | | |
428 | 0 | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
429 | 0 | try decoder.decodeRepeatedEnumField(value: &value) |
430 | 0 | } |
431 | | |
432 | 0 | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
433 | 0 | var v: ValueType = [] |
434 | 0 | try decoder.decodeRepeatedEnumField(value: &v) |
435 | 0 | self.init(protobufExtension: protobufExtension, value: v) |
436 | 0 | } |
437 | | |
438 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
439 | 0 | if value.count > 0 { |
440 | 0 | try visitor.visitPackedEnumField( |
441 | 0 | value: value, |
442 | 0 | fieldNumber: protobufExtension.fieldNumber) |
443 | 0 | } |
444 | 0 | } |
445 | | } |
446 | | |
447 | | // |
448 | | // ========== Message ========== |
449 | | // |
450 | | public struct OptionalMessageExtensionField<M: Message & Equatable>: |
451 | | ExtensionField { |
452 | | public typealias BaseType = M |
453 | | public typealias ValueType = BaseType |
454 | | public var value: ValueType |
455 | | public var protobufExtension: AnyMessageExtension |
456 | | |
457 | | public static func ==(lhs: OptionalMessageExtensionField, |
458 | 0 | rhs: OptionalMessageExtensionField) -> Bool { |
459 | 0 | return lhs.value == rhs.value |
460 | 0 | } |
461 | | |
462 | 8.95k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
463 | 8.95k | self.protobufExtension = protobufExtension |
464 | 8.95k | self.value = value |
465 | 8.95k | } |
466 | | |
467 | | public var debugDescription: String { |
468 | 0 | get { |
469 | 0 | return String(reflecting: value) |
470 | 0 | } |
471 | | } |
472 | | |
473 | | #if swift(>=4.2) |
474 | 0 | public func hash(into hasher: inout Hasher) { |
475 | 0 | value.hash(into: &hasher) |
476 | 0 | } |
477 | | #else // swift(>=4.2) |
478 | | public var hashValue: Int {return value.hashValue} |
479 | | #endif // swift(>=4.2) |
480 | | |
481 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
482 | 0 | let o = other as! OptionalMessageExtensionField<M> |
483 | 0 | return self == o |
484 | 0 | } |
485 | | |
486 | 132k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
487 | 132k | var v: ValueType? = value |
488 | 132k | try decoder.decodeSingularMessageField(value: &v) |
489 | 128k | if let v = v { |
490 | 128k | self.value = v |
491 | 132k | } |
492 | 132k | } |
493 | | |
494 | 16.9k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
495 | 16.9k | var v: ValueType? |
496 | 16.9k | try decoder.decodeSingularMessageField(value: &v) |
497 | 8.95k | if let v = v { |
498 | 8.95k | self.init(protobufExtension: protobufExtension, value: v) |
499 | 16.9k | } else { |
500 | 7.95k | return nil |
501 | 8.95k | } |
502 | 8.95k | } |
503 | | |
504 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
505 | 0 | try visitor.visitSingularMessageField( |
506 | 0 | value: value, fieldNumber: protobufExtension.fieldNumber) |
507 | 0 | } |
508 | | |
509 | 2.40k | public var isInitialized: Bool { |
510 | 2.40k | return value.isInitialized |
511 | 2.40k | } |
512 | | } |
513 | | |
514 | | public struct RepeatedMessageExtensionField<M: Message & Equatable>: |
515 | | ExtensionField { |
516 | | public typealias BaseType = M |
517 | | public typealias ValueType = [BaseType] |
518 | | public var value: ValueType |
519 | | public var protobufExtension: AnyMessageExtension |
520 | | |
521 | | public static func ==(lhs: RepeatedMessageExtensionField, |
522 | 0 | rhs: RepeatedMessageExtensionField) -> Bool { |
523 | 0 | return lhs.value == rhs.value |
524 | 0 | } |
525 | | |
526 | 37.4k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
527 | 37.4k | self.protobufExtension = protobufExtension |
528 | 37.4k | self.value = value |
529 | 37.4k | } |
530 | | |
531 | | #if swift(>=4.2) |
532 | 0 | public func hash(into hasher: inout Hasher) { |
533 | 0 | for e in value { |
534 | 0 | e.hash(into: &hasher) |
535 | 0 | } |
536 | 0 | } |
537 | | #else // swift(>=4.2) |
538 | | public var hashValue: Int { |
539 | | get { |
540 | | var hash = i_2166136261 |
541 | | for e in value { |
542 | | hash = (hash &* i_16777619) ^ e.hashValue |
543 | | } |
544 | | return hash |
545 | | } |
546 | | } |
547 | | #endif // swift(>=4.2) |
548 | | |
549 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
550 | 0 | let o = other as! RepeatedMessageExtensionField<M> |
551 | 0 | return self == o |
552 | 0 | } |
553 | | |
554 | 0 | public var debugDescription: String { |
555 | 0 | return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" |
556 | 0 | } |
557 | | |
558 | 195k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
559 | 195k | try decoder.decodeRepeatedMessageField(value: &value) |
560 | 195k | } |
561 | | |
562 | 42.9k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
563 | 42.9k | var v: ValueType = [] |
564 | 42.9k | try decoder.decodeRepeatedMessageField(value: &v) |
565 | 42.9k | self.init(protobufExtension: protobufExtension, value: v) |
566 | 42.9k | } |
567 | | |
568 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
569 | 0 | if value.count > 0 { |
570 | 0 | try visitor.visitRepeatedMessageField( |
571 | 0 | value: value, fieldNumber: protobufExtension.fieldNumber) |
572 | 0 | } |
573 | 0 | } |
574 | | |
575 | 28.0k | public var isInitialized: Bool { |
576 | 28.0k | return Internal.areAllInitialized(value) |
577 | 28.0k | } |
578 | | } |
579 | | |
580 | | // |
581 | | // ======== Groups within Messages ======== |
582 | | // |
583 | | // Protoc internally treats groups the same as messages, but |
584 | | // they serialize very differently, so we have separate serialization |
585 | | // handling here... |
586 | | public struct OptionalGroupExtensionField<G: Message & Hashable>: |
587 | | ExtensionField { |
588 | | public typealias BaseType = G |
589 | | public typealias ValueType = BaseType |
590 | | public var value: G |
591 | | public var protobufExtension: AnyMessageExtension |
592 | | |
593 | | public static func ==(lhs: OptionalGroupExtensionField, |
594 | 0 | rhs: OptionalGroupExtensionField) -> Bool { |
595 | 0 | return lhs.value == rhs.value |
596 | 0 | } |
597 | | |
598 | 1.85k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
599 | 1.85k | self.protobufExtension = protobufExtension |
600 | 1.85k | self.value = value |
601 | 1.85k | } |
602 | | |
603 | | #if swift(>=4.2) |
604 | 0 | public func hash(into hasher: inout Hasher) { |
605 | 0 | hasher.combine(value) |
606 | 0 | } |
607 | | #else // swift(>=4.2) |
608 | | public var hashValue: Int {return value.hashValue} |
609 | | #endif // swift(>=4.2) |
610 | | |
611 | 0 | public var debugDescription: String { get {return value.debugDescription} } |
612 | | |
613 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
614 | 0 | let o = other as! OptionalGroupExtensionField<G> |
615 | 0 | return self == o |
616 | 0 | } |
617 | | |
618 | 5.14k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
619 | 5.14k | var v: ValueType? = value |
620 | 5.14k | try decoder.decodeSingularGroupField(value: &v) |
621 | 5.10k | if let v = v { |
622 | 5.10k | value = v |
623 | 5.14k | } |
624 | 5.14k | } |
625 | | |
626 | 9.81k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
627 | 9.81k | var v: ValueType? |
628 | 9.81k | try decoder.decodeSingularGroupField(value: &v) |
629 | 1.85k | if let v = v { |
630 | 1.85k | self.init(protobufExtension: protobufExtension, value: v) |
631 | 9.81k | } else { |
632 | 7.96k | return nil |
633 | 1.85k | } |
634 | 1.85k | } |
635 | | |
636 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
637 | 0 | try visitor.visitSingularGroupField( |
638 | 0 | value: value, fieldNumber: protobufExtension.fieldNumber) |
639 | 0 | } |
640 | | |
641 | 461 | public var isInitialized: Bool { |
642 | 461 | return value.isInitialized |
643 | 461 | } |
644 | | } |
645 | | |
646 | | public struct RepeatedGroupExtensionField<G: Message & Hashable>: |
647 | | ExtensionField { |
648 | | public typealias BaseType = G |
649 | | public typealias ValueType = [BaseType] |
650 | | public var value: ValueType |
651 | | public var protobufExtension: AnyMessageExtension |
652 | | |
653 | | public static func ==(lhs: RepeatedGroupExtensionField, |
654 | 0 | rhs: RepeatedGroupExtensionField) -> Bool { |
655 | 0 | return lhs.value == rhs.value |
656 | 0 | } |
657 | | |
658 | 3.37k | public init(protobufExtension: AnyMessageExtension, value: ValueType) { |
659 | 3.37k | self.protobufExtension = protobufExtension |
660 | 3.37k | self.value = value |
661 | 3.37k | } |
662 | | |
663 | | #if swift(>=4.2) |
664 | 0 | public func hash(into hasher: inout Hasher) { |
665 | 0 | hasher.combine(value) |
666 | 0 | } |
667 | | #else // swift(>=4.2) |
668 | | public var hashValue: Int { |
669 | | get { |
670 | | var hash = i_2166136261 |
671 | | for e in value { |
672 | | hash = (hash &* i_16777619) ^ e.hashValue |
673 | | } |
674 | | return hash |
675 | | } |
676 | | } |
677 | | #endif // swift(>=4.2) |
678 | | |
679 | 0 | public var debugDescription: String { |
680 | 0 | return "[" + value.map{$0.debugDescription}.joined(separator: ",") + "]" |
681 | 0 | } |
682 | | |
683 | 0 | public func isEqual(other: AnyExtensionField) -> Bool { |
684 | 0 | let o = other as! RepeatedGroupExtensionField<G> |
685 | 0 | return self == o |
686 | 0 | } |
687 | | |
688 | 11.5k | public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws { |
689 | 11.5k | try decoder.decodeRepeatedGroupField(value: &value) |
690 | 11.5k | } |
691 | | |
692 | 3.51k | public init?<D: Decoder>(protobufExtension: AnyMessageExtension, decoder: inout D) throws { |
693 | 3.51k | var v: ValueType = [] |
694 | 3.51k | try decoder.decodeRepeatedGroupField(value: &v) |
695 | 3.51k | self.init(protobufExtension: protobufExtension, value: v) |
696 | 3.51k | } |
697 | | |
698 | 0 | public func traverse<V: Visitor>(visitor: inout V) throws { |
699 | 0 | if value.count > 0 { |
700 | 0 | try visitor.visitRepeatedGroupField( |
701 | 0 | value: value, fieldNumber: protobufExtension.fieldNumber) |
702 | 0 | } |
703 | 0 | } |
704 | | |
705 | 1.57k | public var isInitialized: Bool { |
706 | 1.57k | return Internal.areAllInitialized(value) |
707 | 1.57k | } |
708 | | } |