From 9e318d069a85716779b840b4974883eedd2159b9 Mon Sep 17 00:00:00 2001 From: r-plus Date: Mon, 10 Jun 2019 14:35:14 +0900 Subject: [PATCH] fix: Could not map to optional RawRepresentable enum for ImmutableMappable --- Sources/ImmutableMappable.swift | 10 ++++++++++ Tests/ObjectMapperTests/ImmutableTests.swift | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/ImmutableMappable.swift b/Sources/ImmutableMappable.swift index 6b8fb083..1f678245 100644 --- a/Sources/ImmutableMappable.swift +++ b/Sources/ImmutableMappable.swift @@ -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(_ 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(_ 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(_ 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. diff --git a/Tests/ObjectMapperTests/ImmutableTests.swift b/Tests/ObjectMapperTests/ImmutableTests.swift index 9a13661b..bafba916 100644 --- a/Tests/ObjectMapperTests/ImmutableTests.swift +++ b/Tests/ObjectMapperTests/ImmutableTests.swift @@ -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().map(JSON: json) XCTAssertEqual(object.immutable?.value, "Hello") + XCTAssertEqual(object.enumValue, .world) } catch { XCTFail() }