-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
163 lines (137 loc) · 3.74 KB
/
encode.go
File metadata and controls
163 lines (137 loc) · 3.74 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package api
import (
"encoding/json"
"encoding/xml"
"errors"
"io"
"mime"
"strconv"
"strings"
)
// Encoder encodes response values to a wire format.
type Encoder interface {
ContentType() string
Encode(w io.Writer, v any) error
}
// Decoder decodes request bodies from a wire format.
type Decoder interface {
ContentType() string
Decode(r io.Reader, v any) error
}
// jsonCodec implements both Encoder and Decoder for JSON.
type jsonCodec struct{}
func (jsonCodec) ContentType() string { return "application/json" }
func (jsonCodec) Encode(w io.Writer, v any) error {
return json.NewEncoder(w).Encode(v)
}
func (jsonCodec) Decode(r io.Reader, v any) error {
err := json.NewDecoder(r).Decode(v)
if errors.Is(err, io.EOF) {
return nil
}
return err
}
// xmlCodec implements both Encoder and Decoder for XML.
type xmlCodec struct{}
func (xmlCodec) ContentType() string { return "application/xml" }
func (xmlCodec) Encode(w io.Writer, v any) error {
if _, err := io.WriteString(w, xml.Header); err != nil {
return err
}
return xml.NewEncoder(w).Encode(v)
}
func (xmlCodec) Decode(r io.Reader, v any) error {
err := xml.NewDecoder(r).Decode(v)
if errors.Is(err, io.EOF) {
return nil
}
return err
}
// codecRegistry holds all registered encoders and decoders.
// Index 0 is always JSON (the default).
type codecRegistry struct {
encoders []Encoder
decoders []Decoder
}
// newCodecRegistry builds a registry with JSON first, XML second, then any
// user-registered encoders and decoders.
func newCodecRegistry(userEncoders []Encoder, userDecoders []Decoder) *codecRegistry {
cr := &codecRegistry{
encoders: make([]Encoder, 0, 2+len(userEncoders)),
decoders: make([]Decoder, 0, 2+len(userDecoders)),
}
cr.encoders = append(cr.encoders, jsonCodec{}, xmlCodec{})
cr.encoders = append(cr.encoders, userEncoders...)
cr.decoders = append(cr.decoders, jsonCodec{}, xmlCodec{})
cr.decoders = append(cr.decoders, userDecoders...)
return cr
}
// negotiate picks an encoder based on the Accept header value.
// Returns (JSON, true) for empty or */* accept values.
// Returns (nil, false) if an explicit Accept has no match.
func (cr *codecRegistry) negotiate(accept string) (Encoder, bool) {
if accept == "" {
return cr.encoders[0], true
}
type candidate struct {
encoder Encoder
quality float64
}
var best candidate
best.quality = -1
for part := range strings.SplitSeq(accept, ",") {
mediaType, params, err := mime.ParseMediaType(strings.TrimSpace(part))
if err != nil {
continue
}
q := 1.0
if qs, ok := params["q"]; ok {
if parsed, err := strconv.ParseFloat(qs, 64); err == nil {
q = parsed
}
}
if q <= best.quality {
continue
}
if mediaType == "*/*" {
best = candidate{encoder: cr.encoders[0], quality: q}
continue
}
for _, enc := range cr.encoders {
if enc.ContentType() == mediaType {
best = candidate{encoder: enc, quality: q}
break
}
}
}
if best.encoder == nil {
return nil, false
}
return best.encoder, true
}
// decoderFor returns the decoder matching the given Content-Type.
// Returns (JSON decoder, true) for empty content type.
// Returns (nil, false) if the content type is present but unrecognized.
func (cr *codecRegistry) decoderFor(contentType string) (Decoder, bool) {
if contentType == "" {
return cr.decoders[0], true
}
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, false
}
for _, dec := range cr.decoders {
if dec.ContentType() == mediaType {
return dec, true
}
}
return nil, false
}
// contentTypes returns all encoder content types (for OpenAPI).
func (cr *codecRegistry) contentTypes() []string {
cts := make([]string, len(cr.encoders))
for i, enc := range cr.encoders {
cts[i] = enc.ContentType()
}
return cts
}