Skip to content

Commit

Permalink
Merge pull request #42 from quan-xie/update_json_lib
Browse files Browse the repository at this point in the history
Update json lib
  • Loading branch information
quan-xie authored Sep 14, 2023
2 parents f28f680 + e21383b commit 84ea8b3
Show file tree
Hide file tree
Showing 489 changed files with 293,414 additions and 4,961 deletions.
34 changes: 20 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/quan-xie/tuba

go 1.17
go 1.18

require (
contrib.go.opencensus.io/exporter/jaeger v0.2.1
contrib.go.opencensus.io/exporter/zipkin v0.1.2
github.com/bytedance/sonic v1.10.1
github.com/cenkalti/backoff/v3 v3.2.2
github.com/elastic/go-elasticsearch/v8 v8.0.0-20210531084204-f01628963386
github.com/go-sql-driver/mysql v1.5.0
Expand All @@ -15,40 +16,45 @@ require (
github.com/openzipkin/zipkin-go v0.4.0
github.com/pkg/errors v0.9.1
go.mongodb.org/mongo-driver v1.5.2
go.opencensus.io v0.23.0
go.opencensus.io v0.24.0
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20220722155237-a158d28d115b
google.golang.org/genproto v0.0.0-20200806141610-86f49bd18e98
google.golang.org/grpc v1.41.0
google.golang.org/protobuf v1.27.1
golang.org/x/net v0.12.0
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98
google.golang.org/grpc v1.56.2
google.golang.org/protobuf v1.31.0
)

require (
github.com/aws/aws-sdk-go v1.34.28 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/lestrrat-go/strftime v1.0.1 // indirect
github.com/tebeka/strftime v0.1.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/uber/jaeger-client-go v2.25.0+incompatible // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20210920023735-84f357641f63 // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/api v0.29.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/api v0.126.0 // indirect
)

replace (
Expand All @@ -64,5 +70,5 @@ replace (
golang.org/x/tools => github.com/golang/tools v0.0.0-20190328211700-ab21143f2384
google.golang.org/appengine => github.com/golang/appengine v1.1.0
google.golang.org/genproto => github.com/google/go-genproto v0.0.0-20180817151627-c66870c02cf8
google.golang.org/grpc => github.com/grpc/grpc-go v1.51.0
google.golang.org/grpc => github.com/grpc/grpc-go v1.58.0
)
163 changes: 141 additions & 22 deletions go.sum

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions transport/httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/bytedance/sonic"
"github.com/quan-xie/tuba/util/xtime"
)

Expand All @@ -24,6 +25,7 @@ func init() {
client = NewHTTPClient(cfg)
}

// go test -v -test.run TestHttpClient_Get
func TestHttpClient_Get(t *testing.T) {
var res interface{}
client.SetRetryCount(5)
Expand All @@ -32,16 +34,20 @@ func TestHttpClient_Get(t *testing.T) {
t.Log(err)
return
}
t.Log(res)

resStr, _ := sonic.MarshalString(&res)
t.Log(resStr)
}

// go test -v -test.run TestHttpClient_Post
func TestHttpClient_Post(t *testing.T) {
var res interface{}
param := make(map[string]interface{})
param["xie"] = "quan"
err := client.Post(context.Background(), "https://http2.pro/api/v1", MIMEJSON, nil, param, &res)
if err != nil {
t.Log(err)
return
}
t.Log(res)
resStr, _ := sonic.MarshalString(&res)
t.Log(resStr)
}
19 changes: 10 additions & 9 deletions transport/httpclient/context_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
xhttp "net/http"
"strings"
"time"

"github.com/bytedance/sonic"

"go.opencensus.io/trace"
"golang.org/x/net/http2"

"github.com/pkg/errors"

"github.com/quan-xie/tuba/backoff"
"github.com/quan-xie/tuba/log"
"github.com/quan-xie/tuba/retry"
"github.com/quan-xie/tuba/util/xtime"
)
Expand Down Expand Up @@ -110,7 +112,7 @@ func (c *HttpClient) Post(ctx context.Context, url, contentType string, headers
}
headers.Set("Content-Type", contentType)
request.Header = headers
paramByte, _ := json.Marshal(param)
paramByte, _ := sonic.Marshal(param)
ats := []trace.Attribute{
trace.StringAttribute("POST URL", url),
trace.StringAttribute("POST PARAM ", string(paramByte)),
Expand All @@ -134,7 +136,7 @@ func (c *HttpClient) Put(ctx context.Context, url, contentType string, headers x
}
headers.Set("Content-Type", contentType)
request.Header = headers
paramByte, _ := json.Marshal(param)
paramByte, _ := sonic.Marshal(param)
ats := []trace.Attribute{
trace.StringAttribute("PUT URL", url),
trace.StringAttribute("PUT PARAM ", string(paramByte)),
Expand All @@ -158,7 +160,7 @@ func (c *HttpClient) PATCH(ctx context.Context, url, contentType string, headers
}
headers.Set("Content-Type", contentType)
request.Header = headers
paramByte, _ := json.Marshal(param)
paramByte, _ := sonic.Marshal(param)
ats := []trace.Attribute{
trace.StringAttribute("PATCH URL", url),
trace.StringAttribute("PATCH PARAM ", string(paramByte)),
Expand All @@ -182,7 +184,7 @@ func (c *HttpClient) Delete(ctx context.Context, url, contentType string, header
}
headers.Set("Content-Type", contentType)
request.Header = headers
paramByte, _ := json.Marshal(param)
paramByte, _ := sonic.Marshal(param)
ats := []trace.Attribute{
trace.StringAttribute("DELETE URL", url),
trace.StringAttribute("DELETE PARAM ", string(paramByte)),
Expand Down Expand Up @@ -227,12 +229,11 @@ func (c *HttpClient) request(ctx context.Context, req *xhttp.Request, res interf
if bs, err = readAll(response.Body, minRead); err != nil {
return
}
err = json.Unmarshal(bs, &res)
err = sonic.Unmarshal(bs, &res)
return
}

func reqBody(contentType string, param interface{}) (body io.Reader) {
var err error
if contentType == MIMEPOSTForm {
enc, ok := param.(string)
if ok {
Expand All @@ -241,8 +242,8 @@ func reqBody(contentType string, param interface{}) (body io.Reader) {
}
if contentType == MIMEJSON {
buff := new(bytes.Buffer)
err = json.NewEncoder(buff).Encode(param)
if err != nil {
if err := sonic.ConfigDefault.NewEncoder(buff).Encode(param); err != nil {
log.Errorf("reqBody error %v", err)
return
}
body = buff
Expand Down
52 changes: 52 additions & 0 deletions vendor/github.com/bytedance/sonic/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/bytedance/sonic/.gitmodules

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions vendor/github.com/bytedance/sonic/.licenserc.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 84ea8b3

Please sign in to comment.