ErrorDetails+AnyPacking.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. internal import SwiftProtobuf
  17. /// A type which can be packed and unpacked from a `Google_Protobuf_Any` message.
  18. internal protocol GoogleProtobufAnyPackable {
  19. static var typeURL: String { get }
  20. /// Pack the value into a `Google_Protobuf_Any`, if possible.
  21. func pack() throws -> Google_Protobuf_Any
  22. /// Unpack the value from a `Google_Protobuf_Any`, if possible.
  23. init?(unpacking any: Google_Protobuf_Any) throws
  24. }
  25. /// A type which is backed by a Protobuf message.
  26. ///
  27. /// This is a convenience protocol to allow for automatic packing/unpacking of messages
  28. /// where possible.
  29. internal protocol ProtobufBacked {
  30. associatedtype Message: SwiftProtobuf.Message
  31. var storage: Message { get set }
  32. init(storage: Message)
  33. }
  34. extension GoogleProtobufAnyPackable where Self: ProtobufBacked {
  35. func pack() throws -> Google_Protobuf_Any {
  36. try .with {
  37. $0.typeURL = Self.typeURL
  38. $0.value = try self.storage.serializedBytes()
  39. }
  40. }
  41. init?(unpacking any: Google_Protobuf_Any) throws {
  42. guard let storage = try any.unpack(Message.self) else { return nil }
  43. self.init(storage: storage)
  44. }
  45. }
  46. extension Google_Protobuf_Any {
  47. func unpack<Unpacked: Message>(_ as: Unpacked.Type) throws -> Unpacked? {
  48. if self.isA(Unpacked.self) {
  49. return try Unpacked(serializedBytes: self.value)
  50. } else {
  51. return nil
  52. }
  53. }
  54. }
  55. extension ErrorDetails {
  56. // Note: this type isn't packable into an 'Any' protobuf so doesn't conform
  57. // to 'GoogleProtobufAnyPackable' despite holding types which are packable.
  58. func pack() throws -> Google_Protobuf_Any {
  59. switch self.wrapped {
  60. case .errorInfo(let info):
  61. return try info.pack()
  62. case .retryInfo(let info):
  63. return try info.pack()
  64. case .debugInfo(let info):
  65. return try info.pack()
  66. case .quotaFailure(let info):
  67. return try info.pack()
  68. case .preconditionFailure(let info):
  69. return try info.pack()
  70. case .badRequest(let info):
  71. return try info.pack()
  72. case .requestInfo(let info):
  73. return try info.pack()
  74. case .resourceInfo(let info):
  75. return try info.pack()
  76. case .help(let info):
  77. return try info.pack()
  78. case .localizedMessage(let info):
  79. return try info.pack()
  80. case .any(let any):
  81. return any
  82. }
  83. }
  84. init(unpacking any: Google_Protobuf_Any) throws {
  85. if let unpacked = try Self.unpack(any: any) {
  86. self = unpacked
  87. } else {
  88. self = .any(any)
  89. }
  90. }
  91. private static func unpack(any: Google_Protobuf_Any) throws -> Self? {
  92. switch any.typeURL {
  93. case ErrorInfo.typeURL:
  94. if let unpacked = try ErrorInfo(unpacking: any) {
  95. return .errorInfo(unpacked)
  96. }
  97. case RetryInfo.typeURL:
  98. if let unpacked = try RetryInfo(unpacking: any) {
  99. return .retryInfo(unpacked)
  100. }
  101. case DebugInfo.typeURL:
  102. if let unpacked = try DebugInfo(unpacking: any) {
  103. return .debugInfo(unpacked)
  104. }
  105. case QuotaFailure.typeURL:
  106. if let unpacked = try QuotaFailure(unpacking: any) {
  107. return .quotaFailure(unpacked)
  108. }
  109. case PreconditionFailure.typeURL:
  110. if let unpacked = try PreconditionFailure(unpacking: any) {
  111. return .preconditionFailure(unpacked)
  112. }
  113. case BadRequest.typeURL:
  114. if let unpacked = try BadRequest(unpacking: any) {
  115. return .badRequest(unpacked)
  116. }
  117. case RequestInfo.typeURL:
  118. if let unpacked = try RequestInfo(unpacking: any) {
  119. return .requestInfo(unpacked)
  120. }
  121. case ResourceInfo.typeURL:
  122. if let unpacked = try ResourceInfo(unpacking: any) {
  123. return .resourceInfo(unpacked)
  124. }
  125. case Help.typeURL:
  126. if let unpacked = try Help(unpacking: any) {
  127. return .help(unpacked)
  128. }
  129. case LocalizedMessage.typeURL:
  130. if let unpacked = try LocalizedMessage(unpacking: any) {
  131. return .localizedMessage(unpacked)
  132. }
  133. default:
  134. return .any(any)
  135. }
  136. return nil
  137. }
  138. }