generateService generates all the code for the named service.
(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int)
| 142 | |
| 143 | // generateService generates all the code for the named service. |
| 144 | func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) { |
| 145 | path := fmt.Sprintf("6,%d", index) // 6 means service. |
| 146 | |
| 147 | origServName := service.GetName() |
| 148 | fullServName := origServName |
| 149 | if pkg := file.GetPackage(); pkg != "" { |
| 150 | fullServName = pkg + "." + fullServName |
| 151 | } |
| 152 | servName := generator.CamelCase(origServName) |
| 153 | deprecated := service.GetOptions().GetDeprecated() |
| 154 | |
| 155 | g.P() |
| 156 | g.P(fmt.Sprintf(`// %sClient is the client API for %s service. |
| 157 | // |
| 158 | // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.`, servName, servName)) |
| 159 | |
| 160 | // Client interface. |
| 161 | if deprecated { |
| 162 | g.P("//") |
| 163 | g.P(deprecationComment) |
| 164 | } |
| 165 | g.P("type ", servName, "Client interface {") |
| 166 | for i, method := range service.Method { |
| 167 | g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service. |
| 168 | g.P(g.generateClientSignature(servName, method)) |
| 169 | } |
| 170 | g.P("}") |
| 171 | g.P() |
| 172 | |
| 173 | // Client structure. |
| 174 | g.P("type ", unexport(servName), "Client struct {") |
| 175 | g.P("cc *", grpcPkg, ".ClientConn") |
| 176 | g.P("}") |
| 177 | g.P() |
| 178 | |
| 179 | // NewClient factory. |
| 180 | if deprecated { |
| 181 | g.P(deprecationComment) |
| 182 | } |
| 183 | g.P("func New", servName, "Client (cc *", grpcPkg, ".ClientConn) ", servName, "Client {") |
| 184 | g.P("return &", unexport(servName), "Client{cc}") |
| 185 | g.P("}") |
| 186 | g.P() |
| 187 | |
| 188 | var methodIndex, streamIndex int |
| 189 | serviceDescVar := "_" + servName + "_serviceDesc" |
| 190 | // Client method implementations. |
| 191 | for _, method := range service.Method { |
| 192 | var descExpr string |
| 193 | if !method.GetServerStreaming() && !method.GetClientStreaming() { |
| 194 | // Unary RPC method |
| 195 | descExpr = fmt.Sprintf("&%s.Methods[%d]", serviceDescVar, methodIndex) |
| 196 | methodIndex++ |
| 197 | } else { |
| 198 | // Streaming RPC method |
| 199 | descExpr = fmt.Sprintf("&%s.Streams[%d]", serviceDescVar, streamIndex) |
| 200 | streamIndex++ |
| 201 | } |
no test coverage detected