forked from venmo/QuizTrain
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
388 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// BulkCases.swift | ||
// QuizTrain | ||
// | ||
// Created by Neha Srivastava on 9/26/22. | ||
// | ||
|
||
public struct BulkCases { | ||
public let offset: Int | ||
public let limit: Int | ||
public let size: Int | ||
public let cases: [Case] | ||
public let _links: Links | ||
} | ||
|
||
// MARK: - JSON Keys | ||
extension BulkCases { | ||
|
||
enum JSONKeys: JSONKey { | ||
case offset = "offset" | ||
case limit = "limit" | ||
case size = "size" | ||
case _links = "_links" | ||
case cases = "cases" | ||
case next = "next" | ||
case prev = "prev" | ||
} | ||
|
||
} | ||
|
||
// MARK: - Serialization | ||
extension BulkCases: JSONDeserializable { | ||
|
||
init?(json: JSONDictionary) { | ||
|
||
let offset = json[JSONKeys.offset.rawValue] as! Int | ||
let limit = json[JSONKeys.limit.rawValue] as! Int | ||
let size = json[JSONKeys.size.rawValue] as! Int | ||
let linksDict = json[JSONKeys._links.rawValue] as! JSONDictionary | ||
let _links = Links(next: linksDict[JSONKeys.next.rawValue] as? String, prev: linksDict[JSONKeys.prev.rawValue] as? String) | ||
let casesDict = json[JSONKeys.cases.rawValue] as! [JSONDictionary] | ||
var cases = [Case]() | ||
for caseDict in casesDict { | ||
let caseResult = Case.init(json: caseDict) | ||
cases.append(caseResult!) | ||
} | ||
|
||
self.init(offset: offset, limit: limit, size: size, cases: cases, _links: _links) | ||
} | ||
} | ||
|
||
extension BulkCases: JSONSerializable { | ||
|
||
func serialized() -> JSONDictionary { | ||
return [JSONKeys.offset.rawValue: offset, | ||
JSONKeys.limit.rawValue: limit, | ||
JSONKeys.size.rawValue: size, | ||
JSONKeys._links.rawValue: _links, | ||
JSONKeys.cases.rawValue: cases] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// BulkSections.swift | ||
// QuizTrain | ||
// | ||
// Created by Neha Srivastava on 9/26/22. | ||
// | ||
|
||
public struct BulkSections { | ||
public let offset: Int | ||
public let limit: Int | ||
public let size: Int | ||
public let sections: [Section] | ||
public let _links: Links | ||
} | ||
|
||
// MARK: - JSON Keys | ||
extension BulkSections { | ||
|
||
enum JSONKeys: JSONKey { | ||
case offset = "offset" | ||
case limit = "limit" | ||
case size = "size" | ||
case _links = "_links" | ||
case sections = "sections" | ||
case next = "next" | ||
case prev = "prev" | ||
} | ||
|
||
} | ||
|
||
// MARK: - Serialization | ||
extension BulkSections: JSONDeserializable { | ||
|
||
init?(json: JSONDictionary) { | ||
|
||
let offset = json[JSONKeys.offset.rawValue] as! Int | ||
let limit = json[JSONKeys.limit.rawValue] as! Int | ||
let size = json[JSONKeys.size.rawValue] as! Int | ||
let linksDict = json[JSONKeys._links.rawValue] as! JSONDictionary | ||
let _links = Links(next: linksDict[JSONKeys.next.rawValue] as? String, prev: linksDict[JSONKeys.prev.rawValue] as? String) | ||
let sectionsDict = json[JSONKeys.sections.rawValue] as! [JSONDictionary] | ||
var sections = [Section]() | ||
for sectionDict in sectionsDict { | ||
if let section = Section.init(json: sectionDict) { | ||
sections.append(section) | ||
} | ||
} | ||
|
||
self.init(offset: offset, limit: limit, size: size, sections: sections, _links: _links) | ||
} | ||
|
||
} | ||
|
||
extension BulkSections: JSONSerializable { | ||
|
||
func serialized() -> JSONDictionary { | ||
return [JSONKeys.offset.rawValue: offset, | ||
JSONKeys.limit.rawValue: limit, | ||
JSONKeys.size.rawValue: size, | ||
JSONKeys._links.rawValue: _links, | ||
JSONKeys.sections.rawValue: sections] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// BulkTests.swift | ||
// QuizTrain | ||
// | ||
// Created by Neha Srivastava on 9/26/22. | ||
// | ||
|
||
public struct BulkTests { | ||
public let offset: Int | ||
public let limit: Int | ||
public let size: Int | ||
public let tests: [Test] | ||
public let _links: Links | ||
} | ||
|
||
// MARK: - JSON Keys | ||
extension BulkTests { | ||
|
||
enum JSONKeys: JSONKey { | ||
case offset = "offset" | ||
case limit = "limit" | ||
case size = "size" | ||
case _links = "_links" | ||
case tests = "tests" | ||
case next = "next" | ||
case prev = "prev" | ||
} | ||
|
||
} | ||
|
||
// MARK: - Serialization | ||
extension BulkTests: JSONDeserializable { | ||
|
||
init?(json: JSONDictionary) { | ||
|
||
let offset = json[JSONKeys.offset.rawValue] as! Int | ||
let limit = json[JSONKeys.limit.rawValue] as! Int | ||
let size = json[JSONKeys.size.rawValue] as! Int | ||
let linksDict = json[JSONKeys._links.rawValue] as! JSONDictionary | ||
let _links = Links(next: linksDict[JSONKeys.next.rawValue] as? String, prev: linksDict[JSONKeys.prev.rawValue] as? String) | ||
let testsDict = json[JSONKeys.tests.rawValue] as! [JSONDictionary] | ||
let tests = testsDict.map({ Test.init(json: $0)! }) | ||
|
||
self.init(offset: offset, limit: limit, size: size, tests: tests, _links: _links) | ||
} | ||
|
||
} | ||
|
||
extension BulkTests: JSONSerializable { | ||
|
||
func serialized() -> JSONDictionary { | ||
return [JSONKeys.offset.rawValue: offset, | ||
JSONKeys.limit.rawValue: limit, | ||
JSONKeys.size.rawValue: size, | ||
JSONKeys._links.rawValue: _links, | ||
JSONKeys.tests.rawValue: tests] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// | ||
// QuizTrainData.swift | ||
// QuizTrain | ||
// | ||
// Created by Neha Srivastava on 9/26/22. | ||
// | ||
|
||
/// Data structure that contains the whole data from a QuizTrain project. It can serialize and write/read data from file all at once | ||
public struct QuizTrainData { | ||
public let project: QuizTrain.Project | ||
public let suites: [QuizTrain.Suite] | ||
public let sections: [QuizTrain.Section] | ||
public let cases: [QuizTrain.Case] | ||
public let statuses: [QuizTrain.Status] | ||
public let users: [QuizTrain.User] | ||
public let currentUser: QuizTrain.User | ||
|
||
public init(project: QuizTrain.Project, suites: [QuizTrain.Suite], sections: [QuizTrain.Section], cases: [QuizTrain.Case], statuses: [QuizTrain.Status], users: [QuizTrain.User], currentUser: QuizTrain.User) { | ||
self.project = project | ||
self.suites = suites | ||
self.sections = sections | ||
self.cases = cases | ||
self.statuses = statuses | ||
self.users = users | ||
self.currentUser = currentUser | ||
} | ||
} | ||
|
||
// MARK: - JSON Keys | ||
extension QuizTrainData { | ||
|
||
enum JSONKeys: JSONKey { | ||
case project | ||
case suites | ||
case sections | ||
case cases | ||
case statuses | ||
case users | ||
case currentUser | ||
} | ||
|
||
} | ||
|
||
// MARK: - Serialization | ||
extension QuizTrainData: JSONDeserializable { | ||
|
||
init?(json: JSONDictionary) { | ||
|
||
guard let projectJson = json[JSONKeys.project.rawValue] as? JSONDictionary, | ||
let currentUserJson = json[JSONKeys.currentUser.rawValue] as? JSONDictionary, | ||
let usersJson = json[JSONKeys.users.rawValue] as? [JSONDictionary], | ||
let suitesJson = json[JSONKeys.suites.rawValue] as? [JSONDictionary], | ||
let sectionsJson = json[JSONKeys.sections.rawValue] as? [JSONDictionary], | ||
let casesJson = json[JSONKeys.cases.rawValue] as? [JSONDictionary], | ||
let statusesJson = json[JSONKeys.statuses.rawValue] as? [JSONDictionary] | ||
else { | ||
return nil | ||
} | ||
|
||
let project = QuizTrain.Project.init(json: projectJson)! | ||
let currentUser = QuizTrain.User.init(json: currentUserJson)! | ||
let users = usersJson.map({ User.init(json: $0)! }) | ||
let suites = suitesJson.map({ Suite.init(json: $0)! }) | ||
let sections = sectionsJson.map({ Section.init(json: $0)! }) | ||
let cases = casesJson.map({ Case.init(json: $0)! }) | ||
let statuses = statusesJson.map({ Status.init(json: $0)! }) | ||
|
||
self.init(project: project, suites: suites, sections: sections, cases: cases, statuses: statuses, users: users, currentUser: currentUser) | ||
|
||
} | ||
} | ||
|
||
extension QuizTrainData: JSONSerializable { | ||
public func serialized() -> JSONDictionary { | ||
return [JSONKeys.project.rawValue: project.serialized(), | ||
JSONKeys.suites.rawValue: suites.map({ $0.serialized() }), | ||
JSONKeys.sections.rawValue: sections.map({ $0.serialized() }), | ||
JSONKeys.statuses.rawValue: statuses.map({ $0.serialized() }), | ||
JSONKeys.cases.rawValue: cases.map({ $0.serialized() }), | ||
JSONKeys.users.rawValue: users.map({ $0.serialized() }), | ||
JSONKeys.currentUser.rawValue: currentUser.serialized() | ||
] | ||
} | ||
} | ||
|
||
extension QuizTrainData { | ||
public static func writeToFile(atPath: URL, quizTrainData: QuizTrainData) { | ||
if !FileManager.default.fileExists(atPath: atPath.path) { | ||
FileManager.default.createFile(atPath: atPath.path, contents: nil) | ||
} | ||
do { | ||
let json = try JSONSerialization.data(withJSONObject: quizTrainData.serialized(), options: .fragmentsAllowed) | ||
try json.write(to: atPath) | ||
} catch { | ||
print("QuizTrain Data Error: Cannot write to file at path: \(atPath)") | ||
return | ||
} | ||
} | ||
|
||
public static func readFromFile(atPath: URL) -> QuizTrainData? { | ||
guard let data = try? Data.init(contentsOf: atPath) else { | ||
print("QuizTrain Data Error: Cannot write to file at path: \(atPath)") | ||
return nil | ||
} | ||
guard let json = try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as? JSONDictionary else { | ||
print("QuizTrain Data Error: Cannot convert data to json object") | ||
return nil | ||
} | ||
return QuizTrainData.init(json: json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// Links.swift | ||
// QuizTrain | ||
// | ||
// Created by Neha Srivastava on 9/26/22. | ||
// | ||
|
||
public struct Links { | ||
public let next: String? | ||
public let prev: String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.