Skip to content

Commit 1da33d3

Browse files
calvincestarigh-action-runner
authored and
gh-action-runner
committed
fix: ObjectData type check (apollographql/apollo-ios-dev#459)
1 parent 65d7a19 commit 1da33d3

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Sources/ApolloAPI/ObjectData.swift

+13-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ 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+
1417
public init(
1518
_transformer: any _ObjectData_Transformer,
1619
_rawData: [String: AnyHashable]
@@ -22,18 +25,18 @@ public struct ObjectData {
2225
@inlinable public subscript(_ key: String) -> (any ScalarType)? {
2326
guard let rawValue = _rawData[key] else { return nil }
2427
var value: AnyHashable = rawValue
25-
26-
// Attempting cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting,
27-
// also need to attempt `Bool` cast first to ensure a bool doesn't get inadvertently converted to `Int`
28-
switch value {
29-
case let boolVal as Bool:
28+
29+
// This check is based on AnyHashable using a canonical representation of the type-erased value so
30+
// instances wrapping the same value of any type compare as equal. Therefore while Int(1) and Int(0)
31+
// 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) {
3033
value = boolVal
31-
case let intVal as Int:
32-
value = intVal
33-
default:
34-
break
34+
35+
// Cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting
36+
} else if let intValue = value as? Int {
37+
value = intValue
3538
}
36-
39+
3740
return _transformer.transform(value)
3841
}
3942

0 commit comments

Comments
 (0)