Skip to content

Commit

Permalink
Merge pull request #1059 from r-plus/fix/optional_rawrepresentable_en…
Browse files Browse the repository at this point in the history
…um_for_immutablemappable

Fix: Could not map to optional RawRepresentable enum for ImmutableMappable
  • Loading branch information
tristanhimmelman committed Jun 25, 2019
2 parents ab1f85f + 9e318d0 commit 19d499c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Sources/ImmutableMappable.swift
Expand Up @@ -84,11 +84,21 @@ public extension Map {
return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
}

/// Returns a RawRepresentable type or throws an error.
func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T? {
return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
}

/// Returns a `[RawRepresentable]` type or throws an error.
func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] {
return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
}

/// Returns a `[RawRepresentable]` type or throws an error.
func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T]? {
return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
}

// MARK: BaseMappable

/// Returns a `BaseMappable` object or throws an error.
Expand Down
9 changes: 8 additions & 1 deletion Tests/ObjectMapperTests/ImmutableTests.swift
Expand Up @@ -339,21 +339,28 @@ class ImmutableObjectTests: XCTestCase {
}
}

enum RawRepresentableEnum: String {
case world
}
struct Object: ImmutableMappable {
let immutable: ImmutableObject?
let enumValue: RawRepresentableEnum?
init(map: Map) throws {
self.immutable = try map.value("immutable")
self.enumValue = try map.value("enum")
}
}

let json: [String: Any] = [
"immutable": [
"value": "Hello"
]
],
"enum": "world"
]
do {
let object = try Mapper<Object>().map(JSON: json)
XCTAssertEqual(object.immutable?.value, "Hello")
XCTAssertEqual(object.enumValue, .world)
} catch {
XCTFail()
}
Expand Down

0 comments on commit 19d499c

Please sign in to comment.