|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "goa.design/goa/v3/codegen" |
| 10 | + "goa.design/goa/v3/expr" |
| 11 | +) |
| 12 | + |
| 13 | +// ExampleInterceptorsFiles returns the files for the example server and client interceptors. |
| 14 | +func ExampleInterceptorsFiles(genpkg string, r *expr.RootExpr) []*codegen.File { |
| 15 | + var fw []*codegen.File |
| 16 | + for _, svc := range r.Services { |
| 17 | + if f := exampleInterceptorsFile(genpkg, svc); f != nil { |
| 18 | + fw = append(fw, f...) |
| 19 | + } |
| 20 | + } |
| 21 | + return fw |
| 22 | +} |
| 23 | + |
| 24 | +// exampleInterceptorsFile returns the example interceptors for the given service. |
| 25 | +func exampleInterceptorsFile(genpkg string, svc *expr.ServiceExpr) []*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: "exmaple-server-interceptor", |
| 53 | + Source: readTemplate("example_server_interceptor"), |
| 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: readTemplate("example_client_interceptor"), |
| 78 | + Data: data, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return files |
| 86 | +} |
0 commit comments