Skip to content

Commit

Permalink
feat(ios): Add Codable support for CAPPluginCall and JSValueContainer (
Browse files Browse the repository at this point in the history
…#7119)

Co-authored-by: Mark Anderson <[email protected]>
  • Loading branch information
Steven0351 and markemer authored Dec 13, 2023
1 parent 0c2ccd9 commit af417e0
Show file tree
Hide file tree
Showing 14 changed files with 1,650 additions and 21 deletions.
161 changes: 160 additions & 1 deletion ios/Capacitor/Capacitor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
A38C3D7B2848BE6F004B3680 /* CapacitorCookieManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A38C3D7A2848BE6F004B3680 /* CapacitorCookieManager.swift */; };
A71289E627F380A500DADDF3 /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = A71289E527F380A500DADDF3 /* Router.swift */; };
A71289EB27F380FD00DADDF3 /* RouterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A71289EA27F380FD00DADDF3 /* RouterTests.swift */; };
A7D8B3522B238A840003FAD6 /* JSValueEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D8B3512B238A840003FAD6 /* JSValueEncoder.swift */; };
A7D8B3632B263B8D0003FAD6 /* NestedCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D8B3622B263B8D0003FAD6 /* NestedCodableTests.swift */; };
A7D8B3642B263B8D0003FAD6 /* Capacitor.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50503EDF1FC08594003606DC /* Capacitor.framework */; };
A7D8B36A2B263B990003FAD6 /* CodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D8B3562B23B2110003FAD6 /* CodableTests.swift */; };
A7D8B36E2B2692300003FAD6 /* SuperCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D8B36D2B2692300003FAD6 /* SuperCodableTests.swift */; };
A7D9312F2B23710300FF59A2 /* JSValueDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D9312E2B23710300FF59A2 /* JSValueDecoder.swift */; };
A7DB03AC29B001E300888AE9 /* CAPBridgedPlugin+getMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7DB03AB29B001E300888AE9 /* CAPBridgedPlugin+getMethod.swift */; };
A7F7EDCD291EC75C0015B73B /* CAPPlugin+LoadInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7F7EDCC291EC75C0015B73B /* CAPPlugin+LoadInstance.swift */; };
A7F7EDD5292BE8520015B73B /* CAPInstancePlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7F7EDD4292BE8520015B73B /* CAPInstancePlugin.swift */; };
Expand Down Expand Up @@ -115,6 +121,13 @@
remoteGlobalIDString = 6296A77A253A2E49005A202A;
remoteInfo = TestsHostApp;
};
A7D8B3652B263B8D0003FAD6 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 50503ED61FC08594003606DC /* Project object */;
proxyType = 1;
remoteGlobalIDString = 50503EDE1FC08594003606DC;
remoteInfo = Capacitor;
};
/* End PBXContainerItemProxy section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -226,6 +239,12 @@
A38C3D7A2848BE6F004B3680 /* CapacitorCookieManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CapacitorCookieManager.swift; sourceTree = "<group>"; };
A71289E527F380A500DADDF3 /* Router.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
A71289EA27F380FD00DADDF3 /* RouterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouterTests.swift; sourceTree = "<group>"; };
A7D8B3512B238A840003FAD6 /* JSValueEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSValueEncoder.swift; sourceTree = "<group>"; };
A7D8B3562B23B2110003FAD6 /* CodableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodableTests.swift; sourceTree = "<group>"; };
A7D8B3602B263B8D0003FAD6 /* CodableTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CodableTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A7D8B3622B263B8D0003FAD6 /* NestedCodableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedCodableTests.swift; sourceTree = "<group>"; };
A7D8B36D2B2692300003FAD6 /* SuperCodableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuperCodableTests.swift; sourceTree = "<group>"; };
A7D9312E2B23710300FF59A2 /* JSValueDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSValueDecoder.swift; sourceTree = "<group>"; };
A7DB03AB29B001E300888AE9 /* CAPBridgedPlugin+getMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CAPBridgedPlugin+getMethod.swift"; sourceTree = "<group>"; };
A7F7EDCC291EC75C0015B73B /* CAPPlugin+LoadInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CAPPlugin+LoadInstance.swift"; sourceTree = "<group>"; };
A7F7EDD4292BE8520015B73B /* CAPInstancePlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CAPInstancePlugin.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -256,6 +275,14 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
A7D8B35D2B263B8D0003FAD6 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
A7D8B3642B263B8D0003FAD6 /* Capacitor.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
Expand All @@ -275,6 +302,7 @@
62959AE12524DA7700A3D7F1 /* Capacitor */,
50503EEC1FC08595003606DC /* CapacitorTests */,
6296A77C253A2E49005A202A /* TestsHostApp */,
A7D8B3612B263B8D0003FAD6 /* CodableTests */,
50503EE01FC08594003606DC /* Products */,
501CBAA51FC0A723009B0D4D /* Frameworks */,
);
Expand All @@ -286,6 +314,7 @@
50503EDF1FC08594003606DC /* Capacitor.framework */,
50503EE81FC08595003606DC /* CapacitorTests.xctest */,
6296A77B253A2E49005A202A /* TestsHostApp.app */,
A7D8B3602B263B8D0003FAD6 /* CodableTests.xctest */,
);
name = Products;
sourceTree = "<group>";
Expand All @@ -312,6 +341,7 @@
62959AE12524DA7700A3D7F1 /* Capacitor */ = {
isa = PBXGroup;
children = (
A7D9312C2B2370EF00FF59A2 /* Codable */,
0F8F33B127DA980A003F49D6 /* PluginConfig.swift */,
62959B0F2524DA7700A3D7F1 /* Capacitor.h */,
62959B132524DA7700A3D7F1 /* CAPPlugin.h */,
Expand Down Expand Up @@ -430,6 +460,25 @@
path = assets;
sourceTree = "<group>";
};
A7D8B3612B263B8D0003FAD6 /* CodableTests */ = {
isa = PBXGroup;
children = (
A7D8B3622B263B8D0003FAD6 /* NestedCodableTests.swift */,
A7D8B3562B23B2110003FAD6 /* CodableTests.swift */,
A7D8B36D2B2692300003FAD6 /* SuperCodableTests.swift */,
);
path = CodableTests;
sourceTree = "<group>";
};
A7D9312C2B2370EF00FF59A2 /* Codable */ = {
isa = PBXGroup;
children = (
A7D9312E2B23710300FF59A2 /* JSValueDecoder.swift */,
A7D8B3512B238A840003FAD6 /* JSValueEncoder.swift */,
);
path = Codable;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -507,13 +556,31 @@
productReference = 6296A77B253A2E49005A202A /* TestsHostApp.app */;
productType = "com.apple.product-type.application";
};
A7D8B35F2B263B8D0003FAD6 /* CodableTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = A7D8B3672B263B8D0003FAD6 /* Build configuration list for PBXNativeTarget "CodableTests" */;
buildPhases = (
A7D8B35C2B263B8D0003FAD6 /* Sources */,
A7D8B35D2B263B8D0003FAD6 /* Frameworks */,
A7D8B35E2B263B8D0003FAD6 /* Resources */,
);
buildRules = (
);
dependencies = (
A7D8B3662B263B8D0003FAD6 /* PBXTargetDependency */,
);
name = CodableTests;
productName = CodableTests;
productReference = A7D8B3602B263B8D0003FAD6 /* CodableTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
50503ED61FC08594003606DC /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1200;
LastSwiftUpdateCheck = 1500;
LastUpgradeCheck = 1240;
ORGANIZATIONNAME = "Drifty Co.";
TargetAttributes = {
Expand All @@ -532,6 +599,10 @@
CreatedOnToolsVersion = 12.0;
ProvisioningStyle = Automatic;
};
A7D8B35F2B263B8D0003FAD6 = {
CreatedOnToolsVersion = 15.0.1;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 50503ED91FC08594003606DC /* Build configuration list for PBXProject "Capacitor" */;
Expand All @@ -556,6 +627,7 @@
50503EDE1FC08594003606DC /* Capacitor */,
50503EE71FC08595003606DC /* CapacitorTests */,
6296A77A253A2E49005A202A /* TestsHostApp */,
A7D8B35F2B263B8D0003FAD6 /* CodableTests */,
);
};
/* End PBXProject section */
Expand Down Expand Up @@ -596,6 +668,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
A7D8B35E2B263B8D0003FAD6 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand All @@ -606,6 +685,7 @@
A38C3D7728484E76004B3680 /* CapacitorCookies.swift in Sources */,
A71289E627F380A500DADDF3 /* Router.swift in Sources */,
A327E6B828DB8B2900CA8B0A /* CapacitorUrlRequest.swift in Sources */,
A7D9312F2B23710300FF59A2 /* JSValueDecoder.swift in Sources */,
62959B362524DA7800A3D7F1 /* CAPBridgeViewController.swift in Sources */,
621ECCB72542045900D3D615 /* CAPBridgedJSTypes.m in Sources */,
62959B402524DA7800A3D7F1 /* TmpViewController.swift in Sources */,
Expand All @@ -617,6 +697,7 @@
621ECCDA254205C400D3D615 /* CapacitorBridge.swift in Sources */,
62959B382524DA7800A3D7F1 /* CAPPluginCall.m in Sources */,
623D690A254C6FDF002D01D1 /* CAPInstanceDescriptor.m in Sources */,
A7D8B3522B238A840003FAD6 /* JSValueEncoder.swift in Sources */,
A7F7EDD5292BE8520015B73B /* CAPInstancePlugin.swift in Sources */,
A38C3D7B2848BE6F004B3680 /* CapacitorCookieManager.swift in Sources */,
62959B1B2524DA7800A3D7F1 /* CAPFile.swift in Sources */,
Expand Down Expand Up @@ -682,6 +763,16 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
A7D8B35C2B263B8D0003FAD6 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A7D8B3632B263B8D0003FAD6 /* NestedCodableTests.swift in Sources */,
A7D8B36A2B263B990003FAD6 /* CodableTests.swift in Sources */,
A7D8B36E2B2692300003FAD6 /* SuperCodableTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
Expand All @@ -695,6 +786,11 @@
target = 6296A77A253A2E49005A202A /* TestsHostApp */;
targetProxy = 6296A796253A2EAE005A202A /* PBXContainerItemProxy */;
};
A7D8B3662B263B8D0003FAD6 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 50503EDE1FC08594003606DC /* Capacitor */;
targetProxy = A7D8B3652B263B8D0003FAD6 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
Expand Down Expand Up @@ -899,6 +995,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
BUILD_LIBRARY_FOR_DISTRIBUTION = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
Expand All @@ -918,6 +1015,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
BUILD_LIBRARY_FOR_DISTRIBUTION = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
Expand Down Expand Up @@ -977,6 +1075,58 @@
};
name = Release;
};
A7D8B3682B263B8D0003FAD6 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 1.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.capacitorjs.ios.CodableTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
A7D8B3692B263B8D0003FAD6 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 1.0;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.capacitorjs.ios.CodableTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
Expand Down Expand Up @@ -1016,6 +1166,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
A7D8B3672B263B8D0003FAD6 /* Build configuration list for PBXNativeTarget "CodableTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
A7D8B3682B263B8D0003FAD6 /* Debug */,
A7D8B3692B263B8D0003FAD6 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 50503ED61FC08594003606DC /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
ReferencedContainer = "container:Capacitor.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A7D8B35F2B263B8D0003FAD6"
BuildableName = "CodableTests.xctest"
BlueprintName = "CodableTests"
ReferencedContainer = "container:Capacitor.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
26 changes: 26 additions & 0 deletions ios/Capacitor/Capacitor/CAPPluginCall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,29 @@ extension CAPPluginCall: JSValueContainer {
errorHandler(CAPPluginCallError(message: message, code: "UNAVAILABLE", error: nil, data: [:]))
}
}

// MARK: Codable Support
public extension CAPPluginCall {
/// Encodes the given value to a ``JSObject`` and resolves the call.
/// - Parameters:
/// - data: The value to encode.
/// - encoder: The encoder to use. Defaults to `JSValueEncoder()`.
/// - Throws: If the value cannot be encoded.
///
/// The data paramater _must_ be encodable as a
/// ``JSObject``, otherwise this method will throw.
func resolve<T: Encodable>(with data: T, encoder: JSValueEncoder = JSValueEncoder()) throws {
let encoded = try encoder.encodeJSObject(data)
resolve(encoded)
}

/// Decodes the options to the given type.
/// - Parameters:
/// - type: The type to decode to.
/// - decoder: The decoder to use. Defaults to `JSValueDecoder()`.
/// - Throws: If the options cannot be decoded.
/// - Returns: The decoded value.
func decode<T: Decodable>(_ type: T.Type, decoder: JSValueDecoder = JSValueDecoder()) throws -> T {
try decoder.decode(type, from: options as? JSObject ?? [:])
}
}
Loading

0 comments on commit af417e0

Please sign in to comment.