Skip to content

Commit 8b146e8

Browse files
committed
Add JSEN custom initializer to init from Any?.
1 parent 5236276 commit 8b146e8

File tree

3 files changed

+156
-2
lines changed

3 files changed

+156
-2
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1250"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "JSEN"
18+
BuildableName = "JSEN"
19+
BlueprintName = "JSEN"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "JSENTests"
32+
BuildableName = "JSENTests"
33+
BlueprintName = "JSENTests"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
</BuildActionEntries>
38+
</BuildAction>
39+
<TestAction
40+
buildConfiguration = "Debug"
41+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
42+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
43+
shouldUseLaunchSchemeArgsEnv = "YES"
44+
codeCoverageEnabled = "YES">
45+
<Testables>
46+
<TestableReference
47+
skipped = "NO"
48+
parallelizable = "YES"
49+
testExecutionOrdering = "random">
50+
<BuildableReference
51+
BuildableIdentifier = "primary"
52+
BlueprintIdentifier = "JSENTests"
53+
BuildableName = "JSENTests"
54+
BlueprintName = "JSENTests"
55+
ReferencedContainer = "container:">
56+
</BuildableReference>
57+
</TestableReference>
58+
</Testables>
59+
</TestAction>
60+
<LaunchAction
61+
buildConfiguration = "Debug"
62+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
63+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
64+
launchStyle = "0"
65+
useCustomWorkingDirectory = "NO"
66+
ignoresPersistentStateOnLaunch = "NO"
67+
debugDocumentVersioning = "YES"
68+
debugServiceExtension = "internal"
69+
allowLocationSimulation = "YES">
70+
</LaunchAction>
71+
<ProfileAction
72+
buildConfiguration = "Release"
73+
shouldUseLaunchSchemeArgsEnv = "YES"
74+
savedToolIdentifier = ""
75+
useCustomWorkingDirectory = "NO"
76+
debugDocumentVersioning = "YES">
77+
<MacroExpansion>
78+
<BuildableReference
79+
BuildableIdentifier = "primary"
80+
BlueprintIdentifier = "JSEN"
81+
BuildableName = "JSEN"
82+
BlueprintName = "JSEN"
83+
ReferencedContainer = "container:">
84+
</BuildableReference>
85+
</MacroExpansion>
86+
</ProfileAction>
87+
<AnalyzeAction
88+
buildConfiguration = "Debug">
89+
</AnalyzeAction>
90+
<ArchiveAction
91+
buildConfiguration = "Release"
92+
revealArchiveInOrganizer = "YES">
93+
</ArchiveAction>
94+
</Scheme>

Sources/JSEN.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ public enum JSEN : Equatable {
1818
/// A null value.
1919
case null
2020

21+
/// Attempts to initialize a JSEN instance from an `Any?` value.
22+
init?(from anyValue: Any?) {
23+
switch anyValue {
24+
case let int as Int: self = .int(int)
25+
case let double as Double: self = .double(double)
26+
case let bool as Bool: self = .bool(bool)
27+
case let string as String: self = .string(string)
28+
case let array as [Any]:
29+
let jsenElements: [JSEN] = array.compactMap { JSEN(from: $0) }
30+
self = .array(jsenElements)
31+
case let dictionary as [String:Any]:
32+
let jsenElements: [String:JSEN] = dictionary.compactMapValues { JSEN(from: $0) }
33+
self = .dictionary(jsenElements)
34+
default: return nil
35+
}
36+
}
37+
2138
/// The extracted value from **self**, or **nil** if **self** is a `.null` case.
2239
public var valueType: Any? {
2340
switch self {

Tests/JSENTests.swift

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class JSENTests : XCTestCase {
2020
}
2121

2222
func test_decodeAsType_withValidTypeFromDictionary_shouldDecode() {
23-
let jsen = JSEN.dictionary([ "key" : "value"])
23+
let jsen = JSEN.dictionary(["key" : "value"])
2424
let decoded = jsen.decode(as: Model.self)
2525
XCTAssertEqual(decoded?.key, "value")
2626
}
@@ -32,7 +32,7 @@ final class JSENTests : XCTestCase {
3232
}
3333

3434
func test_decodeAsType_withInvalidType_shouldReturnNil() {
35-
let jsen = JSEN.dictionary(["wrong_key": "value"])
35+
let jsen = JSEN.dictionary(["wrong_key" : "value"])
3636
let decoded = jsen.decode(as: Model.self, dumpingErrorOnFailure: true)
3737
XCTAssertNil(decoded)
3838
}
@@ -47,4 +47,47 @@ final class JSENTests : XCTestCase {
4747
XCTAssertEqual(JSEN.dictionary(["my_key" : "my_value"]).description, #"["my_key": "my_value"]"#)
4848
XCTAssertEqual(JSEN.null.description, "null")
4949
}
50+
51+
func test_initializer_withFlatValues_shouldMatchTheExpectedValues() {
52+
let int: Any? = 42
53+
XCTAssertEqual(JSEN(from: int), .int(42))
54+
let double: Any? = 42.42
55+
XCTAssertEqual(JSEN(from: double), .double(42.42))
56+
let string: Any? = "testing"
57+
XCTAssertEqual(JSEN(from: string), .string("testing"))
58+
let bool: Any? = true
59+
XCTAssertEqual(JSEN(from: bool), .bool(true))
60+
let array: Any? = [ 42 ]
61+
XCTAssertEqual(JSEN(from: array), .array([42]))
62+
let dictionary: Any? = [ "key" : 42 ]
63+
XCTAssertEqual(JSEN(from: dictionary), .dictionary(["key" : 42]))
64+
}
65+
66+
func test_initializer_withNestedValues_shouldSucceed() {
67+
let array: Any? = [
68+
42,
69+
"42",
70+
true,
71+
[
72+
24,
73+
"24",
74+
false,
75+
],
76+
]
77+
XCTAssertNotNil(JSEN(from: array))
78+
let dictionary: Any? = [
79+
"key" : 42,
80+
"another" : true,
81+
"nested" : [
82+
"key" : true,
83+
"another" : "yes",
84+
"bool" : false,
85+
"double" : 3.14,
86+
"3rd" : [
87+
"yup" : "it works",
88+
]
89+
]
90+
]
91+
XCTAssertNotNil(JSEN(from: dictionary))
92+
}
5093
}

0 commit comments

Comments
 (0)