From 95675018b8ef4d10409c6fe8a100e8a26fc891d1 Mon Sep 17 00:00:00 2001 From: jianggb Date: Sat, 11 May 2024 08:19:29 +0800 Subject: [PATCH] fix: default use json.Codec --- encoding.go | 36 ++++++++---------------------------- encoding_test.go | 14 +++++++------- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/encoding.go b/encoding.go index ce2ce81..15ce6ad 100644 --- a/encoding.go +++ b/encoding.go @@ -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" ) @@ -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. // @@ -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}, } } diff --git a/encoding_test.go b/encoding_test.go index e3f277b..d37fb94 100644 --- a/encoding_test.go +++ b/encoding_test.go @@ -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) { @@ -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) } } @@ -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) } }