14
14
* limitations under the License.
15
15
*/
16
16
17
- /// A `ClientInterceptorPipelineOperation` describes to which RPCs a client interceptor should be applied.
17
+ /// Describes the conditions under which an interceptor should be applied.
18
18
///
19
- /// You can configure a client interceptor to be applied to:
19
+ /// You can configure interceptors to be applied to:
20
20
/// - all RPCs and services;
21
21
/// - requests directed only to specific services; or
22
22
/// - requests directed only to specific methods (of a specific service).
23
23
///
24
- /// - SeeAlso: ``ClientInterceptor`` for more information on client interceptors, and
25
- /// ``ServerInterceptorPipelineOperation`` for the server-side version of this type.
26
- public struct ClientInterceptorPipelineOperation : Sendable {
27
- /// The subject of a ``ClientInterceptorPipelineOperation``.
28
- /// The subject of an interceptor can either be all services and methods, only specific services, or only specific methods.
24
+ /// - SeeAlso: ``ClientInterceptor`` and ``ServerInterceptor`` for more information on client and
25
+ /// server interceptors, respectively.
26
+ public struct ConditionalInterceptor < Interceptor: Sendable > : Sendable {
29
27
public struct Subject : Sendable {
30
28
internal enum Wrapped : Sendable {
31
29
case all
@@ -41,21 +39,19 @@ public struct ClientInterceptorPipelineOperation: Sendable {
41
39
/// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified services.
42
40
/// - Parameters:
43
41
/// - services: The list of service names for which this interceptor should intercept RPCs.
44
- /// - Returns: A ``ClientInterceptorPipelineOperation``.
45
42
public static func services( _ services: Set < ServiceDescriptor > ) -> Self {
46
43
Self ( wrapped: . services( services) )
47
44
}
48
45
49
46
/// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified service methods.
50
47
/// - Parameters:
51
48
/// - methods: The list of method descriptors for which this interceptor should intercept RPCs.
52
- /// - Returns: A ``ClientInterceptorPipelineOperation``.
53
49
public static func methods( _ methods: Set < MethodDescriptor > ) -> Self {
54
50
Self ( wrapped: . methods( methods) )
55
51
}
56
52
57
53
@usableFromInline
58
- internal func applies( to descriptor: MethodDescriptor ) -> Bool {
54
+ package func applies( to descriptor: MethodDescriptor ) -> Bool {
59
55
switch self . wrapped {
60
56
case . all:
61
57
return true
@@ -69,24 +65,15 @@ public struct ClientInterceptorPipelineOperation: Sendable {
69
65
}
70
66
}
71
67
72
- /// The interceptor specified for this operation .
73
- public let interceptor : any ClientInterceptor
68
+ /// The interceptor.
69
+ public let interceptor : Interceptor
74
70
75
71
@usableFromInline
76
72
internal let subject : Subject
77
73
78
- private init ( interceptor: any ClientInterceptor , appliesTo : Subject ) {
74
+ fileprivate init ( interceptor: Interceptor , subject : Subject ) {
79
75
self . interceptor = interceptor
80
- self . subject = appliesTo
81
- }
82
-
83
- /// Create an operation, specifying which ``ClientInterceptor`` to apply and to which ``Subject``.
84
- /// - Parameters:
85
- /// - interceptor: The ``ClientInterceptor`` to register with the client.
86
- /// - subject: The ``Subject`` to which the `interceptor` applies.
87
- /// - Returns: A ``ClientInterceptorPipelineOperation``.
88
- public static func apply( _ interceptor: any ClientInterceptor , to subject: Subject ) -> Self {
89
- Self ( interceptor: interceptor, appliesTo: subject)
76
+ self . subject = subject
90
77
}
91
78
92
79
/// Returns whether this ``ClientInterceptorPipelineOperation`` applies to the given `descriptor`.
@@ -97,3 +84,29 @@ public struct ClientInterceptorPipelineOperation: Sendable {
97
84
self . subject. applies ( to: descriptor)
98
85
}
99
86
}
87
+
88
+ extension ConditionalInterceptor where Interceptor == any ClientInterceptor {
89
+ /// Create an operation, specifying which ``ClientInterceptor`` to apply and to which ``Subject``.
90
+ /// - Parameters:
91
+ /// - interceptor: The ``ClientInterceptor`` to register with the client.
92
+ /// - subject: The ``Subject`` to which the `interceptor` applies.
93
+ public static func apply(
94
+ _ interceptor: any ClientInterceptor ,
95
+ to subject: Subject
96
+ ) -> Self {
97
+ Self ( interceptor: interceptor, subject: subject)
98
+ }
99
+ }
100
+
101
+ extension ConditionalInterceptor where Interceptor == any ServerInterceptor {
102
+ /// Create an operation, specifying which ``ServerInterceptor`` to apply and to which ``Subject``.
103
+ /// - Parameters:
104
+ /// - interceptor: The ``ServerInterceptor`` to register with the server.
105
+ /// - subject: The ``Subject`` to which the `interceptor` applies.
106
+ public static func apply(
107
+ _ interceptor: any ServerInterceptor ,
108
+ to subject: Subject
109
+ ) -> Self {
110
+ Self ( interceptor: interceptor, subject: subject)
111
+ }
112
+ }
0 commit comments