Skip to content

Commit

Permalink
API refactor and rename to Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dreymonde committed Apr 14, 2016
2 parents 9947314 + 3d2ef01 commit 8ccbbd1
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 185 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ xcuserdata
*.xcuserstate
*.xcscmblueprint
XcodeDevelopment/
*.DS_Store
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PackageDescription

let package = Package(
name: "StructuredDataMapper",
name: "Mapper",
dependencies: [
.Package(url: "https://github.com/Zewo/InterchangeData.git", majorVersion: 0, minor: 4)
]
Expand Down
8 changes: 5 additions & 3 deletions Sources/Mappable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@
@_exported import StructuredData

public protocol Mappable: StructuredDataInitializable {
init(map: Mapper) throws

init(mapper: Mapper) throws

}

extension Mappable {
public init(structuredData: StructuredData) throws {
try self.init(map: structuredData.mapper)
try self.init(mapper: structuredData.mapper)
}
}

extension Mappable {
public static func makeWith(structuredData structuredData: StructuredData) -> Self? {
do {
return try self.init(map: structuredData.mapper)
return try self.init(mapper: structuredData.mapper)
} catch {
return nil
}
Expand Down
83 changes: 25 additions & 58 deletions Sources/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class Mapper {
public enum Error: ErrorProtocol {
case cantInitFromRawValue
case noInterchangeData(key: String)
case incompatibleSequence
}

public init(structuredData: StructuredData) {
Expand All @@ -41,19 +42,19 @@ public final class Mapper {
// MARK: - General case

extension Mapper {
public func from<T>(key: String) throws -> T {
public func map<T>(from key: String) throws -> T {
let value: T = try structuredData.get(key)
return value
}

public func from<T: StructuredDataInitializable>(key: String) throws -> T {
public func map<T: StructuredDataInitializable>(from key: String) throws -> T {
if let nested = structuredData[key] {
return try unwrap(T(structuredData: nested))
}
throw Error.noInterchangeData(key: key)
}

public func from<T: RawRepresentable where T.RawValue: StructuredDataInitializable>(key: String) throws -> T {
public func map<T: RawRepresentable where T.RawValue: StructuredDataInitializable>(from key: String) throws -> T {
guard let rawValue = try structuredData[key].flatMap({ try T.RawValue(structuredData: $0) }) else {
throw Error.cantInitFromRawValue
}
Expand All @@ -67,52 +68,43 @@ extension Mapper {
// MARK: - Arrays

extension Mapper {
public func arrayFrom<T>(key: String) throws -> [T] {
return try structuredData.flatMapThrough(key) {
do {
let some: T = try $0.get()
return some
} catch {
return nil
}
}

public func map<T>(arrayFrom key: String) throws -> [T] {
return try structuredData.flatMapThrough(key) { try $0.get() as T }
}

public func arrayFrom<T: StructuredDataInitializable>(key: String) throws -> [T] {
public func map<T where T: StructuredDataInitializable>(arrayFrom key: String) throws -> [T] {
return try structuredData.flatMapThrough(key) { try? T(structuredData: $0) }
}

public func arrayFrom<T: RawRepresentable where T.RawValue: StructuredDataInitializable>(key: String) throws -> [T] {
public func map<T: RawRepresentable where
T.RawValue: StructuredDataInitializable>(arrayFrom key: String) throws -> [T] {
return try structuredData.flatMapThrough(key) {
do {
let rawValue = try T.RawValue(structuredData: $0)
return T(rawValue: rawValue)
} catch {
return nil
}
return (try? T.RawValue(structuredData: $0)).flatMap({ T(rawValue: $0) })
}
}
}

// MARK: - Optionals

extension Mapper {
public func optionalFrom<T>(key: String) -> T? {

public func map<T>(optionalFrom key: String) -> T? {
do {
return try from(key)
return try map(from: key)
} catch {
return nil
}
}

public func optionalFrom<T: StructuredDataInitializable>(key: String) -> T? {
public func map<T: StructuredDataInitializable>(optionalFrom key: String) -> T? {
if let nested = structuredData[key] {
return try? T(structuredData: nested)
}
return nil
}

public func optionalFrom<T: RawRepresentable where T.RawValue: StructuredDataInitializable>(key: String) -> T? {
public func map<T: RawRepresentable where T.RawValue: StructuredDataInitializable>(optionalFrom key: String) -> T? {
do {
if let rawValue = try structuredData[key].flatMap({ try T.RawValue(structuredData: $0) }),
value = T(rawValue: rawValue) {
Expand All @@ -128,44 +120,19 @@ extension Mapper {
// MARK: - Optional arrays

extension Mapper {
public func optionalArrayFrom<T>(key: String) -> [T]? {
do {
let inter: [StructuredData] = try structuredData.get(key)
return inter.flatMap {
do {
let some: T = try $0.get()
return some
} catch {
return nil
}
}
} catch {
return nil
}

public func map<T>(optionalArrayFrom key: String) -> [T]? {
return try? structuredData.flatMapThrough(key) { try $0.get() as T }
}

public func optionalArrayFrom<T: StructuredDataInitializable>(key: String) -> [T]? {
do {
let inter: [StructuredData] = try structuredData.get(key)
return inter.flatMap({ try? T(structuredData: $0) })
} catch {
return nil
}
public func map<T where T: StructuredDataInitializable>(optionalArrayFrom key: String) -> [T]? {
return try? structuredData.flatMapThrough(key) { try? T(structuredData: $0) }
}

public func optionalArrayFrom<T: RawRepresentable where T.RawValue: StructuredDataInitializable>(key: String) -> [T]? {
do {
let inter: [StructuredData] = try structuredData.get(key)
return inter.flatMap {
do {
let rawValue = try T.RawValue(structuredData: $0)
return T(rawValue: rawValue)
} catch {
return nil
}
}
} catch {
return nil
public func map<T: RawRepresentable where
T.RawValue: StructuredDataInitializable>(optionalArrayFrom key: String) -> [T]? {
return try? structuredData.flatMapThrough(key) {
return (try? T.RawValue(structuredData: $0)).flatMap({ T(rawValue: $0) })
}
}
}
Expand Down
Loading

0 comments on commit 8ccbbd1

Please sign in to comment.