@@ -95,49 +95,79 @@ extension Generator {
95
95
self . method = method
96
96
97
97
let rpcType = streamingType ( self . method)
98
- let callType = Types . call ( for: rpcType)
99
- let callTypeWithoutPrefix = Types . call ( for: rpcType, withGRPCPrefix: false )
98
+ printRPCFunctionImplementation ( rpcType: rpcType)
99
+ printRPCFunctionWrapper ( rpcType: rpcType)
100
+ }
101
+ }
102
+ }
100
103
101
- switch rpcType {
102
- case . unary, . serverStreaming:
103
- self . printFunction (
104
- name: self . methodMakeFunctionCallName,
105
- arguments: [
106
- " _ request: \( self . methodInputName) " ,
107
- " callOptions: \( Types . clientCallOptions) ? = nil " ,
108
- ] ,
109
- returnType: " \( callType) < \( self . methodInputName) , \( self . methodOutputName) > " ,
110
- access: self . access
111
- ) {
112
- self . withIndentation ( " return self.make \( callTypeWithoutPrefix) " , braces: . round) {
113
- self . println ( " path: \( self . methodPathUsingClientMetadata) , " )
114
- self . println ( " request: request, " )
115
- self . println ( " callOptions: callOptions ?? self.defaultCallOptions, " )
116
- self . println (
117
- " interceptors: self.interceptors?. \( self . methodInterceptorFactoryName) () ?? [] "
118
- )
119
- }
120
- }
104
+ private func printRPCFunctionImplementation( rpcType: StreamingType ) {
105
+ let argumentsBuilder : ( ( ) -> Void ) ?
106
+ switch rpcType {
107
+ case . unary, . serverStreaming:
108
+ argumentsBuilder = {
109
+ self . println ( " request: request, " )
110
+ }
111
+ default :
112
+ argumentsBuilder = nil
113
+ }
114
+ let callTypeWithoutPrefix = Types . call ( for: rpcType, withGRPCPrefix: false )
115
+ printRPCFunction ( rpcType: rpcType, name: self . methodMakeFunctionCallName) {
116
+ self . withIndentation ( " return self.make \( callTypeWithoutPrefix) " , braces: . round) {
117
+ self . println ( " path: \( self . methodPathUsingClientMetadata) , " )
118
+ argumentsBuilder ? ( )
119
+ self . println ( " callOptions: callOptions ?? self.defaultCallOptions, " )
120
+ self . println (
121
+ " interceptors: self.interceptors?. \( self . methodInterceptorFactoryName) () ?? [] "
122
+ )
123
+ }
124
+ }
125
+ }
121
126
122
- case . clientStreaming, . bidirectionalStreaming:
123
- self . printFunction (
124
- name: self . methodMakeFunctionCallName,
125
- arguments: [ " callOptions: \( Types . clientCallOptions) ? = nil " ] ,
126
- returnType: " \( callType) < \( self . methodInputName) , \( self . methodOutputName) > " ,
127
- access: self . access
128
- ) {
129
- self . withIndentation ( " return self.make \( callTypeWithoutPrefix) " , braces: . round) {
130
- self . println ( " path: \( self . methodPathUsingClientMetadata) , " )
131
- self . println ( " callOptions: callOptions ?? self.defaultCallOptions, " )
132
- self . println (
133
- " interceptors: self.interceptors?. \( self . methodInterceptorFactoryName) () ?? [] "
134
- )
135
- }
136
- }
137
- }
127
+ private func printRPCFunctionWrapper( rpcType: StreamingType ) {
128
+ let functionName = methodMakeFunctionCallName
129
+ let functionWrapperName = methodMakeFunctionCallWrapperName
130
+ guard functionName != functionWrapperName else { return }
131
+ self . println ( )
132
+
133
+ let argumentsBuilder : ( ( ) -> Void ) ?
134
+ switch rpcType {
135
+ case . unary, . serverStreaming:
136
+ argumentsBuilder = {
137
+ self . println ( " request, " )
138
+ }
139
+ default :
140
+ argumentsBuilder = nil
141
+ }
142
+ printRPCFunction ( rpcType: rpcType, name: functionWrapperName) {
143
+ self . withIndentation ( " return self. \( functionName) " , braces: . round) {
144
+ argumentsBuilder ? ( )
145
+ self . println ( " callOptions: callOptions " )
138
146
}
139
147
}
140
148
}
149
+
150
+ private func printRPCFunction( rpcType: StreamingType , name: String , bodyBuilder: ( ( ) -> Void ) ? ) {
151
+ let callType = Types . call ( for: rpcType)
152
+ self . printFunction (
153
+ name: name,
154
+ arguments: rpcFunctionArguments ( rpcType: rpcType) ,
155
+ returnType: " \( callType) < \( self . methodInputName) , \( self . methodOutputName) > " ,
156
+ access: self . access,
157
+ bodyBuilder: bodyBuilder
158
+ )
159
+ }
160
+
161
+ private func rpcFunctionArguments( rpcType: StreamingType ) -> [ String ] {
162
+ var arguments = [ " callOptions: \( Types . clientCallOptions) ? = nil " ]
163
+ switch rpcType {
164
+ case . unary, . serverStreaming:
165
+ arguments. insert ( " _ request: \( self . methodInputName) " , at: . zero)
166
+ default :
167
+ break
168
+ }
169
+ return arguments
170
+ }
141
171
}
142
172
143
173
// MARK: - Client protocol extension: "Simple, but safe" call wrappers.
0 commit comments