Skip to content

Commit

Permalink
Adopt Mapper for Map (#208)
Browse files Browse the repository at this point in the history
* Adopt Mapper for Map

* Fixed inner dependencies
  • Loading branch information
dreymonde authored and paulofaria committed Nov 11, 2016
1 parent 85166f8 commit 534a6e1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 43 deletions.
1 change: 1 addition & 0 deletions Modules/Axis/Sources/Axis/Core.swift
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@_exported import Reflection
@_exported import POSIX
@_exported import Mapper
57 changes: 57 additions & 0 deletions Modules/Axis/Sources/Axis/Map/Map+Mapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extension Map : InMap {

public func get(at indexPath: IndexPathValue) -> Map? {
return try? self.get([indexPath])
}

public func asArray() -> [Map]? {
if case .array(let array) = self {
return array
}
return nil
}

public func get<T>() -> T? {
return self.get()
}

}

extension Map : OutMap {

public static var blank: Map {
return .dictionary([:])
}

public mutating func set(_ map: Map, at indexPath: IndexPathValue) throws {
try self.set(map, for: indexPath)
}

public static func fromArray(_ array: [Map]) -> Map? {
return .array(array)
}

public static func from<T>(_ value: T) -> Map? {
if let representable = value as? MapRepresentable {
return representable.map
}
return nil
}

public static func from(_ int: Int) -> Map? {
return .int(int)
}

public static func from(_ double: Double) -> Map? {
return .double(double)
}

public static func from(_ string: String) -> Map? {
return .string(string)
}

public static func from(_ bool: Bool) -> Map? {
return .bool(bool)
}

}
33 changes: 5 additions & 28 deletions Modules/Axis/Sources/Axis/Map/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,28 +491,17 @@ extension Map {

// MARK: IndexPath

public typealias IndexPath = [IndexPathElement]

extension String {
public func indexPath() -> IndexPath {
public func indexPath() -> [IndexPathValue] {
return self.split(separator: ".").map {
if let index = Int($0) {
return index as IndexPathElement
return .index(index)
}
return $0 as IndexPathElement
return .key($0)
}
}
}

public enum IndexPathValue {
case index(Int)
case key(String)
}

public protocol IndexPathElement {
var indexPathValue: IndexPathValue { get }
}

extension IndexPathElement {
var constructEmptyContainer: Map {
switch indexPathValue {
Expand All @@ -522,18 +511,6 @@ extension IndexPathElement {
}
}

extension Int : IndexPathElement {
public var indexPathValue: IndexPathValue {
return .index(self)
}
}

extension String : IndexPathElement {
public var indexPathValue: IndexPathValue {
return .key(self)
}
}

// MARK: Get

extension Map {
Expand Down Expand Up @@ -562,13 +539,13 @@ extension Map {
return try get(indexPath)
}

public func get(_ indexPath: IndexPath) throws -> Map {
public func get(_ indexPath: [IndexPathElement]) throws -> Map {
var value: Map = self

for element in indexPath {
switch element.indexPathValue {
case .index(let index):
let array = try value.asArray()
let array: [Map] = try value.asArray()
if array.indices.contains(index) {
value = array[index]
} else {
Expand Down
27 changes: 14 additions & 13 deletions Modules/Axis/Tests/AxisTests/Map/MapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try null.asInt())
XCTAssertThrowsError(try null.asString())
XCTAssertThrowsError(try null.asBuffer())
XCTAssertThrowsError(try null.asArray())
XCTAssertThrowsError(try null.asArray(converting: false))
XCTAssertThrowsError(try null.asDictionary())

let nullArrayValue: [Bool]? = nil
Expand All @@ -54,7 +54,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try null.asInt())
XCTAssertThrowsError(try null.asString())
XCTAssertThrowsError(try null.asBuffer())
XCTAssertThrowsError(try null.asArray())
XCTAssertThrowsError(try null.asArray(converting: false))
XCTAssertThrowsError(try null.asDictionary())

let nullArrayOfNullValue: [Bool?]? = nil
Expand All @@ -81,7 +81,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try null.asInt())
XCTAssertThrowsError(try null.asString())
XCTAssertThrowsError(try null.asBuffer())
XCTAssertThrowsError(try null.asArray())
XCTAssertThrowsError(try null.asArray(converting: false))
XCTAssertThrowsError(try null.asDictionary())

let nullDictionaryValue: [String: Bool]? = nil
Expand All @@ -108,7 +108,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try null.asInt())
XCTAssertThrowsError(try null.asString())
XCTAssertThrowsError(try null.asBuffer())
XCTAssertThrowsError(try null.asArray())
XCTAssertThrowsError(try null.asArray(converting: false))
XCTAssertThrowsError(try null.asDictionary())

let nullDictionaryOfNullValue: [String: Bool?]? = nil
Expand All @@ -135,7 +135,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try null.asInt())
XCTAssertThrowsError(try null.asString())
XCTAssertThrowsError(try null.asBuffer())
XCTAssertThrowsError(try null.asArray())
XCTAssertThrowsError(try null.asArray(converting: false))
XCTAssertThrowsError(try null.asDictionary())

let boolValue = true
Expand All @@ -162,7 +162,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try bool.asInt())
XCTAssertThrowsError(try bool.asString())
XCTAssertThrowsError(try bool.asBuffer())
XCTAssertThrowsError(try bool.asArray())
XCTAssertThrowsError(try bool.asArray(converting: false))
XCTAssertThrowsError(try bool.asDictionary())

let doubleValue = 4.20
Expand All @@ -189,7 +189,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try double.asInt())
XCTAssertThrowsError(try double.asString())
XCTAssertThrowsError(try double.asBuffer())
XCTAssertThrowsError(try double.asArray())
XCTAssertThrowsError(try double.asArray(converting: false))
XCTAssertThrowsError(try double.asDictionary())

let intValue = 1969
Expand All @@ -216,7 +216,7 @@ public class MapTests : XCTestCase {
XCTAssertEqual(try int.asInt(), intValue)
XCTAssertThrowsError(try null.asString())
XCTAssertThrowsError(try null.asBuffer())
XCTAssertThrowsError(try null.asArray())
XCTAssertThrowsError(try null.asArray(converting: false))
XCTAssertThrowsError(try null.asDictionary())

let stringValue = "foo"
Expand All @@ -243,7 +243,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try string.asInt())
XCTAssertEqual(try string.asString(), stringValue)
XCTAssertThrowsError(try string.asBuffer())
XCTAssertThrowsError(try string.asArray())
XCTAssertThrowsError(try string.asArray(converting: false))
XCTAssertThrowsError(try string.asDictionary())

let bufferValue = Buffer("foo")
Expand All @@ -269,7 +269,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try buffer.asInt())
XCTAssertThrowsError(try buffer.asString())
XCTAssertEqual(try buffer.asBuffer(), bufferValue)
XCTAssertThrowsError(try buffer.asArray())
XCTAssertThrowsError(try buffer.asArray(converting: false))
XCTAssertThrowsError(try buffer.asDictionary())

let arrayValue = 1969
Expand Down Expand Up @@ -393,7 +393,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try dictionary.asInt())
XCTAssertThrowsError(try dictionary.asString())
XCTAssertThrowsError(try dictionary.asBuffer())
XCTAssertThrowsError(try dictionary.asArray())
XCTAssertThrowsError(try dictionary.asArray(converting: false))
XCTAssertEqual(try dictionary.asDictionary(), ["foo": .int(dictionaryValue)])

let dictionaryOfOptionalValue: Int? = dictionaryValue
Expand Down Expand Up @@ -424,7 +424,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try dictionaryOfOptional.asInt())
XCTAssertThrowsError(try dictionaryOfOptional.asString())
XCTAssertThrowsError(try dictionaryOfOptional.asBuffer())
XCTAssertThrowsError(try dictionaryOfOptional.asArray())
XCTAssertThrowsError(try dictionaryOfOptional.asArray(converting: false))
XCTAssertEqual(try dictionaryOfOptional.asDictionary(), ["foo": .int(dictionaryValue)])

let dictionaryOfNullValue: Int? = nil
Expand Down Expand Up @@ -455,7 +455,7 @@ public class MapTests : XCTestCase {
XCTAssertThrowsError(try dictionaryOfNull.asInt())
XCTAssertThrowsError(try dictionaryOfNull.asString())
XCTAssertThrowsError(try dictionaryOfNull.asBuffer())
XCTAssertThrowsError(try dictionaryOfNull.asArray())
XCTAssertThrowsError(try dictionaryOfNull.asArray(converting: false))
XCTAssertEqual(try dictionaryOfNull.asDictionary(), ["foo": .null])
}

Expand Down Expand Up @@ -800,6 +800,7 @@ public class MapTests : XCTestCase {
let fuuDictionaryB: [String: Baz] = [:]
XCTAssertEqual(try fuuDictionaryB.asMap(), [:])
}

}

extension MapTests {
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ let package = Package(
targets: [
Target(name: "POSIX"),
Target(name: "Reflection"),
Target(name: "Axis", dependencies: ["Reflection", "POSIX"]),
Target(name: "Axis", dependencies: ["Reflection", "POSIX", "Mapper"]),
Target(name: "OpenSSL", dependencies: ["Axis"]),
Target(name: "HTTP", dependencies: ["Axis"]),

Target(name: "Mapper"),
Target(name: "Venice", dependencies: ["Axis"]),
Target(name: "IP", dependencies: ["Axis"]),
Target(name: "TCP", dependencies: ["IP", "OpenSSL"]),
Expand Down

0 comments on commit 534a6e1

Please sign in to comment.