This repository has been archived by the owner on Jul 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from felixjendrusch/0.2.0
Swift 1.2
- Loading branch information
Showing
26 changed files
with
669 additions
and
1,295 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
github "LlamaKit/LlamaKit" ~> 0.5 | ||
github "felixjendrusch/ValueTransformer" ~> 2.2 | ||
github "robb/Monocle" ~> 0.0.1 |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
github "Quick/Quick" ~> 0.2 | ||
github "Quick/Nimble" ~> 0.3 | ||
github "Quick/Quick" ~> 0.3 | ||
github "Quick/Nimble" ~> 0.4 |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
github "LlamaKit/LlamaKit" "v0.5.0" | ||
github "Quick/Nimble" "v0.3.1" | ||
github "Quick/Quick" "v0.2.3" | ||
github "robrix/Box" "1.2.1" | ||
github "robb/Monocle" "0.0.1" | ||
github "Quick/Nimble" "v0.4.2" | ||
github "Quick/Quick" "v0.3.1" | ||
github "antitypical/Result" "0.4" | ||
github "felixjendrusch/ValueTransformer" "2.2.0" |
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
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,7 @@ | ||
// Copyright (c) 2015 Felix Jendrusch. All rights reserved. | ||
|
||
import ValueTransformer | ||
|
||
public protocol AdapterType: ReversibleValueTransformerType { | ||
|
||
} |
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,53 @@ | ||
// Copyright (c) 2015 Felix Jendrusch. All rights reserved. | ||
|
||
import Result | ||
import ValueTransformer | ||
import Monocle | ||
|
||
public struct DictionaryAdapter<Key: Hashable, Value, TransformedValue, Error>: AdapterType { | ||
public typealias Specification = [Key: Lens<Result<Value, Error>, Result<TransformedValue, Error>>] | ||
private let specification: Specification | ||
|
||
public typealias DictionaryTransformer = ReversibleValueTransformer<[Key: TransformedValue], TransformedValue, Error> | ||
private let dictionaryTransformer: DictionaryTransformer | ||
|
||
public typealias ValueClosure = TransformedValue -> Result<Value, Error> | ||
private let valueClosure: ValueClosure | ||
|
||
public init(specification: Specification, dictionaryTransformer: DictionaryTransformer, valueClosure: ValueClosure) { | ||
self.specification = specification | ||
self.dictionaryTransformer = dictionaryTransformer | ||
self.valueClosure = valueClosure | ||
} | ||
|
||
public init(specification: Specification, dictionaryTransformer: DictionaryTransformer, @autoclosure(escaping) value: () -> Value) { | ||
self.init(specification: specification, dictionaryTransformer: dictionaryTransformer, valueClosure: { _ in | ||
return Result.success(value()) | ||
}) | ||
} | ||
|
||
public func transform(value: Value) -> Result<TransformedValue, Error> { | ||
return reduce(specification, Result.success([:])) { (result, element) in | ||
let (key, lens) = element | ||
return result.flatMap { (var dictionary) in | ||
return get(lens, Result.success(value)).map { value in | ||
dictionary[key] = value | ||
return dictionary | ||
} | ||
} | ||
}.flatMap { dictionary in | ||
return dictionaryTransformer.transform(dictionary) | ||
} | ||
} | ||
|
||
public func reverseTransform(transformedValue: TransformedValue) -> Result<Value, Error> { | ||
return dictionaryTransformer.reverseTransform(transformedValue).flatMap { dictionary in | ||
return reduce(self.specification, self.valueClosure(transformedValue)) { (result, element) in | ||
let (key, lens) = element | ||
return map(dictionary[key]) { value in | ||
return set(lens, result, Result.success(value)) | ||
} ?? result | ||
} | ||
} | ||
} | ||
} |
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,37 @@ | ||
// Copyright (c) 2015 Felix Jendrusch. All rights reserved. | ||
|
||
import Result | ||
import ValueTransformer | ||
import Monocle | ||
|
||
// MARK: - Lift | ||
|
||
public func lift<A, B, E>(lens: Lens<A, B>) -> Lens<Result<A, E>, Result<B, E>> { | ||
let get: Result<A, E> -> Result<B, E> = { a in | ||
return a.map(Monocle.get(lens)) | ||
} | ||
|
||
let set: (Result<A, E>, Result<B, E>) -> Result<A, E> = { a, b in | ||
return a.flatMap { a in b.map { b in Monocle.set(lens, a, b) } } | ||
} | ||
|
||
return Lens(get: get, set: set) | ||
} | ||
|
||
// MARK: - Map | ||
|
||
public func map<A, V: ReversibleValueTransformerType>(lens: Lens<A, V.ValueType>, reversibleValueTransformer: V) -> Lens<Result<A, V.ErrorType>, Result<V.TransformedValueType, V.ErrorType>> { | ||
return map(lift(lens), reversibleValueTransformer) | ||
} | ||
|
||
public func map<A, V: ReversibleValueTransformerType>(lens: Lens<Result<A, V.ErrorType>, Result<V.ValueType, V.ErrorType>>, reversibleValueTransformer: V) -> Lens<Result<A, V.ErrorType>, Result<V.TransformedValueType, V.ErrorType>> { | ||
let get: Result<A, V.ErrorType> -> Result<V.TransformedValueType, V.ErrorType> = { a in | ||
return Monocle.get(lens, a).flatMap(transform(reversibleValueTransformer)) | ||
} | ||
|
||
let set: (Result<A, V.ErrorType>, Result<V.TransformedValueType, V.ErrorType>) -> Result<A, V.ErrorType> = { a, c in | ||
return Monocle.set(lens, a, c.flatMap(reverseTransform(reversibleValueTransformer))) | ||
} | ||
|
||
return Lens(get: get, set: set) | ||
} |
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,25 @@ | ||
// Copyright (c) 2015 Felix Jendrusch. All rights reserved. | ||
|
||
import Result | ||
|
||
public struct LazyAdapter<A: AdapterType>: AdapterType { | ||
private let adapter: () -> A | ||
|
||
public init(@autoclosure(escaping) adapter: () -> A) { | ||
self.adapter = adapter | ||
} | ||
|
||
public func transform(value: A.ValueType) -> Result<A.TransformedValueType, A.ErrorType> { | ||
return adapter().transform(value) | ||
} | ||
|
||
public func reverseTransform(transformedValue: A.TransformedValueType) -> Result<A.ValueType, A.ErrorType> { | ||
return adapter().reverseTransform(transformedValue) | ||
} | ||
} | ||
|
||
// MARK: - Fix | ||
|
||
public func fix<A: AdapterType>(f: LazyAdapter<A> -> A) -> A { | ||
return f(LazyAdapter(adapter: fix(f))) | ||
} |
Oops, something went wrong.