/
encode_decode.go
265 lines (250 loc) · 7.11 KB
/
encode_decode.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Code generated by goa v2.2.4, DO NOT EDIT.
//
// chatter gRPC server encoders and decoders
//
// Command:
// $ goa gen goa.design/examples/streaming/design -o
// $(GOPATH)/src/goa.design/examples/streaming
package server
import (
"context"
"strings"
chatter "goa.design/examples/streaming/gen/chatter"
chatterviews "goa.design/examples/streaming/gen/chatter/views"
goa "goa.design/goa"
goagrpc "goa.design/goa/grpc"
"google.golang.org/grpc/metadata"
)
// EncodeLoginResponse encodes responses from the "chatter" service "login"
// endpoint.
func EncodeLoginResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
result, ok := v.(string)
if !ok {
return nil, goagrpc.ErrInvalidType("chatter", "login", "string", v)
}
resp := NewLoginResponse(result)
return resp, nil
}
// DecodeLoginRequest decodes requests sent to "chatter" service "login"
// endpoint.
func DecodeLoginRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
var (
user string
password string
err error
)
{
if vals := md.Get("user"); len(vals) == 0 {
err = goa.MergeErrors(err, goa.MissingFieldError("user", "metadata"))
} else {
user = vals[0]
}
if vals := md.Get("password"); len(vals) == 0 {
err = goa.MergeErrors(err, goa.MissingFieldError("password", "metadata"))
} else {
password = vals[0]
}
}
if err != nil {
return nil, err
}
var payload *chatter.LoginPayload
{
payload = NewLoginPayload(user, password)
}
return payload, nil
}
// EncodeEchoerResponse encodes responses from the "chatter" service "echoer"
// endpoint.
func EncodeEchoerResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
result, ok := v.(string)
if !ok {
return nil, goagrpc.ErrInvalidType("chatter", "echoer", "string", v)
}
resp := NewEchoerResponse(result)
return resp, nil
}
// DecodeEchoerRequest decodes requests sent to "chatter" service "echoer"
// endpoint.
func DecodeEchoerRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
var (
token string
err error
)
{
if vals := md.Get("authorization"); len(vals) == 0 {
err = goa.MergeErrors(err, goa.MissingFieldError("authorization", "metadata"))
} else {
token = vals[0]
}
}
if err != nil {
return nil, err
}
var payload *chatter.EchoerPayload
{
payload = NewEchoerPayload(token)
if strings.Contains(payload.Token, " ") {
// Remove authorization scheme prefix (e.g. "Bearer")
cred := strings.SplitN(payload.Token, " ", 2)[1]
payload.Token = cred
}
}
return payload, nil
}
// EncodeListenerResponse encodes responses from the "chatter" service
// "listener" endpoint.
func EncodeListenerResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
resp := NewListenerResponse()
return resp, nil
}
// DecodeListenerRequest decodes requests sent to "chatter" service "listener"
// endpoint.
func DecodeListenerRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
var (
token string
err error
)
{
if vals := md.Get("authorization"); len(vals) == 0 {
err = goa.MergeErrors(err, goa.MissingFieldError("authorization", "metadata"))
} else {
token = vals[0]
}
}
if err != nil {
return nil, err
}
var payload *chatter.ListenerPayload
{
payload = NewListenerPayload(token)
if strings.Contains(payload.Token, " ") {
// Remove authorization scheme prefix (e.g. "Bearer")
cred := strings.SplitN(payload.Token, " ", 2)[1]
payload.Token = cred
}
}
return payload, nil
}
// EncodeSummaryResponse encodes responses from the "chatter" service "summary"
// endpoint.
func EncodeSummaryResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
vres, ok := v.(chatterviews.ChatSummaryCollection)
if !ok {
return nil, goagrpc.ErrInvalidType("chatter", "summary", "chatterviews.ChatSummaryCollection", v)
}
result := vres.Projected
(*hdr).Append("goa-view", vres.View)
resp := NewChatSummaryCollection(result)
return resp, nil
}
// DecodeSummaryRequest decodes requests sent to "chatter" service "summary"
// endpoint.
func DecodeSummaryRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
var (
token string
err error
)
{
if vals := md.Get("authorization"); len(vals) == 0 {
err = goa.MergeErrors(err, goa.MissingFieldError("authorization", "metadata"))
} else {
token = vals[0]
}
}
if err != nil {
return nil, err
}
var payload *chatter.SummaryPayload
{
payload = NewSummaryPayload(token)
if strings.Contains(payload.Token, " ") {
// Remove authorization scheme prefix (e.g. "Bearer")
cred := strings.SplitN(payload.Token, " ", 2)[1]
payload.Token = cred
}
}
return payload, nil
}
// EncodeSubscribeResponse encodes responses from the "chatter" service
// "subscribe" endpoint.
func EncodeSubscribeResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
result, ok := v.(*chatter.Event)
if !ok {
return nil, goagrpc.ErrInvalidType("chatter", "subscribe", "*chatter.Event", v)
}
resp := NewSubscribeResponse(result)
return resp, nil
}
// DecodeSubscribeRequest decodes requests sent to "chatter" service
// "subscribe" endpoint.
func DecodeSubscribeRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
var (
token string
err error
)
{
if vals := md.Get("authorization"); len(vals) == 0 {
err = goa.MergeErrors(err, goa.MissingFieldError("authorization", "metadata"))
} else {
token = vals[0]
}
}
if err != nil {
return nil, err
}
var payload *chatter.SubscribePayload
{
payload = NewSubscribePayload(token)
if strings.Contains(payload.Token, " ") {
// Remove authorization scheme prefix (e.g. "Bearer")
cred := strings.SplitN(payload.Token, " ", 2)[1]
payload.Token = cred
}
}
return payload, nil
}
// EncodeHistoryResponse encodes responses from the "chatter" service "history"
// endpoint.
func EncodeHistoryResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
vres, ok := v.(*chatterviews.ChatSummary)
if !ok {
return nil, goagrpc.ErrInvalidType("chatter", "history", "*chatterviews.ChatSummary", v)
}
result := vres.Projected
(*hdr).Append("goa-view", vres.View)
resp := NewHistoryResponse(result)
return resp, nil
}
// DecodeHistoryRequest decodes requests sent to "chatter" service "history"
// endpoint.
func DecodeHistoryRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
var (
view *string
token string
err error
)
{
if vals := md.Get("view"); len(vals) > 0 {
view = &vals[0]
}
if vals := md.Get("authorization"); len(vals) == 0 {
err = goa.MergeErrors(err, goa.MissingFieldError("authorization", "metadata"))
} else {
token = vals[0]
}
}
if err != nil {
return nil, err
}
var payload *chatter.HistoryPayload
{
payload = NewHistoryPayload(view, token)
if strings.Contains(payload.Token, " ") {
// Remove authorization scheme prefix (e.g. "Bearer")
cred := strings.SplitN(payload.Token, " ", 2)[1]
payload.Token = cred
}
}
return payload, nil
}