Skip to content

Commit 3f95838

Browse files
committed
refactor!: move encoding logic to sub-package
1 parent c8cd2de commit 3f95838

File tree

21 files changed

+92
-97
lines changed

21 files changed

+92
-97
lines changed

api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
// response.
1414
type Empty struct{}
1515

16-
// DefaultEncoding contains the mime type of the default encoding to fall
17-
// back on if no `Accept` or `Content-Type` header was provided.
16+
// DefaultEncoding contains the media type of the default encoding to fall back
17+
// on if no `Accept` or `Content-Type` header was provided.
1818
var DefaultEncoding = "text/plain"
1919

2020
type Middleware func(fasthttp.RequestHandler) fasthttp.RequestHandler
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package don
1+
package encoding
22

33
import (
44
"context"
@@ -16,33 +16,31 @@ type DecoderConstraint interface {
1616
Unmarshaler | ContextUnmarshaler | RequestParser
1717
}
1818

19-
// RegisterDecoder registers a request decoder.
20-
func RegisterDecoder[T DecoderConstraint](contentType string, dec T, aliases ...string) {
19+
// RegisterDecoder registers a request decoder for a given media type.
20+
func RegisterDecoder[T DecoderConstraint](dec T, mime string, aliases ...string) {
2121
switch d := any(dec).(type) {
2222
case Unmarshaler:
23-
decoders[contentType] = func(ctx *fasthttp.RequestCtx, v any) error {
23+
decoders[mime] = func(ctx *fasthttp.RequestCtx, v any) error {
2424
return d(ctx.Request.Body(), v)
2525
}
2626

2727
case ContextUnmarshaler:
28-
decoders[contentType] = func(ctx *fasthttp.RequestCtx, v any) error {
28+
decoders[mime] = func(ctx *fasthttp.RequestCtx, v any) error {
2929
return d(ctx, ctx.Request.Body(), v)
3030
}
3131

3232
case RequestParser:
33-
decoders[contentType] = d
33+
decoders[mime] = d
3434
}
3535

3636
for _, alias := range aliases {
37-
decoders[alias] = decoders[contentType]
37+
decoders[alias] = decoders[mime]
3838
}
3939
}
4040

41-
func getDecoder(mime string) (RequestParser, error) {
42-
if enc := decoders[mime]; enc != nil {
43-
return enc, nil
44-
}
45-
return nil, ErrUnsupportedMediaType
41+
// GetDecoder returns the request decoder for a given media type.
42+
func GetDecoder(mime string) RequestParser {
43+
return decoders[mime]
4644
}
4745

4846
var decoders = map[string]RequestParser{}

decode_test.go renamed to encoding/decode_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package don_test
1+
package encoding_test
22

33
import (
44
"context"
55
"io"
66
"reflect"
77
"testing"
88

9-
"github.com/abemedia/go-don"
9+
"github.com/abemedia/go-don/encoding"
1010
"github.com/abemedia/go-don/pkg/httptest"
1111
"github.com/valyala/fasthttp"
1212
)
@@ -44,29 +44,29 @@ func TestRegisterDecoder(t *testing.T) {
4444
})
4545
}
4646

47-
func testRegisterDecoder[T don.DecoderConstraint](t *testing.T, dec T, contentType, alias string) {
47+
func testRegisterDecoder[T encoding.DecoderConstraint](t *testing.T, dec T, contentType, alias string) {
4848
t.Helper()
4949

50-
don.RegisterDecoder(contentType, dec, alias)
50+
encoding.RegisterDecoder(dec, contentType, alias)
5151

5252
for _, v := range []string{contentType, alias} {
53-
decode, err := don.GetDecoder(v)
54-
if err != nil {
55-
t.Error(err)
53+
decode := encoding.GetDecoder(v)
54+
if decode == nil {
55+
t.Error("decoder not found")
5656
continue
5757
}
5858

5959
req := httptest.NewRequest("", "", v, nil)
6060

6161
var b []byte
62-
if err = decode(req, &b); err != nil {
62+
if err := decode(req, &b); err != nil {
6363
t.Error(err)
6464
} else if string(b) != v {
6565
t.Error("should decode request")
6666
}
6767

6868
req.Request.SetBody(nil)
69-
if err = decode(req, &b); err == nil {
69+
if err := decode(req, &b); err == nil {
7070
t.Error("should return error")
7171
}
7272
}

encode.go renamed to encoding/encode.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package don
1+
package encoding
22

33
import (
44
"context"
@@ -16,11 +16,11 @@ type EncoderConstraint interface {
1616
Marshaler | ContextMarshaler | ResponseEncoder
1717
}
1818

19-
// RegisterEncoder registers a response encoder.
20-
func RegisterEncoder[T EncoderConstraint](contentType string, enc T, aliases ...string) {
19+
// RegisterEncoder registers a response encoder on a given media type.
20+
func RegisterEncoder[T EncoderConstraint](enc T, mime string, aliases ...string) {
2121
switch e := any(enc).(type) {
2222
case Marshaler:
23-
encoders[contentType] = func(ctx *fasthttp.RequestCtx, v any) error {
23+
encoders[mime] = func(ctx *fasthttp.RequestCtx, v any) error {
2424
b, err := e(v)
2525
if err != nil {
2626
return err
@@ -30,7 +30,7 @@ func RegisterEncoder[T EncoderConstraint](contentType string, enc T, aliases ...
3030
}
3131

3232
case ContextMarshaler:
33-
encoders[contentType] = func(ctx *fasthttp.RequestCtx, v any) error {
33+
encoders[mime] = func(ctx *fasthttp.RequestCtx, v any) error {
3434
b, err := e(ctx, v)
3535
if err != nil {
3636
return err
@@ -40,19 +40,17 @@ func RegisterEncoder[T EncoderConstraint](contentType string, enc T, aliases ...
4040
}
4141

4242
case ResponseEncoder:
43-
encoders[contentType] = e
43+
encoders[mime] = e
4444
}
4545

4646
for _, alias := range aliases {
47-
encoders[alias] = encoders[contentType]
47+
encoders[alias] = encoders[mime]
4848
}
4949
}
5050

51-
func getEncoder(mime string) (ResponseEncoder, error) {
52-
if enc := encoders[mime]; enc != nil {
53-
return enc, nil
54-
}
55-
return nil, ErrNotAcceptable
51+
// GetEncoder returns the response encoder for a given media type.
52+
func GetEncoder(mime string) ResponseEncoder {
53+
return encoders[mime]
5654
}
5755

5856
var encoders = map[string]ResponseEncoder{}

encode_test.go renamed to encoding/encode_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package don_test
1+
package encoding_test
22

33
import (
44
"context"
55
"io"
66
"testing"
77

8-
"github.com/abemedia/go-don"
8+
"github.com/abemedia/go-don/encoding"
99
"github.com/abemedia/go-don/pkg/httptest"
1010
"github.com/valyala/fasthttp"
1111
)
@@ -43,27 +43,27 @@ func TestRegisterEncoder(t *testing.T) {
4343
})
4444
}
4545

46-
func testRegisterEncoder[T don.EncoderConstraint](t *testing.T, dec T, contentType, alias string) {
46+
func testRegisterEncoder[T encoding.EncoderConstraint](t *testing.T, dec T, contentType, alias string) {
4747
t.Helper()
4848

49-
don.RegisterEncoder(contentType, dec, alias)
49+
encoding.RegisterEncoder(dec, contentType, alias)
5050

5151
for _, v := range []string{contentType, alias} {
52-
encode, err := don.GetEncoder(v)
53-
if err != nil {
54-
t.Error(err)
52+
encode := encoding.GetEncoder(v)
53+
if encode == nil {
54+
t.Error("encoder not found")
5555
continue
5656
}
5757

5858
req := httptest.NewRequest("", "", v, nil)
5959

60-
if err = encode(req, []byte(v)); err != nil {
60+
if err := encode(req, []byte(v)); err != nil {
6161
t.Error(err)
6262
} else if string(req.Response.Body()) != v {
6363
t.Error("should encode response")
6464
}
6565

66-
if err = encode(req, []byte{}); err == nil {
66+
if err := encode(req, []byte{}); err == nil {
6767
t.Error("should return error")
6868
}
6969
}

encoding/form/form.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package form
22

33
import (
4-
"github.com/abemedia/go-don"
54
"github.com/abemedia/go-don/decoder"
5+
"github.com/abemedia/go-don/encoding"
66
"github.com/valyala/fasthttp"
77
)
88

@@ -22,6 +22,6 @@ func decodeMultipartForm(ctx *fasthttp.RequestCtx, v any) error {
2222
}
2323

2424
func init() {
25-
don.RegisterDecoder("application/x-www-form-urlencoded", decodeForm)
26-
don.RegisterDecoder("multipart/form-data", decodeMultipartForm)
25+
encoding.RegisterDecoder(decodeForm, "application/x-www-form-urlencoded")
26+
encoding.RegisterDecoder(decodeMultipartForm, "multipart/form-data")
2727
}

encoding/form/form_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ func TestForm(t *testing.T) {
1212
}
1313

1414
t.Run("URLEncoded", func(t *testing.T) {
15-
test.Decode(t, test.EncodingTest[item]{
15+
test.Decode(t, test.EncodingOptions[item]{
1616
Mime: "application/x-www-form-urlencoded",
1717
Raw: "foo=bar",
1818
Parsed: item{Foo: "bar"},
1919
})
2020
})
2121

2222
t.Run("Multipart", func(t *testing.T) {
23-
test.Decode(t, test.EncodingTest[item]{
23+
test.Decode(t, test.EncodingOptions[item]{
2424
Mime: `multipart/form-data;boundary="boundary"`,
2525
Raw: "--boundary\nContent-Disposition: form-data; name=\"foo\"\n\nbar\n--boundary\n",
2626
Parsed: item{Foo: "bar"},

encoding/json/json.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package json
22

33
import (
4-
"github.com/abemedia/go-don"
4+
"github.com/abemedia/go-don/encoding"
55
"github.com/goccy/go-json"
66
"github.com/valyala/fasthttp"
77
)
@@ -15,6 +15,6 @@ func encodeJSON(ctx *fasthttp.RequestCtx, v any) error {
1515
}
1616

1717
func init() {
18-
don.RegisterDecoder("application/json", decodeJSON)
19-
don.RegisterEncoder("application/json", encodeJSON)
18+
encoding.RegisterDecoder(decodeJSON, "application/json")
19+
encoding.RegisterEncoder(encodeJSON, "application/json")
2020
}

encoding/json/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestJSON(t *testing.T) {
1111
Foo string `json:"foo"`
1212
}
1313

14-
test.Encoding(t, test.EncodingTest[item]{
14+
test.Encoding(t, test.EncodingOptions[item]{
1515
Mime: "application/json",
1616
Raw: `{"foo":"bar"}` + "\n",
1717
Parsed: item{Foo: "bar"},

encoding/msgpack/msgpack.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package msgpack
22

33
import (
4-
"github.com/abemedia/go-don"
4+
"github.com/abemedia/go-don/encoding"
55
"github.com/valyala/fasthttp"
66
"github.com/vmihailenco/msgpack/v5"
77
)
@@ -15,6 +15,6 @@ func encodeMsgpack(ctx *fasthttp.RequestCtx, v any) error {
1515
}
1616

1717
func init() {
18-
don.RegisterDecoder("application/x-msgpack", decodeMsgpack)
19-
don.RegisterEncoder("application/x-msgpack", encodeMsgpack)
18+
encoding.RegisterDecoder(decodeMsgpack, "application/x-msgpack")
19+
encoding.RegisterEncoder(encodeMsgpack, "application/x-msgpack")
2020
}

0 commit comments

Comments
 (0)