| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
package yarpc |
| 36 |
|
| 37 |
import ( |
| 38 |
"fmt" |
| 39 |
"path" |
| 40 |
"strconv" |
| 41 |
"strings" |
| 42 |
|
| 43 |
pb "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" |
| 44 |
"github.com/influxdata/yarpc/cmd/protoc-gen-yarpc/generator" |
| 45 |
"github.com/influxdata/yarpc/yarpcproto" |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
const generatedCodeVersion = 1 |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
const ( |
| 57 |
contextPkgPath = "context" |
| 58 |
yarpcPkgPath = "github.com/influxdata/yarpc" |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
type Plugin struct { |
| 64 |
gen *generator.Generator |
| 65 |
} |
| 66 |
|
| 67 |
var _ generator.Plugin = (*Plugin)(nil) |
| 68 |
|
| 69 |
|
| 70 |
func (g *Plugin) Name() string { |
| 71 |
return "yarpc" |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
var ( |
| 78 |
contextPkg string |
| 79 |
yarpcPkg string |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
func (g *Plugin) Init(gen *generator.Generator) { |
| 84 |
g.gen = gen |
| 85 |
contextPkg = generator.RegisterUniquePackageName("context", nil) |
| 86 |
yarpcPkg = generator.RegisterUniquePackageName("yarpc", nil) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
func (g *Plugin) objectNamed(name string) generator.Object { |
| 92 |
g.gen.RecordTypeUse(name) |
| 93 |
return g.gen.ObjectNamed(name) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func (g *Plugin) typeName(str string) string { |
| 98 |
return g.gen.TypeName(g.objectNamed(str)) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func (g *Plugin) P(args ...interface{}) { g.gen.P(args...) } |
| 103 |
|
| 104 |
|
| 105 |
func (g *Plugin) Generate(file *generator.FileDescriptor) { |
| 106 |
if len(file.FileDescriptorProto.Service) == 0 { |
| 107 |
return |
| 108 |
} |
| 109 |
|
| 110 |
g.P("// Reference imports to suppress errors if they are not otherwise used.") |
| 111 |
g.P("var _ ", contextPkg, ".Context") |
| 112 |
g.P("var _ ", yarpcPkg, ".ClientConn") |
| 113 |
g.P() |
| 114 |
|
| 115 |
|
| 116 |
g.P("// This is a compile-time assertion to ensure that this generated file") |
| 117 |
g.P("// is compatible with the yarpc package it is being compiled against.") |
| 118 |
g.P("const _ = ", yarpcPkg, ".SupportPackageIsVersion", generatedCodeVersion) |
| 119 |
g.P() |
| 120 |
|
| 121 |
for i, service := range file.FileDescriptorProto.Service { |
| 122 |
g.generateService(file, service, i) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
func (g *Plugin) GenerateImports(file *generator.FileDescriptor) { |
| 128 |
if len(file.FileDescriptorProto.Service) == 0 { |
| 129 |
return |
| 130 |
} |
| 131 |
g.P("import (") |
| 132 |
g.P(contextPkg, " ", strconv.Quote(path.Join(g.gen.ImportPrefix, contextPkgPath))) |
| 133 |
g.P(yarpcPkg, " ", strconv.Quote(path.Join(g.gen.ImportPrefix, yarpcPkgPath))) |
| 134 |
g.P(")") |
| 135 |
g.P() |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
var reservedClientName = map[string]bool{ |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
func unexport(s string) string { return strings.ToLower(s[:1]) + s[1:] } |
| 144 |
|
| 145 |
func (g *Plugin) getServiceIndex(service *pb.ServiceDescriptorProto) int { |
| 146 |
idx := yarpcproto.GetServiceIndex(service) |
| 147 |
if idx == -1 { |
| 148 |
g.gen.Error(fmt.Errorf("service %s missing yarpc_service_index option", service.GetName())) |
| 149 |
} |
| 150 |
|
| 151 |
if idx > 0xff { |
| 152 |
g.gen.Error(fmt.Errorf("service %s has invalid yarpc_service_index %d; must be less than 256", service.GetName(), idx)) |
| 153 |
} |
| 154 |
return idx |
| 155 |
} |
| 156 |
|
| 157 |
func (g *Plugin) getMethodIndex(service *pb.ServiceDescriptorProto, method *pb.MethodDescriptorProto) int { |
| 158 |
idx := yarpcproto.GetMethodIndex(method) |
| 159 |
if idx == -1 { |
| 160 |
g.gen.Error(fmt.Errorf("method %s.%s missing yarpc_method_index option", service.GetName(), method.GetName())) |
| 161 |
} |
| 162 |
|
| 163 |
if idx > 0xff { |
| 164 |
g.gen.Error(fmt.Errorf("method %s.%s has invalid yarpc_method_index %d; must be less than 256", service.GetName(), method.GetName(), idx)) |
| 165 |
} |
| 166 |
return idx |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
func (g *Plugin) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) { |
| 171 |
cpath := fmt.Sprintf("6,%d", index) |
| 172 |
|
| 173 |
origServName := service.GetName() |
| 174 |
fullServName := origServName |
| 175 |
if pkg := file.GetPackage(); pkg != "" { |
| 176 |
fullServName = pkg + "." + fullServName |
| 177 |
} |
| 178 |
servName := generator.CamelCase(origServName) |
| 179 |
|
| 180 |
g.P() |
| 181 |
g.P("// Client API for ", servName, " service") |
| 182 |
g.P() |
| 183 |
|
| 184 |
|
| 185 |
g.P("type ", servName, "Client interface {") |
| 186 |
for i, method := range service.Method { |
| 187 |
g.gen.PrintComments(fmt.Sprintf("%s,2,%d", cpath, i)) |
| 188 |
g.P(g.generateClientSignature(servName, method)) |
| 189 |
} |
| 190 |
g.P("}") |
| 191 |
g.P() |
| 192 |
|
| 193 |
|
| 194 |
g.P("type ", unexport(servName), "Client struct {") |
| 195 |
g.P("cc *", yarpcPkg, ".ClientConn") |
| 196 |
g.P("}") |
| 197 |
g.P() |
| 198 |
|
| 199 |
|
| 200 |
g.P("func New", servName, "Client (cc *", yarpcPkg, ".ClientConn) ", servName, "Client {") |
| 201 |
g.P("return &", unexport(servName), "Client{cc}") |
| 202 |
g.P("}") |
| 203 |
g.P() |
| 204 |
|
| 205 |
var methodIndex, streamIndex int |
| 206 |
serviceDescVar := "_" + servName + "_serviceDesc" |
| 207 |
|
| 208 |
for _, method := range service.Method { |
| 209 |
sidx := g.getServiceIndex(service) |
| 210 |
midx := g.getMethodIndex(service, method) |
| 211 |
|
| 212 |
var descExpr string |
| 213 |
if !method.GetServerStreaming() && !method.GetClientStreaming() { |
| 214 |
|
| 215 |
descExpr = fmt.Sprintf("&%s.Methods[%d]", serviceDescVar, methodIndex) |
| 216 |
methodIndex++ |
| 217 |
} else { |
| 218 |
|
| 219 |
descExpr = fmt.Sprintf("&%s.Streams[%d]", serviceDescVar, streamIndex) |
| 220 |
streamIndex++ |
| 221 |
} |
| 222 |
|
| 223 |
api := ((sidx << 8) | midx) & 0xffff |
| 224 |
g.generateClientMethod(servName, api, serviceDescVar, method, descExpr) |
| 225 |
} |
| 226 |
|
| 227 |
g.P("// Server API for ", servName, " service") |
| 228 |
g.P() |
| 229 |
|
| 230 |
|
| 231 |
serverType := servName + "Server" |
| 232 |
g.P("type ", serverType, " interface {") |
| 233 |
for i, method := range service.Method { |
| 234 |
g.gen.PrintComments(fmt.Sprintf("%s,2,%d", cpath, i)) |
| 235 |
g.P(g.generateServerSignature(servName, method)) |
| 236 |
} |
| 237 |
g.P("}") |
| 238 |
g.P() |
| 239 |
|
| 240 |
|
| 241 |
g.P("func Register", servName, "Server(s *", yarpcPkg, ".Server, srv ", serverType, ") {") |
| 242 |
g.P("s.RegisterService(&", serviceDescVar, `, srv)`) |
| 243 |
g.P("}") |
| 244 |
g.P() |
| 245 |
|
| 246 |
|
| 247 |
var handlerNames []string |
| 248 |
for _, method := range service.Method { |
| 249 |
hname := g.generateServerMethod(servName, fullServName, method) |
| 250 |
handlerNames = append(handlerNames, hname) |
| 251 |
} |
| 252 |
|
| 253 |
sidx := g.getServiceIndex(service) |
| 254 |
|
| 255 |
|
| 256 |
g.P("var ", serviceDescVar, " = ", yarpcPkg, ".ServiceDesc {") |
| 257 |
g.P("ServiceName: ", strconv.Quote(fullServName), ",") |
| 258 |
g.P("Index: ", sidx, ",") |
| 259 |
g.P("HandlerType: (*", serverType, ")(nil),") |
| 260 |
g.P("Methods: []", yarpcPkg, ".MethodDesc{") |
| 261 |
for i, method := range service.Method { |
| 262 |
if method.GetServerStreaming() || method.GetClientStreaming() { |
| 263 |
continue |
| 264 |
} |
| 265 |
midx := g.getMethodIndex(service, method) |
| 266 |
g.P("{") |
| 267 |
g.P("MethodName: ", strconv.Quote(method.GetName()), ",") |
| 268 |
g.P("Index: ", midx, ",") |
| 269 |
g.P("Handler: ", handlerNames[i], ",") |
| 270 |
g.P("},") |
| 271 |
} |
| 272 |
g.P("},") |
| 273 |
g.P("Streams: []", yarpcPkg, ".StreamDesc{") |
| 274 |
for i, method := range service.Method { |
| 275 |
if !method.GetServerStreaming() && !method.GetClientStreaming() { |
| 276 |
continue |
| 277 |
} |
| 278 |
midx := g.getMethodIndex(service, method) |
| 279 |
g.P("{") |
| 280 |
g.P("StreamName: ", strconv.Quote(method.GetName()), ",") |
| 281 |
g.P("Index: ", midx, ",") |
| 282 |
g.P("Handler: ", handlerNames[i], ",") |
| 283 |
if method.GetServerStreaming() { |
| 284 |
g.P("ServerStreams: true,") |
| 285 |
} |
| 286 |
if method.GetClientStreaming() { |
| 287 |
g.P("ClientStreams: true,") |
| 288 |
} |
| 289 |
g.P("},") |
| 290 |
} |
| 291 |
g.P("},") |
| 292 |
g.P("Metadata: \"", file.GetName(), "\",") |
| 293 |
g.P("}") |
| 294 |
g.P() |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
func (g *Plugin) generateClientSignature(servName string, method *pb.MethodDescriptorProto) string { |
| 299 |
origMethName := method.GetName() |
| 300 |
methName := generator.CamelCase(origMethName) |
| 301 |
if reservedClientName[methName] { |
| 302 |
methName += "_" |
| 303 |
} |
| 304 |
reqArg := ", in *" + g.typeName(method.GetInputType()) |
| 305 |
if method.GetClientStreaming() { |
| 306 |
reqArg = "" |
| 307 |
} |
| 308 |
respName := "*" + g.typeName(method.GetOutputType()) |
| 309 |
if method.GetServerStreaming() || method.GetClientStreaming() { |
| 310 |
respName = servName + "_" + generator.CamelCase(origMethName) + "Client" |
| 311 |
} |
| 312 |
return fmt.Sprintf("%s(ctx %s.Context%s) (%s, error)", methName, contextPkg, reqArg, respName) |
| 313 |
} |
| 314 |
|
| 315 |
func (g *Plugin) generateClientMethod(servName string, api int, serviceDescVar string, method *pb.MethodDescriptorProto, descExpr string) { |
| 316 |
apihex := fmt.Sprintf("0x%04x", api) |
| 317 |
methName := generator.CamelCase(method.GetName()) |
| 318 |
inType := g.typeName(method.GetInputType()) |
| 319 |
outType := g.typeName(method.GetOutputType()) |
| 320 |
|
| 321 |
g.P("func (c *", unexport(servName), "Client) ", g.generateClientSignature(servName, method), "{") |
| 322 |
if !method.GetServerStreaming() && !method.GetClientStreaming() { |
| 323 |
g.P("out := new(", outType, ")") |
| 324 |
|
| 325 |
g.P("err := ", yarpcPkg, `.Invoke(ctx, `, apihex, `, in, out, c.cc)`) |
| 326 |
g.P("if err != nil { return nil, err }") |
| 327 |
g.P("return out, nil") |
| 328 |
g.P("}") |
| 329 |
g.P() |
| 330 |
return |
| 331 |
} |
| 332 |
streamType := unexport(servName) + methName + "Client" |
| 333 |
g.P("stream, err := ", yarpcPkg, ".NewClientStream(ctx, ", descExpr, `, c.cc, `, apihex, `)`) |
| 334 |
g.P("if err != nil { return nil, err }") |
| 335 |
g.P("x := &", streamType, "{stream}") |
| 336 |
if !method.GetClientStreaming() { |
| 337 |
g.P("if err := x.ClientStream.SendMsg(in); err != nil { return nil, err }") |
| 338 |
|
| 339 |
} |
| 340 |
g.P("return x, nil") |
| 341 |
g.P("}") |
| 342 |
g.P() |
| 343 |
|
| 344 |
genSend := method.GetClientStreaming() |
| 345 |
genRecv := method.GetServerStreaming() |
| 346 |
genCloseAndRecv := !method.GetServerStreaming() |
| 347 |
|
| 348 |
|
| 349 |
g.P("type ", servName, "_", methName, "Client interface {") |
| 350 |
if genSend { |
| 351 |
g.P("Send(*", inType, ") error") |
| 352 |
} |
| 353 |
if genRecv { |
| 354 |
g.P("Recv() (*", outType, ", error)") |
| 355 |
} |
| 356 |
if genCloseAndRecv { |
| 357 |
g.P("CloseAndRecv() (*", outType, ", error)") |
| 358 |
} |
| 359 |
g.P(yarpcPkg, ".ClientStream") |
| 360 |
g.P("}") |
| 361 |
g.P() |
| 362 |
|
| 363 |
g.P("type ", streamType, " struct {") |
| 364 |
g.P(yarpcPkg, ".ClientStream") |
| 365 |
g.P("}") |
| 366 |
g.P() |
| 367 |
|
| 368 |
if genSend { |
| 369 |
g.P("func (x *", streamType, ") Send(m *", inType, ") error {") |
| 370 |
g.P("return x.ClientStream.SendMsg(m)") |
| 371 |
g.P("}") |
| 372 |
g.P() |
| 373 |
} |
| 374 |
if genRecv { |
| 375 |
g.P("func (x *", streamType, ") Recv() (*", outType, ", error) {") |
| 376 |
g.P("m := new(", outType, ")") |
| 377 |
g.P("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }") |
| 378 |
g.P("return m, nil") |
| 379 |
g.P("}") |
| 380 |
g.P() |
| 381 |
} |
| 382 |
if genCloseAndRecv { |
| 383 |
g.P("func (x *", streamType, ") CloseAndRecv() (*", outType, ", error) {") |
| 384 |
g.P("if err := x.ClientStream.CloseSend(); err != nil { return nil, err }") |
| 385 |
g.P("m := new(", outType, ")") |
| 386 |
g.P("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }") |
| 387 |
g.P("return m, nil") |
| 388 |
g.P("}") |
| 389 |
g.P() |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
func (g *Plugin) generateServerSignature(servName string, method *pb.MethodDescriptorProto) string { |
| 395 |
origMethName := method.GetName() |
| 396 |
methName := generator.CamelCase(origMethName) |
| 397 |
if reservedClientName[methName] { |
| 398 |
methName += "_" |
| 399 |
} |
| 400 |
|
| 401 |
var reqArgs []string |
| 402 |
ret := "error" |
| 403 |
if !method.GetServerStreaming() && !method.GetClientStreaming() { |
| 404 |
reqArgs = append(reqArgs, contextPkg+".Context") |
| 405 |
ret = "(*" + g.typeName(method.GetOutputType()) + ", error)" |
| 406 |
} |
| 407 |
if !method.GetClientStreaming() { |
| 408 |
reqArgs = append(reqArgs, "*"+g.typeName(method.GetInputType())) |
| 409 |
} |
| 410 |
if method.GetServerStreaming() || method.GetClientStreaming() { |
| 411 |
reqArgs = append(reqArgs, servName+"_"+generator.CamelCase(origMethName)+"Server") |
| 412 |
} |
| 413 |
|
| 414 |
return methName + "(" + strings.Join(reqArgs, ", ") + ") " + ret |
| 415 |
} |
| 416 |
|
| 417 |
func (g *Plugin) generateServerMethod(servName, fullServName string, method *pb.MethodDescriptorProto) string { |
| 418 |
methName := generator.CamelCase(method.GetName()) |
| 419 |
hname := fmt.Sprintf("_%s_%s_Handler", servName, methName) |
| 420 |
inType := g.typeName(method.GetInputType()) |
| 421 |
outType := g.typeName(method.GetOutputType()) |
| 422 |
|
| 423 |
if !method.GetServerStreaming() && !method.GetClientStreaming() { |
| 424 |
g.P("func ", hname, "(srv interface{}, ctx ", contextPkg, ".Context, dec func(interface{}) error) (interface{}, error) {") |
| 425 |
g.P("in := new(", inType, ")") |
| 426 |
g.P("if err := dec(in); err != nil { return nil, err }") |
| 427 |
g.P("return srv.(", servName, "Server).", methName, "(ctx, in)") |
| 428 |
g.P("}") |
| 429 |
g.P() |
| 430 |
return hname |
| 431 |
} |
| 432 |
streamType := unexport(servName) + methName + "Server" |
| 433 |
g.P("func ", hname, "(srv interface{}, stream ", yarpcPkg, ".ServerStream) error {") |
| 434 |
if !method.GetClientStreaming() { |
| 435 |
g.P("m := new(", inType, ")") |
| 436 |
g.P("if err := stream.RecvMsg(m); err != nil { return err }") |
| 437 |
g.P("return srv.(", servName, "Server).", methName, "(m, &", streamType, "{stream})") |
| 438 |
} else { |
| 439 |
g.P("return srv.(", servName, "Server).", methName, "(&", streamType, "{stream})") |
| 440 |
} |
| 441 |
g.P("}") |
| 442 |
g.P() |
| 443 |
|
| 444 |
genSend := method.GetServerStreaming() |
| 445 |
genSendAndClose := !method.GetServerStreaming() |
| 446 |
genRecv := method.GetClientStreaming() |
| 447 |
|
| 448 |
|
| 449 |
g.P("type ", servName, "_", methName, "Server interface {") |
| 450 |
if genSend { |
| 451 |
g.P("Send(*", outType, ") error") |
| 452 |
} |
| 453 |
if genSendAndClose { |
| 454 |
g.P("SendAndClose(*", outType, ") error") |
| 455 |
} |
| 456 |
if genRecv { |
| 457 |
g.P("Recv() (*", inType, ", error)") |
| 458 |
} |
| 459 |
g.P(yarpcPkg, ".ServerStream") |
| 460 |
g.P("}") |
| 461 |
g.P() |
| 462 |
|
| 463 |
g.P("type ", streamType, " struct {") |
| 464 |
g.P(yarpcPkg, ".ServerStream") |
| 465 |
g.P("}") |
| 466 |
g.P() |
| 467 |
|
| 468 |
if genSend { |
| 469 |
g.P("func (x *", streamType, ") Send(m *", outType, ") error {") |
| 470 |
g.P("return x.ServerStream.SendMsg(m)") |
| 471 |
g.P("}") |
| 472 |
g.P() |
| 473 |
} |
| 474 |
if genSendAndClose { |
| 475 |
g.P("func (x *", streamType, ") SendAndClose(m *", outType, ") error {") |
| 476 |
g.P("return x.ServerStream.SendMsg(m)") |
| 477 |
g.P("}") |
| 478 |
g.P() |
| 479 |
} |
| 480 |
if genRecv { |
| 481 |
g.P("func (x *", streamType, ") Recv() (*", inType, ", error) {") |
| 482 |
g.P("m := new(", inType, ")") |
| 483 |
g.P("if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err }") |
| 484 |
g.P("return m, nil") |
| 485 |
g.P("}") |
| 486 |
g.P() |
| 487 |
} |
| 488 |
|
| 489 |
return hname |
| 490 |
} |