Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Action, AttachmentField, Edited and Reply conform to Codable #163

Merged
merged 12 commits into from
Jul 12, 2019
162 changes: 126 additions & 36 deletions SKCore/Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
// THE SOFTWARE.

public struct Action {
fileprivate enum CodingKeys: String {
case name
case text
case type
case value
case url
case style
case confirm
case options
case dataSource = "data_source"
}

public let name: String?
public let text: String?
public let type: String?
Expand All @@ -31,17 +43,17 @@ public struct Action {
public let confirm: Confirm?
public let options: [Option]?
public let dataSource: DataSource?

public init(action: [String: Any]?) {
name = action?["name"] as? String
text = action?["text"] as? String
type = action?["type"] as? String
value = action?["value"] as? String
url = action?["url"] as? String
style = ActionStyle(rawValue: action?["style"] as? String ?? "")
confirm = Confirm(confirm:action?["confirm"] as? [String: Any])
options = (action?["options"] as? [[String: Any]])?.map { Option(option: $0) }
dataSource = DataSource(rawValue: action?["data_source"] as? String ?? "")
name = action?[CodingKeys.name.rawValue] as? String
text = action?[CodingKeys.text.rawValue] as? String
type = action?[CodingKeys.type.rawValue] as? String
value = action?[CodingKeys.value.rawValue] as? String
url = action?[CodingKeys.url.rawValue] as? String
style = ActionStyle(rawValue: action?[CodingKeys.style.rawValue] as? String ?? "")
confirm = Confirm(confirm:action?[CodingKeys.confirm.rawValue] as? [String: Any])
options = (action?[CodingKeys.options.rawValue] as? [[String: Any]])?.map { Option(option: $0) }
dataSource = DataSource(rawValue: action?[CodingKeys.dataSource.rawValue] as? String ?? "")
}

public init(name: String, text: String, type: String = "button", style: ActionStyle = .defaultStyle, value: String? = nil,
Expand All @@ -59,29 +71,36 @@ public struct Action {

public var dictionary: [String: Any] {
var dict = [String: Any]()
dict["name"] = name
dict["text"] = text
dict["type"] = type
dict["value"] = value
dict["url"] = url
dict["style"] = style?.rawValue
dict["confirm"] = confirm?.dictionary
dict["options"] = options?.map { $0.dictionary }
dict["data_source"] = dataSource?.rawValue
dict[CodingKeys.name.rawValue] = name
dict[CodingKeys.text.rawValue] = text
dict[CodingKeys.type.rawValue] = type
dict[CodingKeys.value.rawValue] = value
dict[CodingKeys.url.rawValue] = url
dict[CodingKeys.style.rawValue] = style?.rawValue
dict[CodingKeys.confirm.rawValue] = confirm?.dictionary
dict[CodingKeys.options.rawValue] = options?.map { $0.dictionary }
dict[CodingKeys.dataSource.rawValue] = dataSource?.rawValue
return dict
}

public struct Confirm {
fileprivate enum CodingKeys: String {
case title
case text
case okText = "ok_text"
case dismissText = "dismiss_text"
}

public let title: String?
public let text: String?
public let okText: String?
public let dismissText: String?

public init(confirm: [String: Any]?) {
title = confirm?["title"] as? String
text = confirm?["text"] as? String
okText = confirm?["ok_text"] as? String
dismissText = confirm?["dismiss_text"] as? String
title = confirm?[CodingKeys.title.rawValue] as? String
text = confirm?[CodingKeys.text.rawValue] as? String
okText = confirm?[CodingKeys.okText.rawValue] as? String
dismissText = confirm?[CodingKeys.dismissText.rawValue] as? String
}

public init(text: String, title: String? = nil, okText: String? = nil, dismissText: String? = nil) {
Expand All @@ -93,21 +112,26 @@ public struct Action {

public var dictionary: [String: Any] {
var dict = [String: Any]()
dict["title"] = title
dict["text"] = text
dict["ok_text"] = okText
dict["dismiss_text"] = dismissText
dict[CodingKeys.title.rawValue] = title
dict[CodingKeys.text.rawValue] = text
dict[CodingKeys.okText.rawValue] = okText
dict[CodingKeys.dismissText.rawValue] = dismissText
return dict
}
}

public struct Option {
fileprivate enum CodingKeys: String {
case text
case value
}

public let text: String?
public let value: String?

public init(option: [String: Any]?) {
text = option?["text"] as? String
value = option?["value"] as? String
text = option?[CodingKeys.text.rawValue] as? String
value = option?[CodingKeys.value.rawValue] as? String
}

public init(text: String, value: String) {
Expand All @@ -117,26 +141,92 @@ public struct Action {

public var dictionary: [String: Any] {
var dict = [String: Any]()
dict["text"] = text
dict["value"] = value
dict[CodingKeys.text.rawValue] = text
dict[CodingKeys.value.rawValue] = value
return dict
}
}

public enum DataSource: String {
public enum DataSource: String, Codable {
case users
case channels
case conversations
}
}

public enum ActionStyle: String {
extension Action: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decodeIfPresent(String.self, forKey: .name)
text = try values.decodeIfPresent(String.self, forKey: .text)
type = try values.decodeIfPresent(String.self, forKey: .type)
value = try values.decodeIfPresent(String.self, forKey: .value)
url = try values.decodeIfPresent(String.self, forKey: .url)
style = try values.decodeIfPresent(ActionStyle.self, forKey: .style)
confirm = try values.decodeIfPresent(Confirm.self, forKey: .confirm)
options = try values.decodeIfPresent([Option].self, forKey: .options)
dataSource = try values.decodeIfPresent(DataSource.self, forKey: .dataSource)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(text, forKey: .text)
try container.encode(type, forKey: .type)
try container.encode(value, forKey: .value)
try container.encode(url, forKey: .url)
try container.encode(style, forKey: .style)
try container.encode(confirm, forKey: .confirm)
try container.encode(options, forKey: .options)
try container.encode(dataSource, forKey: .dataSource)
}
}

extension Action.CodingKeys: CodingKey { }

extension Action.Confirm: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
title = try values.decodeIfPresent(String.self, forKey: .title)
text = try values.decodeIfPresent(String.self, forKey: .text)
okText = try values.decodeIfPresent(String.self, forKey: .okText)
dismissText = try values.decodeIfPresent(String.self, forKey: .dismissText)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(title, forKey: .title)
try container.encode(text, forKey: .text)
try container.encode(okText, forKey: .okText)
try container.encode(dismissText, forKey: .dismissText)
}
}

extension Action.Confirm.CodingKeys: CodingKey { }

extension Action.Option: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
text = try values.decodeIfPresent(String.self, forKey: .text)
value = try values.decodeIfPresent(String.self, forKey: .value)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(text, forKey: .text)
try container.encode(value, forKey: .value)
}
}

extension Action.Option.CodingKeys: CodingKey { }

public enum ActionStyle: String, Codable {
case defaultStyle = "default"
case primary = "primary"
case danger = "danger"
}

public enum MessageResponseType: String {
public enum MessageResponseType: String, Codable {
case inChannel = "in_channel"
case ephemeral = "ephemeral"
}
38 changes: 31 additions & 7 deletions SKCore/Sources/AttachmentField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@
// THE SOFTWARE.

public struct AttachmentField {
fileprivate enum CodingKeys: String {
case title
case value
case short
}

public let title: String?
public let value: String?
public let short: Bool?

public init(field: [String: Any]?) {
title = field?["title"] as? String
value = field?["value"] as? String
short = field?["short"] as? Bool
title = field?[CodingKeys.title.rawValue] as? String
value = field?[CodingKeys.value.rawValue] as? String
short = field?[CodingKeys.short.rawValue] as? Bool
}

public init(title: String?, value: String?, short: Bool? = nil) {
Expand All @@ -40,9 +46,27 @@ public struct AttachmentField {

public var dictionary: [String: Any] {
var field = [String: Any]()
field["title"] = title
field["value"] = value
field["short"] = short
field[CodingKeys.title.rawValue] = title
field[CodingKeys.value.rawValue] = value
field[CodingKeys.short.rawValue] = short
return field
}
}

extension AttachmentField: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
title = try values.decodeIfPresent(String.self, forKey: .title)
value = try values.decodeIfPresent(String.self, forKey: .value)
short = try values.decodeIfPresent(Bool.self, forKey: .short)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(title, forKey: .title)
try container.encode(value, forKey: .value)
try container.encode(short, forKey: .short)
}
}

extension AttachmentField.CodingKeys: CodingKey { }
25 changes: 23 additions & 2 deletions SKCore/Sources/Edited.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,32 @@
// THE SOFTWARE.

public struct Edited {
fileprivate enum CodingKeys: String {
case user
case ts
}

public let user: String?
public let ts: String?

public init(edited: [String: Any]?) {
user = edited?["user"] as? String
ts = edited?["ts"] as? String
user = edited?[CodingKeys.user.rawValue] as? String
ts = edited?[CodingKeys.ts.rawValue] as? String
}
}

extension Edited: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
user = try values.decodeIfPresent(String.self, forKey: .user)
ts = try values.decodeIfPresent(String.self, forKey: .ts)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(user, forKey: .user)
try container.encode(ts, forKey: .ts)
}
}

extension Edited.CodingKeys: CodingKey { }
27 changes: 24 additions & 3 deletions SKCore/Sources/Reply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,32 @@
// THE SOFTWARE.

public struct Reply {
fileprivate enum CodingKeys: String {
case user
case ts
}

public let user: String?
public let ts: String?

public init(reply: [String: Any]?) {
user = reply?["user"] as? String
ts = reply?["ts"] as? String
user = reply?[CodingKeys.user.rawValue] as? String
ts = reply?[CodingKeys.ts.rawValue] as? String
}
}

extension Reply: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
user = try values.decodeIfPresent(String.self, forKey: .user)
ts = try values.decodeIfPresent(String.self, forKey: .ts)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(user, forKey: .user)
try container.encode(ts, forKey: .ts)
}
}

extension Reply.CodingKeys: CodingKey { }
Loading