From 340a2feed90961157ef0c62244ddc293d916f0dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=A0vara?= Date: Thu, 10 Aug 2023 11:01:01 +0200 Subject: [PATCH] Move json encoder/decoder configurations to a config inside a Strada namespace. --- Source/Message.swift | 4 ++-- Source/Strada.swift | 5 +++++ Source/StradaConfig.swift | 13 +++++++++++++ Source/StradaJSONCoding.swift | 21 --------------------- Strada.xcodeproj/project.pbxproj | 12 ++++++++---- Tests/MessageTests.swift | 8 ++++---- Tests/Spies/BridgeSpy.swift | 2 +- 7 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 Source/Strada.swift create mode 100644 Source/StradaConfig.swift delete mode 100644 Source/StradaJSONCoding.swift diff --git a/Source/Message.swift b/Source/Message.swift index de76091..173d490 100644 --- a/Source/Message.swift +++ b/Source/Message.swift @@ -45,7 +45,7 @@ extension Message { data: T) -> Message { let updatedData: String? do { - let jsonData = try StradaJSONEncoder.appEncoder.encode(data) + let jsonData = try Strada.config.jsonEncoder.encode(data) updatedData = String(data: jsonData, encoding: .utf8) } catch { debugLog("Error encoding codable object: \(data) -> \(error)") @@ -64,7 +64,7 @@ extension Message { } do { - let decoder = StradaJSONDecoder.appDecoder + let decoder = Strada.config.jsonDecoder return try decoder.decode(T.self, from: data) } catch { debugLog("Error decoding json: \(jsonData) -> \(error)") diff --git a/Source/Strada.swift b/Source/Strada.swift new file mode 100644 index 0000000..602ad9e --- /dev/null +++ b/Source/Strada.swift @@ -0,0 +1,5 @@ +import Foundation + +public enum Strada { + static var config: StradaConfig = StradaConfig() +} diff --git a/Source/StradaConfig.swift b/Source/StradaConfig.swift new file mode 100644 index 0000000..441e4ce --- /dev/null +++ b/Source/StradaConfig.swift @@ -0,0 +1,13 @@ +import Foundation + +public struct StradaConfig { + /// Allows users to set a custom JSON encoder for the library. + /// The custom encoder can be useful when you need to apply specific + /// encoding strategies. + public var jsonEncoder: JSONEncoder = JSONEncoder() + + /// Allows users to set a custom JSON decoder for the library. + /// The custom decoder can be useful when you need to apply specific + /// decoding strategies. + public var jsonDecoder: JSONDecoder = JSONDecoder() +} diff --git a/Source/StradaJSONCoding.swift b/Source/StradaJSONCoding.swift deleted file mode 100644 index 3448d1e..0000000 --- a/Source/StradaJSONCoding.swift +++ /dev/null @@ -1,21 +0,0 @@ -import Foundation - -/// A utility for managing custom JSON encoders for the library. -/// -/// The `StradaJSONEncoder` enum provides a static property `appEncoder` -/// that allows users to set a custom JSON encoder for the library. -/// The custom encoder can be useful when you need to apply specific -/// encoding strategies. -public enum StradaJSONEncoder { - public static var appEncoder: JSONEncoder = JSONEncoder() -} - -/// A utility for managing custom JSON decoders for the library. -/// -/// The `StradaJSONDecoder` enum provides a static property `appDecoder` -/// that allows users to set a custom JSON decoder for the library. -/// The custom decoder can be useful when you need to apply specific -/// decoding strategies. -public enum StradaJSONDecoder { - public static var appDecoder: JSONDecoder = JSONDecoder() -} diff --git a/Strada.xcodeproj/project.pbxproj b/Strada.xcodeproj/project.pbxproj index 5bfe967..22628ed 100644 --- a/Strada.xcodeproj/project.pbxproj +++ b/Strada.xcodeproj/project.pbxproj @@ -18,13 +18,14 @@ C1EB05262588133D00933244 /* MessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1EB05252588133D00933244 /* MessageTests.swift */; }; C1EB052E2588201600933244 /* BridgeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1EB052D2588201600933244 /* BridgeTests.swift */; }; CBAFC52926F9863900C6662E /* PathLoaderXcode.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBAFC52826F9863900C6662E /* PathLoaderXcode.swift */; }; - E200E7D12A814D4500E41FA9 /* StradaJSONCoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = E200E7D02A814D4500E41FA9 /* StradaJSONCoding.swift */; }; E20978422A6E9E6B00CDEEE5 /* InternalMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20978412A6E9E6B00CDEEE5 /* InternalMessage.swift */; }; E20978442A6EAF3600CDEEE5 /* InternalMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20978432A6EAF3600CDEEE5 /* InternalMessageTests.swift */; }; E20978472A7135E700CDEEE5 /* Encodable+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20978462A7135E700CDEEE5 /* Encodable+Utils.swift */; }; E20978492A71366B00CDEEE5 /* Data+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20978482A71366B00CDEEE5 /* Data+Utils.swift */; }; E209784B2A714D4E00CDEEE5 /* String+JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = E209784A2A714D4E00CDEEE5 /* String+JSON.swift */; }; E209784D2A714F1900CDEEE5 /* Dictionary+JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = E209784C2A714F1900CDEEE5 /* Dictionary+JSON.swift */; }; + E22CBEFF2A84D7060024EFB8 /* StradaConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22CBEFE2A84D7060024EFB8 /* StradaConfig.swift */; }; + E22CBF012A84DC380024EFB8 /* Strada.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22CBF002A84DC380024EFB8 /* Strada.swift */; }; E2DB15912A7163B0001EE08C /* BridgeDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2DB15902A7163B0001EE08C /* BridgeDelegate.swift */; }; E2DB15932A7282CF001EE08C /* BridgeComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2DB15922A7282CF001EE08C /* BridgeComponent.swift */; }; E2DB15952A72B0A8001EE08C /* BridgeDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2DB15942A72B0A8001EE08C /* BridgeDelegateTests.swift */; }; @@ -59,13 +60,14 @@ C1EB05252588133D00933244 /* MessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTests.swift; sourceTree = ""; }; C1EB052D2588201600933244 /* BridgeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeTests.swift; sourceTree = ""; }; CBAFC52826F9863900C6662E /* PathLoaderXcode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PathLoaderXcode.swift; sourceTree = ""; }; - E200E7D02A814D4500E41FA9 /* StradaJSONCoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StradaJSONCoding.swift; sourceTree = ""; }; E20978412A6E9E6B00CDEEE5 /* InternalMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternalMessage.swift; sourceTree = ""; }; E20978432A6EAF3600CDEEE5 /* InternalMessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternalMessageTests.swift; sourceTree = ""; }; E20978462A7135E700CDEEE5 /* Encodable+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Encodable+Utils.swift"; sourceTree = ""; }; E20978482A71366B00CDEEE5 /* Data+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Utils.swift"; sourceTree = ""; }; E209784A2A714D4E00CDEEE5 /* String+JSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+JSON.swift"; sourceTree = ""; }; E209784C2A714F1900CDEEE5 /* Dictionary+JSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+JSON.swift"; sourceTree = ""; }; + E22CBEFE2A84D7060024EFB8 /* StradaConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StradaConfig.swift; sourceTree = ""; }; + E22CBF002A84DC380024EFB8 /* Strada.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Strada.swift; sourceTree = ""; }; E2DB15902A7163B0001EE08C /* BridgeDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeDelegate.swift; sourceTree = ""; }; E2DB15922A7282CF001EE08C /* BridgeComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeComponent.swift; sourceTree = ""; }; E2DB15942A72B0A8001EE08C /* BridgeDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeDelegateTests.swift; sourceTree = ""; }; @@ -127,7 +129,8 @@ E20978412A6E9E6B00CDEEE5 /* InternalMessage.swift */, E2DB15902A7163B0001EE08C /* BridgeDelegate.swift */, E2DB15922A7282CF001EE08C /* BridgeComponent.swift */, - E200E7D02A814D4500E41FA9 /* StradaJSONCoding.swift */, + E22CBEFE2A84D7060024EFB8 /* StradaConfig.swift */, + E22CBF002A84DC380024EFB8 /* Strada.swift */, ); path = Source; sourceTree = ""; @@ -282,12 +285,13 @@ files = ( E209784B2A714D4E00CDEEE5 /* String+JSON.swift in Sources */, C11349A62587EFFB000A6E56 /* ScriptMessageHandler.swift in Sources */, - E200E7D12A814D4500E41FA9 /* StradaJSONCoding.swift in Sources */, + E22CBEFF2A84D7060024EFB8 /* StradaConfig.swift in Sources */, E20978472A7135E700CDEEE5 /* Encodable+Utils.swift in Sources */, E2DB15912A7163B0001EE08C /* BridgeDelegate.swift in Sources */, C11349B22587F31E000A6E56 /* JavaScript.swift in Sources */, 9274F20222299715003E85F4 /* Bridge.swift in Sources */, E2DB15932A7282CF001EE08C /* BridgeComponent.swift in Sources */, + E22CBF012A84DC380024EFB8 /* Strada.swift in Sources */, E20978492A71366B00CDEEE5 /* Data+Utils.swift in Sources */, 9274F20422299738003E85F4 /* Message.swift in Sources */, E20978422A6E9E6B00CDEEE5 /* InternalMessage.swift in Sources */, diff --git a/Tests/MessageTests.swift b/Tests/MessageTests.swift index 5d8f959..54fb786 100644 --- a/Tests/MessageTests.swift +++ b/Tests/MessageTests.swift @@ -6,8 +6,8 @@ class MessageTests: XCTestCase { private let metadata = Message.Metadata(url: "https://37signals.com") override func setUp() async throws { - StradaJSONEncoder.appEncoder = JSONEncoder() - StradaJSONDecoder.appDecoder = JSONDecoder() + Strada.config.jsonEncoder = JSONEncoder() + Strada.config.jsonDecoder = JSONDecoder() } // MARK: replacing(event:, jsonData:) @@ -162,7 +162,7 @@ class MessageTests: XCTestCase { func test_decodingWithCustomDecoder() { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase - StradaJSONDecoder.appDecoder = decoder + Strada.config.jsonDecoder = decoder let jsonData = """ {"title":"Page-title","subtitle":"Page-subtitle", "action_name": "go"} @@ -187,7 +187,7 @@ class MessageTests: XCTestCase { func test_encodingWithCustomEncoder() { let encoder = JSONEncoder() encoder.keyEncodingStrategy = .convertToSnakeCase - StradaJSONEncoder.appEncoder = encoder + Strada.config.jsonEncoder = encoder let messageData = MessageData(title: "Page-title", subtitle: "Page-subtitle", diff --git a/Tests/Spies/BridgeSpy.swift b/Tests/Spies/BridgeSpy.swift index 59956a0..9771e34 100644 --- a/Tests/Spies/BridgeSpy.swift +++ b/Tests/Spies/BridgeSpy.swift @@ -3,7 +3,7 @@ import WebKit @testable import Strada final class BridgeSpy: Bridgable { - var delegate: Strada.BridgeDelegate? = nil + var delegate: BridgeDelegate? = nil var webView: WKWebView? = nil var registerComponentWasCalled = false