Skip to content

Commit

Permalink
使用sonic包
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Nov 20, 2024
1 parent 749c0c4 commit 33e3d68
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 28 deletions.
4 changes: 2 additions & 2 deletions configure/fopsConfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package configure
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"time"

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

Expand All @@ -20,7 +20,7 @@ type fopsConfigureVO struct {
}

func getFopsConfigure() ([]fopsConfigureVO, error) {
bodyByte, _ := json.Marshal(map[string]string{"AppName": core.AppName})
bodyByte, _ := sonic.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
11 changes: 6 additions & 5 deletions core/apiResponse.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package core

import (
"encoding/json"
"github.com/farseer-go/fs/dateTime"
"io"

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

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

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

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

Expand Down Expand Up @@ -82,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]
_ = json.Unmarshal(body, &apiResponse)
_ = sonic.Unmarshal(body, &apiResponse)
return apiResponse
}
7 changes: 4 additions & 3 deletions core/eumLogLevel/enum.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package eumLogLevel

import (
"encoding/json"
"strings"

"github.com/bytedance/sonic"
)

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

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

import (
"database/sql/driver"
"encoding/json"

"errors"
"fmt"
"strings"
"time"

"github.com/bytedance/sonic"
)

type DateTime struct {
Expand Down Expand Up @@ -210,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 json.Marshal(receiver.ToString("yyyy-MM-dd hh:mm:ss"))
return sonic.Marshal(receiver.ToString("yyyy-MM-dd hh:mm:ss"))
}

// UnmarshalJSON to deserialize []byte
Expand Down
9 changes: 5 additions & 4 deletions flog/fopsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package flog
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"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/parse"
"github.com/farseer-go/fs/sonyflake"
"github.com/farseer-go/fs/trace"
"net/http"
"time"
)

// FopsProvider 上传到FOPS
Expand Down Expand Up @@ -84,7 +85,7 @@ type UploadRequest struct {
}

func (r *fopsLoggerPersistent) upload(lstLog []*LogData) error {
bodyByte, _ := json.Marshal(UploadRequest{List: lstLog})
bodyByte, _ := sonic.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
5 changes: 3 additions & 2 deletions flog/iFormatter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package flog

import (
"encoding/json"
"fmt"

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

Expand All @@ -16,7 +17,7 @@ type JsonFormatter struct {
}

func (r JsonFormatter) Formatter(log *LogData) string {
marshal, _ := json.Marshal(LogData{
marshal, _ := sonic.Marshal(LogData{
CreateAt: log.CreateAt,
LogLevel: log.LogLevel,
Component: log.Component,
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ go 1.21
toolchain go1.23.3

require (
github.com/bytedance/sonic v1.12.4
github.com/stretchr/testify v1.9.0
github.com/timandy/routine v1.1.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.12.0 // indirect
golang.org/x/sys v0.27.0 // indirect
)
9 changes: 5 additions & 4 deletions parse/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package parse

import (
"encoding/json"
"github.com/farseer-go/fs/dateTime"
"github.com/farseer-go/fs/fastReflect"
"github.com/farseer-go/fs/types"
"reflect"
"strings"
"time"
"unsafe"

"github.com/farseer-go/fs/dateTime"
"github.com/farseer-go/fs/fastReflect"
"github.com/farseer-go/fs/types"
)

var layouts = []string{"2006-01-02 15:04:05", "2006-01-02", "2006-01-02T15:04:05Z07:00", "02/01/2006"}
Expand Down Expand Up @@ -38,7 +39,7 @@ func Convert[T any](source any, defVal T) T {
}

// 枚举转...
if sourceMeta.IsEmum{
if sourceMeta.IsEmum {
switch defValMeta.TypeIdentity {
// 转数字
case "number":
Expand Down
7 changes: 4 additions & 3 deletions test/apiResponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package test

import (
"bytes"
"encoding/json"
"testing"

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

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

Expand Down
7 changes: 4 additions & 3 deletions test/eumLogLevel_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package test

import (
"encoding/json"
"testing"

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

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

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

0 comments on commit 33e3d68

Please sign in to comment.