endpointParser returns the file that implements the command line parser that builds the client endpoint and payload necessary to perform a request.
(genpkg string, services *ServicesData, svr *expr.ServerExpr, data []*cli.CommandData)
| 47 | // endpointParser returns the file that implements the command line parser that |
| 48 | // builds the client endpoint and payload necessary to perform a request. |
| 49 | func endpointParser(genpkg string, services *ServicesData, svr *expr.ServerExpr, data []*cli.CommandData) *codegen.File { |
| 50 | pkg := codegen.SnakeCase(codegen.Goify(svr.Name, true)) |
| 51 | fpath := filepath.Join(codegen.Gendir, "grpc", "cli", pkg, "cli.go") |
| 52 | title := svr.Name + " gRPC client CLI support package" |
| 53 | specs := []*codegen.ImportSpec{ |
| 54 | {Path: "context"}, |
| 55 | {Path: "flag"}, |
| 56 | {Path: "fmt"}, |
| 57 | {Path: "os"}, |
| 58 | {Path: "strconv"}, |
| 59 | {Path: "unicode/utf8"}, |
| 60 | codegen.GoaImport(""), |
| 61 | codegen.GoaNamedImport("grpc", "goagrpc"), |
| 62 | {Path: "google.golang.org/grpc", Name: "grpc"}, |
| 63 | } |
| 64 | // Add structpb import if Any type is used |
| 65 | needsAnyPb := false |
| 66 | for _, svc := range services.Root.API.GRPC.Services { |
| 67 | for _, e := range svc.GRPCEndpoints { |
| 68 | if hasAnyType(e.MethodExpr.Payload) || hasAnyType(e.MethodExpr.Result) { |
| 69 | needsAnyPb = true |
| 70 | break |
| 71 | } |
| 72 | } |
| 73 | if needsAnyPb { |
| 74 | break |
| 75 | } |
| 76 | } |
| 77 | if needsAnyPb { |
| 78 | specs = append(specs, |
| 79 | &codegen.ImportSpec{Path: "google.golang.org/protobuf/types/known/structpb", Name: "structpb"}, |
| 80 | ) |
| 81 | } |
| 82 | for _, svc := range services.Root.API.GRPC.Services { |
| 83 | sd := services.Get(svc.Name()) |
| 84 | if sd == nil { |
| 85 | continue |
| 86 | } |
| 87 | svcName := sd.Service.PathName |
| 88 | specs = append(specs, |
| 89 | &codegen.ImportSpec{Path: path.Join(genpkg, "grpc", svcName, "client"), Name: sd.Service.PkgName + "c"}, |
| 90 | &codegen.ImportSpec{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: svcName + pbPkgName}) |
| 91 | // Add interceptors import if service has client interceptors |
| 92 | if len(sd.Service.ClientInterceptors) > 0 { |
| 93 | specs = append(specs, &codegen.ImportSpec{ |
| 94 | Path: genpkg + "/" + sd.Service.PathName, |
| 95 | Name: sd.Service.PkgName, |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | sections := make([]*codegen.SectionTemplate, 0, 4+len(data)) |
| 101 | sections = append(sections, |
| 102 | codegen.Header(title, "cli", specs), |
| 103 | cli.UsageCommands(data), |
| 104 | cli.UsageExamples(data), |
| 105 | &codegen.SectionTemplate{ |
| 106 | Name: "parse-endpoint-grpc", |
no test coverage detected