-
Notifications
You must be signed in to change notification settings - Fork 68
/
encode_decode.go
166 lines (153 loc) · 4.98 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
// Code generated by goa v3.16.1, DO NOT EDIT.
//
// sommelier HTTP client encoders and decoders
//
// Command:
// $ goa gen goa.design/examples/cellar/design
package client
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
sommelier "goa.design/examples/cellar/gen/sommelier"
sommelierviews "goa.design/examples/cellar/gen/sommelier/views"
goahttp "goa.design/goa/v3/http"
)
// BuildPickRequest instantiates a HTTP request object with method and path set
// to call the "sommelier" service "pick" endpoint
func (c *Client) BuildPickRequest(ctx context.Context, v any) (*http.Request, error) {
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: PickSommelierPath()}
req, err := http.NewRequest("POST", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("sommelier", "pick", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// EncodePickRequest returns an encoder for requests sent to the sommelier pick
// server.
func EncodePickRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Request, any) error {
return func(req *http.Request, v any) error {
p, ok := v.(*sommelier.Criteria)
if !ok {
return goahttp.ErrInvalidType("sommelier", "pick", "*sommelier.Criteria", v)
}
body := NewPickRequestBody(p)
if err := encoder(req).Encode(&body); err != nil {
return goahttp.ErrEncodingError("sommelier", "pick", err)
}
return nil
}
}
// DecodePickResponse returns a decoder for responses returned by the sommelier
// pick endpoint. restoreBody controls whether the response body should be
// restored after having been read.
// DecodePickResponse may return the following errors:
// - "no_criteria" (type sommelier.NoCriteria): http.StatusBadRequest
// - "no_match" (type sommelier.NoMatch): http.StatusNotFound
// - error: internal error
func DecodePickResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (any, error) {
return func(resp *http.Response) (any, error) {
if restoreBody {
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewBuffer(b))
defer func() {
resp.Body = io.NopCloser(bytes.NewBuffer(b))
}()
} else {
defer resp.Body.Close()
}
switch resp.StatusCode {
case http.StatusOK:
var (
body PickResponseBody
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("sommelier", "pick", err)
}
p := NewPickStoredBottleCollectionOK(body)
view := "default"
vres := sommelierviews.StoredBottleCollection{Projected: p, View: view}
if err = sommelierviews.ValidateStoredBottleCollection(vres); err != nil {
return nil, goahttp.ErrValidationError("sommelier", "pick", err)
}
res := sommelier.NewStoredBottleCollection(vres)
return res, nil
case http.StatusBadRequest:
var (
body string
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("sommelier", "pick", err)
}
return nil, NewPickNoCriteria(body)
case http.StatusNotFound:
var (
body string
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("sommelier", "pick", err)
}
return nil, NewPickNoMatch(body)
default:
body, _ := io.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("sommelier", "pick", resp.StatusCode, string(body))
}
}
}
// unmarshalStoredBottleResponseToSommelierviewsStoredBottleView builds a value
// of type *sommelierviews.StoredBottleView from a value of type
// *StoredBottleResponse.
func unmarshalStoredBottleResponseToSommelierviewsStoredBottleView(v *StoredBottleResponse) *sommelierviews.StoredBottleView {
res := &sommelierviews.StoredBottleView{
ID: v.ID,
Name: v.Name,
Vintage: v.Vintage,
Description: v.Description,
Rating: v.Rating,
}
res.Winery = unmarshalWineryResponseToSommelierviewsWineryView(v.Winery)
if v.Composition != nil {
res.Composition = make([]*sommelierviews.ComponentView, len(v.Composition))
for i, val := range v.Composition {
res.Composition[i] = unmarshalComponentResponseToSommelierviewsComponentView(val)
}
}
return res
}
// unmarshalWineryResponseToSommelierviewsWineryView builds a value of type
// *sommelierviews.WineryView from a value of type *WineryResponse.
func unmarshalWineryResponseToSommelierviewsWineryView(v *WineryResponse) *sommelierviews.WineryView {
res := &sommelierviews.WineryView{
Name: v.Name,
Region: v.Region,
Country: v.Country,
URL: v.URL,
}
return res
}
// unmarshalComponentResponseToSommelierviewsComponentView builds a value of
// type *sommelierviews.ComponentView from a value of type *ComponentResponse.
func unmarshalComponentResponseToSommelierviewsComponentView(v *ComponentResponse) *sommelierviews.ComponentView {
if v == nil {
return nil
}
res := &sommelierviews.ComponentView{
Varietal: v.Varietal,
Percentage: v.Percentage,
}
return res
}