Example iterates through the roots and returns files that implement an example service, server, and client.
(genpkg string, roots []eval.Root)
| 14 | // Example iterates through the roots and returns files that implement an |
| 15 | // example service, server, and client. |
| 16 | func Example(genpkg string, roots []eval.Root) ([]*codegen.File, error) { |
| 17 | var files []*codegen.File |
| 18 | for _, root := range roots { |
| 19 | r, ok := root.(*expr.RootExpr) |
| 20 | if !ok { |
| 21 | continue // could be a plugin root expression |
| 22 | } |
| 23 | |
| 24 | // Create service data |
| 25 | services := service.NewServicesData(r) |
| 26 | for _, s := range r.Services { |
| 27 | service.SetUserTypeImports(genpkg, services.Get(s.Name)) |
| 28 | } |
| 29 | |
| 30 | // example service implementation |
| 31 | if fs := service.ExampleServiceFiles(genpkg, r, services); len(fs) != 0 { |
| 32 | files = append(files, fs...) |
| 33 | } |
| 34 | |
| 35 | // example interceptors implementation |
| 36 | if fs := service.ExampleInterceptorsFiles(genpkg, r, services); len(fs) != 0 { |
| 37 | files = append(files, fs...) |
| 38 | } |
| 39 | |
| 40 | // server main |
| 41 | if fs := example.ServerFiles(genpkg, r, services); len(fs) != 0 { |
| 42 | files = append(files, fs...) |
| 43 | } |
| 44 | |
| 45 | // CLI main |
| 46 | if fs := example.CLIFiles(genpkg, r); len(fs) != 0 { |
| 47 | files = append(files, fs...) |
| 48 | } |
| 49 | |
| 50 | // HTTP |
| 51 | if len(r.API.HTTP.Services) > 0 { |
| 52 | httpServices := httpcodegen.NewServicesData(services, r.API.HTTP) |
| 53 | if fs := httpcodegen.ExampleServerFiles(genpkg, httpServices); len(fs) != 0 { |
| 54 | files = append(files, fs...) |
| 55 | } |
| 56 | if fs := httpcodegen.ExampleCLIFiles(genpkg, httpServices); len(fs) != 0 { |
| 57 | files = append(files, fs...) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // JSON-RPC |
| 62 | if len(r.API.JSONRPC.Services) > 0 { |
| 63 | jsonrpcServices := httpcodegen.NewServicesData(services, &r.API.JSONRPC.HTTPExpr) |
| 64 | if fs := jsonrpccodegen.ExampleServerFiles(genpkg, jsonrpcServices, files); len(fs) > 0 { |
| 65 | files = append(files, fs...) |
| 66 | } |
| 67 | if fs := jsonrpccodegen.ExampleCLIFiles(genpkg, jsonrpcServices); len(fs) > 0 { |
| 68 | files = append(files, fs...) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // GRPC |
| 73 | if len(r.API.GRPC.Services) > 0 { |
nothing calls this directly
no test coverage detected