-
Notifications
You must be signed in to change notification settings - Fork 2
/
encode.go
63 lines (49 loc) · 1.6 KB
/
encode.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
package httpc
import (
"encoding/xml"
jsoniter "github.com/json-iterator/go"
"gopkg.in/yaml.v3"
)
// Encoder denotes a generic encoder, including an encoding function and content-type getter
type Encoder interface {
// Encode denotes a generic encoder function / method, usually a Marshal() function
Encode() ([]byte, error)
// ContentType provides an automated way to retrieve / set the content-type header
ContentType() string
}
// JSONEncoder provdes encoding to JSON
type JSONEncoder struct {
v interface{}
}
// Encode fulfills the Encoder interface, performing the actual encoding
func (e JSONEncoder) Encode() ([]byte, error) {
return jsoniter.Marshal(e.v)
}
// ContentType fulfills the Encoder interface, providing the required content-type header
func (e JSONEncoder) ContentType() string {
return "application/json"
}
// YAMLEncoder provdes encoding to YAML
type YAMLEncoder struct {
v interface{}
}
// Encode fulfills the Encoder interface, performing the actual encoding
func (e YAMLEncoder) Encode() ([]byte, error) {
return yaml.Marshal(e.v)
}
// ContentType fulfills the Encoder interface, providing the required content-type header
func (e YAMLEncoder) ContentType() string {
return "application/yaml"
}
// XMLEncoder provdes encoding to XML
type XMLEncoder struct {
v interface{}
}
// Encode fulfills the Encoder interface, performing the actual encoding
func (e XMLEncoder) Encode() ([]byte, error) {
return xml.Marshal(e.v)
}
// ContentType fulfills the Encoder interface, providing the required content-type header
func (e XMLEncoder) ContentType() string {
return "application/xml"
}