forked from dghubble/sling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
34 lines (27 loc) · 1 KB
/
decoder.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
package sling
import (
"encoding/json"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
// ResponseDecoder decodes http responses into struct values.
type ResponseDecoder interface {
// Decode decodes the response into the value pointed to by v.
Decode(bytes []byte, v interface{}) error
}
// jsonDecoder decodes http response JSON into a JSON-tagged struct value.
type jsonDecoder struct {
}
// Decode decodes the Response Body into the value pointed to by v.
// Caller must provide a non-nil v and close the resp.Body.
func (d jsonDecoder) Decode(bytes []byte, v interface{}) error {
return json.Unmarshal(bytes, v)
}
// jsonDecoder decodes http response JSON into a JSON-tagged struct value.
type JsonpbDecoder struct {
}
// Decode decodes the Response Body into the value pointed to by v.
// Caller must provide a non-nil v and close the resp.Body.
func (d JsonpbDecoder) Decode(bytes []byte, v interface{}) error {
return protojson.Unmarshal(bytes, v.(proto.Message))
}