Skip to content

Commit 2d24ec0

Browse files
committed
Throw out easyjson
1 parent 224d24c commit 2d24ec0

File tree

12 files changed

+36
-2861
lines changed

12 files changed

+36
-2861
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/go-chi/cors v1.2.1
1010
github.com/gofrs/uuid/v5 v5.3.1
1111
github.com/lxzan/gws v1.8.8
12-
github.com/mailru/easyjson v0.9.0
1312
github.com/prometheus/client_golang v1.21.1
1413
github.com/stretchr/testify v1.10.0
1514
github.com/subosito/gotenv v1.6.0
@@ -21,7 +20,6 @@ require (
2120
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2221
github.com/davecgh/go-spew v1.1.1 // indirect
2322
github.com/dolthub/maphash v0.1.0 // indirect
24-
github.com/josharian/intern v1.0.0 // indirect
2523
github.com/klauspost/compress v1.17.11 // indirect
2624
github.com/kr/text v0.2.0 // indirect
2725
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ github.com/gofrs/uuid/v5 v5.3.1 h1:aPx49MwJbekCzOyhZDjJVb0hx3A0KLjlbLx6p2gY0p0=
1919
github.com/gofrs/uuid/v5 v5.3.1/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
2020
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2121
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
22-
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
23-
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
2422
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
2523
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
2624
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -31,8 +29,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
3129
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
3230
github.com/lxzan/gws v1.8.8 h1:st193ZG8qN8sSw8/g/UituFhs7etmKzS7jUqhijg5wM=
3331
github.com/lxzan/gws v1.8.8/go.mod h1:FcGeRMB7HwGuTvMLR24ku0Zx0p6RXqeKASeMc4VYgi4=
34-
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
35-
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
3632
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
3733
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
3834
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

internal/api/v1.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
//go:generate easyjson -all ${GOFILE}
2-
31
// This file contains the API methods for the public API
42

53
package api
64

75
import (
6+
json "encoding/json"
87
"errors"
98
"fmt"
109
"log"
1110
"net/http"
11+
"strconv"
1212
"strings"
1313

1414
"github.com/gofrs/uuid/v5"
15-
"github.com/mailru/easyjson"
1615
"github.com/scribble-rs/scribble.rs/internal/config"
1716
"github.com/scribble-rs/scribble.rs/internal/game"
1817
"github.com/scribble-rs/scribble.rs/internal/state"
@@ -22,7 +21,6 @@ import (
2221

2322
var ErrLobbyNotExistent = errors.New("the requested lobby doesn't exist")
2423

25-
//easyjson:skip
2624
type V1Handler struct {
2725
cfg *config.Config
2826
}
@@ -33,7 +31,18 @@ func NewHandler(cfg *config.Config) *V1Handler {
3331
}
3432
}
3533

36-
//easyjson:json
34+
func marshalToHTTPWriter(data any, writer http.ResponseWriter) (bool, error) {
35+
bytes, err := json.Marshal(data)
36+
if err != nil {
37+
return false, err
38+
}
39+
40+
writer.Header().Set("Content-Type", "application/json")
41+
writer.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
42+
_, err = writer.Write(bytes)
43+
return true, err
44+
}
45+
3746
type LobbyEntries []*LobbyEntry
3847

3948
// LobbyEntry is an API object for representing a join-able public lobby.
@@ -75,7 +84,7 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
7584
})
7685
}
7786

78-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyEntries, writer); err != nil {
87+
if started, err := marshalToHTTPWriter(lobbyEntries, writer); err != nil {
7988
if !started {
8089
http.Error(writer, err.Error(), http.StatusInternalServerError)
8190
}
@@ -180,7 +189,7 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
180189

181190
lobbyData := CreateLobbyData(handler.cfg, lobby)
182191

183-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyData, writer); err != nil {
192+
if started, err := marshalToHTTPWriter(lobbyData, writer); err != nil {
184193
if !started {
185194
http.Error(writer, err.Error(), http.StatusInternalServerError)
186195
}
@@ -229,7 +238,7 @@ func (handler *V1Handler) postPlayer(writer http.ResponseWriter, request *http.R
229238
})
230239

231240
if lobbyData != nil {
232-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyData, writer); err != nil {
241+
if started, err := marshalToHTTPWriter(lobbyData, writer); err != nil {
233242
if !started {
234243
http.Error(writer, err.Error(), http.StatusInternalServerError)
235244
}
@@ -415,7 +424,7 @@ func (handler *V1Handler) patchLobby(writer http.ResponseWriter, request *http.R
415424
}
416425

417426
func (handler *V1Handler) getStats(writer http.ResponseWriter, _ *http.Request) {
418-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(state.Stats(), writer); err != nil {
427+
if started, err := marshalToHTTPWriter(state.Stats(), writer); err != nil {
419428
if !started {
420429
http.Error(writer, err.Error(), http.StatusInternalServerError)
421430
}

0 commit comments

Comments
 (0)