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

Function exampleInterceptorsFile

codegen/service/example_interceptors.go:25–86  ·  view source on GitHub ↗

exampleInterceptorsFile returns the example interceptors for the given service.

(genpkg string, svc *expr.ServiceExpr, services *ServicesData)

Source from the content-addressed store, hash-verified

23
24// exampleInterceptorsFile returns the example interceptors for the given service.
25func exampleInterceptorsFile(genpkg string, svc *expr.ServiceExpr, services *ServicesData) []*codegen.File {
26 sdata := services.Get(svc.Name)
27 data := map[string]any{
28 "ServiceName": sdata.Name,
29 "StructName": sdata.StructName,
30 "PkgName": "interceptors",
31 "ServerInterceptors": sdata.ServerInterceptors,
32 "ClientInterceptors": sdata.ClientInterceptors,
33 }
34
35 var files []*codegen.File
36
37 // Generate server interceptor if needed and file doesn't exist
38 if len(sdata.ServerInterceptors) > 0 {
39 serverPath := filepath.Join("interceptors", sdata.PathName+"_server.go")
40 if _, err := os.Stat(serverPath); os.IsNotExist(err) {
41 files = append(files, &codegen.File{
42 Path: serverPath,
43 SectionTemplates: []*codegen.SectionTemplate{
44 codegen.Header(fmt.Sprintf("%s example server interceptors", sdata.Name), "interceptors", []*codegen.ImportSpec{
45 {Path: "context"},
46 {Path: "fmt"},
47 {Path: "goa.design/clue/log"},
48 codegen.GoaImport(""),
49 {Path: path.Join(genpkg, sdata.PathName), Name: sdata.PkgName},
50 }),
51 {
52 Name: "example-server-interceptor",
53 Source: serviceTemplates.Read(exampleServerInterceptorT),
54 Data: data,
55 },
56 },
57 })
58 }
59 }
60
61 // Generate client interceptor if needed and file doesn't exist
62 if len(sdata.ClientInterceptors) > 0 {
63 clientPath := filepath.Join("interceptors", sdata.PathName+"_client.go")
64 if _, err := os.Stat(clientPath); os.IsNotExist(err) {
65 files = append(files, &codegen.File{
66 Path: clientPath,
67 SectionTemplates: []*codegen.SectionTemplate{
68 codegen.Header(fmt.Sprintf("%s example client interceptors", sdata.Name), "interceptors", []*codegen.ImportSpec{
69 {Path: "context"},
70 {Path: "fmt"},
71 {Path: "goa.design/clue/log"},
72 codegen.GoaImport(""),
73 {Path: path.Join(genpkg, sdata.PathName), Name: sdata.PkgName},
74 }),
75 {
76 Name: "example-client-interceptor",
77 Source: serviceTemplates.Read(exampleClientInterceptorT),
78 Data: data,
79 },
80 },
81 })
82 }

Callers 1

ExampleInterceptorsFilesFunction · 0.85

Calls 4

HeaderFunction · 0.92
GoaImportFunction · 0.92
GetMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected