Codec.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import SwiftProtobuf
  16. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  17. class Codec {
  18. // Encode Codable to Protos
  19. func encode(args: any Encodable) throws -> Google_Protobuf_Struct {
  20. do {
  21. let jsonEncoder = JSONEncoder()
  22. let jsonData = try jsonEncoder.encode(args)
  23. let argsStruct = try Google_Protobuf_Struct(jsonUTF8Data: jsonData)
  24. return argsStruct
  25. }
  26. }
  27. // Decode Protos to Codable
  28. func decode<T: Decodable>(result: Google_Protobuf_Struct, asType: T.Type) throws -> T? {
  29. do {
  30. let jsonData = try result.jsonUTF8Data()
  31. let jsonDecoder = JSONDecoder()
  32. let resultAsType = try jsonDecoder.decode(asType, from: jsonData)
  33. print("result as Type \(resultAsType)")
  34. return resultAsType
  35. }
  36. }
  37. func createQueryRequestProto<VariableType: OperationVariable>(connectorName: String,
  38. request: QueryRequest<
  39. VariableType
  40. >) throws
  41. -> Google_Firebase_Dataconnect_V1alpha_ExecuteQueryRequest {
  42. do {
  43. var varStruct: Google_Protobuf_Struct? = nil
  44. if let variables = request.variables {
  45. varStruct = try encode(args: variables)
  46. }
  47. let internalRequest = Google_Firebase_Dataconnect_V1alpha_ExecuteQueryRequest.with { ireq in
  48. ireq.operationName = request.operationName
  49. if let varStruct {
  50. ireq.variables = varStruct
  51. } else {
  52. ireq.variables = Google_Protobuf_Struct()
  53. }
  54. ireq.name = connectorName
  55. }
  56. return internalRequest
  57. }
  58. }
  59. func createMutationRequestProto<VariableType: OperationVariable>(connectorName: String,
  60. request: MutationRequest<
  61. VariableType
  62. >) throws
  63. -> Google_Firebase_Dataconnect_V1alpha_ExecuteMutationRequest {
  64. do {
  65. var varStruct: Google_Protobuf_Struct? = nil
  66. if let variables = request.variables {
  67. varStruct = try encode(args: variables)
  68. }
  69. let internalRequest = Google_Firebase_Dataconnect_V1alpha_ExecuteMutationRequest
  70. .with { ireq in
  71. ireq.operationName = request.operationName
  72. if let varStruct {
  73. ireq.variables = varStruct
  74. } else {
  75. // always provide an empty struct otherwise request fails.
  76. ireq.variables = Google_Protobuf_Struct()
  77. }
  78. ireq.name = connectorName
  79. }
  80. return internalRequest
  81. }
  82. }
  83. }