Skip to content

Commit 4d27a65

Browse files
committed
fix a Hashable risk
1 parent b7363fc commit 4d27a65

3 files changed

Lines changed: 55 additions & 13 deletions

File tree

Sources/CodableWrapper/Codec.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ extension Codec: CustomStringConvertible, CustomDebugStringConvertible {
5252
}
5353
}
5454

55-
extension Codec: Equatable where Value: Equatable { }
55+
extension Codec: Equatable where Value: Equatable {
56+
public static func == (lhs: Codec<Value>, rhs: Codec<Value>) -> Bool {
57+
lhs.wrappedValue == rhs.wrappedValue
58+
}
59+
}
5660

57-
extension Codec: Hashable where Value: Hashable { }
61+
extension Codec: Hashable where Value: Hashable {
62+
public func hash(into hasher: inout Hasher) {
63+
hasher.combine(wrappedValue)
64+
}
65+
}

Sources/CodableWrapper/CodecCore/CodecConstruct.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
enum CodecConstructKey: Hashable {
10+
enum CodecConstructKey {
1111
case noNested(String)
1212
case nested([String])
1313

@@ -25,7 +25,7 @@ enum CodecConstructKey: Hashable {
2525
}
2626
}
2727

28-
final class CodecConstruct<Value>: Hashable {
28+
final class CodecConstruct<Value> {
2929
var codingKeys: [CodecConstructKey] = []
3030
var transformer: AnyTransfromTypeTunk?
3131
var storedValue: Value?
@@ -53,13 +53,4 @@ final class CodecConstruct<Value>: Hashable {
5353
self.codingKeys = codingKeys
5454
self.transformer = transformer
5555
}
56-
57-
func hash(into hasher: inout Hasher) {
58-
hasher.combine(self.codingKeys)
59-
hasher.combine(self.transformer?.hashValue)
60-
}
61-
62-
static func == (lhs: CodecConstruct, rhs: CodecConstruct) -> Bool {
63-
return lhs.hashValue == rhs.hashValue
64-
}
6556
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// ExtensionTest.swift
3+
//
4+
//
5+
// Created by winddpan on 2023/1/5.
6+
//
7+
8+
import CodableWrapper
9+
import XCTest
10+
11+
final class ExtensionTest: XCTestCase {
12+
struct HashableModel: Codable, Hashable {
13+
@Codec var value1: String?
14+
}
15+
16+
struct NavtiveHashableModel: Codable, Hashable {
17+
var value1: String?
18+
}
19+
20+
struct NavtiveHashableModel2: Codable, Hashable {
21+
var value1: String?
22+
}
23+
24+
override func setUpWithError() throws {
25+
// Put setup code here. This method is called before the invocation of each test method in the class.
26+
}
27+
28+
override func tearDownWithError() throws {
29+
// Put teardown code here. This method is called after the invocation of each test method in the class.
30+
}
31+
32+
func testHashable() throws {
33+
let a = HashableModel(value1: "abc")
34+
let b = HashableModel(value1: "abc")
35+
36+
let c = NavtiveHashableModel(value1: "abc")
37+
let d = NavtiveHashableModel2(value1: "abc")
38+
39+
XCTAssertEqual(a.hashValue, b.hashValue)
40+
XCTAssertEqual(a.hashValue, c.hashValue)
41+
XCTAssertEqual(c.hashValue, d.hashValue)
42+
}
43+
}

0 commit comments

Comments
 (0)