Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an abstract json layout to make it's easier to change json library #16528

Merged
merged 6 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build/generate-emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"strings"
"unicode/utf8"

jsoniter "github.com/json-iterator/go"
"code.gitea.io/gitea/modules/json"
)

const (
Expand Down Expand Up @@ -51,7 +51,6 @@ func (e Emoji) MarshalJSON() ([]byte, error) {
x.UnicodeVersion = ""
x.Description = ""
x.SkinTones = false
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(x)
}

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

// unmarshal
var data Gemoji
json := jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"

"gitea.com/go-chi/session"
jsoniter "github.com/json-iterator/go"
archiver "github.com/mholt/archiver/v3"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -306,7 +306,6 @@ func runDump(ctx *cli.Context) error {
var excludes []string
if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" {
var opts session.Options
json := jsoniter.ConfigCompatibleWithStandardLibrary
if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/pprof"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/lfs"

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

json := jsoniter.ConfigCompatibleWithStandardLibrary
enc := json.NewEncoder(os.Stdout)
err = enc.Encode(tokenAuthentication)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions integrations/api_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

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

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

Expand Down
4 changes: 1 addition & 3 deletions integrations/api_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/queue"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/forms"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

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

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

json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body)
pr := api.PullRequest{}
err := decoder.Decode(&pr)
Expand Down
4 changes: 2 additions & 2 deletions integrations/api_pull_review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"

"github.com/stretchr/testify/assert"
)

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

Expand Down
3 changes: 1 addition & 2 deletions integrations/api_repo_lfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/setting"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

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

json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(b.Bytes(), &br))
return &br
}
Expand Down
3 changes: 1 addition & 2 deletions integrations/create_no_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"path/filepath"
"testing"

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"

"gitea.com/go-chi/session"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

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

var config session.Options

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

Expand Down
4 changes: 1 addition & 3 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
Expand All @@ -37,7 +38,6 @@ import (
"code.gitea.io/gitea/routers"

"github.com/PuerkitoBio/goquery"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

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

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

json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body)
assert.NoError(t, decoder.Decode(v))
}
Expand Down
4 changes: 2 additions & 2 deletions integrations/lfs_getobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers/web"

jsoniter "github.com/json-iterator/go"
gzipp "github.com/klauspost/compress/gzip"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestGetLFSRange(t *testing.T) {
assert.Equal(t, tt.out, resp.Body.String())
} else {
var er lfs.ErrorResponse
err := jsoniter.Unmarshal(resp.Body.Bytes(), &er)
err := json.Unmarshal(resp.Body.Bytes(), &er)
assert.NoError(t, err)
assert.Equal(t, tt.out, er.Message)
}
Expand Down
6 changes: 1 addition & 5 deletions integrations/oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"io/ioutil"
"testing"

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -71,7 +71,6 @@ func TestAccessTokenExchange(t *testing.T) {
}
parsed := new(response)

json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
assert.True(t, len(parsed.AccessToken) > 10)
assert.True(t, len(parsed.RefreshToken) > 10)
Expand All @@ -96,7 +95,6 @@ func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
}
parsed := new(response)

json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
assert.True(t, len(parsed.AccessToken) > 10)
assert.True(t, len(parsed.RefreshToken) > 10)
Expand Down Expand Up @@ -186,7 +184,6 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
}
parsed := new(response)

json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
assert.True(t, len(parsed.AccessToken) > 10)
assert.True(t, len(parsed.RefreshToken) > 10)
Expand Down Expand Up @@ -230,7 +227,6 @@ func TestRefreshTokenInvalidation(t *testing.T) {
}
parsed := new(response)

json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))

// test without invalidation
Expand Down
3 changes: 1 addition & 2 deletions integrations/repo_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"path"
"testing"

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -86,7 +86,6 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
}

func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
var statuses []*api.CommitStatus
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses))
var status api.CombinedStatus
Expand Down
3 changes: 1 addition & 2 deletions integrations/testlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"testing"
"time"

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
jsoniter "github.com/json-iterator/go"
)

var (
Expand Down Expand Up @@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider {
// Init inits connection writer with json config.
// json config only need key "level".
func (log *TestLogger) Init(config string) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(config), log)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions models/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package models
import (
"encoding/binary"

jsoniter "github.com/json-iterator/go"
"code.gitea.io/gitea/modules/json"
)

func keysInt64(m map[int64]struct{}) []int64 {
Expand Down Expand Up @@ -37,7 +37,6 @@ func valuesUser(m map[int64]*User) []*User {
// JSONUnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
func JSONUnmarshalHandleDoubleEncode(bs []byte, v interface{}) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(bs, v)
if err != nil {
ok := true
Expand Down
4 changes: 1 addition & 3 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
"unicode/utf8"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"

"xorm.io/builder"
"xorm.io/xorm"
Expand Down Expand Up @@ -654,7 +654,6 @@ func (c *Comment) LoadPushCommits() (err error) {

var data PushActionContent

json := jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal([]byte(c.Content), &data)
if err != nil {
return
Expand Down Expand Up @@ -1249,7 +1248,6 @@ func CreatePushPullComment(pusher *User, pr *PullRequest, oldCommitID, newCommit

ops.Issue = pr.Issue

json := jsoniter.ConfigCompatibleWithStandardLibrary
dataJSON, err := json.Marshal(data)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions models/migrations/testlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"testing"
"time"

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
jsoniter "github.com/json-iterator/go"
)

var (
Expand Down Expand Up @@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider {
// Init inits connection writer with json config.
// json config only need key "level".
func (log *TestLogger) Init(config string) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(config), log)
if err != nil {
return err
Expand Down
Loading