|
| 1 | +@_implementationOnly import SwiftSyntax |
| 2 | + |
| 3 | +/// Represents possible fallback options for decoding failure. |
| 4 | +/// |
| 5 | +/// When decoding fails for variable, variable can have fallback |
| 6 | +/// to throw the failure error or handle it completely or handle |
| 7 | +/// it only when variable is missing or `null`. |
| 8 | +enum DecodingFallback { |
| 9 | + /// Represents no fallback option. |
| 10 | + /// |
| 11 | + /// Indicates decoding failure error |
| 12 | + /// is thrown without any handling. |
| 13 | + case `throw` |
| 14 | + /// Represents fallback option for missing |
| 15 | + /// or `null` value. |
| 16 | + /// |
| 17 | + /// Indicates if variable data is missing or `null`, |
| 18 | + /// provided fallback syntax will be used for initialization. |
| 19 | + case ifMissing(CodeBlockItemListSyntax) |
| 20 | + /// Represents fallback option handling |
| 21 | + /// decoding failure completely. |
| 22 | + /// |
| 23 | + /// Indicates for any type of failure error in decoding, |
| 24 | + /// provided fallback syntax will be used for initialization. |
| 25 | + case ifError(CodeBlockItemListSyntax) |
| 26 | + |
| 27 | + /// Provides the code block list syntax for decoding provided |
| 28 | + /// container applying current fallback options. |
| 29 | + /// |
| 30 | + /// - Parameters: |
| 31 | + /// - container: The container to decode from. |
| 32 | + /// - key: The key from where to decode. |
| 33 | + /// - decoding: The nested container decoding |
| 34 | + /// code block generator. |
| 35 | + /// |
| 36 | + /// - Returns: The generated code block. |
| 37 | + func represented( |
| 38 | + decodingContainer container: TokenSyntax, |
| 39 | + fromKey key: Registrar.Key, |
| 40 | + nestedDecoding decoding: (TokenSyntax) -> CodeBlockItemListSyntax |
| 41 | + ) -> CodeBlockItemListSyntax { |
| 42 | + let nestedContainer: TokenSyntax = "\(key.raw)_\(container)" |
| 43 | + return CodeBlockItemListSyntax { |
| 44 | + switch self { |
| 45 | + case .throw: |
| 46 | + """ |
| 47 | + let \(nestedContainer) = try \(container).nestedContainer(keyedBy: \(key.type), forKey: \(key.expr)) |
| 48 | + """ |
| 49 | + decoding(nestedContainer) |
| 50 | + case .ifMissing(let fallbacks): |
| 51 | + try! IfExprSyntax( |
| 52 | + """ |
| 53 | + if (try? \(container).decodeNil(forKey: \(key.expr))) == false |
| 54 | + """ |
| 55 | + ) { |
| 56 | + """ |
| 57 | + let \(nestedContainer) = try \(container).nestedContainer(keyedBy: \(key.type), forKey: \(key.expr)) |
| 58 | + """ |
| 59 | + decoding(nestedContainer) |
| 60 | + } else: { |
| 61 | + fallbacks |
| 62 | + } |
| 63 | + case .ifError(let fallbacks): |
| 64 | + try! IfExprSyntax( |
| 65 | + """ |
| 66 | + if let \(nestedContainer) = try? \(container).nestedContainer(keyedBy: \(key.type), forKey: \(key.expr)) |
| 67 | + """ |
| 68 | + ) { |
| 69 | + decoding(nestedContainer) |
| 70 | + } else: { |
| 71 | + fallbacks |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +extension Collection where Element == DecodingFallback { |
| 79 | + /// The combined fallback option for all variable elements. |
| 80 | + /// |
| 81 | + /// Represents the fallback to use when decoding container |
| 82 | + /// of all the element variables fails. |
| 83 | + var aggregate: Element { |
| 84 | + var aggregated = Element.ifError(.init()) |
| 85 | + for fallback in self { |
| 86 | + switch (aggregated, fallback) { |
| 87 | + case (_, .throw), (.throw, _): |
| 88 | + return .throw |
| 89 | + case (.ifMissing(var a), .ifMissing(let f)), |
| 90 | + (.ifMissing(var a), .ifError(let f)), |
| 91 | + (.ifError(var a), .ifMissing(let f)): |
| 92 | + a.append(contentsOf: f) |
| 93 | + aggregated = .ifMissing(a) |
| 94 | + case (.ifError(var a), .ifError(let f)): |
| 95 | + a.append(contentsOf: f) |
| 96 | + aggregated = .ifError(a) |
| 97 | + } |
| 98 | + } |
| 99 | + return aggregated |
| 100 | + } |
| 101 | +} |
0 commit comments