Skip to content

Commit

Permalink
Move json encoder/decoder configurations to a config inside a Strada …
Browse files Browse the repository at this point in the history
…namespace.
  • Loading branch information
svara committed Aug 10, 2023
1 parent a76d6a5 commit 340a2fe
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Source/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -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)")
Expand Down
5 changes: 5 additions & 0 deletions Source/Strada.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public enum Strada {
static var config: StradaConfig = StradaConfig()
}
13 changes: 13 additions & 0 deletions Source/StradaConfig.swift
Original file line number Diff line number Diff line change
@@ -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()
}
21 changes: 0 additions & 21 deletions Source/StradaJSONCoding.swift

This file was deleted.

12 changes: 8 additions & 4 deletions Strada.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -59,13 +60,14 @@
C1EB05252588133D00933244 /* MessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTests.swift; sourceTree = "<group>"; };
C1EB052D2588201600933244 /* BridgeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeTests.swift; sourceTree = "<group>"; };
CBAFC52826F9863900C6662E /* PathLoaderXcode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PathLoaderXcode.swift; sourceTree = "<group>"; };
E200E7D02A814D4500E41FA9 /* StradaJSONCoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StradaJSONCoding.swift; sourceTree = "<group>"; };
E20978412A6E9E6B00CDEEE5 /* InternalMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternalMessage.swift; sourceTree = "<group>"; };
E20978432A6EAF3600CDEEE5 /* InternalMessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternalMessageTests.swift; sourceTree = "<group>"; };
E20978462A7135E700CDEEE5 /* Encodable+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Encodable+Utils.swift"; sourceTree = "<group>"; };
E20978482A71366B00CDEEE5 /* Data+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Utils.swift"; sourceTree = "<group>"; };
E209784A2A714D4E00CDEEE5 /* String+JSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+JSON.swift"; sourceTree = "<group>"; };
E209784C2A714F1900CDEEE5 /* Dictionary+JSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+JSON.swift"; sourceTree = "<group>"; };
E22CBEFE2A84D7060024EFB8 /* StradaConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StradaConfig.swift; sourceTree = "<group>"; };
E22CBF002A84DC380024EFB8 /* Strada.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Strada.swift; sourceTree = "<group>"; };
E2DB15902A7163B0001EE08C /* BridgeDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeDelegate.swift; sourceTree = "<group>"; };
E2DB15922A7282CF001EE08C /* BridgeComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeComponent.swift; sourceTree = "<group>"; };
E2DB15942A72B0A8001EE08C /* BridgeDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BridgeDelegateTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
8 changes: 4 additions & 4 deletions Tests/MessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down Expand Up @@ -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"}
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion Tests/Spies/BridgeSpy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 340a2fe

Please sign in to comment.