Files returns the generated files for the given service as well as a map indexing user type names by custom path as defined by the "struct:pkg:path" metadata. The map is built over each invocation of Files to avoid duplicate type definitions.
(genpkg string, service *expr.ServiceExpr, services *ServicesData, userTypePkgs map[string][]string)
| 15 | // metadata. The map is built over each invocation of Files to avoid duplicate |
| 16 | // type definitions. |
| 17 | func Files(genpkg string, service *expr.ServiceExpr, services *ServicesData, userTypePkgs map[string][]string) []*codegen.File { |
| 18 | svc := services.Get(service.Name) |
| 19 | svcName := svc.PathName |
| 20 | svcPath := filepath.Join(codegen.Gendir, svcName, "service.go") |
| 21 | seen := make(map[string]struct{}) |
| 22 | typeDefSections := make(map[string]map[string]*codegen.SectionTemplate) |
| 23 | typesByPath := make(map[string][]string) |
| 24 | svcSections := make([]*codegen.SectionTemplate, 0, 10) |
| 25 | |
| 26 | addTypeDefSection := func(path, name string, section *codegen.SectionTemplate) { |
| 27 | if typeDefSections[path] == nil { |
| 28 | typeDefSections[path] = make(map[string]*codegen.SectionTemplate) |
| 29 | } |
| 30 | typeDefSections[path][name] = section |
| 31 | typesByPath[path] = append(typesByPath[path], name) |
| 32 | seen[name] = struct{}{} |
| 33 | } |
| 34 | |
| 35 | for _, m := range svc.Methods { |
| 36 | payloadPath := pathWithDefault(m.PayloadLoc, svcPath) |
| 37 | resultPath := pathWithDefault(m.ResultLoc, svcPath) |
| 38 | if m.PayloadDef != "" { |
| 39 | if _, ok := seen[m.Payload]; !ok { |
| 40 | addTypeDefSection(payloadPath, m.Payload, &codegen.SectionTemplate{ |
| 41 | Name: "service-payload", |
| 42 | Source: serviceTemplates.Read(payloadT), |
| 43 | Data: m, |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | if m.StreamingPayloadDef != "" { |
| 48 | if _, ok := seen[m.StreamingPayload]; !ok { |
| 49 | addTypeDefSection(payloadPath, m.StreamingPayload, &codegen.SectionTemplate{ |
| 50 | Name: "service-streaming-payload", |
| 51 | Source: serviceTemplates.Read(streamingPayloadT), |
| 52 | Data: m, |
| 53 | }) |
| 54 | } |
| 55 | } |
| 56 | if m.ResultDef != "" { |
| 57 | if _, ok := seen[m.Result]; !ok { |
| 58 | addTypeDefSection(resultPath, m.Result, &codegen.SectionTemplate{ |
| 59 | Name: "service-result", |
| 60 | Source: serviceTemplates.Read(resultT), |
| 61 | Data: m, |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | // Generate streaming result type if different from result |
| 66 | if m.StreamingResultDef != "" && m.StreamingResult != m.Result { |
| 67 | if _, ok := seen[m.StreamingResult]; !ok { |
| 68 | addTypeDefSection(resultPath, m.StreamingResult, &codegen.SectionTemplate{ |
| 69 | Name: "service-streaming-result", |
| 70 | Source: serviceTemplates.Read(resultT), |
| 71 | Data: map[string]any{ |
| 72 | "Result": m.StreamingResult, |
| 73 | "ResultDef": m.StreamingResultDef, |
| 74 | "ResultDesc": m.StreamingResultDesc, |