Newer
Older
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
// Package goadec provides Goa Request Decoders.
package goadec
import (
"fmt"
"io"
"io/ioutil"
"net/http"
goahttp "goa.design/goa/v3/http"
)
// BytesDecoder returns an HTTP request body decoder that
// just reads the request body bytes.
func BytesDecoder(r *http.Request) goahttp.Decoder {
return newBytesDecoder(r.Body)
}
type bytesDecoder struct {
r io.Reader
}
func newBytesDecoder(r io.Reader) *bytesDecoder {
return &bytesDecoder{r: r}
}
func (d *bytesDecoder) Decode(v interface{}) error {
b, err := ioutil.ReadAll(d.r)
if err != nil {
return err
}
switch c := v.(type) {
case *string:
*c = string(b)
case *[]byte:
*c = b
default:
return fmt.Errorf("cannot decode request: unsupported value type: %T", c)
}
return nil
}