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

Support for AnyValue Scalar type #13

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
66 changes: 66 additions & 0 deletions Sources/Scalars/AnyValue.swift
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

/**
aashishpatil-g marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
227 changes: 227 additions & 0 deletions Tests/Integration/AnyScalarTests.swift
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)
}
}
3 changes: 1 addition & 2 deletions Tests/Integration/ConfigSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ actor ProjectConfigurator {
var configureRequest = URLRequest(url: configureUrl)
configureRequest.httpMethod = "POST"

let (data, response) = try await URLSession.shared.upload(
let (_, response) = try await URLSession.shared.upload(
for: configureRequest,
from: configureBody.data(using: .utf8)!
)
print("responseData \(response)")
setupComplete = true
}
}
40 changes: 40 additions & 0 deletions Tests/Integration/Gen/KitchenSink/Sources/KitchenSinkKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@ import Foundation

import FirebaseDataConnect

public struct AnyValueTypeKey {
public private(set) var id: UUID

enum CodingKeys: String, CodingKey {
case id
}
}

extension AnyValueTypeKey: Codable {
public init(from decoder: any Decoder) throws {
var container = try decoder.container(keyedBy: CodingKeys.self)
let codecHelper = CodecHelper<CodingKeys>()

id = try codecHelper.decode(UUID.self, forKey: .id, container: &container)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
let codecHelper = CodecHelper<CodingKeys>()

try codecHelper.encode(id, forKey: .id, container: &container)
}
}

extension AnyValueTypeKey: Equatable {
public static func == (lhs: AnyValueTypeKey, rhs: AnyValueTypeKey) -> Bool {
if lhs.id != rhs.id {
return false
}

return true
}
}

extension AnyValueTypeKey: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}

public struct LargeIntTypeKey {
public private(set) var id: UUID

Expand Down
Loading