-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathresponse_encoder.go.tpl
More file actions
43 lines (43 loc) · 1.54 KB
/
response_encoder.go.tpl
File metadata and controls
43 lines (43 loc) · 1.54 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
{{ printf "%s returns an encoder for responses returned by the %s %s endpoint." .ResponseEncoder .ServiceName .Method.Name | comment }}
func {{ .ResponseEncoder }}(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) func(context.Context, http.ResponseWriter, any) error {
return func(ctx context.Context, w http.ResponseWriter, v any) error {
{{- if .Result.MustInit }}
{{- if .Method.ViewedResult }}
res := v.({{ .Method.ViewedResult.FullRef }})
{{- if not .Method.ViewedResult.ViewName }}
w.Header().Set("goa-view", res.View)
{{- end }}
{{- else }}
res, _ := v.({{ .Result.Ref }})
{{- end }}
{{- range .Result.Responses }}
{{- if .ContentType }}
ctx = context.WithValue(ctx, goahttp.ContentTypeKey, "{{ .ContentType }}")
{{- end }}
{{- if .TagName }}
{{- if .TagPointer }}
if res.{{ if .ViewedResult }}Projected.{{ end }}{{ .TagName }} != nil && *res.{{ if .ViewedResult }}Projected.{{ end }}{{ .TagName }} == {{ printf "%q" .TagValue }} {
{{- else }}
if {{ if .ViewedResult }}*{{ end }}res.{{ if .ViewedResult }}Projected.{{ end }}{{ .TagName }} == {{ printf "%q" .TagValue }} {
{{- end }}
{{- end -}}
{{ template "partial_response" . }}
{{- if .ServerBody }}
return enc.Encode(body)
{{- else }}
return nil
{{- end }}
{{- if .TagName }}
}
{{- end }}
{{- end }}
{{- else }}
{{- with (index .Result.Responses 0) }}
{{- if not $.Method.SkipResponseBodyEncodeDecode }}
w.WriteHeader({{ .StatusCode }})
{{- end }}
return nil
{{- end }}
{{- end }}
}
}