Skip to content

Commit

Permalink
Handle string value case
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif committed Jan 31, 2024
1 parent a8fd39c commit 902fdaf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ extension FileManager {
return try JSONDecoder().decode(Value.self, from: data)
}

/// Read a file's data as the type `String`
///
/// - Parameters:
/// - path: The path to the directory containing the file. The default is `.`, which means the current working directory.
/// - filename: The name of the file to read.
/// - encoding: The String.Encoding to use to get the string.
/// - Returns: The file's data decoded as an instance of `String`.
/// - Throws: If there's an error reading the file or decoding its data.
func string(
path: String = ".",
filename: String,
encoding: String.Encoding = .utf8
) throws -> String {
let data = try data(
path: path,
filename: filename
)

guard let string = String(data: data, encoding: encoding) else {
throw FileError.invalidStringFromData
}

return string
}

/// Read a file's data
///
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ extension Application {
}

do {
return try fileManager.in(path: path, filename: filename)
if
Value.self == String.self,
let stringValue = try? fileManager.string(path: path, filename: filename) as? Value
{
return stringValue
} else {
return try fileManager.in(path: path, filename: filename)
}
} catch {
log(
error: error,
Expand Down

0 comments on commit 902fdaf

Please sign in to comment.