Skip to content

Commit 4c52d6d

Browse files
lunnyAbdulrhmnGhanem
authored andcommitted
Add an abstract json layout to make it's easier to change json library (go-gitea#16528)
* Add an abstract json layout to make it's easier to change json library * Fix import * Fix import sequence * Fix blank lines * Fix blank lines
1 parent fe3a714 commit 4c52d6d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+272
-264
lines changed

build/generate-emoji.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"strings"
2121
"unicode/utf8"
2222

23-
jsoniter "github.com/json-iterator/go"
23+
"code.gitea.io/gitea/modules/json"
2424
)
2525

2626
const (
@@ -51,7 +51,6 @@ func (e Emoji) MarshalJSON() ([]byte, error) {
5151
x.UnicodeVersion = ""
5252
x.Description = ""
5353
x.SkinTones = false
54-
json := jsoniter.ConfigCompatibleWithStandardLibrary
5554
return json.Marshal(x)
5655
}
5756

@@ -103,7 +102,6 @@ func generate() ([]byte, error) {
103102

104103
// unmarshal
105104
var data Gemoji
106-
json := jsoniter.ConfigCompatibleWithStandardLibrary
107105
err = json.Unmarshal(body, &data)
108106
if err != nil {
109107
return nil, err

cmd/dump.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
"time"
1616

1717
"code.gitea.io/gitea/models"
18+
"code.gitea.io/gitea/modules/json"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/setting"
2021
"code.gitea.io/gitea/modules/storage"
2122
"code.gitea.io/gitea/modules/util"
2223

2324
"gitea.com/go-chi/session"
24-
jsoniter "github.com/json-iterator/go"
2525
archiver "github.com/mholt/archiver/v3"
2626
"github.com/urfave/cli"
2727
)
@@ -306,7 +306,6 @@ func runDump(ctx *cli.Context) error {
306306
var excludes []string
307307
if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" {
308308
var opts session.Options
309-
json := jsoniter.ConfigCompatibleWithStandardLibrary
310309
if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil {
311310
return err
312311
}

cmd/serv.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
"time"
1818

1919
"code.gitea.io/gitea/models"
20+
"code.gitea.io/gitea/modules/json"
2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/pprof"
2223
"code.gitea.io/gitea/modules/private"
2324
"code.gitea.io/gitea/modules/setting"
2425
"code.gitea.io/gitea/services/lfs"
2526

2627
"github.com/golang-jwt/jwt"
27-
jsoniter "github.com/json-iterator/go"
2828
"github.com/kballard/go-shellquote"
2929
"github.com/urfave/cli"
3030
)
@@ -265,7 +265,6 @@ func runServ(c *cli.Context) error {
265265
}
266266
tokenAuthentication.Header["Authorization"] = fmt.Sprintf("Bearer %s", tokenString)
267267

268-
json := jsoniter.ConfigCompatibleWithStandardLibrary
269268
enc := json.NewEncoder(os.Stdout)
270269
err = enc.Encode(tokenAuthentication)
271270
if err != nil {

integrations/api_admin_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"testing"
1111

1212
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/json"
1314
api "code.gitea.io/gitea/modules/structs"
1415

15-
jsoniter "github.com/json-iterator/go"
1616
"github.com/stretchr/testify/assert"
1717
)
1818

@@ -190,7 +190,6 @@ func TestAPIEditUser(t *testing.T) {
190190
resp := session.MakeRequest(t, req, http.StatusUnprocessableEntity)
191191

192192
errMap := make(map[string]interface{})
193-
json := jsoniter.ConfigCompatibleWithStandardLibrary
194193
json.Unmarshal(resp.Body.Bytes(), &errMap)
195194
assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string))
196195

integrations/api_helper_for_declarative_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
"time"
1515

1616
"code.gitea.io/gitea/models"
17+
"code.gitea.io/gitea/modules/json"
1718
"code.gitea.io/gitea/modules/queue"
1819
api "code.gitea.io/gitea/modules/structs"
1920
"code.gitea.io/gitea/services/forms"
2021

21-
jsoniter "github.com/json-iterator/go"
2222
"github.com/stretchr/testify/assert"
2323
)
2424

@@ -231,7 +231,6 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
231231
}
232232
resp := ctx.Session.MakeRequest(t, req, expected)
233233

234-
json := jsoniter.ConfigCompatibleWithStandardLibrary
235234
decoder := json.NewDecoder(resp.Body)
236235
pr := api.PullRequest{}
237236
err := decoder.Decode(&pr)
@@ -251,7 +250,6 @@ func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) fu
251250
}
252251
resp := ctx.Session.MakeRequest(t, req, expected)
253252

254-
json := jsoniter.ConfigCompatibleWithStandardLibrary
255253
decoder := json.NewDecoder(resp.Body)
256254
pr := api.PullRequest{}
257255
err := decoder.Decode(&pr)

integrations/api_pull_review_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"testing"
1111

1212
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/json"
1314
api "code.gitea.io/gitea/modules/structs"
14-
jsoniter "github.com/json-iterator/go"
15+
1516
"github.com/stretchr/testify/assert"
1617
)
1718

@@ -189,7 +190,6 @@ func TestAPIPullReview(t *testing.T) {
189190
})
190191
resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
191192
errMap := make(map[string]interface{})
192-
json := jsoniter.ConfigCompatibleWithStandardLibrary
193193
json.Unmarshal(resp.Body.Bytes(), &errMap)
194194
assert.EqualValues(t, "review event COMMENT requires a body or a comment", errMap["message"].(string))
195195

integrations/api_repo_lfs_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"testing"
1414

1515
"code.gitea.io/gitea/models"
16+
"code.gitea.io/gitea/modules/json"
1617
"code.gitea.io/gitea/modules/lfs"
1718
"code.gitea.io/gitea/modules/setting"
1819

19-
jsoniter "github.com/json-iterator/go"
2020
"github.com/stretchr/testify/assert"
2121
)
2222

@@ -86,7 +86,6 @@ func TestAPILFSBatch(t *testing.T) {
8686
decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse {
8787
var br lfs.BatchResponse
8888

89-
json := jsoniter.ConfigCompatibleWithStandardLibrary
9089
assert.NoError(t, json.Unmarshal(b.Bytes(), &br))
9190
return &br
9291
}

integrations/create_no_session_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
"path/filepath"
1313
"testing"
1414

15+
"code.gitea.io/gitea/modules/json"
1516
"code.gitea.io/gitea/modules/setting"
1617
"code.gitea.io/gitea/modules/util"
1718
"code.gitea.io/gitea/routers"
1819

1920
"gitea.com/go-chi/session"
20-
jsoniter "github.com/json-iterator/go"
2121
"github.com/stretchr/testify/assert"
2222
)
2323

@@ -63,7 +63,6 @@ func TestSessionFileCreation(t *testing.T) {
6363

6464
var config session.Options
6565

66-
json := jsoniter.ConfigCompatibleWithStandardLibrary
6766
err := json.Unmarshal([]byte(oldSessionConfig), &config)
6867
assert.NoError(t, err)
6968

integrations/integration_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"code.gitea.io/gitea/modules/base"
2929
"code.gitea.io/gitea/modules/git"
3030
"code.gitea.io/gitea/modules/graceful"
31+
"code.gitea.io/gitea/modules/json"
3132
"code.gitea.io/gitea/modules/log"
3233
"code.gitea.io/gitea/modules/queue"
3334
"code.gitea.io/gitea/modules/setting"
@@ -37,7 +38,6 @@ import (
3738
"code.gitea.io/gitea/routers"
3839

3940
"github.com/PuerkitoBio/goquery"
40-
jsoniter "github.com/json-iterator/go"
4141
"github.com/stretchr/testify/assert"
4242
)
4343

@@ -416,7 +416,6 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string
416416
func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
417417
t.Helper()
418418

419-
json := jsoniter.ConfigCompatibleWithStandardLibrary
420419
jsonBytes, err := json.Marshal(v)
421420
assert.NoError(t, err)
422421
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
@@ -508,7 +507,6 @@ func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
508507
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
509508
t.Helper()
510509

511-
json := jsoniter.ConfigCompatibleWithStandardLibrary
512510
decoder := json.NewDecoder(resp.Body)
513511
assert.NoError(t, decoder.Decode(v))
514512
}

integrations/lfs_getobject_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414

1515
"code.gitea.io/gitea/models"
1616
"code.gitea.io/gitea/modules/git"
17+
"code.gitea.io/gitea/modules/json"
1718
"code.gitea.io/gitea/modules/lfs"
1819
"code.gitea.io/gitea/modules/setting"
1920
"code.gitea.io/gitea/routers/web"
2021

21-
jsoniter "github.com/json-iterator/go"
2222
gzipp "github.com/klauspost/compress/gzip"
2323
"github.com/stretchr/testify/assert"
2424
)
@@ -206,7 +206,7 @@ func TestGetLFSRange(t *testing.T) {
206206
assert.Equal(t, tt.out, resp.Body.String())
207207
} else {
208208
var er lfs.ErrorResponse
209-
err := jsoniter.Unmarshal(resp.Body.Bytes(), &er)
209+
err := json.Unmarshal(resp.Body.Bytes(), &er)
210210
assert.NoError(t, err)
211211
assert.Equal(t, tt.out, er.Message)
212212
}

0 commit comments

Comments
 (0)