From 66e7b01282fb3f551d06b5affd1a9c00b83fda18 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Tue, 13 Oct 2020 17:28:27 -0700 Subject: [PATCH] Refactoring --- DB/Sources/DB/Content/AccountList.swift | 2 +- DB/Sources/DB/Content/AccountListJoin.swift | 2 +- .../DB/Content/AccountPinnedStatusJoin.swift | 2 +- DB/Sources/DB/Content/AccountRecord.swift | 12 +----------- .../DB/Content/ContentDatabaseJSONEncoder.swift | 5 +++-- .../DB/Content/ContentDatabaseRecord.swift | 17 +++++++++++++++++ DB/Sources/DB/Content/LoadMoreRecord.swift | 12 +----------- DB/Sources/DB/Content/StatusAncestorJoin.swift | 2 +- .../DB/Content/StatusDescendantJoin.swift | 2 +- DB/Sources/DB/Content/StatusRecord.swift | 12 +----------- .../Content/StatusShowAttachmentsToggle.swift | 12 +----------- .../DB/Content/StatusShowContentToggle.swift | 12 +----------- DB/Sources/DB/Content/TimelineRecord.swift | 12 +----------- DB/Sources/DB/Content/TimelineStatusJoin.swift | 2 +- .../DB/Extensions/Filter+Extensions.swift | 10 +--------- 15 files changed, 33 insertions(+), 83 deletions(-) rename Mastodon/Sources/Mastodon/Coding/MastodonEncoder.swift => DB/Sources/DB/Content/ContentDatabaseJSONEncoder.swift (79%) create mode 100644 DB/Sources/DB/Content/ContentDatabaseRecord.swift diff --git a/DB/Sources/DB/Content/AccountList.swift b/DB/Sources/DB/Content/AccountList.swift index 31ea610..1f92a58 100644 --- a/DB/Sources/DB/Content/AccountList.swift +++ b/DB/Sources/DB/Content/AccountList.swift @@ -3,7 +3,7 @@ import Foundation import GRDB -public struct AccountList: Codable, FetchableRecord, PersistableRecord { +public struct AccountList: ContentDatabaseRecord { let id: Id public init() { diff --git a/DB/Sources/DB/Content/AccountListJoin.swift b/DB/Sources/DB/Content/AccountListJoin.swift index 5f266b0..d1f06c5 100644 --- a/DB/Sources/DB/Content/AccountListJoin.swift +++ b/DB/Sources/DB/Content/AccountListJoin.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct AccountListJoin: Codable, FetchableRecord, PersistableRecord { +struct AccountListJoin: ContentDatabaseRecord { let accountId: Account.Id let listId: AccountList.Id let index: Int diff --git a/DB/Sources/DB/Content/AccountPinnedStatusJoin.swift b/DB/Sources/DB/Content/AccountPinnedStatusJoin.swift index 1e8d735..42cc604 100644 --- a/DB/Sources/DB/Content/AccountPinnedStatusJoin.swift +++ b/DB/Sources/DB/Content/AccountPinnedStatusJoin.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct AccountPinnedStatusJoin: Codable, FetchableRecord, PersistableRecord { +struct AccountPinnedStatusJoin: ContentDatabaseRecord { let accountId: Account.Id let statusId: Status.Id let index: Int diff --git a/DB/Sources/DB/Content/AccountRecord.swift b/DB/Sources/DB/Content/AccountRecord.swift index 5e863dd..5fd5332 100644 --- a/DB/Sources/DB/Content/AccountRecord.swift +++ b/DB/Sources/DB/Content/AccountRecord.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct AccountRecord: Codable, Hashable { +struct AccountRecord: ContentDatabaseRecord, Hashable { let id: Account.Id let username: String let acct: String @@ -52,16 +52,6 @@ extension AccountRecord { } } -extension AccountRecord: FetchableRecord, PersistableRecord { - static func databaseJSONDecoder(for column: String) -> JSONDecoder { - MastodonDecoder() - } - - static func databaseJSONEncoder(for column: String) -> JSONEncoder { - MastodonEncoder() - } -} - extension AccountRecord { static let moved = belongsTo(AccountRecord.self) static let pinnedStatusJoins = hasMany(AccountPinnedStatusJoin.self) diff --git a/Mastodon/Sources/Mastodon/Coding/MastodonEncoder.swift b/DB/Sources/DB/Content/ContentDatabaseJSONEncoder.swift similarity index 79% rename from Mastodon/Sources/Mastodon/Coding/MastodonEncoder.swift rename to DB/Sources/DB/Content/ContentDatabaseJSONEncoder.swift index 5c7c331..28d7907 100644 --- a/Mastodon/Sources/Mastodon/Coding/MastodonEncoder.swift +++ b/DB/Sources/DB/Content/ContentDatabaseJSONEncoder.swift @@ -1,9 +1,10 @@ // Copyright © 2020 Metabolist. All rights reserved. import Foundation +import Mastodon -public final class MastodonEncoder: JSONEncoder { - public override init() { +final class ContentDatabaseJSONEncoder: JSONEncoder { + override init() { super.init() let dateFormatter = DateFormatter() diff --git a/DB/Sources/DB/Content/ContentDatabaseRecord.swift b/DB/Sources/DB/Content/ContentDatabaseRecord.swift new file mode 100644 index 0000000..d38b62e --- /dev/null +++ b/DB/Sources/DB/Content/ContentDatabaseRecord.swift @@ -0,0 +1,17 @@ +// Copyright © 2020 Metabolist. All rights reserved. + +import Foundation +import GRDB +import Mastodon + +protocol ContentDatabaseRecord: Codable, FetchableRecord, PersistableRecord {} + +extension ContentDatabaseRecord { + public static func databaseJSONDecoder(for column: String) -> JSONDecoder { + MastodonDecoder() + } + + public static func databaseJSONEncoder(for column: String) -> JSONEncoder { + ContentDatabaseJSONEncoder() + } +} diff --git a/DB/Sources/DB/Content/LoadMoreRecord.swift b/DB/Sources/DB/Content/LoadMoreRecord.swift index 5cf319b..20325e8 100644 --- a/DB/Sources/DB/Content/LoadMoreRecord.swift +++ b/DB/Sources/DB/Content/LoadMoreRecord.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct LoadMoreRecord: Codable, Hashable { +struct LoadMoreRecord: ContentDatabaseRecord, Hashable { let timelineId: Timeline.Id let afterStatusId: Status.Id let beforeStatusId: Status.Id @@ -17,13 +17,3 @@ extension LoadMoreRecord { static let beforeStatusId = Column(LoadMoreRecord.CodingKeys.beforeStatusId) } } - -extension LoadMoreRecord: FetchableRecord, PersistableRecord { - static func databaseJSONDecoder(for column: String) -> JSONDecoder { - MastodonDecoder() - } - - static func databaseJSONEncoder(for column: String) -> JSONEncoder { - MastodonEncoder() - } -} diff --git a/DB/Sources/DB/Content/StatusAncestorJoin.swift b/DB/Sources/DB/Content/StatusAncestorJoin.swift index a697a13..07abb35 100644 --- a/DB/Sources/DB/Content/StatusAncestorJoin.swift +++ b/DB/Sources/DB/Content/StatusAncestorJoin.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct StatusAncestorJoin: Codable, FetchableRecord, PersistableRecord { +struct StatusAncestorJoin: ContentDatabaseRecord { let parentId: Status.Id let statusId: Status.Id let index: Int diff --git a/DB/Sources/DB/Content/StatusDescendantJoin.swift b/DB/Sources/DB/Content/StatusDescendantJoin.swift index ece72d0..ed5c2e2 100644 --- a/DB/Sources/DB/Content/StatusDescendantJoin.swift +++ b/DB/Sources/DB/Content/StatusDescendantJoin.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct StatusDescendantJoin: Codable, FetchableRecord, PersistableRecord { +struct StatusDescendantJoin: ContentDatabaseRecord { let parentId: Status.Id let statusId: Status.Id let index: Int diff --git a/DB/Sources/DB/Content/StatusRecord.swift b/DB/Sources/DB/Content/StatusRecord.swift index 9d71421..b80ae33 100644 --- a/DB/Sources/DB/Content/StatusRecord.swift +++ b/DB/Sources/DB/Content/StatusRecord.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct StatusRecord: Codable, Hashable { +struct StatusRecord: ContentDatabaseRecord, Hashable { let id: Status.Id let uri: String let createdAt: Date @@ -70,16 +70,6 @@ extension StatusRecord { } } -extension StatusRecord: FetchableRecord, PersistableRecord { - static func databaseJSONDecoder(for column: String) -> JSONDecoder { - MastodonDecoder() - } - - static func databaseJSONEncoder(for column: String) -> JSONEncoder { - MastodonEncoder() - } -} - extension StatusRecord { static let account = belongsTo(AccountRecord.self) static let accountMoved = hasOne(AccountRecord.self, diff --git a/DB/Sources/DB/Content/StatusShowAttachmentsToggle.swift b/DB/Sources/DB/Content/StatusShowAttachmentsToggle.swift index b5d562f..96257b5 100644 --- a/DB/Sources/DB/Content/StatusShowAttachmentsToggle.swift +++ b/DB/Sources/DB/Content/StatusShowAttachmentsToggle.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct StatusShowAttachmentsToggle: Codable, Hashable { +struct StatusShowAttachmentsToggle: ContentDatabaseRecord, Hashable { let statusId: Status.Id } @@ -13,13 +13,3 @@ extension StatusShowAttachmentsToggle { static let statusId = Column(StatusShowAttachmentsToggle.CodingKeys.statusId) } } - -extension StatusShowAttachmentsToggle: FetchableRecord, PersistableRecord { - static func databaseJSONDecoder(for column: String) -> JSONDecoder { - MastodonDecoder() - } - - static func databaseJSONEncoder(for column: String) -> JSONEncoder { - MastodonEncoder() - } -} diff --git a/DB/Sources/DB/Content/StatusShowContentToggle.swift b/DB/Sources/DB/Content/StatusShowContentToggle.swift index b9277a2..b344678 100644 --- a/DB/Sources/DB/Content/StatusShowContentToggle.swift +++ b/DB/Sources/DB/Content/StatusShowContentToggle.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct StatusShowContentToggle: Codable, Hashable { +struct StatusShowContentToggle: ContentDatabaseRecord, Hashable { let statusId: Status.Id } @@ -13,13 +13,3 @@ extension StatusShowContentToggle { static let statusId = Column(StatusShowContentToggle.CodingKeys.statusId) } } - -extension StatusShowContentToggle: FetchableRecord, PersistableRecord { - static func databaseJSONDecoder(for column: String) -> JSONDecoder { - MastodonDecoder() - } - - static func databaseJSONEncoder(for column: String) -> JSONEncoder { - MastodonEncoder() - } -} diff --git a/DB/Sources/DB/Content/TimelineRecord.swift b/DB/Sources/DB/Content/TimelineRecord.swift index c3c7a62..d6d96a0 100644 --- a/DB/Sources/DB/Content/TimelineRecord.swift +++ b/DB/Sources/DB/Content/TimelineRecord.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct TimelineRecord: Codable, Hashable { +struct TimelineRecord: ContentDatabaseRecord, Hashable { let id: Timeline.Id let listId: List.Id? let listTitle: String? @@ -13,16 +13,6 @@ struct TimelineRecord: Codable, Hashable { let profileCollection: ProfileCollection? } -extension TimelineRecord: FetchableRecord, PersistableRecord { - static func databaseJSONDecoder(for column: String) -> JSONDecoder { - MastodonDecoder() - } - - static func databaseJSONEncoder(for column: String) -> JSONEncoder { - MastodonEncoder() - } -} - extension TimelineRecord { enum Columns { static let id = Column(TimelineRecord.CodingKeys.id) diff --git a/DB/Sources/DB/Content/TimelineStatusJoin.swift b/DB/Sources/DB/Content/TimelineStatusJoin.swift index 8e87649..44239a6 100644 --- a/DB/Sources/DB/Content/TimelineStatusJoin.swift +++ b/DB/Sources/DB/Content/TimelineStatusJoin.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import Mastodon -struct TimelineStatusJoin: Codable, FetchableRecord, PersistableRecord { +struct TimelineStatusJoin: ContentDatabaseRecord { let timelineId: Timeline.Id let statusId: Status.Id diff --git a/DB/Sources/DB/Extensions/Filter+Extensions.swift b/DB/Sources/DB/Extensions/Filter+Extensions.swift index b5fdb8d..6edca7c 100644 --- a/DB/Sources/DB/Extensions/Filter+Extensions.swift +++ b/DB/Sources/DB/Extensions/Filter+Extensions.swift @@ -4,15 +4,7 @@ import Foundation import GRDB import Mastodon -extension Filter: FetchableRecord, PersistableRecord { - public static func databaseJSONDecoder(for column: String) -> JSONDecoder { - MastodonDecoder() - } - - public static func databaseJSONEncoder(for column: String) -> JSONEncoder { - MastodonEncoder() - } -} +extension Filter: ContentDatabaseRecord {} extension Filter { enum Columns: String, ColumnExpression {