Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] genericize wrapper data decoding beyond metaData #122

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ios/packages/core/Sources/Types/Assets/Wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ extension AssetData where Self: Equatable {
}
}

public struct DefaultAdditionalData: Decodable, Equatable {
/// MetaData associated with the asset in this wrapper
public var metaData: MetaData?
}

/**
MetaData associated with an asset
*/
Expand Down
23 changes: 13 additions & 10 deletions ios/packages/swiftui/Sources/types/assets/WrappedAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@

This wrapper decodes either to provide a consistent access mechanism
*/
public typealias WrappedAsset = GenericWrappedAsset<MetaData>
public typealias WrappedAsset = GenericWrappedAsset<DefaultAdditionalData>

public struct GenericWrappedAsset<MetaData>: Decodable, AssetContainer where MetaData: Decodable&Equatable {
public struct GenericWrappedAsset<AdditionalData>: Decodable, AssetContainer where AdditionalData: Decodable&Equatable {
/// The keys used to decode the wrapper
public enum CodingKeys: String, CodingKey {
/// Key to decode asset in a wrapper
case asset
/// Key to decode metadata in a wrapper
case metaData
}

/// The underlying asset if it decoded
public var asset: SwiftUIAsset?

/// MetaData that is associated with the adjacent asset
public var metaData: MetaData?
/// Additional data that is associated with the adjacent asset
public var additionalData: AdditionalData?

/**
Constructs an AssetWrapper, this is used as a fallback when decoding fails
Expand All @@ -54,16 +52,21 @@ public struct GenericWrappedAsset<MetaData>: Decodable, AssetContainer where Met
*/
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.asset = try container.decode(RegistryDecodeShim<SwiftUIAsset>.self, forKey: .asset).asset
self.metaData = try container.decodeIfPresent(MetaData.self, forKey: .metaData)
self.asset = try container.decodeIfPresent(RegistryDecodeShim<SwiftUIAsset>.self, forKey: .asset)?.asset
self.additionalData = try decoder.singleValueContainer().decode(AdditionalData.self)
}
}

extension GenericWrappedAsset where AdditionalData == DefaultAdditionalData {
/// MetaData associated with the sibling key `asset`
public var metaData: MetaData? { additionalData?.metaData }
}

// MARK: - Equatable conformance

extension GenericWrappedAsset: Equatable where MetaData: Equatable {
extension GenericWrappedAsset: Equatable where AdditionalData: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
let isMetaDataSame = lhs.metaData == rhs.metaData
let isMetaDataSame = lhs.additionalData == rhs.additionalData
let areBothAssetsNil = lhs.asset == nil && rhs.asset == nil
var areAssetsEqual: Bool { (lhs.asset?.valueData).flatMap { rhs.asset?.valueData.isEqual($0) } ?? false }
return isMetaDataSame && (areBothAssetsNil || areAssetsEqual)
Expand Down
39 changes: 39 additions & 0 deletions ios/packages/swiftui/Tests/SwiftUIRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,45 @@ class SwiftUIRegistryTests: XCTestCase {
}
}

func testDecodeWrappedAssetWithAdditionalData() {
struct TestAddedData: Decodable, Equatable {
var extra: String
}

struct TestData: AssetData {
var id: String
var type: String

var nested: GenericWrappedAsset<TestAddedData>?
}

class TestNestedAsset: UncontrolledAsset<TestData> {
override var view: AnyView { AnyView(EmptyView()) }
}
let val = context
.evaluateScript("({asset: {id: 'someId', type: 'nested', nested: {asset: {id: 'someOtherId', type: 'text', value: ''}, extra: 'value'}}})")

let partialMatch = PartialMatchFingerprintPlugin()
partialMatch.context = context
partialMatch.setMapping(assetId: "someId", index: 1)
partialMatch.setMapping(assetId: "someOtherId", index: 0)
let registry = SwiftUIRegistry(logger: TapableLogger())
registry.register("text", asset: TextAsset.self)
registry.register("nested", asset: TestNestedAsset.self)
registry.partialMatchRegistry = partialMatch

guard let object = val else { return XCTFail("object should not be nil") }

do {
let decoded = try registry.decodeWrapper(object)
XCTAssertEqual(decoded.asset?.id, "someId")
guard let asset = decoded.asset as? TestNestedAsset else { return XCTFail("Decoded asset was not TestNestedAsset") }
XCTAssertEqual(asset.model.data.nested?.additionalData?.extra, "value")
} catch {
XCTFail("unable to decode wrapper")
}
}

func testWrappedAssetEquivalence() {
let asset1 = context.evaluateScript("({asset: {id: 'someId', type: 'text', value: 'someValue'}})")
let asset2 = context.evaluateScript("({asset: {id: 'someId', type: 'text', value: 'someOtherValue'}})")
Expand Down
2 changes: 1 addition & 1 deletion xcode/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ SPEC CHECKSUMS:
SwiftLint: 4fa9579c63416865179bc416f0a92d55f009600d
ViewInspector: 53313c757eddc5c4842bc7943a66821a68d02d3e

PODFILE CHECKSUM: 6cb074ce93caffedf67f4b2c3a3f608523d73daa
PODFILE CHECKSUM: f03f1fe80c0ccb51f66a36486ef366a9e0030994

COCOAPODS: 1.11.3