Skip to content

Commit

Permalink
Merge pull request #16 from nixzhu/master
Browse files Browse the repository at this point in the history
Pass encoder & decoder with default value
  • Loading branch information
saoudrizwan authored Sep 8, 2018
2 parents 5f1d0c0 + 273ee1c commit 19a709b
Showing 1 changed file with 10 additions and 10 deletions.
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

0 comments on commit 19a709b

Please sign in to comment.