-
-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathclient_types.go
More file actions
116 lines (109 loc) · 3.04 KB
/
client_types.go
File metadata and controls
116 lines (109 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package codegen
import (
"path"
"path/filepath"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
)
// ClientTypeFiles returns the client types files containing all the client
// interfaces and types needed to implement gRPC client.
func ClientTypeFiles(genpkg string, services *ServicesData) []*codegen.File {
fw := make([]*codegen.File, len(services.Root.API.GRPC.Services))
for i, svc := range services.Root.API.GRPC.Services {
fw[i] = clientType(genpkg, svc, services)
}
return fw
}
// clientType returns the file defining the gRPC client types.
func clientType(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData) *codegen.File {
var (
initData []*InitData
sd = services.Get(svc.Name())
)
{
seen := make(map[string]struct{})
collect := func(c *ConvertData) {
if c.Init == nil {
return
}
if _, ok := seen[c.Init.Name]; ok {
return
}
seen[c.Init.Name] = struct{}{}
initData = append(initData, c.Init)
}
for _, a := range svc.GRPCEndpoints {
ed := sd.Endpoint(a.Name())
if c := ed.Request.ClientConvert; c != nil {
collect(c)
}
if c := ed.Response.ClientConvert; c != nil {
collect(c)
}
if ed.ClientStream != nil {
if c := ed.ClientStream.RecvConvert; c != nil {
collect(c)
}
if c := ed.ClientStream.SendConvert; c != nil {
collect(c)
}
}
for _, e := range ed.Errors {
if c := e.Response.ClientConvert; c != nil {
collect(c)
}
}
}
}
var (
fpath string
sections []*codegen.SectionTemplate
)
{
svcName := sd.Service.PathName
fpath = filepath.Join(codegen.Gendir, "grpc", svcName, "client", "types.go")
imports := []*codegen.ImportSpec{
{Path: "unicode/utf8"},
codegen.GoaImport(""),
{Path: path.Join(genpkg, svcName), Name: sd.Service.PkgName},
{Path: path.Join(genpkg, svcName, "views"), Name: sd.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: sd.PkgName},
}
imports = append(imports, sd.Service.ProtoImports...)
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC client types", "client", imports)}
for _, init := range initData {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-type-init",
Source: readTemplate("type_init"),
Data: init,
FuncMap: map[string]any{
"isAlias": expr.IsAlias,
"fullName": func(dt expr.DataType) string {
if loc := codegen.UserTypeLocation(dt); loc != nil {
return loc.PackageName() + "." + dt.Name()
}
return dt.Name()
},
},
})
}
for _, data := range sd.validations {
if data.Kind == validateServer {
continue
}
sections = append(sections, &codegen.SectionTemplate{
Name: "client-validate",
Source: readTemplate("validate"),
Data: data,
})
}
for _, h := range sd.transformHelpers {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-transform-helper",
Source: readTemplate("transform_helper"),
Data: h,
})
}
}
return &codegen.File{Path: fpath, SectionTemplates: sections}
}