Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit c5125c3

Browse files
committed
more options
1 parent 1a8e73f commit c5125c3

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// MoreOptions.swift
3+
//
4+
//
5+
// Created by Joseph Hinkle on 9/19/21.
6+
//
7+
8+
import TypeWrapper
9+
10+
//
11+
// implement for simple type (Bool) with more options
12+
//
13+
extension TypeWrapper {
14+
func boolMoreOptions(_ options: _BoolExtraOptions) throws -> AnyWithTypeWrapper {
15+
try self.send(options, as: {
16+
($0 as? _Bool)?.onReceive(input:)
17+
})
18+
}
19+
}
20+
protocol _Bool {
21+
func onReceive(input: Any) throws -> AnyWithTypeWrapper
22+
}
23+
struct _BoolExtraOptions {
24+
let someBool: Any
25+
let otherBool: Any
26+
let op: String
27+
}
28+
extension AttemptIfConformsStruct: _Bool where Wrapped == Bool {
29+
public func onReceive(input: Any) throws -> AnyWithTypeWrapper {
30+
let options = input as! _BoolExtraOptions
31+
if let someBool: Bool = options.someBool as? Bool,
32+
let otherBool: Bool = options.otherBool as? Bool {
33+
if options.op == "||" {
34+
let result = someBool || otherBool
35+
return addTypeWrapper(result)
36+
} else if options.op == "&&" {
37+
let result = someBool && otherBool
38+
return addTypeWrapper(result)
39+
} else {
40+
fatalError("bad op")
41+
}
42+
} else {
43+
fatalError("bad data")
44+
}
45+
}
46+
}

Tests/TypeWrapperTests/TypeWrapperTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,20 @@ final class TypeWrapperTests: XCTestCase {
113113
XCTFail()
114114
} catch {}
115115
}
116+
func testMoreOptionsExample() throws {
117+
let bool1 = false
118+
let bool2 = true
119+
120+
let and = bool1 && bool2
121+
let or = bool1 || bool2
122+
123+
let anyBool1: AnyWithTypeWrapper = addTypeWrapper(bool1)
124+
let anyBool2: AnyWithTypeWrapper = addTypeWrapper(bool2)
125+
126+
let andResult = try anyBool1.typeWrapper.boolMoreOptions(_BoolExtraOptions(someBool: anyBool1.any, otherBool: anyBool2.any, op: "&&"))
127+
let orResult = try anyBool1.typeWrapper.boolMoreOptions(_BoolExtraOptions(someBool: anyBool1.any, otherBool: anyBool2.any, op: "||"))
128+
129+
XCTAssertEqual("\(and)", "\(andResult.any)")
130+
XCTAssertEqual("\(or)", "\(orResult.any)")
131+
}
116132
}

0 commit comments

Comments
 (0)