-
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.
- Loading branch information
Showing
12 changed files
with
531 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,27 @@ | ||
name: CI | ||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
strategy: | ||
matrix: | ||
os: [macos-latest, ubuntu-latest] | ||
runs-on: ${{matrix.os}} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Tests | ||
run: swift test --enable-code-coverage | ||
- name: codecov | ||
run: | | ||
os=$(echo "${{ matrix.os }}" | cut -f1 -d"-") | ||
if [ "$os" = "ubuntu" ]; then | ||
sudo ln -s /usr/lib/llvm-9/bin/llvm-cov /usr/local/bin/llvm-cov | ||
object=".build/debug/ComposePackageTests.xctest" | ||
elif [ "$os" = "macos" ]; then | ||
sudo ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/llvm-cov /usr/local/bin/llvm-cov | ||
object=".build/debug/ComposePackageTests.xctest/Contents/MacOS/ComposePackageTests" | ||
fi | ||
llvm-cov export -format="lcov" --object "$object" -instr-profile .build/debug/codecov/default.profdata > info.lcov | ||
bash <(curl https://codecov.io/bash) -cF "$os" |
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,141 @@ | ||
|
||
# Created by https://www.gitignore.io/api/xcode,macos,swift,swiftpm,swiftpackagemanager | ||
# Edit at https://www.gitignore.io/?templates=xcode,macos,swift,swiftpm,swiftpackagemanager | ||
|
||
### macOS ### | ||
# General | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
### Swift ### | ||
# Xcode | ||
# | ||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
||
## Build generated | ||
build/ | ||
DerivedData/ | ||
|
||
## Various settings | ||
*.pbxuser | ||
!default.pbxuser | ||
*.mode1v3 | ||
!default.mode1v3 | ||
*.mode2v3 | ||
!default.mode2v3 | ||
*.perspectivev3 | ||
!default.perspectivev3 | ||
xcuserdata/ | ||
|
||
## Other | ||
*.moved-aside | ||
*.xccheckout | ||
*.xcscmblueprint | ||
|
||
## Obj-C/Swift specific | ||
*.hmap | ||
*.ipa | ||
*.dSYM.zip | ||
*.dSYM | ||
|
||
## Playgrounds | ||
timeline.xctimeline | ||
playground.xcworkspace | ||
|
||
# Swift Package Manager | ||
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. | ||
# Packages/ | ||
# Package.pins | ||
# Package.resolved | ||
.build/ | ||
# Add this line if you want to avoid checking in Xcode SPM integration. | ||
.swiftpm/xcode | ||
|
||
# CocoaPods | ||
# We recommend against adding the Pods directory to your .gitignore. However | ||
# you should judge for yourself, the pros and cons are mentioned at: | ||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||
# Pods/ | ||
# Add this line if you want to avoid checking in source code from the Xcode workspace | ||
# *.xcworkspace | ||
|
||
# Carthage | ||
# Add this line if you want to avoid checking in source code from Carthage dependencies. | ||
# Carthage/Checkouts | ||
|
||
Carthage/Build | ||
|
||
# Accio dependency management | ||
Dependencies/ | ||
.accio/ | ||
|
||
# fastlane | ||
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the | ||
# screenshots whenever they are needed. | ||
# For more information about the recommended setup visit: | ||
# https://docs.fastlane.tools/best-practices/source-control/#source-control | ||
|
||
fastlane/report.xml | ||
fastlane/Preview.html | ||
fastlane/screenshots/**/*.png | ||
fastlane/test_output | ||
|
||
# Code Injection | ||
# After new code Injection tools there's a generated folder /iOSInjectionProject | ||
# https://github.com/johnno1962/injectionforxcode | ||
|
||
iOSInjectionProject/ | ||
|
||
### SwiftPackageManager ### | ||
Packages | ||
xcuserdata | ||
*.xcodeproj | ||
|
||
|
||
### SwiftPM ### | ||
|
||
|
||
### Xcode ### | ||
# Xcode | ||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
||
## User settings | ||
|
||
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) | ||
|
||
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) | ||
|
||
## Xcode Patch | ||
*.xcodeproj/* | ||
!*.xcodeproj/project.pbxproj | ||
!*.xcodeproj/xcshareddata/ | ||
!*.xcworkspace/contents.xcworkspacedata | ||
/*.gcno | ||
|
||
### Xcode Patch ### | ||
**/xcshareddata/WorkspaceSettings.xcsettings | ||
|
||
# End of https://www.gitignore.io/api/xcode,macos,swift,swiftpm,swiftpackagemanager |
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 Andrés Cecilia Luque | ||
|
||
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,21 @@ | ||
// swift-tools-version:5.0 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Compose", | ||
products: [ | ||
.library(name: "Compose", targets: ["Compose"]), | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target( | ||
name: "Compose", | ||
dependencies: [] | ||
), | ||
.testTarget( | ||
name: "ComposeTests", | ||
dependencies: ["Compose"] | ||
), | ||
] | ||
) |
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,47 @@ | ||
# Compose | ||
|
||
[![CI](https://github.com/acecilia/Compose/workflows/CI/badge.svg?branch=master)](https://github.com/acecilia/Compose/actions) | ||
[![CI](https://codecov.io/gh/acecilia/Compose/branch/master/graph/badge.svg)](https://codecov.io/github/acecilia/Compose) | ||
|
||
## What is this? | ||
|
||
A Swift library for composing structs from other structs, which relies on the `KeyPath` and `@dynamicMemberLookup` features to provide a clean and typesafe API: | ||
|
||
```swift | ||
import Compose | ||
|
||
struct Developer: Codable, Hashable { | ||
var name: String | ||
var age: Int | ||
} | ||
|
||
struct RemoteLocation: Codable, Hashable { | ||
var country: String | ||
var city: String | ||
} | ||
|
||
typealias RemoteDeveloper = Compose<Developer, RemoteLocation> | ||
|
||
let remoteDeveloper = RemoteDeveloper( | ||
.init(name: "Andres", age: 26), | ||
.init(country: "Spain", city: "Madrid") | ||
) | ||
print(remoteDeveloper.name) // Andres | ||
print(remoteDeveloper.city) // Madrid | ||
``` | ||
|
||
For an in depth explanation please [follow this link](https://www.sforswift.com/posts/composition-using-keypath-and-dynamic-member-lookup/). | ||
|
||
## Installation | ||
|
||
### Swift Package Manager | ||
|
||
Add the following to the dependencies inside your `Package.swift` file: | ||
|
||
```swift | ||
.package(url: "https://github.com/acecilia/Compose.git", .upToNextMajor(from: "0.0.1")), | ||
``` | ||
|
||
## License | ||
|
||
Compose is licensed under the MIT license. See [LICENSE](LICENSE) for more info. |
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,76 @@ | ||
import Foundation | ||
|
||
@dynamicMemberLookup | ||
public struct Compose<Element1, Element2> { | ||
public var element1: Element1 | ||
public var element2: Element2 | ||
|
||
public subscript<T>(dynamicMember keyPath: WritableKeyPath<Element1, T>) -> T { | ||
get { element1[keyPath: keyPath] } | ||
set { element1[keyPath: keyPath] = newValue } | ||
} | ||
|
||
public subscript<T>(dynamicMember keyPath: WritableKeyPath<Element2, T>) -> T { | ||
get { element2[keyPath: keyPath] } | ||
set { element2[keyPath: keyPath] = newValue } | ||
} | ||
|
||
public init(_ element1: Element1, _ element2: Element2) { | ||
self.element1 = element1 | ||
self.element2 = element2 | ||
} | ||
} | ||
|
||
// MARK - Conformances | ||
|
||
extension Compose: Encodable where Element1: Encodable, Element2: Encodable { | ||
public func encode(to encoder: Encoder) throws { | ||
try element1.encode(to: encoder) | ||
try element2.encode(to: encoder) | ||
} | ||
} | ||
|
||
extension Compose: Decodable where Element1: Decodable, Element2: Decodable { | ||
public init(from decoder: Decoder) throws { | ||
self.element1 = try Element1(from: decoder) | ||
self.element2 = try Element2(from: decoder) | ||
} | ||
} | ||
|
||
extension Compose: Equatable where Element1: Equatable, Element2: Equatable { } | ||
|
||
extension Compose: Hashable where Element1: Hashable, Element2: Hashable { } | ||
|
||
extension Compose: Error where Element1: Error, Element2: Error { } | ||
|
||
extension Compose: LocalizedError where Element1: LocalizedError, Element2: LocalizedError { | ||
public var errorDescription: String? { | ||
[element1.errorDescription, element2.errorDescription].filterAndJoin() | ||
} | ||
public var failureReason: String? { | ||
[element1.failureReason, element2.failureReason].filterAndJoin() | ||
} | ||
public var recoverySuggestion: String? { | ||
[element1.recoverySuggestion, element2.recoverySuggestion].filterAndJoin() | ||
} | ||
public var helpAnchor: String? { | ||
[element1.helpAnchor, element2.helpAnchor].filterAndJoin() | ||
} | ||
} | ||
|
||
private extension Array where Element == String? { | ||
func filterAndJoin() -> String { | ||
return compactMap { $0 }.joined(separator: "\n") | ||
} | ||
} | ||
|
||
// MARK - typealiases for composition with multiple types | ||
|
||
public typealias Compose3<T1, T2, T3> = | ||
Compose<T1, Compose<T2, T3>> | ||
|
||
public typealias Compose4<T1, T2, T3, T4> = | ||
Compose<Compose<T1, T2>, Compose<T3, T4>> | ||
|
||
public typealias Compose5<T1, T2, T3, T4, T5> = | ||
Compose<Compose<T1, T2>, Compose<T3, Compose<T4, T5>>> |
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,47 @@ | ||
import XCTest | ||
import Foundation | ||
import Compose | ||
|
||
final class ComposeMultipleTests: XCTestCase { | ||
func testGetSet() { | ||
let expectedName = "Kike" | ||
let expectedCity = "Segovia" | ||
let expectedTeam = "Android" | ||
|
||
var mutable = remoteTeamLead | ||
mutable.name = expectedName | ||
mutable.city = expectedCity | ||
mutable.team = expectedTeam | ||
|
||
XCTAssertEqual(mutable.name, expectedName) | ||
XCTAssertEqual(mutable.city, expectedCity) | ||
XCTAssertEqual(mutable.team, expectedTeam) | ||
} | ||
|
||
func testCodable() throws { | ||
guard #available(OSX 10.13, *) else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
let remoteTeamLeadJson = """ | ||
{ | ||
"age" : 26, | ||
"city" : "Madrid", | ||
"country" : "Spain", | ||
"name" : "Andres", | ||
"salary" : 1000000, | ||
"team" : "iOS" | ||
} | ||
""" | ||
|
||
let decoder = JSONDecoder() | ||
let remoteDeveloper = try decoder.decode(RemoteTeamLead.self, from: Data(remoteTeamLeadJson.utf8)) | ||
|
||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys] | ||
let data = try encoder.encode(remoteDeveloper) | ||
|
||
XCTAssertEqual(remoteTeamLeadJson, String(data: data, encoding: .utf8)) | ||
} | ||
} |
Oops, something went wrong.