Skip to content

Commit a541544

Browse files
authored
perf(encoding): improve encoding performance (#113)
1 parent 4cd799c commit a541544

5 files changed

Lines changed: 6 additions & 25 deletions

File tree

encoding/form/form.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func decodeMultipartForm(ctx *fasthttp.RequestCtx, v any) error {
1717
if err != nil {
1818
return err
1919
}
20-
2120
return dec.Decode(decoder.Map(f.Value), v)
2221
}
2322

encoding/json/json.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@ package json
33
import (
44
"github.com/abemedia/go-don/encoding"
55
"github.com/goccy/go-json"
6-
"github.com/valyala/fasthttp"
76
)
87

9-
func decodeJSON(ctx *fasthttp.RequestCtx, v any) error {
10-
return json.NewDecoder(ctx.RequestBodyStream()).Decode(v)
11-
}
12-
13-
func encodeJSON(ctx *fasthttp.RequestCtx, v any) error {
14-
return json.NewEncoder(ctx).Encode(v)
15-
}
16-
178
func init() {
18-
encoding.RegisterDecoder(decodeJSON, "application/json")
19-
encoding.RegisterEncoder(encodeJSON, "application/json")
9+
encoding.RegisterDecoder(json.Unmarshal, "application/json")
10+
encoding.RegisterEncoder(json.Marshal, "application/json")
2011
}

encoding/json/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type item struct {
1212

1313
var opt = test.EncodingOptions[item]{
1414
Mime: "application/json",
15-
Raw: `{"foo":"bar"}` + "\n",
15+
Raw: `{"foo":"bar"}`,
1616
Parsed: item{Foo: "bar"},
1717
}
1818

encoding/msgpack/msgpack.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@ package msgpack
22

33
import (
44
"github.com/abemedia/go-don/encoding"
5-
"github.com/valyala/fasthttp"
65
"github.com/vmihailenco/msgpack/v5"
76
)
87

9-
func decodeMsgpack(ctx *fasthttp.RequestCtx, v any) error {
10-
return msgpack.NewDecoder(ctx.RequestBodyStream()).Decode(v)
11-
}
12-
13-
func encodeMsgpack(ctx *fasthttp.RequestCtx, v any) error {
14-
return msgpack.NewEncoder(ctx).Encode(v)
15-
}
16-
178
func init() {
18-
encoding.RegisterDecoder(decodeMsgpack, "application/x-msgpack")
19-
encoding.RegisterEncoder(encodeMsgpack, "application/x-msgpack")
9+
encoding.RegisterDecoder(msgpack.Unmarshal, "application/x-msgpack")
10+
encoding.RegisterEncoder(msgpack.Marshal, "application/x-msgpack")
2011
}

handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestHandlerResponse(t *testing.T) {
9191
message: "should return null",
9292
want: response{
9393
Code: fasthttp.StatusOK,
94-
Body: "null\n",
94+
Body: "null",
9595
Header: map[string]string{
9696
"Content-Length": "0",
9797
"Content-Type": "application/json; charset=utf-8",

0 commit comments

Comments
 (0)