Skip to content

Commit

Permalink
调整sonic的使用
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Nov 29, 2024
1 parent b2c7287 commit bc1e69f
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 49 deletions.
14 changes: 7 additions & 7 deletions apiDoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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("</textarea>")
Expand All @@ -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("")
Expand All @@ -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))
}
}
Expand All @@ -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("</textarea>")
Expand Down
14 changes: 2 additions & 12 deletions context/httpRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions context/httpResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"reflect"

"github.com/bytedance/sonic"
"github.com/farseer-go/fs/snc"
)

type HttpResponse struct {
Expand Down Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions context/httpRoute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 路由表
Expand Down Expand Up @@ -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类型,需要做注入操作
Expand Down
4 changes: 2 additions & 2 deletions test/benchmark_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions test/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions test/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions test/newApplicationBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions test/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions test/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions test/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions test/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions websocket/baseContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions websocket/webSocketContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bc1e69f

Please sign in to comment.