MCPcopy Create free account
hub / github.com/goadesign/goa / endpointParser

Function endpointParser

grpc/codegen/client_cli.go:49–121  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

47// endpointParser returns the file that implements the command line parser that
48// builds the client endpoint and payload necessary to perform a request.
49func 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",

Callers 1

ClientCLIFilesFunction · 0.70

Calls 13

SnakeCaseFunction · 0.92
GoifyFunction · 0.92
GoaImportFunction · 0.92
GoaNamedImportFunction · 0.92
HeaderFunction · 0.92
UsageCommandsFunction · 0.92
UsageExamplesFunction · 0.92
FlagsCodeFunction · 0.92
CommandUsageFunction · 0.92
hasAnyTypeFunction · 0.85
NameMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected