Skip to content

Commit

Permalink
fix: replace encoding/json w/ sonic
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Nov 6, 2024
1 parent 2d86b7a commit c98022d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions api/grpc/tgbot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package tgbot

import (
"context"
"encoding/json"
"github.com/awakari/bot-telegram/api/http/reader"
"github.com/awakari/bot-telegram/service/messages"
"github.com/awakari/client-sdk-go/api"
"github.com/bytedance/sonic"
tgverifier "github.com/electrofocus/telegram-auth-verifier"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -54,7 +54,7 @@ func NewController(
func (c controller) Authenticate(ctx context.Context, req *AuthenticateRequest) (resp *AuthenticateResponse, err error) {
resp = &AuthenticateResponse{}
var creds tgverifier.Credentials
err = json.Unmarshal(req.Data, &creds)
err = sonic.Unmarshal(req.Data, &creds)
if err == nil {
err = creds.Verify(c.secretToken)
}
Expand Down
4 changes: 2 additions & 2 deletions api/grpc/tgbot/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestController_Validate(t *testing.T) {
},
"invalid json": {
token: "https://t.me/i/userpic/321/eZwlVwBo7HPBjQVUYv91UGeeKSFoXBbnt28fwa1Htsg.png",
err: status.Error(codes.Unauthenticated, "invalid character 'h' looking for beginning of value"),
err: status.Error(codes.Unauthenticated, "\"Syntax error at index 1: invalid char\\n\\n\\thttps://t.me/i/userpic/321/eZwlV\\n\\t.^..............................\\n\""),
},
"ok": {
token: `{
Expand All @@ -90,7 +90,7 @@ func TestController_Validate(t *testing.T) {
}`,
},
"empty": {
err: status.Error(codes.Unauthenticated, "unexpected end of JSON input"),
err: status.Error(codes.Unauthenticated, "\"Syntax error no sources available, the input json is empty: errors.SyntaxError{Pos:0, Src:\\\"\\\", Code:0x1, Msg:\\\"\\\"}\""),
},
}
//
Expand Down
6 changes: 3 additions & 3 deletions api/http/reader/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package reader
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/bytedance/sonic"
"io"
"net/http"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func (svc service) GetCallback(ctx context.Context, subId, url string) (cb Callb
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
err = json.NewDecoder(resp.Body).Decode(&cb)
err = sonic.ConfigDefault.NewDecoder(resp.Body).Decode(&cb)
if err != nil {
err = fmt.Errorf("%w: %s", ErrInternal, err)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (svc service) ListByUrl(ctx context.Context, limit uint32, url, cursor stri
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
err = json.NewDecoder(resp.Body).Decode(&ip)
err = sonic.ConfigDefault.NewDecoder(resp.Body).Decode(&ip)
if err != nil {
err = fmt.Errorf("%w: %s", ErrInternal, err)
}
Expand Down
4 changes: 2 additions & 2 deletions service/chats/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package chats

import (
"context"
"encoding/json"
"errors"
"fmt"
apiHttpReader "github.com/awakari/bot-telegram/api/http/reader"
"github.com/awakari/bot-telegram/service"
"github.com/awakari/bot-telegram/service/messages"
"github.com/awakari/client-sdk-go/api"
"github.com/bytedance/sonic"
"github.com/bytedance/sonic/utf8"
"github.com/cenkalti/backoff/v4"
ceProto "github.com/cloudevents/sdk-go/binding/format/protobuf/v2"
Expand Down Expand Up @@ -122,7 +122,7 @@ func (h handler) DeliverMessages(ctx *gin.Context) {

defer ctx.Request.Body.Close()
var evts []*ce.Event
err = json.NewDecoder(ctx.Request.Body).Decode(&evts)
err = sonic.ConfigDefault.NewDecoder(ctx.Request.Body).Decode(&evts)
if err != nil {
ctx.String(http.StatusBadRequest, fmt.Sprintf("failed to deserialize the request payload: %s", err))
return
Expand Down
4 changes: 2 additions & 2 deletions service/logging.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package service

import (
"encoding/json"
"github.com/bytedance/sonic"
"gopkg.in/telebot.v3"
"log/slog"
)

func LoggingHandlerFunc(next telebot.HandlerFunc, log *slog.Logger) telebot.HandlerFunc {
return func(ctx telebot.Context) error {
data, _ := json.Marshal(ctx.Update())
data, _ := sonic.Marshal(ctx.Update())
log.Debug(string(data))
return next(ctx)
}
Expand Down
4 changes: 2 additions & 2 deletions service/subscriptions/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package subscriptions

import (
"context"
"encoding/json"
"fmt"
"github.com/awakari/bot-telegram/service"
"github.com/awakari/client-sdk-go/api"
"github.com/awakari/client-sdk-go/api/grpc/subscriptions"
"github.com/awakari/client-sdk-go/model/subscription"
"github.com/awakari/client-sdk-go/model/subscription/condition"
"github.com/bytedance/sonic"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/telebot.v3"
Expand All @@ -30,7 +30,7 @@ var jsonToProtoOpts = protojson.UnmarshalOptions{
func (ch ConditionHandler) Update(tgCtx telebot.Context, args ...string) (err error) {
payload := []byte(args[0])
var sp subPayload
err = json.Unmarshal(payload, &sp)
err = sonic.Unmarshal(payload, &sp)
condProto := &subscriptions.Condition{}
if err == nil {
err = convertConditionJsonToProto(payload, condProto)
Expand Down

0 comments on commit c98022d

Please sign in to comment.