-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.go
56 lines (41 loc) · 1.29 KB
/
encoding.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
package clientx
import (
"encoding/json"
"encoding/xml"
"io"
)
type EncoderDecoder interface {
Encoder
Decoder
}
// Encoder is a general interface responsibles for encoding payloads.
type Encoder interface {
Encode(w io.Writer, v any) error
}
// Decoder is a general interface responsibles for decoding responses.
type Decoder interface {
Decode(r io.Reader, dst any) error
}
// JSON Encoder/Decoder realization.
var JSONEncoderDecoder = &jsonEncoderDecoder{}
type jsonEncoderDecoder struct{}
func (jsonEncoderDecoder) Encode(w io.Writer, v any) error {
return json.NewEncoder(w).Encode(v)
}
func (jsonEncoderDecoder) Decode(r io.Reader, dst any) error {
return json.NewDecoder(r).Decode(dst)
}
// XML Encoder/Decoder realization.
var XMLEncoderDecoder = &xmlEncoderDecoder{}
type xmlEncoderDecoder struct{}
func (xmlEncoderDecoder) Encode(w io.Writer, v any) error {
return xml.NewEncoder(w).Encode(v)
}
func (xmlEncoderDecoder) Decode(r io.Reader, dst any) error {
return xml.NewDecoder(r).Decode(dst)
}
// Blank (No Action) Encoder/Decoder realization.
var BlankEncoderDecoder = &blankEncoderDecoder{}
type blankEncoderDecoder struct{}
func (blankEncoderDecoder) Encode(v any) ([]byte, error) { return nil, nil }
func (blankEncoderDecoder) Decode(r io.Reader, dst any) error { return nil }