-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): use Codable types (#120)
* Start Codable support on Storage * wip * Use Request type * Use default parameter value * Fix Storage tests * Remove old code * Remove commented code
- Loading branch information
Showing
23 changed files
with
553 additions
and
469 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,28 +1,38 @@ | ||
public struct Bucket: Hashable { | ||
import Foundation | ||
|
||
public struct Bucket: Identifiable, Hashable, Codable { | ||
public var id: String | ||
public var name: String | ||
public var owner: String | ||
public var isPublic: Bool | ||
public var createdAt: String | ||
public var updatedAt: String | ||
|
||
init?(from dictionary: [String: Any]) { | ||
guard | ||
let id = dictionary["id"] as? String, | ||
let name = dictionary["name"] as? String, | ||
let owner = dictionary["owner"] as? String, | ||
let createdAt = dictionary["created_at"] as? String, | ||
let updatedAt = dictionary["updated_at"] as? String, | ||
let isPublic = dictionary["public"] as? Bool | ||
else { | ||
return nil | ||
} | ||
public var createdAt: Date | ||
public var updatedAt: Date | ||
public var allowedMimeTypes: [String]? | ||
public var fileSizeLimit: Int? | ||
|
||
public init( | ||
id: String, name: String, owner: String, isPublic: Bool, createdAt: Date, updatedAt: Date, | ||
allowedMimeTypes: [String]?, | ||
fileSizeLimit: Int? | ||
) { | ||
self.id = id | ||
self.name = name | ||
self.owner = owner | ||
self.isPublic = isPublic | ||
self.createdAt = createdAt | ||
self.updatedAt = updatedAt | ||
self.allowedMimeTypes = allowedMimeTypes | ||
self.fileSizeLimit = fileSizeLimit | ||
} | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case id | ||
case name | ||
case owner | ||
case isPublic = "public" | ||
case createdAt = "created_at" | ||
case updatedAt = "updated_at" | ||
case allowedMimeTypes = "allowed_mime_types" | ||
case fileSizeLimit = "file_size_limit" | ||
} | ||
} |
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,36 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 18/10/23. | ||
// | ||
|
||
import Foundation | ||
|
||
extension JSONEncoder { | ||
public static let defaultStorageEncoder: JSONEncoder = { | ||
JSONEncoder() | ||
}() | ||
} | ||
|
||
extension JSONDecoder { | ||
public static let defaultStorageDecoder: JSONDecoder = { | ||
let decoder = JSONDecoder() | ||
let formatter = ISO8601DateFormatter() | ||
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] | ||
|
||
decoder.dateDecodingStrategy = .custom { decoder in | ||
let container = try decoder.singleValueContainer() | ||
let string = try container.decode(String.self) | ||
|
||
if let date = formatter.date(from: string) { | ||
return date | ||
} | ||
|
||
throw DecodingError.dataCorruptedError( | ||
in: container, debugDescription: "Invalid date format: \(string)") | ||
} | ||
|
||
return decoder | ||
}() | ||
} |
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 |
---|---|---|
@@ -1,37 +1,41 @@ | ||
public struct FileObject { | ||
import Foundation | ||
import _Helpers | ||
|
||
public struct FileObject: Identifiable, Codable { | ||
public var name: String | ||
public var bucketId: String? | ||
public var owner: String? | ||
public var id: String | ||
public var updatedAt: String | ||
public var createdAt: String | ||
public var lastAccessedAt: String | ||
public var metadata: [String: Any] | ||
public var updatedAt: Date | ||
public var createdAt: Date | ||
public var lastAccessedAt: Date | ||
public var metadata: [String: AnyJSON] | ||
public var buckets: Bucket? | ||
|
||
public init?(from dictionary: [String: Any]) { | ||
guard | ||
let name = dictionary["name"] as? String, | ||
let id = dictionary["id"] as? String, | ||
let updatedAt = dictionary["updated_at"] as? String, | ||
let createdAt = dictionary["created_at"] as? String, | ||
let lastAccessedAt = dictionary["last_accessed_at"] as? String, | ||
let metadata = dictionary["metadata"] as? [String: Any] | ||
else { | ||
return nil | ||
} | ||
|
||
public init( | ||
name: String, bucketId: String? = nil, owner: String? = nil, id: String, updatedAt: Date, | ||
createdAt: Date, lastAccessedAt: Date, metadata: [String: AnyJSON], buckets: Bucket? = nil | ||
) { | ||
self.name = name | ||
self.bucketId = dictionary["bucket_id"] as? String | ||
self.owner = dictionary["owner"] as? String | ||
self.bucketId = bucketId | ||
self.owner = owner | ||
self.id = id | ||
self.updatedAt = updatedAt | ||
self.createdAt = createdAt | ||
self.lastAccessedAt = lastAccessedAt | ||
self.metadata = metadata | ||
self.buckets = buckets | ||
} | ||
|
||
if let buckets = dictionary["buckets"] as? [String: Any] { | ||
self.buckets = Bucket(from: buckets) | ||
} | ||
enum CodingKeys: String, CodingKey { | ||
case name | ||
case bucketId = "bucket_id" | ||
case owner | ||
case id | ||
case updatedAt = "updated_at" | ||
case createdAt = "created_at" | ||
case lastAccessedAt = "last_accessed_at" | ||
case metadata | ||
case buckets | ||
} | ||
} |
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,18 @@ | ||
import Foundation | ||
|
||
public struct SignedURL: Decodable { | ||
/// An optional error message. | ||
public var error: String? | ||
|
||
/// The signed url. | ||
public var signedURL: URL | ||
|
||
/// The path of the file. | ||
public var path: String | ||
|
||
public init(error: String? = nil, signedURL: URL, path: String) { | ||
self.error = error | ||
self.signedURL = signedURL | ||
self.path = path | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
public struct SortBy { | ||
public struct SortBy: Encodable { | ||
public var column: String? | ||
public var order: String? | ||
|
||
|
Oops, something went wrong.