@@ -29,35 +29,41 @@ func InterceptorsFiles(genpkg string, service *expr.ServiceExpr) []*codegen.File
29
29
}
30
30
31
31
// interceptorFile returns the file defining the interceptors.
32
+ // This method is called twice, once for the server and once for the client.
32
33
func interceptorFile (svc * Data , server bool ) * codegen.File {
33
34
filename := "client_interceptors.go"
34
35
template := "client_interceptors"
35
- section := "client-interceptors"
36
+ section := "client-interceptors-type "
36
37
desc := "Client Interceptors"
37
- var data []* InterceptorData
38
38
if server {
39
39
filename = "service_interceptors.go"
40
40
template = "server_interceptors"
41
- section = "server-interceptors"
41
+ section = "server-interceptors-type "
42
42
desc = "Server Interceptors"
43
- data = svc .ServerInterceptors
44
43
}
45
44
desc = svc .Name + desc
46
45
path := filepath .Join (codegen .Gendir , svc .PathName , filename )
47
46
47
+ interceptors := svc .ServerInterceptors
48
48
if ! server {
49
- // We don't want to generate duplicate interceptor info data structures for
50
- // interceptors that are both server and client side.
51
- serverInterceptors := make (map [string ]struct {}, len (svc .ServerInterceptors ))
52
- for _ , intr := range svc .ServerInterceptors {
53
- serverInterceptors [intr .Name ] = struct {}{}
49
+ interceptors = svc .ClientInterceptors
50
+ }
51
+
52
+ // We don't want to generate duplicate interceptor info data structures for
53
+ // interceptors that are both server and client side so remove interceptors
54
+ // that are both server and client side when generating the client.
55
+ if ! server {
56
+ names := make (map [string ]struct {}, len (svc .ServerInterceptors ))
57
+ for _ , sin := range svc .ServerInterceptors {
58
+ names [sin .Name ] = struct {}{}
54
59
}
55
- for _ , intr := range svc .ClientInterceptors {
56
- if _ , ok := serverInterceptors [intr .Name ]; ok {
57
- continue
60
+ filtered := make ([]* InterceptorData , 0 , len (interceptors ))
61
+ for _ , in := range interceptors {
62
+ if _ , ok := names [in .Name ]; ! ok {
63
+ filtered = append (filtered , in )
58
64
}
59
- data = append (data , intr )
60
65
}
66
+ interceptors = filtered
61
67
}
62
68
63
69
sections := []* codegen.SectionTemplate {
@@ -66,45 +72,59 @@ func interceptorFile(svc *Data, server bool) *codegen.File {
66
72
codegen .GoaImport ("" ),
67
73
}),
68
74
{
69
- Name : " section" + "-struct" ,
75
+ Name : section ,
70
76
Source : readTemplate (template ),
71
77
Data : svc ,
72
78
},
73
79
}
74
- if len (data ) > 0 {
80
+ if len (interceptors ) > 0 {
75
81
sections = append (sections , & codegen.SectionTemplate {
76
- Name : section ,
77
- Source : readTemplate ("interceptors " ),
78
- Data : data ,
82
+ Name : "interceptor-types" ,
83
+ Source : readTemplate ("interceptors_types " ),
84
+ Data : interceptors ,
79
85
FuncMap : map [string ]any {
80
86
"hasPrivateImplementationTypes" : hasPrivateImplementationTypes ,
81
87
},
82
88
})
83
89
}
84
90
85
- // Add wrapper sections for each method that has interceptors
91
+ template = "endpoint_wrappers"
92
+ section = "endpoint-wrapper"
93
+ if ! server {
94
+ template = "client_wrappers"
95
+ section = "client-wrapper"
96
+ }
86
97
for _ , m := range svc .Methods {
87
- if server && len (m .ServerInterceptors ) == 0 || ! server && len (m .ClientInterceptors ) == 0 {
88
- continue
89
- }
90
- template := "endpoint_wrappers"
91
- templateName := "endpoint-wrapper"
98
+ ints := m .ServerInterceptors
92
99
if ! server {
93
- template = "client_wrappers"
94
- templateName = "client-wrapper"
100
+ ints = m .ClientInterceptors
101
+ }
102
+ if len (ints ) == 0 {
103
+ continue
95
104
}
96
105
sections = append (sections , & codegen.SectionTemplate {
97
- Name : templateName ,
106
+ Name : section ,
98
107
Source : readTemplate (template ),
99
108
Data : map [string ]interface {}{
100
- "MethodVarName" : codegen .Goify (m .Name , true ),
101
- "Method" : m .Name ,
102
- "Service" : svc .Name ,
103
- "ServerInterceptors" : m .ServerInterceptors ,
104
- "ClientInterceptors" : m .ClientInterceptors ,
109
+ "MethodVarName" : m .VarName ,
110
+ "Method" : m .Name ,
111
+ "Service" : svc .Name ,
112
+ "Interceptors" : ints ,
105
113
},
106
114
})
107
115
}
116
+
117
+ if len (interceptors ) > 0 {
118
+ sections = append (sections , & codegen.SectionTemplate {
119
+ Name : "interceptors" ,
120
+ Source : readTemplate ("interceptors" ),
121
+ Data : interceptors ,
122
+ FuncMap : map [string ]any {
123
+ "hasPrivateImplementationTypes" : hasPrivateImplementationTypes ,
124
+ },
125
+ })
126
+ }
127
+
108
128
return & codegen.File {Path : path , SectionTemplates : sections }
109
129
}
110
130
0 commit comments