-
Notifications
You must be signed in to change notification settings - Fork 1
Support for AnyValue Scalar type #13
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import Foundation | ||
|
|
||
| /** | ||
| AnyValue represents the Any graphql scalar, which represents a scalar Codable data (Int, Double, String) or a JSON object | ||
|
|
||
| */ | ||
| @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
| public struct AnyValue { | ||
| public private(set) var value: Data | ||
|
|
||
| public init(codableValue: Codable) throws { | ||
| do { | ||
| let jsonEncoder = JSONEncoder() | ||
| value = try jsonEncoder.encode(codableValue) | ||
| } | ||
| } | ||
|
|
||
| public func decodeValue<T: Decodable>(_ type: T.Type) throws -> T? { | ||
| do { | ||
| let jsonDecoder = JSONDecoder() | ||
| let decodedResult = try jsonDecoder.decode(type, from: value) | ||
| return decodedResult | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
| extension AnyValue: Codable { | ||
| public init(from decoder: any Decoder) throws { | ||
| let singleValueContainer = try decoder.singleValueContainer() | ||
| value = try singleValueContainer.decode(Data.self) | ||
| } | ||
|
|
||
| public func encode(to encoder: any Encoder) throws { | ||
| var container = encoder.singleValueContainer() | ||
| try container.encode(value) | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
| extension AnyValue: Equatable { | ||
| public static func == (lhs: Self, rhs: Self) -> Bool { | ||
| return lhs.value == rhs.value | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
| extension AnyValue: Hashable { | ||
| public func hash(into hasher: inout Hasher) { | ||
| hasher.combine(value) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import XCTest | ||
|
|
||
| import FirebaseCore | ||
| @testable import FirebaseDataConnect | ||
|
|
||
| @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
| final class AnyScalarTests: IntegrationTestBase { | ||
| override func setUp(completion: @escaping ((any Error)?) -> Void) { | ||
| Task { | ||
| do { | ||
| try await ProjectConfigurator.shared.configureProject() | ||
| completion(nil) | ||
| } catch { | ||
| completion(error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testAnyValueString() async throws { | ||
| let testData = "Test String Data \(Int.random(in: 1 ... 10000))" | ||
| let anyTestData = try AnyValue(codableValue: testData) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient.createAnyValueTypeMutationRef( | ||
| id: anyValueId, | ||
| props: anyTestData | ||
| ).execute() | ||
| print(anyValueId) | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(String.self) | ||
|
|
||
| XCTAssertEqual(testData, decodedResult) | ||
| } | ||
|
|
||
| func testAnyValueInt() async throws { | ||
| let testNumber = Int.random(in: 1 ... 9999) | ||
| let anyTestData = try AnyValue(codableValue: testNumber) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient.createAnyValueTypeMutationRef( | ||
| id: anyValueId, | ||
| props: anyTestData | ||
| ).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(Int.self) | ||
|
|
||
| XCTAssertEqual(testNumber, decodedResult) | ||
| } | ||
|
|
||
| func testAnyValueDouble() async throws { | ||
| let testDouble = Double | ||
| .random(in: Double.leastNormalMagnitude ... Double.greatestFiniteMagnitude) | ||
| let anyTestData = try AnyValue(codableValue: testDouble) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient.createAnyValueTypeMutationRef( | ||
| id: anyValueId, | ||
| props: anyTestData | ||
| ).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(Double.self) | ||
|
|
||
| XCTAssertEqual(testDouble, decodedResult) | ||
| } | ||
|
|
||
| func testAnyValueInt64() async throws { | ||
| let testInt64 = Int64.random(in: Int64.min ... Int64.max) | ||
| let anyTestData = try AnyValue(codableValue: testInt64) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient.createAnyValueTypeMutationRef( | ||
| id: anyValueId, | ||
| props: anyTestData | ||
| ).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(Int64.self) | ||
|
|
||
| XCTAssertEqual(testInt64, decodedResult) | ||
| } | ||
|
|
||
| func testAnyValueInt64Max() async throws { | ||
| let int64Max = Int64.max | ||
| let anyTestData = try AnyValue(codableValue: int64Max) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient | ||
| .createAnyValueTypeMutationRef(id: anyValueId, props: anyTestData).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(Int64.self) | ||
|
|
||
| XCTAssertEqual(int64Max, decodedResult) | ||
| } | ||
|
|
||
| func testAnyValueInt64Min() async throws { | ||
| let int64Min = Int64.min | ||
| let anyTestData = try AnyValue(codableValue: int64Min) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient | ||
| .createAnyValueTypeMutationRef(id: anyValueId, props: anyTestData).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(Int64.self) | ||
|
|
||
| XCTAssertEqual(int64Min, decodedResult) | ||
| } | ||
|
|
||
| func testAnyValueDoubleMax() async throws { | ||
| let testDouble = Double.greatestFiniteMagnitude | ||
| let anyTestData = try AnyValue(codableValue: testDouble) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient.createAnyValueTypeMutationRef( | ||
| id: anyValueId, | ||
| props: anyTestData | ||
| ).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(Double.self) | ||
|
|
||
| XCTAssertEqual(testDouble, decodedResult) | ||
| } | ||
|
|
||
| func testAnyValueDoubleMin() async throws { | ||
| let testDouble = Double.leastNormalMagnitude | ||
| let anyTestData = try AnyValue(codableValue: testDouble) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient.createAnyValueTypeMutationRef( | ||
| id: anyValueId, | ||
| props: anyTestData | ||
| ).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(Double.self) | ||
|
|
||
| XCTAssertEqual(testDouble, decodedResult) | ||
| } | ||
|
|
||
| struct AnyValueEmbeddedStruct: Codable, Equatable { | ||
| var stringVal = "Hello World \(Int.random(in: 1 ... 1000))" | ||
| var doubleVal = Double.random(in: 1 ... 10000) | ||
| var dictValInt: [String: Int] = [ | ||
| "keyOne": Int.random(in: 1 ... 1000), | ||
| "KeyTwo": Int.random(in: 1 ... 100_000), | ||
| ] | ||
|
|
||
| static func == (lhs: AnyValueEmbeddedStruct, rhs: AnyValueEmbeddedStruct) -> Bool { | ||
| return lhs.stringVal == rhs.stringVal && | ||
| lhs.doubleVal == rhs.doubleVal && | ||
| lhs.dictValInt == rhs.dictValInt | ||
| } | ||
| } | ||
|
|
||
| struct AnyValueTestStruct: Codable, Equatable { | ||
| var stringVal = "Test Data \(Int.random(in: 10 ... 1000))" | ||
| var intVal = Int.random(in: 1 ... 10000) | ||
| var doubleVal = Double.random(in: 1 ... 1000) | ||
| var dictVal: [String: String] = ["key1": "val1", "key2": "val2"] | ||
| var dictValDouble: [String: Double] = [ | ||
| "key1": Double.random(in: 1 ... 1000), | ||
| "key2": Double.random(in: 1 ... 10000), | ||
| ] | ||
| var structVal = AnyValueEmbeddedStruct() | ||
|
|
||
| static func == (lhs: AnyValueTestStruct, rhs: AnyValueTestStruct) -> Bool { | ||
| return lhs.stringVal == rhs.stringVal && | ||
| lhs.intVal == rhs.intVal && | ||
| lhs.doubleVal == rhs.doubleVal && | ||
| lhs.dictVal == rhs.dictVal && | ||
| lhs.dictValDouble == rhs.dictValDouble | ||
| } | ||
| } | ||
|
|
||
| func testAnyValueStruct() async throws { | ||
| let structVal = AnyValueTestStruct() | ||
| let anyValStruct = try AnyValue(codableValue: structVal) | ||
|
|
||
| let anyValueId = UUID() | ||
| _ = try await DataConnect.kitchenSinkClient.createAnyValueTypeMutationRef( | ||
| id: anyValueId, | ||
| props: anyValStruct | ||
| ).execute() | ||
|
|
||
| let result = try await DataConnect.kitchenSinkClient.getAnyValueTypeQueryRef(id: anyValueId) | ||
| .execute() | ||
| let anyValueResult = result.data.anyValueType?.props | ||
| let decodedResult = try anyValueResult?.decodeValue(AnyValueTestStruct.self) | ||
|
|
||
| XCTAssertEqual(structVal, decodedResult) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.