Skip to content

Commit

Permalink
Merge pull request #11 from ikesyo/not-only-operators
Browse files Browse the repository at this point in the history
Expose extraction methods in Extractor as public APIs
  • Loading branch information
ikesyo committed May 21, 2015
2 parents a14943f + 0649c20 commit d2818fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 15 additions & 3 deletions Himotoki/Extractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,29 @@ public struct Extractor {
}
}

internal func value<T: Decodable where T.DecodedType == T>(keyPath: String) -> T? {
public func value<T: Decodable where T.DecodedType == T>(keyPath: String) -> Optional<T> {
return rawValue(keyPath).flatMap(decode)
}

internal func array<T: Decodable where T.DecodedType == T>(keyPath: String) -> [T]? {
public func valueOptional<T: Decodable where T.DecodedType == T>(keyPath: String) -> Optional<T?> {
return Optional(value(keyPath))
}

public func array<T: Decodable where T.DecodedType == T>(keyPath: String) -> Optional<[T]> {
return rawValue(keyPath).flatMap(decode)
}

internal func dictionary<T: Decodable where T.DecodedType == T>(keyPath: String) -> [String: T]? {
public func arrayOptional<T: Decodable where T.DecodedType == T>(keyPath: String) -> Optional<[T]?> {
return Optional(array(keyPath))
}

public func dictionary<T: Decodable where T.DecodedType == T>(keyPath: String) -> Optional<[String: T]> {
return rawValue(keyPath).flatMap(decode)
}

public func dictionaryOptional<T: Decodable where T.DecodedType == T>(keyPath: String) -> Optional<[String: T]?> {
return Optional(dictionary(keyPath))
}
}

// Implement it as a tail recursive function.
Expand Down
6 changes: 3 additions & 3 deletions Himotoki/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ public func <| <T: Decodable where T.DecodedType == T>(e: Extractor, keyPath: St
}

public func <|? <T: Decodable where T.DecodedType == T>(e: Extractor, keyPath: String) -> Optional<T?> {
return Optional(e <| keyPath)
return e.valueOptional(keyPath)
}

public func <|| <T: Decodable where T.DecodedType == T>(e: Extractor, keyPath: String) -> Optional<[T]> {
return e.array(keyPath)
}

public func <||? <T: Decodable where T.DecodedType == T>(e: Extractor, keyPath: String) -> Optional<[T]?> {
return Optional(e <|| keyPath)
return e.arrayOptional(keyPath)
}

public func <|-| <T: Decodable where T.DecodedType == T>(e: Extractor, keyPath: String) -> Optional<[String: T]> {
return e.dictionary(keyPath)
}

public func <|-|? <T: Decodable where T.DecodedType == T>(e: Extractor, keyPath: String) -> Optional<[String: T]?> {
return Optional(e <|-| keyPath)
return e.dictionaryOptional(keyPath)
}

0 comments on commit d2818fe

Please sign in to comment.