Skip to content

Commit 6c96744

Browse files
calvincestarigh-action-runner
authored and
gh-action-runner
committed
fix: Cherry pick ObjectData type check to ListData (apollographql/apollo-ios-dev#473)
1 parent 6620447 commit 6c96744

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

Sources/ApolloAPI/ObjectData.swift

+19-13
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ public struct ObjectData {
1111
public let _transformer: any _ObjectData_Transformer
1212
public let _rawData: [String: AnyHashable]
1313

14-
@usableFromInline internal static let _boolTrue = AnyHashable(true)
15-
@usableFromInline internal static let _boolFalse = AnyHashable(false)
16-
1714
public init(
1815
_transformer: any _ObjectData_Transformer,
1916
_rawData: [String: AnyHashable]
@@ -29,7 +26,7 @@ public struct ObjectData {
2926
// This check is based on AnyHashable using a canonical representation of the type-erased value so
3027
// instances wrapping the same value of any type compare as equal. Therefore while Int(1) and Int(0)
3128
// might be representable as Bool they will never equal Bool(true) nor Bool(false).
32-
if let boolVal = value as? Bool, (value == Self._boolTrue || value == Self._boolFalse) {
29+
if let boolVal = value as? Bool, value.isCanonicalBool {
3330
value = boolVal
3431

3532
// Cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting
@@ -72,17 +69,17 @@ public struct ListData {
7269
@inlinable public subscript(_ key: Int) -> (any ScalarType)? {
7370
var value: AnyHashable = _rawData[key]
7471

75-
// Attempting cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting,
76-
// also need to attempt `Bool` cast first to ensure a bool doesn't get inadvertently converted to `Int`
77-
switch value {
78-
case let boolVal as Bool:
72+
// This check is based on AnyHashable using a canonical representation of the type-erased value so
73+
// instances wrapping the same value of any type compare as equal. Therefore while Int(1) and Int(0)
74+
// might be representable as Bool they will never equal Bool(true) nor Bool(false).
75+
if let boolVal = value as? Bool, value.isCanonicalBool {
7976
value = boolVal
80-
case let intVal as Int:
81-
value = intVal
82-
default:
83-
break
77+
78+
// Cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting
79+
} else if let intValue = value as? Int {
80+
value = intValue
8481
}
85-
82+
8683
return _transformer.transform(value)
8784
}
8885

@@ -96,3 +93,12 @@ public struct ListData {
9693
return _transformer.transform(_rawData[key])
9794
}
9895
}
96+
97+
extension AnyHashable {
98+
fileprivate static let boolTrue = AnyHashable(true)
99+
fileprivate static let boolFalse = AnyHashable(false)
100+
101+
@usableFromInline var isCanonicalBool: Bool {
102+
self == Self.boolTrue || self == Self.boolFalse
103+
}
104+
}

0 commit comments

Comments
 (0)