Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better equality for AnyCodable collections #101

Merged
merged 2 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions Sources/AnyCodable/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public struct AnyCodable {
public let value: Any

public init<T>(_ value: T?) {
self.value = value ?? ()
if let dictionary = value as? [String: Any] {
self.value = dictionary.mapValues(AnyCodable.init) as [AnyHashable: AnyCodable]
} else if let array = value as? [Any] {
self.value = array.map(AnyCodable.init)
} else {
self.value = value ?? ()
}
}
}

Expand Down Expand Up @@ -89,9 +95,9 @@ extension AnyCodable: Decodable {
} else if let string = try? container.decode(String.self) {
self.init(string)
} else if let array = try? container.decode([AnyCodable].self) {
self.init(array.map { $0.value })
self.init(array)
} else if let dictionary = try? container.decode([String: AnyCodable].self) {
self.init(dictionary.mapValues { $0.value })
self.init(dictionary)
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Value cannot be decoded")
}
Expand Down Expand Up @@ -132,7 +138,7 @@ extension AnyCodable: Equatable {
return lhs == rhs
case let (lhs as String, rhs as String):
return lhs == rhs
case let (lhs as [String: AnyCodable], rhs as [String: AnyCodable]):
case let (lhs as [AnyHashable: AnyCodable], rhs as [AnyHashable: AnyCodable]):
return lhs == rhs
case let (lhs as [AnyCodable], rhs as [AnyCodable]):
return lhs == rhs
Expand Down Expand Up @@ -165,3 +171,41 @@ extension AnyCodable: CustomDebugStringConvertible {
}
}
}

extension AnyCodable: ExpressibleByNilLiteral {}
extension AnyCodable: ExpressibleByBooleanLiteral {}
extension AnyCodable: ExpressibleByIntegerLiteral {}
extension AnyCodable: ExpressibleByFloatLiteral {}
extension AnyCodable: ExpressibleByExtendedGraphemeClusterLiteral {}
extension AnyCodable: ExpressibleByStringLiteral {}
extension AnyCodable: ExpressibleByArrayLiteral {}
extension AnyCodable: ExpressibleByDictionaryLiteral {}
public extension AnyCodable {
init(nilLiteral _: ()) {
self.init(nil as Any?)
}

init(booleanLiteral value: Bool) {
self.init(value)
}

init(integerLiteral value: Int) {
self.init(value)
}

init(floatLiteral value: Double) {
self.init(value)
}

init(stringLiteral value: String) {
self.init(value)
}

init(arrayLiteral elements: Any...) {
self.init(elements)
}

init(dictionaryLiteral elements: (AnyHashable, Any)...) {
self.init([AnyHashable: Any](elements, uniquingKeysWith: { first, _ in first }))
}
}
38 changes: 31 additions & 7 deletions Tests/AnyCodableTests/AnyCodable+EquatableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,39 @@ class AnyCodableEquatableTests: XCTestCase {
/// Can all simple types be compared?
func testEquality() throws {
// Given
let array = [1, 2, 3].map(AnyCodable.init)
let dictionary = ["a": 1, "b": 2, "c": 3].mapValues(AnyCodable.init)
let values = [nil, true,
1 as Int, 2 as Int8, 3 as Int16, 4 as Int32, 5 as Int64,
1 as UInt, 2 as UInt8, 3 as UInt16, 4 as UInt32, 5 as UInt64,
3.14159265358979323846 as Float, 3.14159265358979323846 as Double,
"string", array, dictionary].map(AnyCodable.init)
let array: AnyCodable = [1, 2, 3]
let dictionary: AnyCodable = ["a": 1, "b": 2, "c": 3]
let values: [AnyCodable] = [
nil,
true,
AnyCodable(1 as Int),
AnyCodable(2 as Int8),
AnyCodable(3 as Int16),
AnyCodable(4 as Int32),
AnyCodable(5 as Int64),
AnyCodable(1 as UInt),
AnyCodable(2 as UInt8),
AnyCodable(3 as UInt16),
AnyCodable(4 as UInt32),
AnyCodable(5 as UInt64),
AnyCodable(3.14159265358979323846 as Float),
AnyCodable(3.14159265358979323846 as Double),
"string",
array,
dictionary
]
// Then
values.forEach { XCTAssertEqual($0, $0) }
XCTAssertNotEqual(values[3], values[10])
}

/// Can all nested dictionary and arrays be compared?
func testNestedEquality() throws {
// Given
let dictionaryLiteral: AnyCodable = ["userInfo": ["things": ["nothing", "something", "anything"]],
"userInfo": 42]
let dictionary = AnyCodable(["userInfo": ["things": ["nothing", "something", "anything"]]])
// Then
XCTAssertEqual(dictionaryLiteral, dictionary)
}
}