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

fix: ScalarType casting for Int values #3037

Merged
merged 3 commits into from
May 23, 2023
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
30 changes: 28 additions & 2 deletions Sources/ApolloAPI/ObjectData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ public struct ObjectData {
}

@inlinable public subscript(_ key: String) -> (any ScalarType)? {
guard let value = _rawData[key] else { return nil }
guard let rawValue = _rawData[key] else { return nil }
var value: AnyHashable = rawValue

// Attempting cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting,
// also need to attempt `Bool` cast first to ensure a bool doesn't get inadvertently converted to `Int`
switch value {
case let boolVal as Bool:
value = boolVal
case let intVal as Int:
value = intVal
default:
break
}

return _transformer.transform(value)
}

Expand Down Expand Up @@ -54,7 +67,20 @@ public struct ListData {
}

@inlinable public subscript(_ key: Int) -> (any ScalarType)? {
return _transformer.transform(_rawData[key])
var value: AnyHashable = _rawData[key]

// Attempting cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting,
// also need to attempt `Bool` cast first to ensure a bool doesn't get inadvertently converted to `Int`
switch value {
case let boolVal as Bool:
value = boolVal
case let intVal as Int:
value = intVal
default:
break
}

return _transformer.transform(value)
}

@_disfavoredOverload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class NetworkResponseExecutionSource_OpaqueObjectDataWrapper_Tests: XCTestCase {

// MARK: - Scalar Fields

func test__subscript__forScalarField_returnsValue() throws {
func test__subscript__forStringScalarField_returnsValue() throws {
// given
let data = [
"name": "Luke Skywalker"
Expand All @@ -34,6 +34,96 @@ class NetworkResponseExecutionSource_OpaqueObjectDataWrapper_Tests: XCTestCase {
// then
expect(actual as? String).to(equal("Luke Skywalker"))
}

func test__subscript_forIntScalarField_returnsValue() throws {
// given
let data = [
"value": Int(10)
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when
let actual = objectData["value"]

// then
expect(actual as? Int).to(equal(10))
}

func test__subscript_forInt32ScalarField_returnsValue() throws {
// given
let data = [
"value": Int32(10)
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when
let actual = objectData["value"]

// then
expect(actual as? Int).to(equal(10))
}

func test__subscript_forInt64ScalarField_returnsValue() throws {
// given
let data = [
"value": Int64(10)
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when
let actual = objectData["value"]

// then
expect(actual as? Int).to(equal(10))
}

func test__subscript_forBoolScalarField_returnsScalarTypeValue() throws {
// given
let data = [
"value": true
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when
let actual = objectData["value"]

// then
expect(actual as? Bool).to(equal(true))
}

func test__subscript_forDoubleScalarField_returnsScalarTypeValue() throws {
// given
let data = [
"value": Double(10.5)
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when
let actual = objectData["value"]

// then
expect(actual as? Double).to(equal(10.5))
}

func test__subscript_forFloatScalarField_returnsScalarTypeValue() throws {
// given
let data = [
"value": Float(10.5)
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when
let actual = objectData["value"]

// then
expect(actual as? Float).to(equal(10.5))
}

// MARK: Object Fields

Expand Down Expand Up @@ -89,5 +179,101 @@ class NetworkResponseExecutionSource_OpaqueObjectDataWrapper_Tests: XCTestCase {
// then
expect(actual as? String).to(equal("Luke Skywalker"))
}

func test__subscript__forListOfIntFields_returnsValue() throws {
// given
let data = [
"values": [Int(10), Int(20)]
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when

let actual = objectData["values"]?[0]

// then
expect(actual as? Int).to(equal(10))
}

func test__subscript__forListOfInt32Fields_returnsValue() throws {
// given
let data = [
"values": [Int32(10), Int32(20)]
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when

let actual = objectData["values"]?[0]

// then
expect(actual as? Int).to(equal(10))
}

func test__subscript__forListOfInt64Fields_returnsValue() throws {
// given
let data = [
"values": [Int64(10), Int64(20)]
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when

let actual = objectData["values"]?[0]

// then
expect(actual as? Int).to(equal(10))
}

func test__subscript__forListOfBoolFields_returnsValue() throws {
// given
let data = [
"values": [true, false]
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when

let actual = objectData["values"]?[0]

// then
expect(actual as? Bool).to(equal(true))
}

func test__subscript__forListOfDoubleFields_returnsValue() throws {
// given
let data = [
"values": [Double(10.5), Double(20.5)]
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when

let actual = objectData["values"]?[0]

// then
expect(actual as? Double).to(equal(10.5))
}

func test__subscript__forListOfFloatFields_returnsValue() throws {
// given
let data = [
"values": [Float(10.5), Float(20.5)]
]

let objectData = subject.opaqueObjectDataWrapper(for: data)

// when

let actual = objectData["values"]?[0]

// then
expect(actual as? Float).to(equal(10.5))
}

}
Loading