clientType return the file containing the type definitions used by the HTTP transport for the given service client. seen keeps track of the names of the types that have already been generated to prevent duplicate code generation. Below are the rules governing whether values are pointers or not. Not
(genpkg string, svc *expr.HTTPServiceExpr, seen map[string]struct{}, services *ServicesData)
| 41 | // - Response header variables hold pointers when not required and have no |
| 42 | // default value. |
| 43 | func clientType(genpkg string, svc *expr.HTTPServiceExpr, seen map[string]struct{}, services *ServicesData) *codegen.File { |
| 44 | var ( |
| 45 | path string |
| 46 | data = services.Get(svc.Name()) |
| 47 | svcName = data.Service.PathName |
| 48 | ) |
| 49 | path = filepath.Join(codegen.Gendir, "http", svcName, "client", "types.go") |
| 50 | imports := []*codegen.ImportSpec{ |
| 51 | {Path: "encoding/json"}, |
| 52 | {Path: "fmt"}, |
| 53 | {Path: "unicode/utf8"}, |
| 54 | {Path: genpkg + "/" + svcName, Name: data.Service.PkgName}, |
| 55 | {Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg}, |
| 56 | codegen.GoaImport(""), |
| 57 | } |
| 58 | header := codegen.Header(svc.Name()+" HTTP client types", "client", imports) |
| 59 | |
| 60 | var ( |
| 61 | initData []*InitData |
| 62 | validatedTypes []*TypeData |
| 63 | seenValidated = make(map[string]struct{}) // Track validated types to avoid duplicates |
| 64 | seenInit = make(map[string]struct{}) // Track init functions to avoid duplicates |
| 65 | |
| 66 | sections = []*codegen.SectionTemplate{header} |
| 67 | ) |
| 68 | |
| 69 | // request body types |
| 70 | for _, a := range svc.HTTPEndpoints { |
| 71 | adata := data.Endpoint(a.Name()) |
| 72 | if data := adata.Payload.Request.ClientBody; data != nil { |
| 73 | if _, ok := seen[data.Ref]; ok { |
| 74 | continue |
| 75 | } |
| 76 | seen[data.Ref] = struct{}{} |
| 77 | if data.Def != "" { |
| 78 | sections = append(sections, &codegen.SectionTemplate{ |
| 79 | Name: "client-request-body", |
| 80 | Source: httpTemplates.Read(typeDeclT), |
| 81 | Data: data, |
| 82 | }) |
| 83 | } |
| 84 | if data.Init != nil { |
| 85 | if _, ok := seenInit[data.Init.Name]; !ok { |
| 86 | seenInit[data.Init.Name] = struct{}{} |
| 87 | initData = append(initData, data.Init) |
| 88 | } |
| 89 | } |
| 90 | if data.ValidateDef != "" { |
| 91 | if _, ok := seenValidated[data.Name]; !ok { |
| 92 | seenValidated[data.Name] = struct{}{} |
| 93 | validatedTypes = append(validatedTypes, data) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | if adata.ClientWebSocket != nil { |
| 98 | if data := adata.ClientWebSocket.Payload; data != nil { |
| 99 | if _, ok := seen[data.Ref]; ok { |
| 100 | continue |