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

Codable vol.2 - CustomProfileField, DoNotDisturbStatus, TeamIcon and Topic #166

Merged
merged 4 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions SKCore/Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public struct Action {
public let dataSource: DataSource?

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

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

public var dictionary: [String: Any] {
var dict = [String: Any]()
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
dict[CodingKeys.name] = name
dict[CodingKeys.text] = text
dict[CodingKeys.type] = type
dict[CodingKeys.value] = value
dict[CodingKeys.url] = url
dict[CodingKeys.style] = style?.rawValue
dict[CodingKeys.confirm] = confirm?.dictionary
dict[CodingKeys.options] = options?.map { $0.dictionary }
dict[CodingKeys.dataSource] = dataSource?.rawValue
return dict
}

Expand All @@ -97,10 +97,10 @@ public struct Action {
public let dismissText: String?

public init(confirm: [String: Any]?) {
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
title = confirm?[CodingKeys.title] as? String
text = confirm?[CodingKeys.text] as? String
okText = confirm?[CodingKeys.okText] as? String
dismissText = confirm?[CodingKeys.dismissText] as? String
}

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

public var dictionary: [String: Any] {
var dict = [String: Any]()
dict[CodingKeys.title.rawValue] = title
dict[CodingKeys.text.rawValue] = text
dict[CodingKeys.okText.rawValue] = okText
dict[CodingKeys.dismissText.rawValue] = dismissText
dict[CodingKeys.title] = title
dict[CodingKeys.text] = text
dict[CodingKeys.okText] = okText
dict[CodingKeys.dismissText] = dismissText
return dict
}
}
Expand All @@ -130,8 +130,8 @@ public struct Action {
public let value: String?

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

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

public var dictionary: [String: Any] {
var dict = [String: Any]()
dict[CodingKeys.text.rawValue] = text
dict[CodingKeys.value.rawValue] = value
dict[CodingKeys.text] = text
dict[CodingKeys.value] = value
return dict
}
}
Expand Down
12 changes: 6 additions & 6 deletions SKCore/Sources/AttachmentField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public struct AttachmentField {
public let short: Bool?

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

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

public var dictionary: [String: Any] {
var field = [String: Any]()
field[CodingKeys.title.rawValue] = title
field[CodingKeys.value.rawValue] = value
field[CodingKeys.short.rawValue] = short
field[CodingKeys.title] = title
field[CodingKeys.value] = value
field[CodingKeys.short] = short
return field
}
}
Expand Down
65 changes: 55 additions & 10 deletions SKCore/Sources/CustomProfileField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
// THE SOFTWARE.

public struct CustomProfileField {
fileprivate enum CodingKeys: String {
case id
case alt
case value
case hidden = "is_hidden"
case hint
case label
case options
case ordering
case possibleValues = "possible_values"
case type
}

public var id: String?
public var alt: String?
public var value: String?
Expand All @@ -34,16 +47,16 @@ public struct CustomProfileField {
public var type: String?

public init(field: [String: Any]?) {
id = field?["id"] as? String
alt = field?["alt"] as? String
value = field?["value"] as? String
hidden = field?["is_hidden"] as? Bool
hint = field?["hint"] as? String
label = field?["label"] as? String
options = field?["options"] as? String
ordering = field?["ordering"] as? Int
possibleValues = field?["possible_values"] as? [String]
type = field?["type"] as? String
id = field?[CodingKeys.id] as? String
alt = field?[CodingKeys.alt] as? String
value = field?[CodingKeys.value] as? String
hidden = field?[CodingKeys.hidden] as? Bool
hint = field?[CodingKeys.hint] as? String
label = field?[CodingKeys.label] as? String
options = field?[CodingKeys.options] as? String
ordering = field?[CodingKeys.ordering] as? Int
possibleValues = field?[CodingKeys.possibleValues] as? [String]
type = field?[CodingKeys.type] as? String
}

public init(id: String?) {
Expand All @@ -63,3 +76,35 @@ public struct CustomProfileField {
type = profile?.type != nil ? profile?.type : type
}
}

extension CustomProfileField: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decodeIfPresent(String.self, forKey: .id)
alt = try values.decodeIfPresent(String.self, forKey: .alt)
value = try values.decodeIfPresent(String.self, forKey: .value)
hidden = try values.decodeIfPresent(Bool.self, forKey: .hidden)
hint = try values.decodeIfPresent(String.self, forKey: .hint)
label = try values.decodeIfPresent(String.self, forKey: .label)
options = try values.decodeIfPresent(String.self, forKey: .options)
ordering = try values.decodeIfPresent(Int.self, forKey: .ordering)
possibleValues = try values.decodeIfPresent([String].self, forKey: .possibleValues)
type = try values.decodeIfPresent(String.self, forKey: .type)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(alt, forKey: .alt)
try container.encode(value, forKey: .value)
try container.encode(hidden, forKey: .hidden)
try container.encode(hint, forKey: .hint)
try container.encode(label, forKey: .label)
try container.encode(options, forKey: .options)
try container.encode(ordering, forKey: .ordering)
try container.encode(possibleValues, forKey: .possibleValues)
try container.encode(type, forKey: .type)
}
}

extension CustomProfileField.CodingKeys: CodingKey { }
40 changes: 35 additions & 5 deletions SKCore/Sources/DoNotDisturbStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,47 @@
// THE SOFTWARE.

public struct DoNotDisturbStatus {
fileprivate enum CodingKeys: String {
case enabled = "dnd_enabled"
case nextDoNotDisturbStart = "next_dnd_start_ts"
case nextDoNotDisturbEnd = "next_dnd_end_ts"
case snoozeEnabled = "snooze_enabled"
case snoozeEndtime = "snooze_endtime"
}

public var enabled: Bool?
public var nextDoNotDisturbStart: Int?
public var nextDoNotDisturbEnd: Int?
public var snoozeEnabled: Bool?
public var snoozeEndtime: Int?

public init(status: [String: Any]?) {
enabled = status?["dnd_enabled"] as? Bool
nextDoNotDisturbStart = status?["next_dnd_start_ts"] as? Int
nextDoNotDisturbEnd = status?["next_dnd_end_ts"] as? Int
snoozeEnabled = status?["snooze_enabled"] as? Bool
snoozeEndtime = status?["snooze_endtime"] as? Int
enabled = status?[CodingKeys.enabled] as? Bool
nextDoNotDisturbStart = status?[CodingKeys.nextDoNotDisturbStart] as? Int
nextDoNotDisturbEnd = status?[CodingKeys.nextDoNotDisturbEnd] as? Int
snoozeEnabled = status?[CodingKeys.snoozeEnabled] as? Bool
snoozeEndtime = status?[CodingKeys.snoozeEndtime] as? Int
}
}

extension DoNotDisturbStatus: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
enabled = try values.decodeIfPresent(Bool.self, forKey: .enabled)
nextDoNotDisturbStart = try values.decodeIfPresent(Int.self, forKey: .nextDoNotDisturbStart)
nextDoNotDisturbEnd = try values.decodeIfPresent(Int.self, forKey: .nextDoNotDisturbEnd)
snoozeEnabled = try values.decodeIfPresent(Bool.self, forKey: .snoozeEnabled)
snoozeEndtime = try values.decodeIfPresent(Int.self, forKey: .snoozeEndtime)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(enabled, forKey: .enabled)
try container.encode(nextDoNotDisturbStart, forKey: .nextDoNotDisturbStart)
try container.encode(nextDoNotDisturbEnd, forKey: .nextDoNotDisturbEnd)
try container.encode(snoozeEnabled, forKey: .snoozeEnabled)
try container.encode(snoozeEndtime, forKey: .snoozeEndtime)
}
}

extension DoNotDisturbStatus.CodingKeys: CodingKey { }
4 changes: 2 additions & 2 deletions SKCore/Sources/Edited.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public struct Edited {
public let ts: String?

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

Expand Down
12 changes: 12 additions & 0 deletions SKCore/Sources/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public extension UInt64 {
}
}

public extension Dictionary where Key == String, Value == Any {
subscript(codingKey: CodingKey) -> Any? {
get {
return self[codingKey.stringValue]
}

set {
self[codingKey.stringValue] = newValue
}
}
}

public func filterNilParameters(_ parameters: [String: Any?]) -> [String: Any] {
var finalParameters = [String: Any]()
for (key, value) in parameters {
Expand Down
4 changes: 2 additions & 2 deletions SKCore/Sources/Reply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public struct Reply {
public let ts: String?

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

Expand Down
55 changes: 47 additions & 8 deletions SKCore/Sources/TeamIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
// THE SOFTWARE.

public struct TeamIcon {
fileprivate enum CodingKeys: String {
case image34 = "image_34"
case image44 = "image_44"
case image68 = "image_68"
case image88 = "image_88"
case image102 = "image_102"
case image132 = "image_132"
case imageOriginal = "image_original"
case imageDefault = "image_default"
}

public var image34: String?
public var image44: String?
public var image68: String?
Expand All @@ -32,13 +43,41 @@ public struct TeamIcon {
public var imageDefault: Bool?

public init(icon: [String: Any]?) {
image34 = icon?["image_34"] as? String
image44 = icon?["image_44"] as? String
image68 = icon?["image_68"] as? String
image88 = icon?["image_88"] as? String
image102 = icon?["image_102"] as? String
image132 = icon?["image_132"] as? String
imageOriginal = icon?["image_original"] as? String
imageDefault = icon?["image_default"] as? Bool
image34 = icon?[CodingKeys.image34] as? String
image44 = icon?[CodingKeys.image44] as? String
image68 = icon?[CodingKeys.image68] as? String
image88 = icon?[CodingKeys.image88] as? String
image102 = icon?[CodingKeys.image102] as? String
image132 = icon?[CodingKeys.image132] as? String
imageOriginal = icon?[CodingKeys.imageOriginal] as? String
imageDefault = icon?[CodingKeys.imageDefault] as? Bool
}
}

extension TeamIcon: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
image34 = try values.decodeIfPresent(String.self, forKey: .image34)
image44 = try values.decodeIfPresent(String.self, forKey: .image44)
image68 = try values.decodeIfPresent(String.self, forKey: .image68)
image88 = try values.decodeIfPresent(String.self, forKey: .image88)
image102 = try values.decodeIfPresent(String.self, forKey: .image102)
image132 = try values.decodeIfPresent(String.self, forKey: .image132)
imageOriginal = try values.decodeIfPresent(String.self, forKey: .imageOriginal)
imageDefault = try values.decodeIfPresent(Bool.self, forKey: .imageDefault)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(image34, forKey: .image34)
try container.encode(image44, forKey: .image44)
try container.encode(image68, forKey: .image68)
try container.encode(image88, forKey: .image88)
try container.encode(image102, forKey: .image102)
try container.encode(image132, forKey: .image132)
try container.encode(imageOriginal, forKey: .imageOriginal)
try container.encode(imageDefault, forKey: .imageDefault)
}
}

extension TeamIcon.CodingKeys: CodingKey { }
Loading