|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021-2023 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +/// A RenderSection value that can be diffed. |
| 12 | +/// |
| 13 | +/// An `AnyRenderSection` value forwards difference operations to the underlying base type, each of which determine the difference differently. |
| 14 | +struct AnyRenderSection: Equatable, Encodable, RenderJSONDiffable { |
| 15 | + var value: RenderSection |
| 16 | + |
| 17 | + init(_ value: RenderSection) { |
| 18 | + self.value = value |
| 19 | + } |
| 20 | + |
| 21 | + func encode(to encoder: Encoder) throws { |
| 22 | + try value.encode(to: encoder) |
| 23 | + } |
| 24 | + |
| 25 | + /// Forwards the difference methods on to the correct concrete type. |
| 26 | + func difference(from other: AnyRenderSection, at path: CodablePath) -> JSONPatchDifferences { |
| 27 | + switch (self.value.kind, other.value.kind) { |
| 28 | + |
| 29 | + // MARK: Symbol Sections |
| 30 | + |
| 31 | + case (.attributes, .attributes): |
| 32 | + return (value as! AttributesRenderSection).difference(from: (other.value as! AttributesRenderSection), at: path) |
| 33 | + case (.discussion, .discussion), (.content, .content): |
| 34 | + return (value as! ContentRenderSection).difference(from: (other.value as! ContentRenderSection), at: path) |
| 35 | + case (.declarations, .declarations): |
| 36 | + return (value as! DeclarationsRenderSection).difference(from: (other.value as! DeclarationsRenderSection), at: path) |
| 37 | + case (.parameters, .parameters): |
| 38 | + return (value as! ParametersRenderSection).difference(from: (other.value as! ParametersRenderSection), at: path) |
| 39 | + case (.plistDetails, .plistDetails): |
| 40 | + return (value as! PlistDetailsRenderSection).difference(from: (other.value as! PlistDetailsRenderSection), at: path) |
| 41 | + case (.possibleValues, .possibleValues): |
| 42 | + return (value as! PossibleValuesRenderSection).difference(from: (other.value as! PossibleValuesRenderSection), at: path) |
| 43 | + case (.relationships, .relationships): |
| 44 | + return (value as! RelationshipsRenderSection).difference(from: (other.value as! RelationshipsRenderSection), at: path) |
| 45 | + case (.restBody, .restBody): |
| 46 | + return (value as! RESTBodyRenderSection).difference(from: (other.value as! RESTBodyRenderSection), at: path) |
| 47 | + case (.restEndpoint, .restEndpoint): |
| 48 | + return (value as! RESTEndpointRenderSection).difference(from: (other.value as! RESTEndpointRenderSection), at: path) |
| 49 | + case (.restParameters, .restParameters): |
| 50 | + return (value as! RESTParametersRenderSection).difference(from: (other.value as! RESTParametersRenderSection), at: path) |
| 51 | + case (.restResponses, .restResponses): |
| 52 | + return (value as! RESTResponseRenderSection).difference(from: (other.value as! RESTResponseRenderSection), at: path) |
| 53 | + case (.sampleDownload, .sampleDownload): |
| 54 | + return (value as! SampleDownloadSection).difference(from: (other.value as! SampleDownloadSection), at: path) |
| 55 | + case (.taskGroup, .taskGroup): |
| 56 | + return (value as! TaskGroupRenderSection).difference(from: (other.value as! TaskGroupRenderSection), at: path) |
| 57 | + |
| 58 | + // MARK: Tutorial Sections |
| 59 | + |
| 60 | + case (.intro, .intro), (.hero, .hero): |
| 61 | + return (value as! IntroRenderSection).difference(from: (other.value as! IntroRenderSection), at: path) |
| 62 | + case (.assessments, .assessments): |
| 63 | + return (value as! TutorialAssessmentsRenderSection).difference(from: (other.value as! TutorialAssessmentsRenderSection), at: path) |
| 64 | + case (.tasks, .tasks): |
| 65 | + return (value as! TutorialSectionsRenderSection).difference(from: (other.value as! TutorialSectionsRenderSection), at: path) |
| 66 | + |
| 67 | + // MARK: Tutorial Article Sections |
| 68 | + |
| 69 | + case (.articleBody, .articleBody): |
| 70 | + return (value as! TutorialArticleSection).difference(from: (other.value as! TutorialArticleSection), at: path) |
| 71 | + |
| 72 | + // MARK: Tutorials Overview Sections |
| 73 | + |
| 74 | + case (.callToAction, .callToAction): |
| 75 | + return (value as! CallToActionSection).difference(from: (other.value as! CallToActionSection), at: path) |
| 76 | + case (.contentAndMediaGroup, .contentAndMediaGroup): |
| 77 | + return (value as! ContentAndMediaGroupSection).difference(from: (other.value as! ContentAndMediaGroupSection), at: path) |
| 78 | + case (.contentAndMedia, .contentAndMedia): |
| 79 | + return (value as! ContentAndMediaSection).difference(from: (other.value as! ContentAndMediaSection), at: path) |
| 80 | + case (.resources, .resources): |
| 81 | + return (value as! ResourcesRenderSection).difference(from: (other.value as! ResourcesRenderSection), at: path) |
| 82 | + case (.volume, .volume): |
| 83 | + return (value as! VolumeRenderSection).difference(from: (other.value as! VolumeRenderSection), at: path) |
| 84 | + |
| 85 | + default: |
| 86 | + assertionFailure("Case diffing \(value) with \(other.value) is not implemented.") |
| 87 | + return [] |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + static func == (lhs: AnyRenderSection, rhs: AnyRenderSection) -> Bool { |
| 92 | + switch (lhs.value.kind, rhs.value.kind) { |
| 93 | + |
| 94 | + // MARK: Symbol Sections |
| 95 | + |
| 96 | + case (.attributes, .attributes): |
| 97 | + return (lhs.value as! AttributesRenderSection) == (rhs.value as! AttributesRenderSection) |
| 98 | + case (.discussion, .discussion), (.content, .content): |
| 99 | + return (lhs.value as! ContentRenderSection) == (rhs.value as! ContentRenderSection) |
| 100 | + case (.declarations, .declarations): |
| 101 | + return (lhs.value as! DeclarationsRenderSection) == (rhs.value as! DeclarationsRenderSection) |
| 102 | + case (.parameters, .parameters): |
| 103 | + return (lhs.value as! ParametersRenderSection) == (rhs.value as! ParametersRenderSection) |
| 104 | + case (.plistDetails, .plistDetails): |
| 105 | + return (lhs.value as! PlistDetailsRenderSection) == (rhs.value as! PlistDetailsRenderSection) |
| 106 | + case (.possibleValues, .possibleValues): |
| 107 | + return (lhs.value as! PossibleValuesRenderSection) == (rhs.value as! PossibleValuesRenderSection) |
| 108 | + case (.relationships, .relationships): |
| 109 | + return (lhs.value as! RelationshipsRenderSection) == (rhs.value as! RelationshipsRenderSection) |
| 110 | + case (.restBody, .restBody): |
| 111 | + return (lhs.value as! RESTBodyRenderSection) == (rhs.value as! RESTBodyRenderSection) |
| 112 | + case (.restEndpoint, .restEndpoint): |
| 113 | + return (lhs.value as! RESTEndpointRenderSection) == (rhs.value as! RESTEndpointRenderSection) |
| 114 | + case (.restParameters, .restParameters): |
| 115 | + return (lhs.value as! RESTParametersRenderSection) == (rhs.value as! RESTParametersRenderSection) |
| 116 | + case (.restResponses, .restResponses): |
| 117 | + return (lhs.value as! RESTResponseRenderSection) == (rhs.value as! RESTResponseRenderSection) |
| 118 | + case (.sampleDownload, .sampleDownload): |
| 119 | + return (lhs.value as! SampleDownloadSection) == (rhs.value as! SampleDownloadSection) |
| 120 | + case (.taskGroup, .taskGroup): |
| 121 | + return (lhs.value as! TaskGroupRenderSection) == (rhs.value as! TaskGroupRenderSection) |
| 122 | + |
| 123 | + // MARK: Tutorial Sections |
| 124 | + |
| 125 | + case (.intro, .intro), (.hero, .hero): |
| 126 | + return (lhs.value as! IntroRenderSection) == (rhs.value as! IntroRenderSection) |
| 127 | + case (.assessments, .assessments): |
| 128 | + return (lhs.value as! TutorialAssessmentsRenderSection) == (rhs.value as! TutorialAssessmentsRenderSection) |
| 129 | + case (.tasks, .tasks): |
| 130 | + return (lhs.value as! TutorialSectionsRenderSection) == (rhs.value as! TutorialSectionsRenderSection) |
| 131 | + |
| 132 | + // MARK: Tutorial Article Sections |
| 133 | + |
| 134 | + case (.articleBody, .articleBody): |
| 135 | + return (lhs.value as! TutorialArticleSection) == (rhs.value as! TutorialArticleSection) |
| 136 | + |
| 137 | + // MARK: Tutorials Overview Sections |
| 138 | + |
| 139 | + case (.callToAction, .callToAction): |
| 140 | + return (lhs.value as! CallToActionSection) == (rhs.value as! CallToActionSection) |
| 141 | + case (.contentAndMediaGroup, .contentAndMediaGroup): |
| 142 | + return (lhs.value as! ContentAndMediaGroupSection) == (rhs.value as! ContentAndMediaGroupSection) |
| 143 | + case (.contentAndMedia, .contentAndMedia): |
| 144 | + return (lhs.value as! ContentAndMediaSection) == (rhs.value as! ContentAndMediaSection) |
| 145 | + case (.resources, .resources): |
| 146 | + return (lhs.value as! ResourcesRenderSection) == (rhs.value as! ResourcesRenderSection) |
| 147 | + case (.volume, .volume): |
| 148 | + return (lhs.value as! VolumeRenderSection) == (rhs.value as! VolumeRenderSection) |
| 149 | + |
| 150 | + default: |
| 151 | + assertionFailure("Case diffing \(lhs.value) with \(rhs.value) is not implemented.") |
| 152 | + return false |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + func isSimilar(to other: AnyRenderSection) -> Bool { |
| 157 | + return self.value.kind == other.value.kind |
| 158 | + } |
| 159 | +} |
0 commit comments