15
15
*/
16
16
17
17
public import GRPCCore
18
- internal import SwiftProtobuf
18
+ public import SwiftProtobuf
19
19
20
20
/// An error containing structured details which can be delivered to the client.
21
21
///
@@ -74,31 +74,63 @@ public struct GoogleRPCStatus: Error {
74
74
}
75
75
}
76
76
77
- extension GoogleRPCStatus : GoogleProtobufAnyPackable {
78
- // See https://protobuf.dev/programming-guides/proto3/#any
79
- internal static var typeURL : String { " type.googleapis.com/google.rpc.Status " }
80
-
81
- init ? ( unpacking any: Google_Protobuf_Any ) throws {
82
- guard any. isA ( Google_Rpc_Status . self) else { return nil }
83
- let status = try Google_Rpc_Status ( serializedBytes: any. value)
77
+ extension GoogleRPCStatus {
78
+ /// Creates a new message by decoding the given `SwiftProtobufContiguousBytes` value
79
+ /// containing a serialized message in Protocol Buffer binary format.
80
+ ///
81
+ /// - Parameters:
82
+ /// - serializedBytes: The binary-encoded message data to decode.
83
+ /// - extensions: An `ExtensionMap` used to look up and decode any
84
+ /// extensions in this message or messages nested within this message's
85
+ /// fields.
86
+ /// - partial: If `false` (the default), this method will check if the `Message`
87
+ /// is initialized after decoding to verify that all required fields are present.
88
+ /// If any are missing, this method throws `BinaryDecodingError`.
89
+ /// - options: The `BinaryDecodingOptions` to use.
90
+ /// - Throws: `BinaryDecodingError` if decoding fails.
91
+ public init < Bytes: SwiftProtobufContiguousBytes > (
92
+ serializedBytes bytes: Bytes ,
93
+ extensions: ( any ExtensionMap ) ? = nil ,
94
+ partial: Bool = false ,
95
+ options: BinaryDecodingOptions = BinaryDecodingOptions ( )
96
+ ) throws {
97
+ let status = try Google_Rpc_Status (
98
+ serializedBytes: bytes,
99
+ extensions: extensions,
100
+ partial: partial,
101
+ options: options
102
+ )
84
103
85
104
let statusCode = Status . Code ( rawValue: Int ( status. code) )
86
105
self . code = statusCode. flatMap { RPCError . Code ( $0) } ?? . unknown
87
106
self . message = status. message
88
107
self . details = try status. details. map { try ErrorDetails ( unpacking: $0) }
89
108
}
90
109
91
- func pack( ) throws -> Google_Protobuf_Any {
110
+ /// Returns a `SwiftProtobufContiguousBytes` instance containing the Protocol Buffer binary
111
+ /// format serialization of the message.
112
+ ///
113
+ /// - Parameters:
114
+ /// - partial: If `false` (the default), this method will check
115
+ /// `Message.isInitialized` before encoding to verify that all required
116
+ /// fields are present. If any are missing, this method throws.
117
+ /// `BinaryEncodingError/missingRequiredFields`.
118
+ /// - options: The `BinaryEncodingOptions` to use.
119
+ /// - Returns: A `SwiftProtobufContiguousBytes` instance containing the binary serialization
120
+ /// of the message.
121
+ ///
122
+ /// - Throws: `SwiftProtobufError` or `BinaryEncodingError` if encoding fails.
123
+ public func serializedBytes< Bytes: SwiftProtobufContiguousBytes > (
124
+ partial: Bool = false ,
125
+ options: BinaryEncodingOptions = BinaryEncodingOptions ( )
126
+ ) throws -> Bytes {
92
127
let status = try Google_Rpc_Status . with {
93
128
$0. code = Int32 ( self . code. rawValue)
94
129
$0. message = self . message
95
130
$0. details = try self . details. map { try $0. pack ( ) }
96
131
}
97
132
98
- return try . with {
99
- $0. typeURL = Self . typeURL
100
- $0. value = try status. serializedBytes ( )
101
- }
133
+ return try status. serializedBytes ( partial: partial, options: options)
102
134
}
103
135
}
104
136
@@ -107,8 +139,7 @@ extension GoogleRPCStatus: RPCErrorConvertible {
107
139
public var rpcErrorMessage : String { self . message }
108
140
public var rpcErrorMetadata : Metadata {
109
141
do {
110
- let any = try self . pack ( )
111
- let bytes : [ UInt8 ] = try any. serializedBytes ( )
142
+ let bytes : [ UInt8 ] = try self . serializedBytes ( )
112
143
return [ Metadata . statusDetailsBinKey: . binary( bytes) ]
113
144
} catch {
114
145
// Failed to serialize error details. Not a lot can be done here.
0 commit comments