From bc1e69fcf2ef4b2108987f5b7b75370219eb64e9 Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Sat, 30 Nov 2024 02:40:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4sonic=E7=9A=84=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apiDoc.go | 14 +++++++------- context/httpRequest.go | 14 ++------------ context/httpResponse.go | 4 ++-- context/httpRoute.go | 4 ++-- test/benchmark_run_test.go | 4 ++-- test/controller_test.go | 6 +++--- test/cors_test.go | 6 +++--- test/newApplicationBuilder_test.go | 4 ++-- test/request_test.go | 8 ++++---- test/response_test.go | 6 +++--- test/routes_test.go | 4 ++-- test/validate_test.go | 6 +++--- websocket/baseContext.go | 4 ++-- websocket/webSocketContext.go | 4 ++-- 14 files changed, 39 insertions(+), 49 deletions(-) diff --git a/apiDoc.go b/apiDoc.go index 9beb5af..8a35d6a 100644 --- a/apiDoc.go +++ b/apiDoc.go @@ -5,11 +5,11 @@ import ( "reflect" "strings" - "github.com/bytedance/sonic" "github.com/farseer-go/collections" "github.com/farseer-go/fs/core" "github.com/farseer-go/fs/fastReflect" "github.com/farseer-go/fs/parse" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/fs/types" "github.com/farseer-go/webapi/action" "github.com/farseer-go/webapi/context" @@ -40,7 +40,7 @@ func (r *applicationBuilder) UseApiDoc() { // dto模式转成json if httpRoute.RequestParamIsModel { val := reflect.New(httpRoute.RequestParamType.First()).Interface() - indent, _ := sonic.MarshalIndent(val, "", " ") + indent, _ := snc.MarshalIndent(val, "", " ") lstBody.Add(string(indent)) } else { var mapVal = make(map[string]any) @@ -53,7 +53,7 @@ func (r *applicationBuilder) UseApiDoc() { mapVal[paramName] = reflect.New(fieldType).Elem().Interface() } } - indent, _ := sonic.MarshalIndent(mapVal, "", " ") + indent, _ := snc.MarshalIndent(mapVal, "", " ") lstBody.Add(string(indent)) } lstBody.Add("") @@ -63,7 +63,7 @@ func (r *applicationBuilder) UseApiDoc() { // 使用ApiResponse,则返回ApiResponse格式 if r.useApiResponse { rsp := core.Success[any]("成功", nil) - indent, _ := sonic.MarshalIndent(rsp, "", " ") + indent, _ := snc.MarshalIndent(rsp, "", " ") lstBody.Add(string(indent)) } else { lstBody.Add("") @@ -85,14 +85,14 @@ func (r *applicationBuilder) UseApiDoc() { // 使用ApiResponse,则返回ApiResponse格式 if r.useApiResponse { rsp := core.Success[any]("成功", responseBody) - indent, _ := sonic.MarshalIndent(rsp, "", " ") + indent, _ := snc.MarshalIndent(rsp, "", " ") lstBody.Add(string(indent)) } else { // 基本类型直接转string if httpRoute.IsGoBasicType { lstBody.Add(parse.ToString(responseBody)) } else { // dto - indent, _ := sonic.MarshalIndent(responseBody, "", " ") + indent, _ := snc.MarshalIndent(responseBody, "", " ") lstBody.Add(string(indent)) } } @@ -102,7 +102,7 @@ func (r *applicationBuilder) UseApiDoc() { httpRoute.ResponseBodyType.Foreach(func(item *reflect.Type) { lst.Add(reflect.New(*item).Interface()) }) - indent, _ := sonic.MarshalIndent(lst, "", " ") + indent, _ := snc.MarshalIndent(lst, "", " ") lstBody.Add(string(indent)) } lstBody.Add("") diff --git a/context/httpRequest.go b/context/httpRequest.go index 8eccc89..d83d844 100644 --- a/context/httpRequest.go +++ b/context/httpRequest.go @@ -7,19 +7,9 @@ import ( "reflect" "strings" - "github.com/bytedance/sonic" + "github.com/farseer-go/fs/snc" ) -var snc sonic.API - -func init() { - snc = sonic.Config{ - CompactMarshaler: true, - SortMapKeys: true, - UseNumber: true, - }.Froze() -} - type HttpRequest struct { Body io.ReadCloser BodyString string @@ -34,7 +24,7 @@ type HttpRequest struct { func (r *HttpRequest) jsonToMap() map[string]any { mapVal := make(map[string]any) //_ = json.Unmarshal(r.BodyBytes, &mapVal) - // d := sonic.NewDecoder(bytes.NewReader(r.BodyBytes)) + // d := json.NewDecoder(bytes.NewReader(r.BodyBytes)) // d.UseNumber() // _ = d.Decode(&mapVal) snc.Unmarshal(r.BodyBytes, &mapVal) diff --git a/context/httpResponse.go b/context/httpResponse.go index 31888a9..40595a8 100644 --- a/context/httpResponse.go +++ b/context/httpResponse.go @@ -4,7 +4,7 @@ import ( "net/http" "reflect" - "github.com/bytedance/sonic" + "github.com/farseer-go/fs/snc" ) type HttpResponse struct { @@ -38,7 +38,7 @@ func (receiver *HttpResponse) WriteString(content string) { // WriteJson 将响应内容转成json后写入http流 func (receiver *HttpResponse) WriteJson(content any) { - receiver.BodyBytes, _ = sonic.Marshal(content) + receiver.BodyBytes, _ = snc.Marshal(content) receiver.W.Header().Set("Content-Type", "application/json") } diff --git a/context/httpRoute.go b/context/httpRoute.go index bb79957..88fea20 100644 --- a/context/httpRoute.go +++ b/context/httpRoute.go @@ -6,10 +6,10 @@ import ( "reflect" "strings" - "github.com/bytedance/sonic" "github.com/farseer-go/collections" "github.com/farseer-go/fs/container" "github.com/farseer-go/fs/parse" + "github.com/farseer-go/fs/snc" ) // HttpRoute 路由表 @@ -44,7 +44,7 @@ func (receiver *HttpRoute) JsonToParams(request *HttpRequest) []reflect.Value { // 第一个参数,将json反序列化到dto firstParamType := receiver.RequestParamType.First() // 先取第一个参数 val := reflect.New(firstParamType).Interface() - _ = sonic.Unmarshal(request.BodyBytes, val) + _ = snc.Unmarshal(request.BodyBytes, val) returnVal := []reflect.Value{reflect.ValueOf(val).Elem()} // 第2个参数起,为interface类型,需要做注入操作 diff --git a/test/benchmark_run_test.go b/test/benchmark_run_test.go index 766ba3c..2f79403 100644 --- a/test/benchmark_run_test.go +++ b/test/benchmark_run_test.go @@ -7,9 +7,9 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" ) @@ -29,7 +29,7 @@ func BenchmarkRun(b *testing.B) { time.Sleep(10 * time.Millisecond) b.ReportAllocs() sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) for i := 0; i < b.N; i++ { rsp, _ := http.Post("http://127.0.0.1:8094/dto", "application/json", bytes.NewReader(marshal)) diff --git a/test/controller_test.go b/test/controller_test.go index bcf10e5..14809fe 100644 --- a/test/controller_test.go +++ b/test/controller_test.go @@ -10,10 +10,10 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" "github.com/farseer-go/fs/core" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" "github.com/farseer-go/webapi/controller" "github.com/farseer-go/webapi/middleware" @@ -86,7 +86,7 @@ func TestController(t *testing.T) { t.Run("api/1.0/test/hello1", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8079/api/1.0/testheader/hello1", "application/json", bytes.NewReader(marshal)) apiResponse := core.NewApiResponseByReader[string](rsp.Body) _ = rsp.Body.Close() @@ -110,7 +110,7 @@ func TestController(t *testing.T) { t.Run("api/1.0/test/hello2-application/json", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8079/api/1.0/testheader/hello2", "application/json", bytes.NewReader(marshal)) apiResponse := core.NewApiResponseByReader[pageSizeRequest](rsp.Body) _ = rsp.Body.Close() diff --git a/test/cors_test.go b/test/cors_test.go index 1b446f2..7138ec5 100644 --- a/test/cors_test.go +++ b/test/cors_test.go @@ -7,9 +7,9 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" "github.com/stretchr/testify/assert" ) @@ -27,7 +27,7 @@ func TestCors(t *testing.T) { t.Run("/cors/test:8080", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) req, _ := http.NewRequest("DELETE", "http://127.0.0.1:8080/cors/test", bytes.NewReader(marshal)) req.Header.Set("Content-Type", "application/json") rsp, _ := http.DefaultClient.Do(req) @@ -39,7 +39,7 @@ func TestCors(t *testing.T) { t.Run("/cors/test:8080-OPTIONS", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) req, _ := http.NewRequest("OPTIONS", "http://127.0.0.1:8080/cors/test", bytes.NewReader(marshal)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Origin", "localhost") diff --git a/test/newApplicationBuilder_test.go b/test/newApplicationBuilder_test.go index 11ddef5..3bc53ef 100644 --- a/test/newApplicationBuilder_test.go +++ b/test/newApplicationBuilder_test.go @@ -7,9 +7,9 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" "github.com/stretchr/testify/assert" ) @@ -25,7 +25,7 @@ func TestNewApplicationBuilder(t *testing.T) { t.Run("mini/test2:8083", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8083/mini/test", "application/json", bytes.NewReader(marshal)) body, _ := io.ReadAll(rsp.Body) _ = rsp.Body.Close() diff --git a/test/request_test.go b/test/request_test.go index 8e250e8..896179c 100644 --- a/test/request_test.go +++ b/test/request_test.go @@ -11,10 +11,10 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" "github.com/farseer-go/fs/core" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" "github.com/stretchr/testify/assert" ) @@ -60,7 +60,7 @@ func TestRequest(t *testing.T) { t.Run("dto-json", func(t *testing.T) { sizeRequest := psRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8085/dto", "application/json", bytes.NewReader(marshal)) apiResponse := core.NewApiResponseByReader[string](rsp.Body) _ = rsp.Body.Close() @@ -117,7 +117,7 @@ func TestRequest(t *testing.T) { t.Run("multiParam-json", func(t *testing.T) { sizeRequest := psRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) req, _ := http.NewRequest("PUT", "http://127.0.0.1:8085/multiParam", bytes.NewReader(marshal)) req.Header.Set("Content-Type", "application/json") rsp, _ := http.DefaultClient.Do(req) @@ -165,7 +165,7 @@ func TestRequest(t *testing.T) { Ids []int Enable bool } - b, _ := sonic.Marshal(array{Ids: []int{1, 2, 3}, Enable: true}) + b, _ := snc.Marshal(array{Ids: []int{1, 2, 3}, Enable: true}) rsp, _ := http.Post("http://127.0.0.1:8085/array", "application/json", bytes.NewReader(b)) apiResponse := core.NewApiResponseByReader[[]int](rsp.Body) _ = rsp.Body.Close() diff --git a/test/response_test.go b/test/response_test.go index 76d292d..d73da7f 100644 --- a/test/response_test.go +++ b/test/response_test.go @@ -8,10 +8,10 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" "github.com/farseer-go/fs/core" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" "github.com/stretchr/testify/assert" ) @@ -34,7 +34,7 @@ func TestResponse(t *testing.T) { t.Run("multiResponse", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) req, _ := http.NewRequest("DELETE", "http://127.0.0.1:8086/multiResponse", bytes.NewReader(marshal)) req.Header.Set("Content-Type", "application/json") rsp, _ := http.DefaultClient.Do(req) @@ -46,7 +46,7 @@ func TestResponse(t *testing.T) { t.Run("basicTypeResponse", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8086/basicTypeResponse", "application/json", bytes.NewReader(marshal)) apiResponse := core.NewApiResponseByReader[string](rsp.Body) _ = rsp.Body.Close() diff --git a/test/routes_test.go b/test/routes_test.go index 09f6c7e..e3f32a4 100644 --- a/test/routes_test.go +++ b/test/routes_test.go @@ -8,9 +8,9 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" "github.com/stretchr/testify/assert" ) @@ -27,7 +27,7 @@ func TestRoutes(t *testing.T) { t.Run("mini/test1:8095-POST", func(t *testing.T) { sizeRequest := pageSizeRequest{PageSize: 10, PageIndex: 2} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8095/mini/test1", "application/json", bytes.NewReader(marshal)) body, _ := io.ReadAll(rsp.Body) _ = rsp.Body.Close() diff --git a/test/validate_test.go b/test/validate_test.go index ca44bb2..2a36fca 100644 --- a/test/validate_test.go +++ b/test/validate_test.go @@ -8,9 +8,9 @@ import ( "testing" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs" "github.com/farseer-go/fs/configure" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/webapi" "github.com/stretchr/testify/assert" ) @@ -32,7 +32,7 @@ func TestValidate(t *testing.T) { t.Run("/Validate error", func(t *testing.T) { sizeRequest := ValidateRequest{Name: "", Age: 200} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8092/Validate", "application/json", bytes.NewReader(marshal)) body, _ := io.ReadAll(rsp.Body) _ = rsp.Body.Close() @@ -42,7 +42,7 @@ func TestValidate(t *testing.T) { t.Run("/Validate success", func(t *testing.T) { sizeRequest := ValidateRequest{Name: "steden", Age: 37} - marshal, _ := sonic.Marshal(sizeRequest) + marshal, _ := snc.Marshal(sizeRequest) rsp, _ := http.Post("http://127.0.0.1:8092/Validate", "application/json", bytes.NewReader(marshal)) body, _ := io.ReadAll(rsp.Body) _ = rsp.Body.Close() diff --git a/websocket/baseContext.go b/websocket/baseContext.go index c6b6df6..4cb2fb9 100644 --- a/websocket/baseContext.go +++ b/websocket/baseContext.go @@ -8,12 +8,12 @@ import ( "net" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/container" "github.com/farseer-go/fs/exception" "github.com/farseer-go/fs/fastReflect" "github.com/farseer-go/fs/flog" "github.com/farseer-go/fs/parse" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/fs/trace" "github.com/farseer-go/webapi/context" "github.com/timandy/routine" @@ -115,7 +115,7 @@ func (receiver *BaseContext) Send(msg any) error { message = parse.ToString(msg) } else { // 其余类型,一律使用json - marshal, _ := sonic.Marshal(msg) + marshal, _ := snc.Marshal(msg) message = string(marshal) } err = websocket.Message.Send(receiver.HttpContext.WebsocketConn, message) diff --git a/websocket/webSocketContext.go b/websocket/webSocketContext.go index fabc4c4..1988b3d 100644 --- a/websocket/webSocketContext.go +++ b/websocket/webSocketContext.go @@ -7,11 +7,11 @@ import ( "reflect" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/container" "github.com/farseer-go/fs/exception" "github.com/farseer-go/fs/flog" "github.com/farseer-go/fs/parse" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/fs/trace" "github.com/farseer-go/webapi/context" "github.com/timandy/routine" @@ -143,7 +143,7 @@ reopen: // 序列化 var t T - if err := sonic.Unmarshal([]byte(message), &t); err != nil { + if err := snc.Unmarshal([]byte(message), &t); err != nil { receiver.errorIsClose(err) flog.Warningf("路由:%s 接收数据时,出现反序列失败:%s", receiver.HttpContext.Route.RouteUrl, err.Error()) goto reopen