Skip to content

Commit

Permalink
fix: default use json.Codec
Browse files Browse the repository at this point in the history
  • Loading branch information
jianggb committed May 11, 2024
1 parent 8ffd1a8 commit 9567501
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
36 changes: 8 additions & 28 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"net/url"
"strings"

"google.golang.org/protobuf/encoding/protojson"

"github.com/things-go/encoding/codec"
"github.com/things-go/encoding/form"
"github.com/things-go/encoding/jsonpb"
"github.com/things-go/encoding/json"
"github.com/things-go/encoding/proto"
)

Expand Down Expand Up @@ -60,11 +58,11 @@ type Encoding struct {
//
// MIMEPOSTForm: form.Codec
// MIMEMultipartPOSTForm: form.MultipartCodec
// MIMEJSON: jsonpb.Codec
// MIMEJSON: json.Codec
// MIMEPROTOBUF: proto.Codec
// mimeQuery: form.QueryCodec
// mimeUri: form.UriCodec
// mimeWildcard: HTTPBodyCodec
// mimeWildcard: json.Codec
//
// you can manually register your custom Marshaler.
//
Expand All @@ -79,30 +77,12 @@ func New() *Encoding {
mimeMap: map[string]codec.Marshaler{
MIMEPOSTForm: form.New("json"),
MIMEMultipartPOSTForm: &form.MultipartCodec{Codec: form.New("json")},
MIMEJSON: &jsonpb.Codec{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
UseEnumNumbers: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
},
MIMEPROTOBUF: &proto.Codec{},
},
mimeQuery: &form.QueryCodec{Codec: form.New("json")},
mimeUri: &form.UriCodec{Codec: form.New("json")},
mimeWildcard: &HTTPBodyCodec{
Marshaler: &jsonpb.Codec{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
UseEnumNumbers: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
},
MIMEJSON: &json.Codec{UseNumber: true, DisallowUnknownFields: true},
MIMEPROTOBUF: &proto.Codec{},
},
mimeQuery: &form.QueryCodec{Codec: form.New("json")},
mimeUri: &form.UriCodec{Codec: form.New("json")},
mimeWildcard: &json.Codec{UseNumber: true, DisallowUnknownFields: true},
}
}

Expand Down
14 changes: 7 additions & 7 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Test_Encoding_Register(t *testing.T) {
require.NoError(t, err)

got = registry.Get(MIMEPROTOBUF)
_, ok = got.(*HTTPBodyCodec)
_, ok = got.(*json.Codec)
require.True(t, ok, "should be got MIME wildcard marshaler")
})
t.Run("remove not allow MIME type", func(t *testing.T) {
Expand All @@ -80,12 +80,12 @@ func Test_Encoding_Inbound_Or_OutBound_ForRequest_Wildcard(t *testing.T) {
r.Header.Set("Accept", "application/unknown")
r.Header.Set("Content-Type", "application/unknown")
_, in := registry.InboundForRequest(r)
if _, ok := in.(*HTTPBodyCodec); !ok {
t.Errorf("in = %#v; want a HTTPBodyCodec", in)
if _, ok := in.(*json.Codec); !ok {
t.Errorf("in = %#v; want a json.Codec", in)
}
out := registry.OutboundForRequest(r)
if _, ok := out.(*HTTPBodyCodec); !ok {
t.Errorf("out = %#v; want a HTTPBodyCodec", out)
if _, ok := out.(*json.Codec); !ok {
t.Errorf("out = %#v; want a json.Codec", out)
}
}

Expand Down Expand Up @@ -674,8 +674,8 @@ func Test_Encoding_InBound_ForResponse_Wildcard(t *testing.T) {
resp.Header.Set("Content-Type", "application/unknown")

out := registry.InboundForResponse(resp)
if _, ok := out.(*HTTPBodyCodec); !ok {
t.Errorf("out = %#v; want a HTTPBodyCodec", out)
if _, ok := out.(*json.Codec); !ok {
t.Errorf("out = %#v; want a json.Codec", out)
}
}

Expand Down

0 comments on commit 9567501

Please sign in to comment.