-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
db3b5b5
commit d36e5fd
Showing
3 changed files
with
152 additions
and
1 deletion.
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,47 @@ | ||
// | ||
// BridgesRow.swift | ||
// Bridges | ||
// | ||
// Created by Mihael Isaev on 18.05.2020. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol BridgesRow { | ||
func decode<D>(model type: D.Type) throws -> D where D: Decodable | ||
func decode<D>(model type: D.Type, prefix: String?) throws -> D where D: Decodable | ||
} | ||
|
||
extension BridgesRow { | ||
public func decode<D>(model type: D.Type) throws -> D where D : Decodable { | ||
try decode(model: type, prefix: nil) | ||
} | ||
} | ||
|
||
public protocol BridgesRows { | ||
var rows: [BridgesRow] { get } | ||
} | ||
|
||
extension BridgesRows { | ||
public func first<R>(as type: R.Type) throws -> R? where R: Decodable { | ||
try rows.first?.decode(model: type) | ||
} | ||
|
||
public func all<R>(as type: R.Type) throws -> [R] where R: Decodable { | ||
try rows.map { try $0.decode(model: type) } | ||
} | ||
} | ||
|
||
extension EventLoopFuture where Value: BridgesRows { | ||
public func first<R>(decoding type: R.Type) -> EventLoopFuture<R?> where R: Decodable { | ||
flatMapThrowing { try $0.first(as: type) } | ||
} | ||
|
||
public func all<R>(decoding type: R.Type) -> EventLoopFuture<[R]> where R: Decodable { | ||
flatMapThrowing { try $0.all(as: type) } | ||
} | ||
} | ||
|
||
extension Array: BridgesRows where Element: BridgesRow { | ||
public var rows: [BridgesRow] { self } | ||
} |
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