Last active
November 22, 2016 10:12
-
-
Save tarunon/2e318ad9277fa789f12ec45831dd8ed5 to your computer and use it in GitHub Desktop.
Himotoki+Barrel
This file contains hidden or 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
| // | |
| // Extensions.swift | |
| // Barrel_Himotoki | |
| // | |
| // Created by Nobuo Saito on 2016/11/22. | |
| // Copyright © 2016年 line. All rights reserved. | |
| // | |
| import Foundation | |
| import Himotoki | |
| import Barrel | |
| infix operator <- | |
| extension Barrel.KeyPath { | |
| var himotoki: Himotoki.KeyPath { | |
| switch self { | |
| case .self: | |
| return Himotoki.KeyPath([]) | |
| case .keypath(let keyPath): | |
| return Himotoki.KeyPath(keyPath) | |
| case .subKeypath(let keyPath, let parent): | |
| return Himotoki.KeyPath([keyPath] + parent.himotoki.components) | |
| } | |
| } | |
| } | |
| extension Extractor { | |
| func get<T: Decodable>(_ attribute: Attribute<T>) throws -> T { | |
| return try self <| attribute.keyPath.himotoki | |
| } | |
| func get<T: Decodable>(_ attribute: Attribute<T?>) throws -> T? { | |
| return try self <|? attribute.keyPath.himotoki | |
| } | |
| func get<T: Decodable>(_ attribute: Attribute<[T]>) throws -> [T] { | |
| return try self <|| attribute.keyPath.himotoki | |
| } | |
| func get<T: Decodable>(_ attribute: Attribute<[String: T]>) throws -> [String: T] { | |
| return try self <|-| attribute.keyPath.himotoki | |
| } | |
| func get<T: Decodable>(_ attribute: Attribute<[T]?>) throws -> [T]? { | |
| return try self <||? attribute.keyPath.himotoki | |
| } | |
| func get<T: Decodable>(_ attribute: Attribute<[String: T]?>) throws -> [String: T]? { | |
| return try self <|-|? attribute.keyPath.himotoki | |
| } | |
| } | |
| protocol ExpressionDecodable: ExpressionType { | |
| static func decode(_ e: Extractor, _ fields: Attribute<A>) throws -> A | |
| } | |
| extension Decodable where Self: ExpressionDecodable { | |
| static func decode(_ e: Extractor) throws -> A { | |
| return try self.decode(e, Attribute()) | |
| } | |
| } | |
| struct A { | |
| var str: String | |
| var int: Int? | |
| var array: [String] | |
| } | |
| extension A: ExpressionType { | |
| typealias ValueType = A | |
| } | |
| extension AttributeType where ValueType == A { | |
| var str: Attribute<String> { return attribute() } | |
| var int: Attribute<Int?> { return attribute() } | |
| var array: Attribute<[String]> { return attribute() } | |
| } | |
| extension A: ExpressionDecodable { | |
| static func decode(_ e: Extractor, _ fields: Attribute<A>) throws -> A { | |
| return try A.init( | |
| str: e.get(fields.str), | |
| int: e.get(fields.int), | |
| array: e.get(fields.array) | |
| ) | |
| } | |
| } | |
| extension A: Decodable { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment