Skip to content

Commit

Permalink
#1 Add JSEN Custom Initializer to Init From Any?
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerluan committed Jun 13, 2021
2 parents 5236276 + 8b146e8 commit 2fe1e53
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
94 changes: 94 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/JSEN.xcscheme
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "JSEN"
BuildableName = "JSEN"
BlueprintName = "JSEN"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "JSENTests"
BuildableName = "JSENTests"
BlueprintName = "JSENTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO"
parallelizable = "YES"
testExecutionOrdering = "random">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "JSENTests"
BuildableName = "JSENTests"
BlueprintName = "JSENTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "JSEN"
BuildableName = "JSEN"
BlueprintName = "JSEN"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
17 changes: 17 additions & 0 deletions Sources/JSEN.swift
Expand Up @@ -18,6 +18,23 @@ public enum JSEN : Equatable {
/// A null value.
case null

/// Attempts to initialize a JSEN instance from an `Any?` value.
init?(from anyValue: Any?) {
switch anyValue {
case let int as Int: self = .int(int)
case let double as Double: self = .double(double)
case let bool as Bool: self = .bool(bool)
case let string as String: self = .string(string)
case let array as [Any]:
let jsenElements: [JSEN] = array.compactMap { JSEN(from: $0) }
self = .array(jsenElements)
case let dictionary as [String:Any]:
let jsenElements: [String:JSEN] = dictionary.compactMapValues { JSEN(from: $0) }
self = .dictionary(jsenElements)
default: return nil
}
}

/// The extracted value from **self**, or **nil** if **self** is a `.null` case.
public var valueType: Any? {
switch self {
Expand Down
47 changes: 45 additions & 2 deletions Tests/JSENTests.swift
Expand Up @@ -20,7 +20,7 @@ final class JSENTests : XCTestCase {
}

func test_decodeAsType_withValidTypeFromDictionary_shouldDecode() {
let jsen = JSEN.dictionary([ "key" : "value"])
let jsen = JSEN.dictionary(["key" : "value"])
let decoded = jsen.decode(as: Model.self)
XCTAssertEqual(decoded?.key, "value")
}
Expand All @@ -32,7 +32,7 @@ final class JSENTests : XCTestCase {
}

func test_decodeAsType_withInvalidType_shouldReturnNil() {
let jsen = JSEN.dictionary(["wrong_key": "value"])
let jsen = JSEN.dictionary(["wrong_key" : "value"])
let decoded = jsen.decode(as: Model.self, dumpingErrorOnFailure: true)
XCTAssertNil(decoded)
}
Expand All @@ -47,4 +47,47 @@ final class JSENTests : XCTestCase {
XCTAssertEqual(JSEN.dictionary(["my_key" : "my_value"]).description, #"["my_key": "my_value"]"#)
XCTAssertEqual(JSEN.null.description, "null")
}

func test_initializer_withFlatValues_shouldMatchTheExpectedValues() {
let int: Any? = 42
XCTAssertEqual(JSEN(from: int), .int(42))
let double: Any? = 42.42
XCTAssertEqual(JSEN(from: double), .double(42.42))
let string: Any? = "testing"
XCTAssertEqual(JSEN(from: string), .string("testing"))
let bool: Any? = true
XCTAssertEqual(JSEN(from: bool), .bool(true))
let array: Any? = [ 42 ]
XCTAssertEqual(JSEN(from: array), .array([42]))
let dictionary: Any? = [ "key" : 42 ]
XCTAssertEqual(JSEN(from: dictionary), .dictionary(["key" : 42]))
}

func test_initializer_withNestedValues_shouldSucceed() {
let array: Any? = [
42,
"42",
true,
[
24,
"24",
false,
],
]
XCTAssertNotNil(JSEN(from: array))
let dictionary: Any? = [
"key" : 42,
"another" : true,
"nested" : [
"key" : true,
"another" : "yes",
"bool" : false,
"double" : 3.14,
"3rd" : [
"yup" : "it works",
]
]
]
XCTAssertNotNil(JSEN(from: dictionary))
}
}

0 comments on commit 2fe1e53

Please sign in to comment.