Skip to content

Commit 81835c3

Browse files
committed
Update InterceptorInfo comment and replace Send and Recv boolean fields with a CallType enum; organize interceptor wrappers file sections as types, functions, then methods
1 parent 79eafc5 commit 81835c3

8 files changed

+188
-122
lines changed

codegen/service/interceptors.go

+52-7
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,41 @@ func wrapperFile(svc *Data) *codegen.File {
140140
codegen.GoaImport(""),
141141
}))
142142

143-
// Generate the interceptor wrapper functions first (only once)
143+
// Generate any interceptor stream wrapper struct types first
144+
var wrappedServerStreams, wrappedClientStreams []*StreamInterceptorData
145+
if len(svc.ServerInterceptors) > 0 {
146+
wrappedServerStreams = collectWrappedStreams(svc.ServerInterceptors, true)
147+
if len(wrappedServerStreams) > 0 {
148+
sections = append(sections, &codegen.SectionTemplate{
149+
Name: "server-interceptor-stream-wrapper-types",
150+
Source: readTemplate("server_interceptor_stream_wrapper_types"),
151+
Data: map[string]interface{}{
152+
"WrappedServerStreams": wrappedServerStreams,
153+
},
154+
})
155+
}
156+
}
157+
if len(svc.ClientInterceptors) > 0 {
158+
wrappedClientStreams = collectWrappedStreams(svc.ClientInterceptors, false)
159+
if len(wrappedClientStreams) > 0 {
160+
sections = append(sections, &codegen.SectionTemplate{
161+
Name: "client-interceptor-stream-wrapper-types",
162+
Source: readTemplate("client_interceptor_stream_wrapper_types"),
163+
Data: map[string]interface{}{
164+
"WrappedClientStreams": wrappedClientStreams,
165+
},
166+
})
167+
}
168+
}
169+
170+
// Generate the interceptor wrapper functions next (only once)
144171
if len(svc.ServerInterceptors) > 0 {
145172
sections = append(sections, &codegen.SectionTemplate{
146173
Name: "server-interceptor-wrappers",
147174
Source: readTemplate("server_interceptor_wrappers"),
148175
Data: map[string]interface{}{
149-
"Service": svc.Name,
150-
"ServerInterceptors": svc.ServerInterceptors,
151-
"WrappedServerStreams": collectWrappedStreams(svc.ServerInterceptors, true),
176+
"Service": svc.Name,
177+
"ServerInterceptors": svc.ServerInterceptors,
152178
},
153179
})
154180
}
@@ -157,9 +183,28 @@ func wrapperFile(svc *Data) *codegen.File {
157183
Name: "client-interceptor-wrappers",
158184
Source: readTemplate("client_interceptor_wrappers"),
159185
Data: map[string]interface{}{
160-
"Service": svc.Name,
161-
"ClientInterceptors": svc.ClientInterceptors,
162-
"WrappedClientStreams": collectWrappedStreams(svc.ClientInterceptors, false),
186+
"Service": svc.Name,
187+
"ClientInterceptors": svc.ClientInterceptors,
188+
},
189+
})
190+
}
191+
192+
// Generate any interceptor stream wrapper struct methods last
193+
if len(wrappedServerStreams) > 0 {
194+
sections = append(sections, &codegen.SectionTemplate{
195+
Name: "server-interceptor-stream-wrappers",
196+
Source: readTemplate("server_interceptor_stream_wrappers"),
197+
Data: map[string]interface{}{
198+
"WrappedServerStreams": wrappedServerStreams,
199+
},
200+
})
201+
}
202+
if len(wrappedClientStreams) > 0 {
203+
sections = append(sections, &codegen.SectionTemplate{
204+
Name: "client-interceptor-stream-wrappers",
205+
Source: readTemplate("client_interceptor_stream_wrappers"),
206+
Data: map[string]interface{}{
207+
"WrappedClientStreams": wrappedClientStreams,
163208
},
164209
})
165210
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{- range .WrappedClientStreams }}
2+
3+
{{ comment (printf "wrapped%s is a client interceptor wrapper for the %s stream." .Interface .Interface) }}
4+
type wrapped{{ .Interface }} struct {
5+
ctx context.Context
6+
{{- if ne .SendTypeRef "" }}
7+
sendWithContext func(context.Context, {{ .SendTypeRef }}) (context.Context, error)
8+
{{- end }}
9+
{{- if ne .RecvTypeRef "" }}
10+
recvWithContext func(context.Context) ({{ .RecvTypeRef }}, context.Context, error)
11+
{{- end }}
12+
stream {{ .Interface }}
13+
}
14+
{{- end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{- range .WrappedClientStreams }}
2+
3+
{{- if ne .SendTypeRef "" }}
4+
5+
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}
6+
func (w *wrapped{{ .Interface }}) {{ .SendName }}(v {{ .SendTypeRef }}) error {
7+
_, err := w.SendWithContext(w.ctx, v)
8+
return err
9+
}
10+
11+
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor with context." .SendWithContextName .Interface) }}
12+
func (w *wrapped{{ .Interface }}) {{ .SendWithContextName }}(ctx context.Context, v {{ .SendTypeRef }}) (context.Context, error) {
13+
if w.sendWithContext == nil {
14+
return w.stream.{{ .SendWithContextName }}(ctx, v)
15+
}
16+
return w.sendWithContext(ctx, v)
17+
}
18+
{{- end }}
19+
{{- if ne .RecvTypeRef "" }}
20+
21+
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor." .RecvName .Interface) }}
22+
func (w *wrapped{{ .Interface }}) {{ .RecvName }}() ({{ .RecvTypeRef }}, error) {
23+
res, _, err := w.RecvWithContext(w.ctx)
24+
return res, err
25+
}
26+
27+
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor with context." .RecvWithContextName .Interface) }}
28+
func (w *wrapped{{ .Interface }}) {{ .RecvWithContextName }}(ctx context.Context) ({{ .RecvTypeRef }}, context.Context, error) {
29+
if w.recvWithContext == nil {
30+
return w.stream.{{ .RecvWithContextName }}(ctx)
31+
}
32+
return w.recvWithContext(ctx)
33+
}
34+
{{- end }}
35+
{{- if .MustClose }}
36+
37+
// Close closes the stream.
38+
func (w *wrapped{{ .Interface }}) Close() error {
39+
return w.stream.Close()
40+
}
41+
{{- end }}
42+
{{- end }}

codegen/service/templates/client_interceptor_wrappers.go.tpl

+4-55
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func wrapClient{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i
1010
info := &{{ $interceptor.Name }}Info{
1111
Service: "{{ $.Service }}",
1212
Method: "{{ .MethodName }}",
13+
CallType: goa.InterceptorUnary,
1314
RawPayload: req,
1415
}
1516
res, err := i.{{ $interceptor.Name }}(ctx, info, endpoint)
@@ -27,7 +28,7 @@ func wrapClient{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i
2728
info := &{{ $interceptor.Name }}Info{
2829
Service: "{{ $.Service }}",
2930
Method: "{{ .MethodName }}",
30-
Send: true,
31+
CallType: goa.InterceptorStreamingSend,
3132
RawPayload: req,
3233
}
3334
var rCtx context.Context
@@ -44,7 +45,7 @@ func wrapClient{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i
4445
info := &{{ $interceptor.Name }}Info{
4546
Service: "{{ $.Service }}",
4647
Method: "{{ .MethodName }}",
47-
Recv: true,
48+
CallType: goa.InterceptorStreamingRecv,
4849
RawPayload: req,
4950
}
5051
var rCtx context.Context
@@ -65,64 +66,12 @@ func wrapClient{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i
6566
info := &{{ $interceptor.Name }}Info{
6667
Service: "{{ $.Service }}",
6768
Method: "{{ .MethodName }}",
69+
CallType: goa.InterceptorUnary,
6870
RawPayload: req,
6971
}
7072
return i.{{ $interceptor.Name }}(ctx, info, endpoint)
7173
{{- end }}
7274
}
7375
}
74-
{{ end }}
7576
{{- end }}
76-
{{- range .WrappedClientStreams }}
77-
78-
{{ comment (printf "wrapped%s is a client interceptor wrapper for the %s stream." .Interface .Interface) }}
79-
type wrapped{{ .Interface }} struct {
80-
ctx context.Context
81-
{{- if ne .SendTypeRef "" }}
82-
sendWithContext func(context.Context, {{ .SendTypeRef }}) (context.Context, error)
83-
{{- end }}
84-
{{- if ne .RecvTypeRef "" }}
85-
recvWithContext func(context.Context) ({{ .RecvTypeRef }}, context.Context, error)
86-
{{- end }}
87-
stream {{ .Interface }}
88-
}
89-
{{- if ne .SendTypeRef "" }}
90-
91-
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}
92-
func (w *wrapped{{ .Interface }}) {{ .SendName }}(v {{ .SendTypeRef }}) error {
93-
_, err := w.SendWithContext(w.ctx, v)
94-
return err
95-
}
96-
97-
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor with context." .SendWithContextName .Interface) }}
98-
func (w *wrapped{{ .Interface }}) {{ .SendWithContextName }}(ctx context.Context, v {{ .SendTypeRef }}) (context.Context, error) {
99-
if w.sendWithContext == nil {
100-
return w.stream.{{ .SendWithContextName }}(ctx, v)
101-
}
102-
return w.sendWithContext(ctx, v)
103-
}
104-
{{- end }}
105-
{{- if ne .RecvTypeRef "" }}
106-
107-
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor." .RecvName .Interface) }}
108-
func (w *wrapped{{ .Interface }}) {{ .RecvName }}() ({{ .RecvTypeRef }}, error) {
109-
res, _, err := w.RecvWithContext(w.ctx)
110-
return res, err
111-
}
112-
113-
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor with context." .RecvWithContextName .Interface) }}
114-
func (w *wrapped{{ .Interface }}) {{ .RecvWithContextName }}(ctx context.Context) ({{ .RecvTypeRef }}, context.Context, error) {
115-
if w.recvWithContext == nil {
116-
return w.stream.{{ .RecvWithContextName }}(ctx)
117-
}
118-
return w.recvWithContext(ctx)
119-
}
120-
{{- end }}
121-
{{- if .MustClose }}
122-
123-
// Close closes the stream.
124-
func (w *wrapped{{ .Interface }}) Close() error {
125-
return w.stream.Close()
126-
}
127-
{{- end }}
12877
{{- end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{- range .WrappedServerStreams }}
2+
3+
{{ comment (printf "wrapped%s is a server interceptor wrapper for the %s stream." .Interface .Interface) }}
4+
type wrapped{{ .Interface }} struct {
5+
ctx context.Context
6+
{{- if ne .SendTypeRef "" }}
7+
sendWithContext func(context.Context, {{ .SendTypeRef }}) (context.Context, error)
8+
{{- end }}
9+
{{- if ne .RecvTypeRef "" }}
10+
recvWithContext func(context.Context) ({{ .RecvTypeRef }}, context.Context, error)
11+
{{- end }}
12+
stream {{ .Interface }}
13+
}
14+
{{- end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{- range .WrappedServerStreams }}
2+
3+
{{- if ne .SendTypeRef "" }}
4+
5+
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}
6+
func (w *wrapped{{ .Interface }}) {{ .SendName }}(v {{ .SendTypeRef }}) error {
7+
_, err := w.SendWithContext(w.ctx, v)
8+
return err
9+
}
10+
11+
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor with context." .SendWithContextName .Interface) }}
12+
func (w *wrapped{{ .Interface }}) {{ .SendWithContextName }}(ctx context.Context, v {{ .SendTypeRef }}) (context.Context, error) {
13+
if w.sendWithContext == nil {
14+
return w.stream.{{ .SendWithContextName }}(ctx, v)
15+
}
16+
return w.sendWithContext(ctx, v)
17+
}
18+
{{- end }}
19+
{{- if ne .RecvTypeRef "" }}
20+
21+
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor." .RecvName .Interface) }}
22+
func (w *wrapped{{ .Interface }}) {{ .RecvName }}() ({{ .RecvTypeRef }}, error) {
23+
res, _, err := w.RecvWithContext(w.ctx)
24+
return res, err
25+
}
26+
27+
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor with context." .RecvWithContextName .Interface) }}
28+
func (w *wrapped{{ .Interface }}) {{ .RecvWithContextName }}(ctx context.Context) ({{ .RecvTypeRef }}, context.Context, error) {
29+
if w.recvWithContext == nil {
30+
return w.stream.{{ .RecvWithContextName }}(ctx)
31+
}
32+
return w.recvWithContext(ctx)
33+
}
34+
{{- end }}
35+
{{- if .MustClose }}
36+
37+
// Close closes the stream.
38+
func (w *wrapped{{ .Interface }}) Close() error {
39+
return w.stream.Close()
40+
}
41+
{{- end }}
42+
{{- end }}

codegen/service/templates/server_interceptor_wrappers.go.tpl

+4-55
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func wrap{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i Serve
1010
info := &{{ $interceptor.Name }}Info{
1111
Service: "{{ $.Service }}",
1212
Method: "{{ .MethodName }}",
13+
CallType: goa.InterceptorUnary,
1314
RawPayload: req,
1415
}
1516
res, err := i.{{ $interceptor.Name }}(ctx, info, endpoint)
@@ -27,7 +28,7 @@ func wrap{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i Serve
2728
info := &{{ $interceptor.Name }}Info{
2829
Service: "{{ $.Service }}",
2930
Method: "{{ .MethodName }}",
30-
Send: true,
31+
CallType: goa.InterceptorStreamingSend,
3132
RawPayload: req,
3233
}
3334
var rCtx context.Context
@@ -44,7 +45,7 @@ func wrap{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i Serve
4445
info := &{{ $interceptor.Name }}Info{
4546
Service: "{{ $.Service }}",
4647
Method: "{{ .MethodName }}",
47-
Recv: true,
48+
CallType: goa.InterceptorStreamingRecv,
4849
RawPayload: req,
4950
}
5051
var rCtx context.Context
@@ -65,6 +66,7 @@ func wrap{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i Serve
6566
info := &{{ $interceptor.Name }}Info{
6667
Service: "{{ $.Service }}",
6768
Method: "{{ .MethodName }}",
69+
CallType: goa.InterceptorUnary,
6870
RawPayload: req,
6971
}
7072
return i.{{ $interceptor.Name }}(ctx, info, endpoint)
@@ -73,56 +75,3 @@ func wrap{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i Serve
7375
}
7476
{{- end }}
7577
{{- end }}
76-
{{- range .WrappedServerStreams }}
77-
78-
{{ comment (printf "wrapped%s is a server interceptor wrapper for the %s stream." .Interface .Interface) }}
79-
type wrapped{{ .Interface }} struct {
80-
ctx context.Context
81-
{{- if ne .SendTypeRef "" }}
82-
sendWithContext func(context.Context, {{ .SendTypeRef }}) (context.Context, error)
83-
{{- end }}
84-
{{- if ne .RecvTypeRef "" }}
85-
recvWithContext func(context.Context) ({{ .RecvTypeRef }}, context.Context, error)
86-
{{- end }}
87-
stream {{ .Interface }}
88-
}
89-
{{- if ne .SendTypeRef "" }}
90-
91-
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}
92-
func (w *wrapped{{ .Interface }}) {{ .SendName }}(v {{ .SendTypeRef }}) error {
93-
_, err := w.SendWithContext(w.ctx, v)
94-
return err
95-
}
96-
97-
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor with context." .SendWithContextName .Interface) }}
98-
func (w *wrapped{{ .Interface }}) {{ .SendWithContextName }}(ctx context.Context, v {{ .SendTypeRef }}) (context.Context, error) {
99-
if w.sendWithContext == nil {
100-
return w.stream.{{ .SendWithContextName }}(ctx, v)
101-
}
102-
return w.sendWithContext(ctx, v)
103-
}
104-
{{- end }}
105-
{{- if ne .RecvTypeRef "" }}
106-
107-
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor." .RecvName .Interface) }}
108-
func (w *wrapped{{ .Interface }}) {{ .RecvName }}() ({{ .RecvTypeRef }}, error) {
109-
res, _, err := w.RecvWithContext(w.ctx)
110-
return res, err
111-
}
112-
113-
{{ comment (printf "%s reads instances of \"%s\" from the stream after executing the applied interceptor with context." .RecvWithContextName .Interface) }}
114-
func (w *wrapped{{ .Interface }}) {{ .RecvWithContextName }}(ctx context.Context) ({{ .RecvTypeRef }}, context.Context, error) {
115-
if w.recvWithContext == nil {
116-
return w.stream.{{ .RecvWithContextName }}(ctx)
117-
}
118-
return w.recvWithContext(ctx)
119-
}
120-
{{- end }}
121-
{{- if .MustClose }}
122-
123-
// Close closes the stream.
124-
func (w *wrapped{{ .Interface }}) Close() error {
125-
return w.stream.Close()
126-
}
127-
{{- end }}
128-
{{- end }}

0 commit comments

Comments
 (0)