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

Add a convenience function to decode the Message's json data #9

Merged
merged 3 commits into from
Aug 8, 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 Source/JsonDataDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public struct JsonDataDecoder {
public static var appDecoder: JSONDecoder = JSONDecoder()
}
17 changes: 17 additions & 0 deletions Source/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,20 @@ extension Message {
public let url: String
}
}

extension Message {
public func decodedJsonData<T: Decodable>() -> T? {
guard let data = jsonData.data(using: .utf8) else {
debugLog("Error converting json string to data: \(jsonData)")
return nil
}

do {
let decoder = JsonDataDecoder.appDecoder
return try decoder.decode(T.self, from: data)
} catch {
debugLog("Error decoding json: \(jsonData) -> \(error)")
return nil
}
}
}
4 changes: 4 additions & 0 deletions Strada.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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 /* JsonDataDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = E200E7D02A814D4500E41FA9 /* JsonDataDecoder.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 */; };
Expand Down Expand Up @@ -54,6 +55,7 @@
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 /* JsonDataDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonDataDecoder.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>"; };
Expand Down Expand Up @@ -117,6 +119,7 @@
E20978412A6E9E6B00CDEEE5 /* InternalMessage.swift */,
E2DB15902A7163B0001EE08C /* BridgeDelegate.swift */,
E2DB15922A7282CF001EE08C /* BridgeComponent.swift */,
E200E7D02A814D4500E41FA9 /* JsonDataDecoder.swift */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -259,6 +262,7 @@
files = (
E209784B2A714D4E00CDEEE5 /* String+JSON.swift in Sources */,
C11349A62587EFFB000A6E56 /* ScriptMessageHandler.swift in Sources */,
E200E7D12A814D4500E41FA9 /* JsonDataDecoder.swift in Sources */,
E20978472A7135E700CDEEE5 /* Encodable+Utils.swift in Sources */,
E2DB15912A7163B0001EE08C /* BridgeDelegate.swift in Sources */,
C11349B22587F31E000A6E56 /* JavaScript.swift in Sources */,
Expand Down
50 changes: 50 additions & 0 deletions Tests/MessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,54 @@ class MessageTests: XCTestCase {
XCTAssertEqual(newMessage.metadata, metadata)
XCTAssertEqual(newMessage.jsonData, jsonData)
}

func test_decodingWithDefaultDecoder() {
let metadata = Message.Metadata(url: "https://37signals.com")
let jsonData = """
{"title":"Page-title","subtitle":"Page-subtitle", "eventName": "test"}
"""
let message = Message(id: "1",
component: "page",
event: "connect",
metadata: metadata,
jsonData: jsonData)

let dataObject = TestEvent(title: "Page-title",
subtitle: "Page-subtitle",
eventName: "test")

let event: TestEvent? = message.decodedJsonData()

XCTAssertEqual(dataObject, event)
}

func test_decodingWithCustomDecoder() {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
JsonDataDecoder.appDecoder = decoder

let metadata = Message.Metadata(url: "https://37signals.com")
let jsonData = """
{"title":"Page-title","subtitle":"Page-subtitle", "event_name": "test"}
"""
let message = Message(id: "1",
component: "page",
event: "connect",
metadata: metadata,
jsonData: jsonData)

let dataObject = TestEvent(title: "Page-title",
subtitle: "Page-subtitle",
eventName: "test")

let event: TestEvent? = message.decodedJsonData()

XCTAssertEqual(dataObject, event)
}
}

private struct TestEvent: Codable, Equatable {
svara marked this conversation as resolved.
Show resolved Hide resolved
let title: String
let subtitle: String
let eventName: String
}