-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update README.md * change package name to SwiftBuilder * 📦 rename inline document * 🧾 SwiftBuilder now have an associate readme * ⚒ codecov xcode scheme name update * 🦄 update readme badge icon
- Loading branch information
Showing
11 changed files
with
146 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ jobs: | |
- name: Generate xcode project | ||
run: swift package generate-xcodeproj --enable-code-coverage | ||
- name: Generate xcode test report | ||
run: xcodebuild -scheme FluentInterface-Package clean test | ||
run: xcodebuild -scheme SwiftBuilder-Package clean test | ||
- name: Codecov | ||
uses: codecov/[email protected] | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/// [SwiftBuilder] Assign poperty within a wrapper | ||
/// ## before | ||
/// ```swift | ||
/// let point: CGPoint = { | ||
/// var point = CGPoint() | ||
/// point.x = 1 | ||
/// return point | ||
/// }() | ||
/// ``` | ||
/// ## after | ||
/// ```swift | ||
/// let point = CGPoint()+ | ||
/// .x(1)- | ||
/// ``` | ||
/// resource https://www.appcoda.com.tw/fluent-interface/ | ||
@dynamicMemberLookup public struct Builder<Subject> { | ||
/// [SwiftBuilder] create a Builder for Subject | ||
/// - Parameter subject: The object that was created | ||
@inlinable | ||
public init(_ subject: Subject) { | ||
self.subject = subject | ||
} | ||
|
||
// typealias FISetter<Value> = ((Value) -> Builder<Subject>) | ||
|
||
/// the Subject than builder build for | ||
public let subject: Subject | ||
|
||
// 因為要動到 subject 的屬性,所以 keyPath 的型別必須是 WritableKeyPath | ||
// 回傳值是一個 Setter 方法 | ||
@inlinable | ||
public subscript<Value>(dynamicMember keyPath: WritableKeyPath<Subject, Value>) | ||
-> FISetter<Subject, Value> { | ||
|
||
// var subject = self.subject | ||
|
||
return FISetter(target: subject, keyPath: keyPath) | ||
} | ||
|
||
/// [SwiftBuilder] get the subject. | ||
@inlinable | ||
public func build() -> Subject { | ||
subject | ||
} | ||
/// [SwiftBuilder] Quick way to touch subject and remain fluent interface | ||
/// - Parameter handlel: A cloure to get the subject | ||
@inlinable | ||
public nonmutating func handlingSubject(_ handle: | ||
(Subject) -> Void) -> Self { | ||
handle(subject) | ||
return self | ||
} | ||
/// [SwiftBuilder] Quick way to manipulate subject and remain fluent interface | ||
/// - Parameter handle: A cloure to inout set subject | ||
@inlinable | ||
public nonmutating func manipulateSubjct(_ handle: | ||
(inout Subject) -> Void) -> Self { | ||
var subject = self.subject | ||
handle(&subject) | ||
return Builder(subject) | ||
} | ||
/// Returns a new builder, mapping subject value using the given | ||
/// transformation. | ||
/// | ||
/// Use this method when you need to transform the value of a `Builder` | ||
/// instance when it represents a object. The following example transforms | ||
/// the integer value to a string: | ||
/// | ||
/// func getBuilder() -> Builder<Int> { /* ... */ } | ||
/// | ||
/// let integerBuilder = getNextBuilder() | ||
/// // integerBuilder.subject == 5 | ||
/// let stringBuilder = integerBuilder.map({ String($0) }) | ||
/// // stringBuilder.subject == "5" | ||
/// | ||
/// - Parameter transform: A closure that takes the success value of this | ||
/// instance. | ||
/// - Returns: A `Result` instance with the result of evaluating `transform` | ||
/// as the new success value if this instance represents a success. | ||
@inlinable | ||
public func map<T>(_ transformer: (Subject) throws -> T) rethrows -> Builder<T> { | ||
return try Builder<T>(transformer(self.subject)) | ||
} | ||
} | ||
|
||
public struct FISetter<Target,Value> { | ||
@inlinable | ||
internal init(target: Target, keyPath: WritableKeyPath<Target, Value>) { | ||
self.target = target | ||
self.keyPath = keyPath | ||
} | ||
|
||
public let target:Target | ||
public let keyPath: WritableKeyPath<Target,Value> | ||
nonmutating | ||
public func callAsFunction(_ newValue:Value) -> Builder<Target> { | ||
var newTarget = target | ||
newTarget[keyPath: keyPath] = newValue | ||
return Builder(newTarget) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
17 changes: 5 additions & 12 deletions
17
...tInterfaceTests/CustomOperatorTests.swift → ...sts/SwiftBuilderCustomOperatorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.