-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.go
More file actions
118 lines (104 loc) · 1.77 KB
/
codec.go
File metadata and controls
118 lines (104 loc) · 1.77 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package framework
import (
"io"
)
type Encoder interface {
EncodingType() string
Encode(interface{}) ([]byte, error)
EncodeTo(io.Writer, interface{}) error
}
type Encoders []Encoder
func (e Encoders) Get(encodingType string) Encoder {
for _, ce := range e {
if ce.EncodingType() == encodingType {
return ce
}
}
return nil
}
func (e *Encoders) Append(nc ...Encoder) {
if nc == nil {
return
}
for _, nce := range nc {
found := false
for i, ce := range *e {
if ce.EncodingType() == nce.EncodingType() {
(*e)[i] = nce
found = true
break
}
}
if !found {
*e = append(*e, nce)
}
}
}
type Decoder interface {
DecodingType() string
Decode([]byte, interface{}) error
DecodeFrom(io.Reader, interface{}) error
}
type Decoders []Decoder
func (d Decoders) Get(decodingType string) Decoder {
for _, ce := range d {
if ce.DecodingType() == decodingType {
return ce
}
}
return nil
}
func (d *Decoders) Append(nc ...Decoder) {
if nc == nil {
return
}
for _, nce := range nc {
found := false
for i, ce := range *d {
if ce.DecodingType() == nce.DecodingType() {
(*d)[i] = nce
found = true
break
}
}
if !found {
*d = append(*d, nce)
}
}
}
type Codec interface {
MIMEType() string
Encoder
Decoder
}
type Encodable interface {
Encode() ([]byte, error)
EncodeTo(io.Writer) error
}
type Codecs []Codec
func (c Codecs) Get(mimeType string) Codec {
for _, ce := range c {
if ce.MIMEType() == mimeType {
return ce
}
}
return nil
}
func (c *Codecs) Append(nc ...Codec) {
if nc == nil {
return
}
for _, nce := range nc {
found := false
for i, ce := range *c {
if ce.MIMEType() == nce.MIMEType() {
(*c)[i] = nce
found = true
break
}
}
if !found {
*c = append(*c, nce)
}
}
}