ClientFile returns the client file for the given service.
(_ string, service *expr.ServiceExpr, services *ServicesData)
| 14 | |
| 15 | // ClientFile returns the client file for the given service. |
| 16 | func ClientFile(_ string, service *expr.ServiceExpr, services *ServicesData) *codegen.File { |
| 17 | svc := services.Get(service.Name) |
| 18 | data := endpointData(svc) |
| 19 | path := filepath.Join(codegen.Gendir, svc.PathName, "client.go") |
| 20 | var ( |
| 21 | sections []*codegen.SectionTemplate |
| 22 | ) |
| 23 | { |
| 24 | imports := []*codegen.ImportSpec{ |
| 25 | {Path: "context"}, |
| 26 | {Path: "io"}, |
| 27 | codegen.GoaImport(""), |
| 28 | } |
| 29 | header := codegen.Header(service.Name+" client", svc.PkgName, imports) |
| 30 | def := &codegen.SectionTemplate{ |
| 31 | Name: "client-struct", |
| 32 | Source: serviceTemplates.Read(serviceClientT), |
| 33 | Data: data, |
| 34 | } |
| 35 | init := &codegen.SectionTemplate{ |
| 36 | Name: "client-init", |
| 37 | Source: serviceTemplates.Read(serviceClientInitT), |
| 38 | Data: data, |
| 39 | } |
| 40 | sections = make([]*codegen.SectionTemplate, 0, 3+len(data.Methods)) |
| 41 | sections = append(sections, header, def, init) |
| 42 | for _, m := range data.Methods { |
| 43 | sections = append(sections, &codegen.SectionTemplate{ |
| 44 | Name: "client-method", |
| 45 | Source: serviceTemplates.Read(serviceClientMethodT), |
| 46 | Data: m, |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return &codegen.File{Path: path, SectionTemplates: sections} |
| 52 | } |