111 lines
4.0 KiB
Swift
111 lines
4.0 KiB
Swift
import Foundation
|
|
|
|
public struct WFPost {
|
|
public var postId: String?
|
|
public var slug: String?
|
|
public var appearance: String?
|
|
public var language: String?
|
|
public var rtl: Bool?
|
|
public var createdDate: Date?
|
|
public var updatedDate: Date?
|
|
public var title: String?
|
|
public var body: String
|
|
public var tags: [String]?
|
|
public var views: Int?
|
|
public var collectionAlias: String?
|
|
}
|
|
|
|
extension WFPost: Decodable {
|
|
enum CodingKeys: String, CodingKey {
|
|
case postId = "id"
|
|
case slug
|
|
case appearance
|
|
case language
|
|
case rtl
|
|
case createdDate = "created"
|
|
case updatedDate = "updated"
|
|
case title
|
|
case body
|
|
case tags
|
|
case views
|
|
case collectionAlias = "collection"
|
|
|
|
enum CollectionKeys: String, CodingKey {
|
|
case alias
|
|
}
|
|
}
|
|
|
|
/// Creates a basic `WFPost` object.
|
|
///
|
|
/// This initializer creates a bare-minimum `WFPost` object for sending to the server; use the decoder-based
|
|
/// initializer to populate its other properties from the server response.
|
|
///
|
|
/// Only the `body` parameter is required. If other properties are not provided, they will be generated by
|
|
/// the server.
|
|
///
|
|
/// - Parameters:
|
|
/// - body: The body text for the post.
|
|
/// - title: The title for the post.
|
|
/// - appearance: The appearance for the post; one of `sans`, `serif`/`norm`, `wrap`, `mono`, or `code`.
|
|
/// Defaults to `serif`.
|
|
/// - language: An ISO 639-1 language code.
|
|
/// - rtl: Set to `true` to show content right-to-left.
|
|
/// - createdDate: The published date for the post.
|
|
public init(
|
|
body: String,
|
|
title: String? = nil,
|
|
appearance: String? = nil,
|
|
language: String? = nil,
|
|
rtl: Bool? = nil,
|
|
createdDate: Date? = nil
|
|
) {
|
|
// These properties are initialized when the user creates a post.
|
|
self.body = body
|
|
self.title = title
|
|
self.appearance = appearance
|
|
self.language = language
|
|
self.rtl = rtl
|
|
self.createdDate = createdDate
|
|
|
|
// These properties come from a server response.
|
|
self.postId = nil
|
|
self.slug = nil
|
|
self.updatedDate = nil
|
|
self.tags = nil
|
|
self.views = nil
|
|
self.collectionAlias = nil
|
|
}
|
|
|
|
/// Creates a `WFPost` object from the server response.
|
|
///
|
|
/// Primarily used by the `WFClient` to create a `WFPost` object from the JSON returned by the server.
|
|
///
|
|
/// - Parameter decoder: The decoder to use for translating the server response to a Swift object.
|
|
/// - Throws: Error thrown by the `try` attempt when decoding any given property.
|
|
public init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
postId = try container.decodeIfPresent(String.self, forKey: .postId)
|
|
slug = try container.decodeIfPresent(String.self, forKey: .slug)
|
|
appearance = try container.decodeIfPresent(String.self, forKey: .appearance)
|
|
language = try container.decodeIfPresent(String.self, forKey: .language)
|
|
rtl = try container.decodeIfPresent(Bool.self, forKey: .rtl)
|
|
createdDate = try container.decodeIfPresent(Date.self, forKey: .createdDate)
|
|
updatedDate = try container.decodeIfPresent(Date.self, forKey: .updatedDate)
|
|
title = try container.decodeIfPresent(String.self, forKey: .title)
|
|
body = try container.decode(String.self, forKey: .body)
|
|
tags = try container.decodeIfPresent([String].self, forKey: .tags)
|
|
views = try container.decodeIfPresent(Int.self, forKey: .views)
|
|
|
|
do {
|
|
let collectionContainer = try container.nestedContainer(
|
|
keyedBy: CodingKeys.CollectionKeys.self,
|
|
forKey: .collectionAlias
|
|
)
|
|
collectionAlias = try collectionContainer.decodeIfPresent(String.self, forKey: .alias)
|
|
} catch {
|
|
collectionAlias = nil
|
|
return
|
|
}
|
|
}
|
|
}
|