Skip to content

Commit f6814e5

Browse files
committed
Added ResponseType extension and type check when decoding SiteSettings
1 parent ee78bd0 commit f6814e5

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

Networking/Networking/Model/SiteSetting.swift

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,47 @@ public struct SiteSetting: Decodable, Equatable, GeneratedFakeable, GeneratedCop
3636
let settingID = try container.decode(String.self, forKey: .settingID)
3737
let label = try container.decodeIfPresent(String.self, forKey: .label) ?? ""
3838
let settingDescription = try container.decodeIfPresent(String.self, forKey: .settingDescription) ?? ""
39-
40-
// Note: `value` is a mixed type per the documentation — usually a String but could be an Array, Int, etc
41-
// For the specific settings we are interested in, it is a String type.
42-
// See: https://woocommerce.github.io/woocommerce-rest-api-docs/#setting-options for more details.
39+
let responseType = try container.decodeIfPresent(ResponseType.self, forKey: .type) ?? .unknown
4340
var value = ""
44-
if let stringValue = try? container.decode(String.self, forKey: .value) {
45-
value = stringValue
46-
} else {
47-
DDLogWarn("⚠️ Could not successfully decode SiteSetting value for \(settingID)")
41+
if responseType.isSupported {
42+
if let stringValue = try? container.decode(String.self, forKey: .value) {
43+
value = stringValue
44+
} else {
45+
DDLogWarn("⚠️ Could not successfully decode SiteSetting value for \(settingID)")
46+
}
4847
}
49-
5048
self.init(siteID: siteID, settingID: settingID, label: label, settingDescription: settingDescription, value: value, settingGroupKey: settingGroupKey)
5149
}
5250
}
51+
/// Defines all of the response types of SiteSettings options
52+
/// See: https://woocommerce.github.io/woocommerce-rest-api-docs/#setting-option-properties
53+
///
54+
private extension SiteSetting {
55+
enum ResponseType: String, Codable {
56+
case text
57+
case email
58+
case number
59+
case color
60+
case password
61+
case textarea
62+
case select
63+
case multiselect
64+
case radio
65+
case imageWidth = "image_width"
66+
case checkbox
67+
// For types not contemplated by the API
68+
case unknown
5369

70+
var isSupported: Bool {
71+
switch self {
72+
case .multiselect:
73+
return false
74+
default:
75+
return true
76+
}
77+
}
78+
}
79+
}
5480

5581
/// Defines all of the SiteSetting CodingKeys.
5682
///
@@ -61,6 +87,7 @@ private extension SiteSetting {
6187
case label = "label"
6288
case settingDescription = "description"
6389
case value = "value"
90+
case type = "type"
6491
}
6592
}
6693

0 commit comments

Comments
 (0)