-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
EKeyValue
, EOptional
and comments-reading
- Loading branch information
Showing
9 changed files
with
269 additions
and
9 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
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,30 @@ | ||
import Mustache | ||
|
||
struct EKeyValue { | ||
let key: EString | ||
let value: EString | ||
|
||
init(key: EString, value: EString) { | ||
self.key = key | ||
self.value = value | ||
} | ||
} | ||
|
||
extension EKeyValue: CustomStringConvertible { | ||
var description: String { | ||
"(key: \(key), value: \(value))" | ||
} | ||
} | ||
|
||
extension EKeyValue: MustacheTransformable { | ||
func transform(_ name: String) -> Any? { | ||
switch name { | ||
case "key": | ||
return self.key | ||
case "value": | ||
return self.value | ||
default: | ||
return nil | ||
} | ||
} | ||
} |
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,90 @@ | ||
import Mustache | ||
|
||
enum EOptional<Wrapped> { | ||
case none | ||
case some(Wrapped) | ||
|
||
init(_ optional: Optional<Wrapped>) { | ||
switch optional { | ||
case .none: | ||
self = .none | ||
case let .some(value): | ||
self = .some(value) | ||
} | ||
} | ||
|
||
func toOptional() -> Optional<Wrapped> { | ||
switch self { | ||
case .none: | ||
return .none | ||
case let .some(value): | ||
return .some(value) | ||
} | ||
} | ||
|
||
func map<U>(_ transform: (Wrapped) throws -> U) rethrows -> EOptional<U> { | ||
switch self { | ||
case .none: | ||
return .none | ||
case .some(let wrapped): | ||
return .some( | ||
try transform(wrapped) | ||
) | ||
} | ||
} | ||
|
||
func flatMap<U>(_ transform: (Wrapped) throws -> U?) rethrows -> EOptional<U> { | ||
switch self { | ||
case .none: | ||
return .none | ||
case .some(let wrapped): | ||
let transformed = try transform(wrapped) | ||
switch transformed { | ||
case let .some(value): | ||
return .some(value) | ||
case .none: | ||
return .none | ||
} | ||
} | ||
} | ||
|
||
static func ?? (lhs: Self, rhs: Wrapped) -> Wrapped { | ||
switch lhs { | ||
case .none: | ||
return rhs | ||
case .some(let wrapped): | ||
return wrapped | ||
} | ||
} | ||
} | ||
|
||
extension EOptional: CustomStringConvertible { | ||
var description: String { | ||
switch self { | ||
case .none: | ||
return "" | ||
case .some(let wrapped): | ||
return String(describing: wrapped) | ||
} | ||
} | ||
} | ||
|
||
extension EOptional: MustacheTransformable { | ||
func transform(_ name: String) -> Any? { | ||
switch self { | ||
case .none: | ||
switch name { | ||
case "bool": | ||
return false | ||
default: | ||
return nil | ||
} | ||
case let .some(value): | ||
if let value = value as? MustacheTransformable { | ||
return value.transform(name) | ||
} else { | ||
return nil | ||
} | ||
} | ||
} | ||
} |
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
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