-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecoder.go
30 lines (24 loc) · 883 Bytes
/
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
package courier
import (
"context"
"encoding/base64"
"encoding/json"
"io"
)
// DecoderFunc is used to create a Decoder from io.Reader stream
// of message bytes before calling MessageHandler;
// the context.Context value may be used to select appropriate Decoder.
type DecoderFunc func(context.Context, io.Reader) Decoder
// Decoder helps to decode message bytes into the desired object
type Decoder interface {
// Decode decodes message bytes into the passed object
Decode(v interface{}) error
}
// DefaultDecoderFunc is a DecoderFunc that uses a json.Decoder as the Decoder.
func DefaultDecoderFunc(_ context.Context, r io.Reader) Decoder {
return json.NewDecoder(r)
}
func base64JsonDecoder(_ context.Context, r io.Reader) Decoder {
return json.NewDecoder(base64.NewDecoder(base64.StdEncoding, r))
}
func (f DecoderFunc) apply(o *clientOptions) { o.newDecoder = f }