Skip to content

Commit 3044487

Browse files
authored
chore: use easyjson for most used json Unmarshaling/Marshaling (#18)
1 parent 26260da commit 3044487

File tree

16 files changed

+3441
-36
lines changed

16 files changed

+3441
-36
lines changed

emote/emote_easyjson.go

Lines changed: 188 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

emote/replacer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package emote
33
import (
44
"context"
55
"encoding/base64"
6-
"encoding/json"
76
"errors"
87
"fmt"
98
"image"
@@ -24,6 +23,7 @@ import (
2423

2524
"github.com/charmbracelet/lipgloss"
2625
"github.com/gen2brain/avif"
26+
"github.com/mailru/easyjson"
2727
"github.com/rs/zerolog/log"
2828
_ "golang.org/x/image/webp"
2929
)
@@ -36,12 +36,14 @@ var (
3636

3737
var errUnsupportedAnimatedFormat = errors.New("emote is animated but in non supported format")
3838

39+
//easyjson:json
3940
type DecodedEmote struct {
4041
ID int `json:"-"`
4142
Cols int `json:"cols"`
4243
Images []DecodedImage `json:"images"`
4344
}
4445

46+
//easyjson:json
4547
type DecodedImage struct {
4648
Width int `json:"width"`
4749
Height int `json:"height"`
@@ -388,7 +390,7 @@ func SaveCache(e Emote, dec DecodedEmote) error {
388390

389391
defer f.Close()
390392

391-
encoded, err := json.Marshal(dec)
393+
encoded, err := easyjson.Marshal(dec)
392394
if err != nil {
393395
return err
394396
}
@@ -420,7 +422,7 @@ func fsOpenCached(e Emote) (DecodedEmote, bool, error) {
420422
}
421423

422424
var decoded DecodedEmote
423-
if err := json.Unmarshal(data, &decoded); err != nil {
425+
if err := easyjson.Unmarshal(data, &decoded); err != nil {
424426
return decoded, false, err
425427
}
426428

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/gen2brain/avif v0.3.2
2727
github.com/jellydator/ttlcache/v3 v3.2.0
2828
github.com/lithammer/fuzzysearch v1.1.8
29+
github.com/mailru/easyjson v0.7.7
2930
github.com/vbauerster/mpb/v8 v8.8.2
3031
github.com/zalando/go-keyring v0.2.5
3132
golang.org/x/image v0.19.0
@@ -48,6 +49,7 @@ require (
4849
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
4950
github.com/godbus/dbus/v5 v5.1.0 // indirect
5051
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
52+
github.com/josharian/intern v1.0.0 // indirect
5153
github.com/mattn/go-localereader v0.0.1 // indirect
5254
github.com/ncruces/go-strftime v0.1.9 // indirect
5355
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
6565
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
6666
github.com/jellydator/ttlcache/v3 v3.2.0 h1:6lqVJ8X3ZaUwvzENqPAobDsXNExfUJd61u++uW8a3LE=
6767
github.com/jellydator/ttlcache/v3 v3.2.0/go.mod h1:hi7MGFdMAwZna5n2tuvh63DvFLzVKySzCVW6+0gA2n4=
68+
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
69+
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
6870
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
6971
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
7072
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
7173
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
7274
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
7375
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
7476
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
77+
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
78+
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
7579
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
7680
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
7781
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import (
3131
ttvCommand "github.com/julez-dev/chatuino/twitch/command"
3232
"github.com/julez-dev/chatuino/twitch/seventv"
3333
"github.com/julez-dev/chatuino/ui/mainui"
34+
_ "github.com/mailru/easyjson"
3435
"github.com/rs/zerolog"
3536
"github.com/urfave/cli/v3"
36-
3737
_ "modernc.org/sqlite"
3838
)
3939

@@ -50,6 +50,10 @@ const (
5050
var maybeLogFile *os.File
5151

5252
//go:generate go run github.com/vektra/mockery/v2@latest --dir=./ui/mainui --dir=./emote --dir=./save/messagelog --with-expecter=true --all
53+
//go:generate go run github.com/mailru/easyjson/easyjson@latest -snake_case -no_std_marshalers -pkg ./twitch/command
54+
//go:generate go run github.com/mailru/easyjson/easyjson@latest -snake_case -no_std_marshalers -pkg ./emote
55+
//go:generate go run github.com/mailru/easyjson/easyjson@latest -snake_case -pkg ./twitch
56+
//go:generate go run github.com/mailru/easyjson/easyjson@latest -snake_case -pkg ./twitch/recentmessage
5357
func main() {
5458
defer func() {
5559
if maybeLogFile != nil {

mocks/DB.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

save/messagelog/logger.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package messagelog
22

33
import (
44
"database/sql"
5-
"encoding/json"
65
"errors"
76
"fmt"
87
"slices"
98
"strings"
109
"time"
1110

1211
"github.com/julez-dev/chatuino/twitch/command"
12+
"github.com/mailru/easyjson"
1313
"github.com/rs/zerolog"
1414
)
1515

@@ -205,7 +205,8 @@ func (b *BatchedMessageLogger) scanRows(rows *sql.Rows) ([]LogEntry, error) {
205205
return logEntries, err
206206
}
207207

208-
if err := json.Unmarshal(rawPayload, &entry.PrivateMessage); err != nil {
208+
entry.PrivateMessage = &command.PrivateMessage{}
209+
if err := easyjson.Unmarshal(rawPayload, entry.PrivateMessage); err != nil {
209210
return logEntries, err
210211
}
211212

@@ -229,7 +230,7 @@ func (b *BatchedMessageLogger) createLogEntries(twitchMsgs []*command.PrivateMes
229230
valueStrings := make([]string, 0, len(twitchMsgs))
230231
valueArgs := make([]any, 0, len(twitchMsgs)*7) // 7 args per row
231232
for _, msg := range twitchMsgs {
232-
payloadJSON, err := json.Marshal(msg)
233+
payloadJSON, err := easyjson.Marshal(msg)
233234
if err != nil {
234235
return fmt.Errorf("failed to marshal JSON payload for message %s: %w", msg.ID, err)
235236
}

twitch/api.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,11 @@ func (a *API) BanUser(ctx context.Context, broadcasterID string, moderatorID str
114114
url := fmt.Sprintf("/moderation/bans?%s", values.Encode())
115115

116116
body, err := json.Marshal(BanUserRequest{Data: data})
117-
118117
if err != nil {
119118
return err
120119
}
121120

122121
_, err = doAuthenticatedUserRequest[any](ctx, a, http.MethodPost, url, body)
123-
124122
if err != nil {
125123
return err
126124
}
@@ -141,7 +139,6 @@ func (a *API) UnbanUser(ctx context.Context, broadcasterID string, moderatorID s
141139
url := fmt.Sprintf("/moderation/bans?%s", values.Encode())
142140

143141
_, err := doAuthenticatedUserRequest[any](ctx, a, http.MethodDelete, url, nil)
144-
145142
if err != nil {
146143
return err
147144
}
@@ -273,13 +270,11 @@ func (a *API) ResolveBanRequest(ctx context.Context, broadcasterID, moderatorID,
273270
url := fmt.Sprintf("/moderation/unban_requests?%s", values.Encode())
274271

275272
resp, err := doAuthenticatedUserRequest[GetUnbanRequestsResponse](ctx, a, http.MethodPatch, url, nil)
276-
277273
if err != nil {
278274
return UnbanRequest{}, err
279275
}
280276

281277
return resp.Data[0], nil
282-
283278
}
284279

285280
func (a *API) GetUsers(ctx context.Context, logins []string, ids []string) (UserResponse, error) {
@@ -345,13 +340,11 @@ func (a *API) CreateEventSubSubscription(ctx context.Context, reqData CreateEven
345340
}
346341

347342
reqBytes, err := json.Marshal(reqData)
348-
349343
if err != nil {
350344
return CreateEventSubSubscriptionResponse{}, err
351345
}
352346

353347
resp, err := doAuthenticatedUserRequest[CreateEventSubSubscriptionResponse](ctx, a, http.MethodPost, "/eventsub/subscriptions", reqBytes)
354-
355348
if err != nil {
356349
return CreateEventSubSubscriptionResponse{}, err
357350
}
@@ -593,7 +586,6 @@ func doAuthenticatedUserRequest[T any](ctx context.Context, api *API, method, ur
593586

594587
return accessToken, nil
595588
})
596-
597589
if err != nil {
598590
log.Logger.Err(err).Str("user-id", user.ID).Bool("shared", shared).Msg("could not refresh token")
599591

0 commit comments

Comments
 (0)