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

refactor: direct method to init DatafileContent from JSONString #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions Sources/FeaturevisorSDK/Instance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions Sources/FeaturevisorTypes/Extensions/DatafileContent+Init.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down