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

Pass encoder & decoder with default value #16

Merged
merged 1 commit into from
Sep 8, 2018
Merged
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
20 changes: 10 additions & 10 deletions Sources/Disk+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public extension Disk {
/// - value: the Encodable struct to store
/// - directory: user directory to store the file in
/// - path: file location to store the data (i.e. "Folder/file.json")
/// - useEncoder: encoder to encode value
/// - Throws: Error if there were any issues encoding the struct or writing it to disk
static func save<T: Encodable>(_ value: T, to directory: Directory, as path: String) throws {
static func save<T: Encodable>(_ value: T, to directory: Directory, as path: String, useEncoder encoder: JSONEncoder = JSONEncoder()) throws {
if path.hasSuffix("/") {
throw createInvalidFileNameForStructsError()
}
do {
let url = try createURL(for: path, in: directory)
let encoder = JSONEncoder()
let data = try encoder.encode(value)
try createSubfoldersBeforeCreatingFile(at: url)
try data.write(to: url, options: .atomic)
Expand All @@ -51,8 +51,10 @@ public extension Disk {
/// - value: the struct to store to disk
/// - path: file location to store the data (i.e. "Folder/file.json")
/// - directory: user directory to store the file in
/// - useDecoder: decoder to decode value
/// - useEncoder: encoder to encode value
/// - Throws: Error if there were any issues with encoding/decoding or writing the encoded struct to disk
static func append<T: Codable>(_ value: T, to path: String, in directory: Directory) throws {
static func append<T: Codable>(_ value: T, to path: String, in directory: Directory, useDecoder decoder: JSONDecoder = JSONDecoder(), useEncoder encoder: JSONEncoder = JSONEncoder()) throws {
if path.hasSuffix("/") {
throw createInvalidFileNameForStructsError()
}
Expand All @@ -62,7 +64,6 @@ public extension Disk {
if !(oldData.count > 0) {
try save([value], to: directory, as: path)
} else {
let decoder = JSONDecoder()
let new: [T]
if let old = try? decoder.decode(T.self, from: oldData) {
new = [old, value]
Expand All @@ -72,7 +73,6 @@ public extension Disk {
} else {
throw createDeserializationErrorForAppendingStructToInvalidType(url: url, type: value)
}
let encoder = JSONEncoder()
let newData = try encoder.encode(new)
try newData.write(to: url, options: .atomic)
}
Expand All @@ -90,8 +90,10 @@ public extension Disk {
/// - value: the Codable struct array to store
/// - path: file location to store the data (i.e. "Folder/file.json")
/// - directory: user directory to store the file in
/// - useDecoder: decoder to decode value
/// - useEncoder: encoder to encode value
/// - Throws: Error if there were any issues writing the encoded struct array to disk
static func append<T: Codable>(_ value: [T], to path: String, in directory: Directory) throws {
static func append<T: Codable>(_ value: [T], to path: String, in directory: Directory, useDecoder decoder: JSONDecoder = JSONDecoder(), useEncoder encoder: JSONEncoder = JSONEncoder()) throws {
if path.hasSuffix("/") {
throw createInvalidFileNameForStructsError()
}
Expand All @@ -101,7 +103,6 @@ public extension Disk {
if !(oldData.count > 0) {
try save(value, to: directory, as: path)
} else {
let decoder = JSONDecoder()
let new: [T]
if let old = try? decoder.decode(T.self, from: oldData) {
new = [old] + value
Expand All @@ -111,7 +112,6 @@ public extension Disk {
} else {
throw createDeserializationErrorForAppendingStructToInvalidType(url: url, type: value)
}
let encoder = JSONEncoder()
let newData = try encoder.encode(new)
try newData.write(to: url, options: .atomic)
}
Expand All @@ -129,16 +129,16 @@ public extension Disk {
/// - path: path of the file holding desired data
/// - directory: user directory to retrieve the file from
/// - type: struct type (i.e. Message.self or [Message].self)
/// - useDecoder: decoder to decode value
/// - Returns: decoded structs of data
/// - Throws: Error if there were any issues retrieving the data or decoding it to the specified type
static func retrieve<T: Decodable>(_ path: String, from directory: Directory, as type: T.Type) throws -> T {
static func retrieve<T: Decodable>(_ path: String, from directory: Directory, as type: T.Type, useDecoder decoder: JSONDecoder = JSONDecoder()) throws -> T {
if path.hasSuffix("/") {
throw createInvalidFileNameForStructsError()
}
do {
let url = try getExistingFileURL(for: path, in: directory)
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let value = try decoder.decode(type, from: data)
return value
} catch {
Expand Down