-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformdecoder.go
More file actions
55 lines (46 loc) · 1.21 KB
/
formdecoder.go
File metadata and controls
55 lines (46 loc) · 1.21 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
44
45
46
47
48
49
50
51
52
53
54
55
package prefab
import (
"io"
"net/url"
"github.com/dpup/prefab/errors"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
"google.golang.org/protobuf/proto"
)
// Forked from pending issue here:
// https://github.com/grpc-ecosystem/grpc-gateway/issues/7
type formDecoder struct {
runtime.Marshaler
}
// ContentType means the content type of the response.
func (u formDecoder) ContentType(_ interface{}) string {
return "application/json"
}
func (u formDecoder) Marshal(v interface{}) ([]byte, error) {
// can marshal the response in proto message format
j := runtime.JSONPb{}
return j.Marshal(v)
}
// NewDecoder indicates how to decode the request.
func (u formDecoder) NewDecoder(r io.Reader) runtime.Decoder {
return runtime.DecoderFunc(func(p interface{}) error {
msg, ok := p.(proto.Message)
if !ok {
return errors.New("not proto message")
}
formData, err := io.ReadAll(r)
if err != nil {
return err
}
values, err := url.ParseQuery(string(formData))
if err != nil {
return err
}
filter := &utilities.DoubleArray{}
err = runtime.PopulateQueryParameters(msg, values, filter)
if err != nil {
return err
}
return nil
})
}