Skip to content

Commit

Permalink
Merge pull request #17 from ra1028/feature/reimpl_handlers
Browse files Browse the repository at this point in the history
Reimplement value/error handler
  • Loading branch information
ra1028 authored Oct 10, 2016
2 parents 7fa268b + a536fd4 commit 7117ebb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,22 @@ let message: String = try j.distil("number_of_apples", as: Int.self)
.catch { error in "Anything not found... | Error: \(error)" }
```

Alembic enable you to receive a value with closure.
```Swift
let jsonObject = ["user": ["name": "Robert Downey, Jr."]]
let j = JSON(jsonObject)
```
```Swift
j.distil(["user", "name"], to: String.self)
.map { name in "User name is \(name)" }
.value { message in
print(message) // "Robert Downey, Jr."
}
.error { error in
print(error) // NOP
}
```

### Error handling
Alembic has powerfull error handling designs as following.
It will be help your fail-safe coding.
Expand Down
22 changes: 22 additions & 0 deletions Sources/InsecureDistillate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ public extension InsecureDistillate {
return try thunk()
}

@discardableResult
func value(_ handler: (Value) -> Void) -> InsecureDistillate<Value> {
do {
let v = try value()
handler(v)
return .init { v }
} catch let e {
return .init { throw e }
}
}

@discardableResult
func error(_ handler: (Error) -> Void) -> InsecureDistillate<Value> {
do {
let v = try value()
return .init { v }
} catch let e {
handler(e)
return .init { throw e }
}
}

func to(_: Value.Type) throws -> Value {
return try value()
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/SecureDistillate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public extension SecureDistillate {
return thunk()
}

@discardableResult
func value(_ handler: (Value) -> Void) -> SecureDistillate<Value> {
let v = thunk()
handler(v)
return .init { v }
}

func to(_: Value.Type) -> Value {
return value()
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/AlembicTests/DistilTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ class DistilTest: XCTestCase {
}
}

func testDistilHandler() {
let j = JSON(object)

j.distil(["user", "name"], as: String.self)
.value { XCTAssertEqual($0, "ra1028") }
.error { XCTFail("\($0)") }

j.option("null", as: (String?).self)
.value { XCTAssertEqual($0, nil) }
.error { XCTFail("\($0)") }

j.distil(["user", "name"], as: String.self)
.map { s -> String in "User name is " + s }
.value { XCTAssertEqual($0, "User name is ra1028") }
.error { XCTFail("\($0)") }
.filter { _ in false }
.value { _ in XCTFail("Expect the error to occur") }
.error {
if case let DistillError.filteredValue(type, value) = $0 {
XCTAssertNotNil(type as? String.Type)
XCTAssertEqual(value as? String, "User name is ra1028")
return
}
XCTFail("\($0)")
}
}

func testDistillError() {
let j = JSON(object)

Expand Down Expand Up @@ -133,6 +160,7 @@ extension DistilTest {
return [
("testDistil", testDistil),
("testDistilSubscript", testDistilSubscript),
("testDistilHandler", testDistilHandler),
("testDistillError", testDistillError),
("testClassMapping", testClassMapping),
("testStructMapping", testStructMapping),
Expand Down
1 change: 1 addition & 0 deletions Tests/AlembicTests/TestJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let distilTestJSONObject: [String: Any] = [
"double": 77.7,
"float": 77.7 as Float,
"bool": true,
"null": NSNull(),
"array": [
"A", "B", "C"
],
Expand Down

0 comments on commit 7117ebb

Please sign in to comment.