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

Change == behaviours to use hashValue #60

Closed
Closed
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
47 changes: 4 additions & 43 deletions Sources/AnyCodable/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,6 @@

extension AnyCodable: _AnyEncodable, _AnyDecodable {}

extension AnyCodable: Equatable {
public static func == (lhs: AnyCodable, rhs: AnyCodable) -> Bool {
switch (lhs.value, rhs.value) {
case is (Void, Void):
return true
case let (lhs as Bool, rhs as Bool):
return lhs == rhs
case let (lhs as Int, rhs as Int):
return lhs == rhs
case let (lhs as Int8, rhs as Int8):
return lhs == rhs
case let (lhs as Int16, rhs as Int16):
return lhs == rhs
case let (lhs as Int32, rhs as Int32):
return lhs == rhs
case let (lhs as Int64, rhs as Int64):
return lhs == rhs
case let (lhs as UInt, rhs as UInt):
return lhs == rhs
case let (lhs as UInt8, rhs as UInt8):
return lhs == rhs
case let (lhs as UInt16, rhs as UInt16):
return lhs == rhs
case let (lhs as UInt32, rhs as UInt32):
return lhs == rhs
case let (lhs as UInt64, rhs as UInt64):
return lhs == rhs
case let (lhs as Float, rhs as Float):
return lhs == rhs
case let (lhs as Double, rhs as Double):
return lhs == rhs
case let (lhs as String, rhs as String):
return lhs == rhs
case let (lhs as [String: AnyCodable], rhs as [String: AnyCodable]):
return lhs == rhs
case let (lhs as [AnyCodable], rhs as [AnyCodable]):
return lhs == rhs
default:
return false
}
}
}

extension AnyCodable: CustomStringConvertible {
public var description: String {
switch value {
Expand Down Expand Up @@ -136,4 +93,8 @@ extension AnyCodable: Hashable {
break
}
}

public static func ==(lhs: AnyCodable, rhs: AnyCodable) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
49 changes: 4 additions & 45 deletions Sources/AnyCodable/AnyDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,51 +77,6 @@ extension _AnyDecodable {
}
}

extension AnyDecodable: Equatable {
public static func == (lhs: AnyDecodable, rhs: AnyDecodable) -> Bool {
switch (lhs.value, rhs.value) {
#if canImport(Foundation)
case is (NSNull, NSNull), is (Void, Void):
return true
#endif
case let (lhs as Bool, rhs as Bool):
return lhs == rhs
case let (lhs as Int, rhs as Int):
return lhs == rhs
case let (lhs as Int8, rhs as Int8):
return lhs == rhs
case let (lhs as Int16, rhs as Int16):
return lhs == rhs
case let (lhs as Int32, rhs as Int32):
return lhs == rhs
case let (lhs as Int64, rhs as Int64):
return lhs == rhs
case let (lhs as UInt, rhs as UInt):
return lhs == rhs
case let (lhs as UInt8, rhs as UInt8):
return lhs == rhs
case let (lhs as UInt16, rhs as UInt16):
return lhs == rhs
case let (lhs as UInt32, rhs as UInt32):
return lhs == rhs
case let (lhs as UInt64, rhs as UInt64):
return lhs == rhs
case let (lhs as Float, rhs as Float):
return lhs == rhs
case let (lhs as Double, rhs as Double):
return lhs == rhs
case let (lhs as String, rhs as String):
return lhs == rhs
case let (lhs as [String: AnyDecodable], rhs as [String: AnyDecodable]):
return lhs == rhs
case let (lhs as [AnyDecodable], rhs as [AnyDecodable]):
return lhs == rhs
default:
return false
}
}
}

extension AnyDecodable: CustomStringConvertible {
public var description: String {
switch value {
Expand Down Expand Up @@ -185,4 +140,8 @@ extension AnyDecodable: Hashable {
break
}
}

public static func ==(lhs: AnyDecodable, rhs: AnyDecodable) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
47 changes: 4 additions & 43 deletions Sources/AnyCodable/AnyEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,49 +137,6 @@ extension _AnyEncodable {
#endif
}

extension AnyEncodable: Equatable {
public static func == (lhs: AnyEncodable, rhs: AnyEncodable) -> Bool {
switch (lhs.value, rhs.value) {
case is (Void, Void):
return true
case let (lhs as Bool, rhs as Bool):
return lhs == rhs
case let (lhs as Int, rhs as Int):
return lhs == rhs
case let (lhs as Int8, rhs as Int8):
return lhs == rhs
case let (lhs as Int16, rhs as Int16):
return lhs == rhs
case let (lhs as Int32, rhs as Int32):
return lhs == rhs
case let (lhs as Int64, rhs as Int64):
return lhs == rhs
case let (lhs as UInt, rhs as UInt):
return lhs == rhs
case let (lhs as UInt8, rhs as UInt8):
return lhs == rhs
case let (lhs as UInt16, rhs as UInt16):
return lhs == rhs
case let (lhs as UInt32, rhs as UInt32):
return lhs == rhs
case let (lhs as UInt64, rhs as UInt64):
return lhs == rhs
case let (lhs as Float, rhs as Float):
return lhs == rhs
case let (lhs as Double, rhs as Double):
return lhs == rhs
case let (lhs as String, rhs as String):
return lhs == rhs
case let (lhs as [String: AnyEncodable], rhs as [String: AnyEncodable]):
return lhs == rhs
case let (lhs as [AnyEncodable], rhs as [AnyEncodable]):
return lhs == rhs
default:
return false
}
}
}

extension AnyEncodable: CustomStringConvertible {
public var description: String {
switch value {
Expand Down Expand Up @@ -286,4 +243,8 @@ extension AnyEncodable: Hashable {
break
}
}

public static func ==(lhs: AnyEncodable, rhs: AnyEncodable) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
22 changes: 22 additions & 0 deletions Tests/AnyCodableTests/AnyCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
import XCTest

class AnyCodableTests: XCTestCase {

func testEquivalency() {
let boolean = true
let integer = 42
let double = 3.141592653589793
let string = "string"
let array = [1, 2, 3]
let nested = [
"a": "alpha",
"b": "bravo",
"c": "charlie",
]
XCTAssertEqual(AnyCodable(boolean), AnyCodable(boolean))
XCTAssertEqual(AnyCodable(integer), AnyCodable(integer))
XCTAssertEqual(AnyCodable(double), AnyCodable(double))
XCTAssertEqual(AnyCodable(string), AnyCodable(string))
XCTAssertEqual(AnyCodable(array), AnyCodable(array))
XCTAssertEqual(AnyCodable(nested), AnyCodable(nested))
XCTAssertNotEqual(AnyCodable(array), AnyCodable([1, 2, 4]))
XCTAssertNotEqual(AnyCodable(nested), AnyCodable(["a": "apple", "b": "banana", "c": "cherry"]))
}

func testJSONDecoding() throws {
let json = """
{
Expand Down
22 changes: 22 additions & 0 deletions Tests/AnyCodableTests/AnyDecodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
import XCTest

class AnyDecodableTests: XCTestCase {

func testEquivalency() {
let boolean = true
let integer = 42
let double = 3.141592653589793
let string = "string"
let array = [1, 2, 3]
let nested = [
"a": "alpha",
"b": "bravo",
"c": "charlie",
]
XCTAssertEqual(AnyDecodable(boolean), AnyDecodable(boolean))
XCTAssertEqual(AnyDecodable(integer), AnyDecodable(integer))
XCTAssertEqual(AnyDecodable(double), AnyDecodable(double))
XCTAssertEqual(AnyDecodable(string), AnyDecodable(string))
XCTAssertEqual(AnyDecodable(array), AnyDecodable(array))
XCTAssertEqual(AnyDecodable(nested), AnyDecodable(nested))
XCTAssertNotEqual(AnyDecodable(array), AnyDecodable([1, 2, 4]))
XCTAssertNotEqual(AnyDecodable(nested), AnyDecodable(["a": "apple", "b": "banana", "c": "cherry"]))
}

func testJSONDecoding() throws {
let json = """
{
Expand Down
22 changes: 22 additions & 0 deletions Tests/AnyCodableTests/AnyEncodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
import XCTest

class AnyEncodableTests: XCTestCase {

func testEquivalency() {
let boolean = true
let integer = 42
let double = 3.141592653589793
let string = "string"
let array = [1, 2, 3]
let nested = [
"a": "alpha",
"b": "bravo",
"c": "charlie",
]
XCTAssertEqual(AnyEncodable(boolean), AnyEncodable(boolean))
XCTAssertEqual(AnyEncodable(integer), AnyEncodable(integer))
XCTAssertEqual(AnyEncodable(double), AnyEncodable(double))
XCTAssertEqual(AnyEncodable(string), AnyEncodable(string))
XCTAssertEqual(AnyEncodable(array), AnyEncodable(array))
XCTAssertEqual(AnyEncodable(nested), AnyEncodable(nested))
XCTAssertNotEqual(AnyEncodable(array), AnyEncodable([1, 2, 4]))
XCTAssertNotEqual(AnyEncodable(nested), AnyEncodable(["a": "apple", "b": "banana", "c": "cherry"]))
}

func testJSONEncoding() throws {
let dictionary: [String: AnyEncodable] = [
"boolean": true,
Expand Down