analyze creates the data necessary to render the code of the given service.
(gs *expr.GRPCServiceExpr)
| 468 | |
| 469 | // analyze creates the data necessary to render the code of the given service. |
| 470 | func (d *ServicesData) analyze(gs *expr.GRPCServiceExpr) *ServiceData { |
| 471 | svc := d.ServicesData.Get(gs.Name()) |
| 472 | scope := codegen.NewNameScope() |
| 473 | pkg := codegen.SnakeCase(codegen.Goify(svc.Name, false)) + pbPkgName |
| 474 | svcVarN := scope.HashedUnique(gs.ServiceExpr, codegen.Goify(svc.Name, true)) |
| 475 | sd := &ServiceData{ |
| 476 | Service: svc, |
| 477 | Name: svcVarN, |
| 478 | Description: svc.Description, |
| 479 | PkgName: pkg, |
| 480 | ServerStruct: "Server", |
| 481 | ClientStruct: "Client", |
| 482 | ServerInit: "New", |
| 483 | ClientInit: "NewClient", |
| 484 | ServerInterface: svcVarN + "Server", |
| 485 | ClientInterface: svcVarN + "Client", |
| 486 | ClientInterfaceInit: fmt.Sprintf("%s.New%sClient", pkg, svcVarN), |
| 487 | Scope: scope, |
| 488 | } |
| 489 | seen, imported := make(map[string]struct{}), make(map[string]struct{}) |
| 490 | for _, e := range gs.GRPCEndpoints { |
| 491 | hasRequestMessage := !isEmpty(e.Request.Type) |
| 492 | useStreamEnvelope := usesStreamEnvelope(e) |
| 493 | |
| 494 | // convert request and response types to protocol buffer message types |
| 495 | e.Request = makeProtoBufMessage(e.Request, protoBufify(e.Name()+"_request", true, true), sd) |
| 496 | if e.MethodExpr.StreamingPayload.Type != expr.Empty { |
| 497 | streamMessageName := protoBufify(e.Name()+"_streaming_request", true, true) |
| 498 | if useStreamEnvelope { |
| 499 | streamMessageName = protoBufify(e.Name()+"_stream_item", true, true) |
| 500 | } |
| 501 | e.StreamingRequest = makeProtoBufMessage(e.StreamingRequest, streamMessageName, sd) |
| 502 | } |
| 503 | var requestEnvelope *expr.AttributeExpr |
| 504 | if useStreamEnvelope { |
| 505 | requestEnvelope = makeProtoBufStreamEnvelope( |
| 506 | e.Request, |
| 507 | e.StreamingRequest, |
| 508 | protoBufify(e.Name()+"_streaming_request", true, true), |
| 509 | sd, |
| 510 | ) |
| 511 | } |
| 512 | e.Response.Message = makeProtoBufMessage(e.Response.Message, protoBufify(e.Name()+"_response", true, true), sd) |
| 513 | for _, er := range e.GRPCErrors { |
| 514 | if er.Type == expr.ErrorResult || !expr.IsObject(er.Type) { |
| 515 | continue |
| 516 | } |
| 517 | er.Response.Message = makeProtoBufMessage(er.Response.Message, protoBufify(e.Name()+"_"+er.Name+"_error", true, true), sd) |
| 518 | } |
| 519 | |
| 520 | // collect all the nested messages and return the top-level message |
| 521 | // Also collect all proto imports specified via Meta. |
| 522 | collect := func(att *expr.AttributeExpr) *service.UserTypeData { |
| 523 | msgs, imports := collectMessages(att, sd, seen) |
| 524 | if len(imports) > 0 { |
| 525 | for _, imp := range imports { |
| 526 | if _, ok := imported[imp]; ok { |
| 527 | continue |
no test coverage detected