-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8e2e98e
Showing
14 changed files
with
749 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Maxim Krouk | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,67 @@ | ||
// swift-tools-version:5.3 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "swift-declarative-configuration", | ||
products: [ | ||
.library( | ||
name: "DeclarativeConfiguration", | ||
targets: ["DeclarativeConfiguration"] | ||
), | ||
.library( | ||
name: "FunctionalBuilder", | ||
targets: ["FunctionalBuilder"] | ||
), | ||
.library( | ||
name: "FunctionalConfigurator", | ||
targets: ["FunctionalConfigurator"] | ||
), | ||
.library( | ||
name: "FunctionalKeyPath", | ||
targets: ["FunctionalKeyPath"] | ||
), | ||
.library( | ||
name: "FunctionalModification", | ||
targets: ["FunctionalModification"] | ||
), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/pointfreeco/swift-case-paths.git", from: "0.1.2") | ||
], | ||
targets: [ | ||
.target( | ||
name: "DeclarativeConfiguration", | ||
dependencies: [ | ||
.target(name: "FunctionalBuilder"), | ||
.target(name: "FunctionalConfigurator"), | ||
.target(name: "FunctionalKeyPath"), | ||
.target(name: "FunctionalModification"), | ||
.product(name: "CasePaths", package: "swift-case-paths") | ||
] | ||
), | ||
.target( | ||
name: "FunctionalBuilder", | ||
dependencies: [ | ||
.target(name: "FunctionalConfigurator"), | ||
.target(name: "FunctionalKeyPath"), | ||
.target(name: "FunctionalModification") | ||
] | ||
), | ||
.target( | ||
name: "FunctionalConfigurator", | ||
dependencies: [ | ||
.target(name: "FunctionalKeyPath"), | ||
.target(name: "FunctionalModification") | ||
] | ||
), | ||
.target(name: "FunctionalKeyPath"), | ||
.target(name: "FunctionalModification"), | ||
.testTarget( | ||
name: "FunctionalConfigurationTests", | ||
dependencies: [ | ||
.target(name: "FunctionalBuilder") | ||
] | ||
), | ||
] | ||
) |
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,136 @@ | ||
# Swift Declarative Configuration | ||
|
||
[![Swift 5.3](https://img.shields.io/badge/swift-5.3-ED523F.svg?style=flat)](https://swift.org/download/) [![SwiftPM](https://img.shields.io/badge/SwiftPM-ED523F.svg?style=flat)](https://swift.org/package-manager/) [![@maximkrouk](https://img.shields.io/badge/[email protected]?style=flat)](https://twitter.com/maximkrouk) | ||
|
||
Swift Declarative Configuration (SDC, for short) is a tiny library, that enables you to configure your objects in a declarative, consistent and understandable way, with ergonomics in mind. It can be used to configure any objects on any platform, including server-side-swift. | ||
|
||
## Products | ||
|
||
- **[FunctionalModification](./Sources/FunctionalModification)** | ||
|
||
Provides modification functions for copying and modifying immutable stuff. It is useful for self-configuring objects like builder, when modificating methods should return modified `self` | ||
|
||
- **[FunctionalKeyPath](./Sources/FunctionalKeyPath)** & **[CasePaths](https://github.com/pointfreeco/swift-case-paths)** | ||
|
||
KeyPath functional wrappers, one is generalized and the other is for enums. [CasePath is a dependency](https://github.com/pointfreeco/swift-case-paths). | ||
|
||
- **[FunctionalConfigurator](./Sources/FunctionalConfigurator)** | ||
|
||
Funtional configurator for anything, enables you to specify modification of an object and to apply the modification later. | ||
|
||
- **[FunctionalBuilder](./Sources/FunctionalBuilder)** | ||
|
||
Functional builder for anything, enables you to modify object instances in a declarative way. Also contains BuilderProvider protocol with a computed `builder` property and implements that protocol on NSObject type. | ||
|
||
- **[DeclarativeConfiguration](./Sources/DeclarativeConfiguration)** | ||
|
||
Wraps and exports all the products. | ||
|
||
## Basic Usage | ||
|
||
### UIKit & FunctionalConfigurator | ||
|
||
Maybe it worth to make another abstraction over configurator for UI setup, but for example I'll be using pure version. | ||
|
||
```swift | ||
import FunctionalConfigurator | ||
|
||
class ImageViewController: UIViewController { | ||
enum StyleSheet { | ||
static let imageView = Configurator<UIImageView> | ||
.contentMode(.scaleAspectFit) | ||
.backgroundColor(.black) | ||
.layer.masksToBounds(true) | ||
.layer.cornerRadius(10) | ||
} | ||
|
||
let imageView: UIImageView = .init() | ||
|
||
override func loadView() { | ||
self.view = imageView | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
StyleSheet.imageView.configure(imageView) | ||
} | ||
} | ||
``` | ||
|
||
### UIKit & FunctionalBuilder | ||
```swift | ||
import FunctionalBuilder | ||
|
||
class ImageViewController: UIViewController { | ||
let imageView = UIImageView().builder | ||
.contentMode(.scaleAspectFit) | ||
.backgroundColor(.black) | ||
.layer.masksToBounds(true) | ||
.layer.cornerRadius(10) | ||
.build() | ||
|
||
override func loadView() { | ||
self.view = imageView | ||
} | ||
} | ||
``` | ||
|
||
### Modification | ||
|
||
```swift | ||
import FunctionalModification | ||
|
||
struct MyModel { | ||
var value1 = 0 | ||
init() {} | ||
} | ||
|
||
let model_0 = MyModel() | ||
let model_1 = modification(of: model_0) { $0.value = 1 } | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
@discardableResult | ||
func cornerRadius(_ value: CGFloat) -> Self { | ||
modification(of: self) { view in | ||
view.layer.cornerRadius = value | ||
view.layer.masksToBounds = true | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
### Basic | ||
|
||
You can add DeclarativeConfiguration to an Xcode project by adding it as a package dependency. | ||
|
||
1. From the **File** menu, select **Swift Packages › Add Package Dependency…** | ||
2. Enter "https://github.com/makeupstudio/swift-declarative-configuration" into the package repository URL text field | ||
3. Choose products you need to link them to your project. | ||
|
||
### Recommended | ||
|
||
If you use SwiftPM for your project, you can add DeclarativeConfiguration to your package file. Also my advice will be to use SSH. | ||
|
||
```swift | ||
.package( | ||
url: "[email protected]:makeupstudio/swift-declarative-configuration.git", | ||
from: "0.0.1" | ||
) | ||
``` | ||
|
||
Do not forget about target dependencies: | ||
|
||
```swift | ||
.product( | ||
name: "DeclarativeConfiguration", | ||
package: "swift-declarative-configuration" | ||
) | ||
``` | ||
|
||
## License | ||
|
||
This library is released under the MIT license. See [LICENSE](./LICENSE) for details. |
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,5 @@ | ||
@_exported import FunctionalBuilder | ||
@_exported import FunctionalConfigurator | ||
@_exported import FunctionalKeyPath | ||
@_exported import FunctionalModification | ||
@_exported import CasePaths |
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,126 @@ | ||
import FunctionalConfigurator | ||
import FunctionalKeyPath | ||
|
||
@dynamicMemberLookup | ||
public struct Builder<Base> { | ||
private var _initialValue: () -> Base | ||
private var _configurator: Configurator<Base> | ||
|
||
public func build() -> Base { _configurator.configure(_initialValue()) } | ||
|
||
@inlinable | ||
public func apply() where Base: AnyObject { _ = build() } | ||
|
||
@inlinable | ||
public func reinforce(_ transform: @escaping (inout Base) -> Void) -> Builder { | ||
Builder(build()).set(transform) | ||
} | ||
|
||
public init(_ initialValue: @escaping @autoclosure () -> Base) { | ||
self.init( | ||
initialValue, | ||
Configurator<Base>() | ||
) | ||
} | ||
|
||
private init( | ||
_ initialValue: @escaping () -> Base, | ||
_ configurator: Configurator<Base> | ||
) { | ||
_initialValue = initialValue | ||
_configurator = configurator | ||
} | ||
|
||
public func set( | ||
_ transform: @escaping (inout Base) -> Void | ||
) -> Builder { | ||
Builder( | ||
_initialValue, | ||
_configurator.set(transform) | ||
) | ||
} | ||
|
||
public subscript<Value>( | ||
dynamicMember keyPath: WritableKeyPath<Base, Value> | ||
) -> CallableBlock<Value> { | ||
.init( | ||
builder: self, | ||
keyPath: .init(keyPath) | ||
) | ||
} | ||
|
||
public subscript<Value>( | ||
dynamicMember keyPath: KeyPath<Base, Value> | ||
) -> NonCallableBlock<Value> where Base: AnyObject, Value: AnyObject { | ||
.init( | ||
builder: self, | ||
keyPath: .getonly(keyPath) | ||
) | ||
} | ||
|
||
} | ||
|
||
extension Builder { | ||
@dynamicMemberLookup | ||
public struct CallableBlock<Value> { | ||
private var _block: NonCallableBlock<Value> | ||
|
||
init( | ||
builder: Builder, | ||
keyPath: FunctionalKeyPath<Base, Value> | ||
) { | ||
self._block = .init( | ||
builder: builder, | ||
keyPath: keyPath | ||
) | ||
} | ||
|
||
public func callAsFunction(_ value: @escaping @autoclosure () -> Value) -> Builder { | ||
Builder( | ||
_block.builder._initialValue, | ||
_block.builder._configurator.appendingConfiguration { base in | ||
_block.keyPath.embed(value(), in: base) | ||
} | ||
) | ||
} | ||
|
||
public subscript<LocalValue>( | ||
dynamicMember keyPath: WritableKeyPath<Value, LocalValue> | ||
) -> CallableBlock<LocalValue> { | ||
.init( | ||
builder: _block.builder, | ||
keyPath: _block.keyPath.appending(path: .init(keyPath)) | ||
) | ||
} | ||
|
||
public subscript<LocalValue>( | ||
dynamicMember keyPath: KeyPath<Value, LocalValue> | ||
) -> NonCallableBlock<LocalValue> where Value: AnyObject, LocalValue: AnyObject { | ||
_block[dynamicMember: keyPath] | ||
} | ||
} | ||
|
||
@dynamicMemberLookup | ||
public struct NonCallableBlock<Value> { | ||
var builder: Builder | ||
var keyPath: FunctionalKeyPath<Base, Value> | ||
|
||
public subscript<LocalValue>( | ||
dynamicMember keyPath: WritableKeyPath<Value, LocalValue> | ||
) -> CallableBlock<LocalValue> where Value: AnyObject { | ||
.init( | ||
builder: self.builder, | ||
keyPath: self.keyPath.appending(path: .init(keyPath)) | ||
) | ||
} | ||
|
||
public subscript<LocalValue>( | ||
dynamicMember keyPath: KeyPath<Value, LocalValue> | ||
) -> NonCallableBlock<LocalValue> where Value: AnyObject, LocalValue: AnyObject { | ||
.init( | ||
builder: self.builder, | ||
keyPath: self.keyPath.appending(path: .getonly(keyPath)) | ||
) | ||
} | ||
} | ||
} |
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,8 @@ | ||
import Foundation | ||
|
||
public protocol BuilderProvider {} | ||
extension BuilderProvider { | ||
public var builder: Builder<Self> { .init(self) } | ||
} | ||
|
||
extension NSObject: BuilderProvider {} |
Oops, something went wrong.