-
Notifications
You must be signed in to change notification settings - Fork 1
/
encodec.go
85 lines (71 loc) · 2.31 KB
/
encodec.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
package http
import (
"bytes"
"io"
"io/ioutil"
"net/url"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/phogolabs/inflate"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
)
var (
// WithFormMarshaler allows marshaling application/x-www-form-urlencoded requests
WithFormMarshaler = runtime.WithMarshalerOption(ContentTypeForm, &runtime.HTTPBodyMarshaler{
Marshaler: &FormMarshaler{},
})
// WithJSONMarshaler allows marshaling application/json requests
WithJSONMarshaler = runtime.WithMarshalerOption(ContentTypeJSON, &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
},
})
)
// FormMarshaler defines a conversion between byte sequence and gRPC payloads / fields.
type FormMarshaler struct{}
// Marshal marshals "v" into byte sequence.
func (m *FormMarshaler) Marshal(v interface{}) ([]byte, error) {
return nil, status.Error(codes.Unimplemented, "Method Marshal is not supported operation")
}
// Unmarshal unmarshals "data" into "v".
// "v" must be a pointer value.
func (m *FormMarshaler) Unmarshal(data []byte, v interface{}) error {
decoder := &FormDecoder{Reader: bytes.NewBuffer(data)}
return decoder.Decode(v)
}
// NewDecoder returns a Decoder which reads byte sequence from "r".
func (m *FormMarshaler) NewDecoder(r io.Reader) runtime.Decoder {
return &FormDecoder{Reader: r}
}
// NewEncoder returns an Encoder which writes bytes sequence into "w".
func (m *FormMarshaler) NewEncoder(w io.Writer) runtime.Encoder {
panic(status.Error(codes.Unimplemented, "Method NewEncoder is not supported operation"))
}
// ContentType returns the Content-Type which this marshaler is responsible for.
func (m *FormMarshaler) ContentType(_ interface{}) string {
return ContentTypeForm
}
// FormDecoder represents a form decoder
type FormDecoder struct {
Reader io.Reader
}
// Decode decodes the request
func (d *FormDecoder) Decode(v interface{}) error {
// read the body
body, err := ioutil.ReadAll(d.Reader)
if err != nil {
return err
}
// parse the body
values, err := url.ParseQuery(string(body))
if err != nil {
return err
}
return inflate.NewFormDecoder(values).Decode(v)
}