diff --git a/Sources/FeaturevisorSDK/Instance.swift b/Sources/FeaturevisorSDK/Instance.swift index 4518dea..08ec8b8 100644 --- a/Sources/FeaturevisorSDK/Instance.swift +++ b/Sources/FeaturevisorSDK/Instance.swift @@ -258,14 +258,17 @@ public class FeaturevisorInstance { } func setDatafile(_ datafileJSON: String) { - - guard let data = datafileJSON.data(using: .utf8) else { - logger.error("could not get datafile as data representation") - return - } - do { - let datafileContent = try JSONDecoder().decode(DatafileContent.self, from: data) + let datafileContent = try DatafileContent.from(string: datafileJSON) + + guard let datafileContent else { + logger.error( + "could not get datafile as data representation", + ["jsonDatafile": datafileJSON] + ) + return + } + datafileReader = DatafileReader(datafileContent: datafileContent) } catch { diff --git a/Sources/FeaturevisorTypes/Extensions/DatafileContent+Init.swift b/Sources/FeaturevisorTypes/Extensions/DatafileContent+Init.swift new file mode 100644 index 0000000..bd74a8e --- /dev/null +++ b/Sources/FeaturevisorTypes/Extensions/DatafileContent+Init.swift @@ -0,0 +1,13 @@ +import Foundation + +extension DatafileContent { + + public static func from(string datafileJSONString: String) throws -> DatafileContent? { + + guard let data = datafileJSONString.data(using: .utf8) else { + return nil + } + + return try JSONDecoder().decode(DatafileContent.self, from: data) + } +} diff --git a/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift b/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift index 66b62ae..c1d8aab 100644 --- a/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift +++ b/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift @@ -15,7 +15,7 @@ final class FeaturevisorTypesTests: XCTestCase { // WHEN let json = try Data(contentsOf: path) - let result = try JSONDecoder().decode(DatafileContent.self, from: json) + let result = try DatafileContent.from(string: String(decoding: json, as: UTF8.self))! // THEN XCTAssertEqual(result.revision, "0.0.13")