Skip to content

Commit 2bb077b

Browse files
authored
refact(getValue): Simplifying getValue by default nil value for jsonPath. (#339)
1 parent 4ccb419 commit 2bb077b

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

Sources/Optimizely/OptimizelyJSON.swift

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,31 @@ public class OptimizelyJSON: NSObject {
7171
///
7272
/// If JSON Data is {"k1":true, "k2":{"k3":"v3"}}
7373
///
74-
/// Set jsonPath to "k2" to access {"k3":"v3"} or set it to "k2.k3" to access "v3"
75-
/// Set it to nil or empty to access the entire JSON data.
74+
/// Set jsonPath to "k2" to access {"k3":"v3"} or set it to "k2.k3" to access "v3".
75+
/// Set it to nil or empty to access the entire JSON data. See more examples below:
76+
///
77+
///
78+
/// struct Student: Decodable {
79+
/// let name: String
80+
/// let age: Int
81+
/// let address: Address
82+
/// }
83+
///
84+
/// struct Address: Decodable {
85+
/// let state: String
86+
/// let emails: [String]
87+
/// }
88+
///
89+
/// let student: Student? = optimizelyJSON.getValue(jsonPath: nil)
90+
/// let address: Address? = optimizelyJSON.getValue(jsonPath: "address")
91+
/// let name: String? = optimizelyJSON.getValue(jsonPath: "name")
92+
/// let emails: [String]? = optimizelyJSON.getValue(jsonPath: "address.emails")
93+
///
7694
///
7795
/// - Parameters:
7896
/// - jsonPath: Key path for the value.
7997
/// - Returns: Value if decoded successfully
80-
public func getValue<T: Decodable>(jsonPath: String? = nil) -> T? {
98+
public func getValue<T: Decodable>(jsonPath: String?) -> T? {
8199
func handler(value: Any) -> T? {
82100
guard JSONSerialization.isValidJSONObject(value) else {
83101
// Try and typecast value to required return type
@@ -102,8 +120,26 @@ public class OptimizelyJSON: NSObject {
102120
///
103121
/// If JSON Data is {"k1":true, "k2":{"k3":"v3"}}
104122
///
105-
/// Set jsonPath to "k2" to access {"k3":"v3"} or set it to "k2.k3" to access "v3"
106-
/// Set it to nil or empty to access the entire JSON data.
123+
/// Set jsonPath to "k2" to access {"k3":"v3"} or set it to "k2.k3" to access "v3".
124+
/// Set it to nil or empty to access the entire JSON data. See more examples below:
125+
///
126+
///
127+
/// struct Student: Decodable {
128+
/// let name: String
129+
/// let age: Int
130+
/// let address: Address
131+
/// }
132+
///
133+
/// struct Address: Decodable {
134+
/// let state: String
135+
/// let emails: [String]
136+
/// }
137+
///
138+
/// let student: Student? = optimizelyJSON.getValue(jsonPath: nil)
139+
/// let address: Address? = optimizelyJSON.getValue(jsonPath: "address")
140+
/// let name: String? = optimizelyJSON.getValue(jsonPath: "name")
141+
/// let emails: [String]? = optimizelyJSON.getValue(jsonPath: "address.emails")
142+
///
107143
///
108144
/// - Parameters:
109145
/// - jsonPath: Key path for the value.

Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyJSON.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ extension OptimizelyClientTests_OptimizelyJSON {
280280
}
281281

282282
func testGetValueForNilJSONKeyAndInvalidType() {
283-
let value: Int? = self.optimizelyJSON.getValue()
283+
let value: Int? = self.optimizelyJSON.getValue(jsonPath: nil)
284284
XCTAssertNil(value)
285285
}
286286

@@ -290,7 +290,7 @@ extension OptimizelyClientTests_OptimizelyJSON {
290290
}
291291

292292
func testGetValueForNilJSONKeyAndEmptyDecodableStruct() {
293-
let value: EmptyDecodableStruct? = self.optimizelyJSON.getValue()
293+
let value: EmptyDecodableStruct? = self.optimizelyJSON.getValue(jsonPath: nil)
294294
XCTAssertNotNil(value)
295295
}
296296

@@ -314,7 +314,7 @@ extension OptimizelyClientTests_OptimizelyJSON {
314314
}
315315

316316
func testGetValueForNilJsonKeyAndValidDecodableStruct() {
317-
let value: ValidDecodableStruct? = self.optimizelyJSON.getValue()
317+
let value: ValidDecodableStruct? = self.optimizelyJSON.getValue(jsonPath: nil)
318318
XCTAssertNotNil(value)
319319

320320
let expectedStruct = ValidDecodableStruct(

0 commit comments

Comments
 (0)