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 c65a0e4 commit c1d6e55
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 20 deletions.
4 changes: 2 additions & 2 deletions configure/fopsConfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http"
"time"

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

// DomainObject 配置中心
Expand All @@ -20,7 +20,7 @@ type fopsConfigureVO struct {
}

func getFopsConfigure() ([]fopsConfigureVO, error) {
bodyByte, _ := sonic.Marshal(map[string]string{"AppName": core.AppName})
bodyByte, _ := snc.Marshal(map[string]string{"AppName": core.AppName})
url := fopsServer + "configure/list"
newRequest, _ := http.NewRequest("POST", url, bytes.NewReader(bodyByte))
newRequest.Header.Set("Content-Type", "application/json")
Expand Down
8 changes: 4 additions & 4 deletions core/apiResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package core
import (
"io"

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

// ApiResponse 标准的API Response结构
Expand Down Expand Up @@ -33,13 +33,13 @@ func (receiver *ApiResponse[TData]) SetData(data TData) {

// ToJson 转成Json
func (receiver *ApiResponse[TData]) ToJson() string {
bytes, _ := sonic.Marshal(receiver)
bytes, _ := snc.Marshal(receiver)
return string(bytes)
}

// ToBytes 转成Json字节
func (receiver *ApiResponse[TData]) ToBytes() []byte {
bytes, _ := sonic.Marshal(receiver)
bytes, _ := snc.Marshal(receiver)
return bytes
}

Expand Down Expand Up @@ -83,6 +83,6 @@ func NewApiResponseByReader[TData any](reader io.Reader) ApiResponse[TData] {
// NewApiResponseByByte 创建实例
func NewApiResponseByByte[TData any](body []byte) ApiResponse[TData] {
var apiResponse ApiResponse[TData]
_ = sonic.Unmarshal(body, &apiResponse)
_ = snc.Unmarshal(body, &apiResponse)
return apiResponse
}
6 changes: 3 additions & 3 deletions core/eumLogLevel/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package eumLogLevel
import (
"strings"

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

// Enum 日志等级
Expand Down Expand Up @@ -60,13 +60,13 @@ func (receiver Enum) ToString() string {
// MarshalJSON to output non base64 encoded []byte
// 此处不能用指针,否则json序列化时不执行
func (receiver Enum) MarshalJSON() ([]byte, error) {
return sonic.Marshal(receiver.ToString())
return snc.Marshal(receiver.ToString())
}

// UnmarshalJSON to deserialize []byte
func (receiver *Enum) UnmarshalJSON(b []byte) error {
var numStr string
err := sonic.Unmarshal(b, &numStr)
err := snc.Unmarshal(b, &numStr)
*receiver = GetEnum(numStr)
return err
}
4 changes: 2 additions & 2 deletions dateTime/dt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

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

type DateTime struct {
Expand Down Expand Up @@ -212,7 +212,7 @@ func (receiver DateTime) Before(dt DateTime) bool {
// MarshalJSON to output non base64 encoded []byte
// 此处不能用指针,否则json序列化时不执行
func (receiver DateTime) MarshalJSON() ([]byte, error) {
return sonic.Marshal(receiver.ToString("yyyy-MM-dd hh:mm:ss"))
return snc.Marshal(receiver.ToString("yyyy-MM-dd hh:mm:ss"))
}

// UnmarshalJSON to deserialize []byte
Expand Down
4 changes: 2 additions & 2 deletions flog/fopsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"strconv"
"time"

"github.com/bytedance/sonic"
"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/core/eumLogLevel"
"github.com/farseer-go/fs/dateTime"
"github.com/farseer-go/fs/snc"
"github.com/farseer-go/fs/sonyflake"
"github.com/farseer-go/fs/trace"
)
Expand Down Expand Up @@ -85,7 +85,7 @@ type UploadRequest struct {
}

func (r *fopsLoggerPersistent) upload(lstLog []*LogData) error {
bodyByte, _ := sonic.Marshal(UploadRequest{List: lstLog})
bodyByte, _ := snc.Marshal(UploadRequest{List: lstLog})
url := r.fopsServer + "flog/upload"
newRequest, _ := http.NewRequest("POST", url, bytes.NewReader(bodyByte))
newRequest.Header.Set("Content-Type", "application/json")
Expand Down
4 changes: 2 additions & 2 deletions flog/iFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package flog
import (
"fmt"

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

// IFormatter 日志格式
Expand All @@ -17,7 +17,7 @@ type JsonFormatter struct {
}

func (r JsonFormatter) Formatter(log *LogData) string {
marshal, _ := sonic.Marshal(LogData{
marshal, _ := snc.Marshal(LogData{
CreateAt: log.CreateAt,
LogLevel: log.LogLevel,
Component: log.Component,
Expand Down
3 changes: 2 additions & 1 deletion modules/farseerKernelModule.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package modules

import (
"time"

"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/timingWheel"
"github.com/farseer-go/fs/trace"
"time"
)

type FarseerKernelModule struct {
Expand Down
30 changes: 30 additions & 0 deletions snc/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package snc

import (
"github.com/bytedance/sonic"
"github.com/bytedance/sonic/option"
)

var snc sonic.API

func init() {
// 设置较小的缓冲区
option.DefaultEncoderBufferSize = 32 * 1024
snc = sonic.Config{
CompactMarshaler: true,
UseNumber: true,
CopyString: true,
}.Froze()
}

func Unmarshal(data []byte, v any) error {
return snc.Unmarshal(data, v)
}

func Marshal(val any) ([]byte, error) {
return snc.Marshal(val)
}

func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
return snc.MarshalIndent(v, prefix, indent)
}
4 changes: 2 additions & 2 deletions test/apiResponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"testing"

"github.com/bytedance/sonic"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/snc"
"github.com/stretchr/testify/assert"
)

func TestApiResponse(t *testing.T) {
api := core.Success("成功", "nice")
apiByte, _ := sonic.Marshal(api)
apiByte, _ := snc.Marshal(api)
assert.Equal(t, string(apiByte), api.ToJson())
assert.Equal(t, apiByte, api.ToBytes())

Expand Down
4 changes: 2 additions & 2 deletions test/eumLogLevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package test
import (
"testing"

"github.com/bytedance/sonic"
"github.com/farseer-go/fs/core/eumLogLevel"
"github.com/farseer-go/fs/snc"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -33,7 +33,7 @@ func TestEumLogLevel(t *testing.T) {
assert.Equal(t, eumLogLevel.Warning, eumLogLevel.GetEnum("Warn"))

var e = eumLogLevel.Debug
b, _ := sonic.Marshal(e)
b, _ := snc.Marshal(e)
e = eumLogLevel.Information
_ = e.UnmarshalJSON(b)
assert.True(t, e == eumLogLevel.Debug)
Expand Down

0 comments on commit c1d6e55

Please sign in to comment.