|
| 1 | +/* |
| 2 | + * Copyright 2021, 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 | +import SwiftProtobuf |
| 17 | +import SwiftProtobufPluginLibrary |
| 18 | + |
| 19 | +// MARK: - Client protocol |
| 20 | + |
| 21 | +extension Generator { |
| 22 | + internal func printAsyncServiceClientProtocol() { |
| 23 | + let comments = self.service.protoSourceComments() |
| 24 | + if !comments.isEmpty { |
| 25 | + // Source comments already have the leading '///' |
| 26 | + self.println(comments, newline: false) |
| 27 | + } |
| 28 | + |
| 29 | + self.printAvailabilityForAsyncAwait() |
| 30 | + self.println("\(self.access) protocol \(self.asyncClientProtocolName): GRPCClient {") |
| 31 | + self.withIndentation { |
| 32 | + self.println("var serviceName: String { get }") |
| 33 | + self.println("var interceptors: \(self.clientInterceptorProtocolName)? { get }") |
| 34 | + |
| 35 | + for method in service.methods { |
| 36 | + self.println() |
| 37 | + self.method = method |
| 38 | + |
| 39 | + let rpcType = streamingType(self.method) |
| 40 | + let callType = Types.call(for: rpcType) |
| 41 | + |
| 42 | + let arguments: [String] |
| 43 | + switch rpcType { |
| 44 | + case .unary, .serverStreaming: |
| 45 | + arguments = [ |
| 46 | + "_ request: \(self.methodInputName)", |
| 47 | + "callOptions: \(Types.clientCallOptions)?", |
| 48 | + ] |
| 49 | + |
| 50 | + case .clientStreaming, .bidirectionalStreaming: |
| 51 | + arguments = [ |
| 52 | + "callOptions: \(Types.clientCallOptions)?", |
| 53 | + ] |
| 54 | + } |
| 55 | + |
| 56 | + self.printFunction( |
| 57 | + name: "make\(self.method.name)Call", |
| 58 | + arguments: arguments, |
| 59 | + returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>", |
| 60 | + bodyBuilder: nil |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + self.println("}") // protocol |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// MARK: - Client protocol default implementation: Calls |
| 69 | + |
| 70 | +extension Generator { |
| 71 | + internal func printAsyncClientProtocolExtension() { |
| 72 | + self.printAvailabilityForAsyncAwait() |
| 73 | + self.withIndentation("extension \(self.asyncClientProtocolName)", braces: .curly) { |
| 74 | + // Service name. TODO: use static metadata. |
| 75 | + self.withIndentation("\(self.access) var serviceName: String", braces: .curly) { |
| 76 | + self.println("return \"\(self.servicePath)\"") |
| 77 | + } |
| 78 | + self.println() |
| 79 | + |
| 80 | + // Interceptor factory. |
| 81 | + self.withIndentation( |
| 82 | + "\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?", |
| 83 | + braces: .curly |
| 84 | + ) { |
| 85 | + self.println("return nil") |
| 86 | + } |
| 87 | + |
| 88 | + // 'Unsafe' calls. |
| 89 | + for method in self.service.methods { |
| 90 | + self.println() |
| 91 | + self.method = method |
| 92 | + |
| 93 | + let rpcType = streamingType(self.method) |
| 94 | + let callType = Types.call(for: rpcType) |
| 95 | + let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false) |
| 96 | + |
| 97 | + switch rpcType { |
| 98 | + case .unary, .serverStreaming: |
| 99 | + self.printFunction( |
| 100 | + name: "make\(self.method.name)Call", |
| 101 | + arguments: [ |
| 102 | + "_ request: \(self.methodInputName)", |
| 103 | + "callOptions: \(Types.clientCallOptions)? = nil", |
| 104 | + ], |
| 105 | + returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>", |
| 106 | + access: self.access |
| 107 | + ) { |
| 108 | + self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) { |
| 109 | + self.println("path: \(self.methodPath),") |
| 110 | + self.println("request: request,") |
| 111 | + self.println("callOptions: callOptions ?? self.defaultCallOptions") |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + case .clientStreaming, .bidirectionalStreaming: |
| 116 | + self.printFunction( |
| 117 | + name: "make\(self.method.name)Call", |
| 118 | + arguments: ["callOptions: \(Types.clientCallOptions)? = nil"], |
| 119 | + returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>", |
| 120 | + access: self.access |
| 121 | + ) { |
| 122 | + self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) { |
| 123 | + self.println("path: \(self.methodPath),") |
| 124 | + self.println("callOptions: callOptions ?? self.defaultCallOptions") |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// MARK: - Client protocol implementation |
| 134 | + |
| 135 | +extension Generator { |
| 136 | + internal func printAsyncServiceClientImplementation() { |
| 137 | + self.printAvailabilityForAsyncAwait() |
| 138 | + self.withIndentation( |
| 139 | + "\(self.access) struct \(self.asyncClientClassName): \(self.asyncClientProtocolName)", |
| 140 | + braces: .curly |
| 141 | + ) { |
| 142 | + self.println("\(self.access) var channel: GRPCChannel") |
| 143 | + self.println("\(self.access) var defaultCallOptions: CallOptions") |
| 144 | + self.println("\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?") |
| 145 | + self.println() |
| 146 | + |
| 147 | + self.println("\(self.access) init(") |
| 148 | + self.withIndentation { |
| 149 | + self.println("channel: GRPCChannel,") |
| 150 | + self.println("defaultCallOptions: CallOptions = CallOptions(),") |
| 151 | + self.println("interceptors: \(self.clientInterceptorProtocolName)? = nil") |
| 152 | + } |
| 153 | + self.println(") {") |
| 154 | + self.withIndentation { |
| 155 | + self.println("self.channel = channel") |
| 156 | + self.println("self.defaultCallOptions = defaultCallOptions") |
| 157 | + self.println("self.interceptors = interceptors") |
| 158 | + } |
| 159 | + self.println("}") |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments