From 610299e0eec97e779c0320a43ceacf6610e45810 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Jul 2021 10:28:30 +0800 Subject: [PATCH 1/6] Add an abstract json layout to make it's easier to change json library --- build/generate-emoji.go | 4 +- cmd/dump.go | 4 +- cmd/serv.go | 3 +- integrations/api_admin_test.go | 4 +- .../api_helper_for_declarative_test.go | 5 +- integrations/api_pull_review_test.go | 4 +- integrations/api_repo_lfs_test.go | 4 +- integrations/create_no_session_test.go | 3 +- integrations/integration_test.go | 5 +- integrations/oauth_test.go | 7 +- integrations/repo_commits_test.go | 4 +- integrations/testlogger.go | 5 +- models/issue_comment.go | 5 +- models/migrations/testlogger_test.go | 5 +- models/migrations/v130.go | 5 +- models/migrations/v180.go | 5 +- models/repo_unit.go | 12 +- models/task.go | 8 +- models/user_heatmap_test.go | 4 +- models/webhook.go | 10 +- models/webhook_test.go | 5 +- modules/cache/cache_twoqueue.go | 4 +- modules/context/context.go | 5 +- modules/eventsource/event.go | 6 +- modules/httplib/httplib.go | 6 +- modules/indexer/code/elastic_search.go | 5 +- modules/json/json.go | 123 ++++++++++++++++++ modules/lfs/http_client.go | 1 - modules/lfs/transferadapter.go | 1 - modules/log/conn.go | 5 +- modules/log/console.go | 5 +- modules/log/file.go | 5 +- modules/log/level.go | 6 +- modules/log/level_test.go | 4 +- modules/log/smtp.go | 5 +- modules/notification/action/action.go | 6 +- modules/private/hook.go | 6 +- modules/private/internal.go | 5 +- modules/private/mail.go | 5 +- modules/private/manager.go | 7 +- modules/private/restore_repo.go | 5 +- modules/private/serv.go | 7 +- modules/queue/helper.go | 6 +- modules/queue/manager.go | 4 +- modules/queue/queue_bytefifo.go | 7 +- modules/queue/queue_test.go | 3 +- modules/queue/setting.go | 3 +- modules/recaptcha/recaptcha.go | 5 +- modules/session/virtual.go | 4 +- modules/setting/log.go | 3 +- modules/setting/session.go | 3 +- modules/setting/setting.go | 4 +- modules/setting/setting_test.go | 4 +- modules/storage/helper.go | 4 +- modules/structs/hook.go | 25 ++-- modules/structs/user.go | 5 +- modules/task/migrate.go | 5 +- modules/task/task.go | 3 +- modules/templates/helper.go | 4 +- routers/api/v1/utils/hook.go | 5 +- routers/private/mail.go | 4 +- routers/private/manager.go | 4 +- routers/private/restore_repo.go | 4 +- routers/web/admin/admin.go | 4 +- routers/web/events/events.go | 4 +- routers/web/repo/editor.go | 4 +- routers/web/repo/webhook.go | 12 +- routers/web/user/home.go | 5 +- routers/web/user/task.go | 4 +- services/gitdiff/gitdiff_test.go | 4 +- services/lfs/locks.go | 6 +- services/pull/pull.go | 4 +- services/webhook/dingtalk.go | 4 +- services/webhook/discord.go | 8 +- services/webhook/feishu.go | 4 +- services/webhook/matrix.go | 11 +- services/webhook/msteams.go | 4 +- services/webhook/slack.go | 8 +- services/webhook/telegram.go | 6 +- 79 files changed, 320 insertions(+), 214 deletions(-) create mode 100644 modules/json/json.go diff --git a/build/generate-emoji.go b/build/generate-emoji.go index 53c5285b23eca..c97d865bdc294 100644 --- a/build/generate-emoji.go +++ b/build/generate-emoji.go @@ -20,7 +20,7 @@ import ( "strings" "unicode/utf8" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) const ( @@ -51,7 +51,6 @@ func (e Emoji) MarshalJSON() ([]byte, error) { x.UnicodeVersion = "" x.Description = "" x.SkinTones = false - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(x) } @@ -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 diff --git a/cmd/dump.go b/cmd/dump.go index 629993b6c80b1..326062c49533c 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -6,6 +6,7 @@ package cmd import ( + "encoding/json" "fmt" "io/ioutil" "os" @@ -21,7 +22,6 @@ import ( "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" ) @@ -306,7 +306,7 @@ 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 } diff --git a/cmd/serv.go b/cmd/serv.go index 3b180fc626806..6a924fa5c0892 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -6,6 +6,7 @@ package cmd import ( + "encoding/json" "fmt" "net/http" "net/url" @@ -24,7 +25,6 @@ import ( "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" ) @@ -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 { diff --git a/integrations/api_admin_test.go b/integrations/api_admin_test.go index b93179d21df0b..988d6b07b18d3 100644 --- a/integrations/api_admin_test.go +++ b/integrations/api_admin_test.go @@ -5,6 +5,7 @@ package integrations import ( + "encoding/json" "fmt" "net/http" "testing" @@ -12,7 +13,6 @@ import ( "code.gitea.io/gitea/models" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -190,7 +190,7 @@ 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)) diff --git a/integrations/api_helper_for_declarative_test.go b/integrations/api_helper_for_declarative_test.go index d56ae5d27c145..5a541bdec01e4 100644 --- a/integrations/api_helper_for_declarative_test.go +++ b/integrations/api_helper_for_declarative_test.go @@ -6,6 +6,8 @@ package integrations import ( "context" + "encoding/json" + "fmt" "io/ioutil" "net/http" @@ -18,7 +20,6 @@ import ( api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/services/forms" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -231,7 +232,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) @@ -251,7 +251,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) diff --git a/integrations/api_pull_review_test.go b/integrations/api_pull_review_test.go index bcc0cbffcb381..cc8590d5ea6d8 100644 --- a/integrations/api_pull_review_test.go +++ b/integrations/api_pull_review_test.go @@ -5,13 +5,13 @@ package integrations import ( + "encoding/json" "fmt" "net/http" "testing" "code.gitea.io/gitea/models" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -189,7 +189,7 @@ 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)) diff --git a/integrations/api_repo_lfs_test.go b/integrations/api_repo_lfs_test.go index 9e1e2b0418944..4b73ae7aa9e77 100644 --- a/integrations/api_repo_lfs_test.go +++ b/integrations/api_repo_lfs_test.go @@ -6,6 +6,8 @@ package integrations import ( "bytes" + "encoding/json" + "net/http" "path" "strconv" @@ -16,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -86,7 +87,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 } diff --git a/integrations/create_no_session_test.go b/integrations/create_no_session_test.go index 46f111b6f7d84..4253713553c11 100644 --- a/integrations/create_no_session_test.go +++ b/integrations/create_no_session_test.go @@ -5,6 +5,7 @@ package integrations import ( + "encoding/json" "io/ioutil" "net/http" "net/http/httptest" @@ -17,7 +18,6 @@ import ( "code.gitea.io/gitea/routers" "gitea.com/go-chi/session" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -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) diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 8a008ac621760..1d160caf58ccc 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -8,6 +8,8 @@ import ( "bytes" "context" "database/sql" + "encoding/json" + "fmt" "hash" "hash/fnv" @@ -37,7 +39,6 @@ import ( "code.gitea.io/gitea/routers" "github.com/PuerkitoBio/goquery" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -416,7 +417,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)) @@ -508,7 +508,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)) } diff --git a/integrations/oauth_test.go b/integrations/oauth_test.go index 2da182d0c80f1..20074f1a6a34a 100644 --- a/integrations/oauth_test.go +++ b/integrations/oauth_test.go @@ -6,12 +6,13 @@ package integrations import ( "bytes" + "encoding/json" + "io/ioutil" "testing" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -71,7 +72,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) @@ -96,7 +96,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) @@ -186,7 +185,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) @@ -230,7 +228,6 @@ func TestRefreshTokenInvalidation(t *testing.T) { } parsed := new(response) - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed)) // test without invalidation diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index 8dcbcd368350c..cf799e871a79d 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -5,6 +5,7 @@ package integrations import ( + "encoding/json" "net/http" "net/http/httptest" "path" @@ -13,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -86,7 +86,7 @@ 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 diff --git a/integrations/testlogger.go b/integrations/testlogger.go index 9ea357ba27bfc..6ee929741eb2b 100644 --- a/integrations/testlogger.go +++ b/integrations/testlogger.go @@ -6,6 +6,8 @@ package integrations import ( "context" + "encoding/json" + "fmt" "os" "runtime" @@ -16,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" - jsoniter "github.com/json-iterator/go" ) var ( @@ -158,7 +159,7 @@ 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 diff --git a/models/issue_comment.go b/models/issue_comment.go index 4c5b77ff8e556..54be79a5559bc 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -8,6 +8,8 @@ package models import ( "container/list" + "encoding/json" + "fmt" "regexp" "strconv" @@ -21,7 +23,6 @@ import ( "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" @@ -654,7 +655,6 @@ func (c *Comment) LoadPushCommits() (err error) { var data PushActionContent - json := jsoniter.ConfigCompatibleWithStandardLibrary err = json.Unmarshal([]byte(c.Content), &data) if err != nil { return @@ -1249,7 +1249,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 diff --git a/models/migrations/testlogger_test.go b/models/migrations/testlogger_test.go index 8d6e61ae644ee..5f477f6de0fa7 100644 --- a/models/migrations/testlogger_test.go +++ b/models/migrations/testlogger_test.go @@ -6,6 +6,8 @@ package migrations import ( "context" + "encoding/json" + "fmt" "os" "runtime" @@ -16,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" - jsoniter "github.com/json-iterator/go" ) var ( @@ -158,7 +159,7 @@ 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 diff --git a/models/migrations/v130.go b/models/migrations/v130.go index 08a8d5fca1a80..f931869f06186 100644 --- a/models/migrations/v130.go +++ b/models/migrations/v130.go @@ -5,8 +5,9 @@ package migrations import ( + "encoding/json" + "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" "xorm.io/xorm" ) @@ -70,7 +71,7 @@ func expandWebhooks(x *xorm.Engine) error { for _, res := range results { var events HookEvent - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err = json.Unmarshal([]byte(res.Events), &events); err != nil { return err } diff --git a/models/migrations/v180.go b/models/migrations/v180.go index 8a7637debaf14..b71bc9fa0b571 100644 --- a/models/migrations/v180.go +++ b/models/migrations/v180.go @@ -5,9 +5,10 @@ package migrations import ( + "encoding/json" + "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" "xorm.io/builder" "xorm.io/xorm" ) @@ -105,7 +106,7 @@ func removeCredentials(payload string) (string, error) { } var opts MigrateOptions - json := jsoniter.ConfigCompatibleWithStandardLibrary + err := json.Unmarshal([]byte(payload), &opts) if err != nil { return "", err diff --git a/models/repo_unit.go b/models/repo_unit.go index c5eac2656f864..2780690905e88 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -5,11 +5,11 @@ package models import ( + "encoding/json" "fmt" "code.gitea.io/gitea/modules/timeutil" - jsoniter "github.com/json-iterator/go" "xorm.io/xorm" "xorm.io/xorm/convert" ) @@ -33,7 +33,7 @@ func (cfg *UnitConfig) FromDB(bs []byte) error { // ToDB exports a UnitConfig to a serialized format. func (cfg *UnitConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.Marshal(cfg) } @@ -49,7 +49,7 @@ func (cfg *ExternalWikiConfig) FromDB(bs []byte) error { // ToDB exports a ExternalWikiConfig to a serialized format. func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.Marshal(cfg) } @@ -67,7 +67,7 @@ func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error { // ToDB exports a ExternalTrackerConfig to a serialized format. func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.Marshal(cfg) } @@ -85,7 +85,7 @@ func (cfg *IssuesConfig) FromDB(bs []byte) error { // ToDB exports a IssuesConfig to a serialized format. func (cfg *IssuesConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.Marshal(cfg) } @@ -109,7 +109,7 @@ func (cfg *PullRequestsConfig) FromDB(bs []byte) error { // ToDB exports a PullRequestsConfig to a serialized format. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.Marshal(cfg) } diff --git a/models/task.go b/models/task.go index 5f9ccc6bfaf5f..607aed1db9f6e 100644 --- a/models/task.go +++ b/models/task.go @@ -5,6 +5,7 @@ package models import ( + "encoding/json" "fmt" migration "code.gitea.io/gitea/modules/migrations/base" @@ -13,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" "xorm.io/builder" ) @@ -114,7 +114,7 @@ func (task *Task) UpdateCols(cols ...string) error { func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) { if task.Type == structs.TaskTypeMigrateRepo { var opts migration.MigrateOptions - json := jsoniter.ConfigCompatibleWithStandardLibrary + err := json.Unmarshal([]byte(task.PayloadContent), &opts) if err != nil { return nil, err @@ -190,7 +190,7 @@ func GetMigratingTaskByID(id, doerID int64) (*Task, *migration.MigrateOptions, e } var opts migration.MigrateOptions - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(task.PayloadContent), &opts); err != nil { return nil, nil, err } @@ -244,7 +244,7 @@ func FinishMigrateTask(task *Task) error { conf.AuthPasswordEncrypted = "" conf.AuthTokenEncrypted = "" conf.CloneAddrEncrypted = "" - json := jsoniter.ConfigCompatibleWithStandardLibrary + confBytes, err := json.Marshal(conf) if err != nil { return err diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go index b2aaea6499486..bed2e9de396f1 100644 --- a/models/user_heatmap_test.go +++ b/models/user_heatmap_test.go @@ -5,10 +5,10 @@ package models import ( + "encoding/json" "fmt" "testing" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -68,7 +68,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) { assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase %d", i)) // Test JSON rendering - json := jsoniter.ConfigCompatibleWithStandardLibrary + jsonData, err := json.Marshal(heatmap) assert.NoError(t, err) assert.Equal(t, tc.JSONResult, string(jsonData)) diff --git a/models/webhook.go b/models/webhook.go index 9c8c0a568bd51..a1b9633d73697 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -7,6 +7,8 @@ package models import ( "context" + "encoding/json" + "fmt" "strings" "time" @@ -17,7 +19,6 @@ import ( "code.gitea.io/gitea/modules/timeutil" gouuid "github.com/google/uuid" - jsoniter "github.com/json-iterator/go" ) // HookContentType is the content type of a web hook @@ -161,7 +162,6 @@ type Webhook struct { func (w *Webhook) AfterLoad() { w.HookEvent = &HookEvent{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { log.Error("Unmarshal[%d]: %v", w.ID, err) } @@ -174,7 +174,7 @@ func (w *Webhook) History(page int) ([]*HookTask, error) { // UpdateEvent handles conversion from HookEvent to Events. func (w *Webhook) UpdateEvent() error { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.Marshal(w.HookEvent) w.Events = string(data) return err @@ -687,7 +687,7 @@ func (t *HookTask) AfterLoad() { } t.RequestInfo = &HookRequest{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err) } @@ -701,7 +701,7 @@ func (t *HookTask) AfterLoad() { } func (t *HookTask) simpleMarshalJSON(v interface{}) string { - json := jsoniter.ConfigCompatibleWithStandardLibrary + p, err := json.Marshal(v) if err != nil { log.Error("Marshal [%d]: %v", t.ID, err) diff --git a/models/webhook_test.go b/models/webhook_test.go index cab44a120e3e3..3273b05f580fd 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -6,12 +6,13 @@ package models import ( "context" + "encoding/json" + "testing" "time" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -58,7 +59,7 @@ func TestWebhook_UpdateEvent(t *testing.T) { assert.NoError(t, webhook.UpdateEvent()) assert.NotEmpty(t, webhook.Events) actualHookEvent := &HookEvent{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent)) assert.Equal(t, *hookEvent, *actualHookEvent) } diff --git a/modules/cache/cache_twoqueue.go b/modules/cache/cache_twoqueue.go index 7d8fa7c934163..5af9fcebc109e 100644 --- a/modules/cache/cache_twoqueue.go +++ b/modules/cache/cache_twoqueue.go @@ -5,13 +5,13 @@ package cache import ( + "encoding/json" "strconv" "sync" "time" mc "gitea.com/go-chi/cache" lru "github.com/hashicorp/golang-lru" - jsoniter "github.com/json-iterator/go" ) // TwoQueueCache represents a LRU 2Q cache adapter implementation @@ -177,7 +177,7 @@ func (c *TwoQueueCache) StartAndGC(opts mc.Options) error { size, err = strconv.Atoi(opts.AdapterConfig) } if err != nil { - json := jsoniter.ConfigCompatibleWithStandardLibrary + if !json.Valid([]byte(opts.AdapterConfig)) { return err } diff --git a/modules/context/context.go b/modules/context/context.go index 8949dd71498f7..9adf4b8695110 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -9,6 +9,8 @@ import ( "context" "crypto/sha256" "encoding/hex" + "encoding/json" + "fmt" "html" "html/template" @@ -34,7 +36,6 @@ import ( "gitea.com/go-chi/cache" "gitea.com/go-chi/session" "github.com/go-chi/chi" - jsoniter "github.com/json-iterator/go" "github.com/unknwon/com" "github.com/unknwon/i18n" "github.com/unrolled/render" @@ -408,7 +409,7 @@ func (ctx *Context) Error(status int, contents ...string) { func (ctx *Context) JSON(status int, content interface{}) { ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8") ctx.Resp.WriteHeader(status) - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.NewEncoder(ctx.Resp).Encode(content); err != nil { ctx.ServerError("Render JSON failed", err) } diff --git a/modules/eventsource/event.go b/modules/eventsource/event.go index a3407a94d2124..1a37dce00b88a 100644 --- a/modules/eventsource/event.go +++ b/modules/eventsource/event.go @@ -6,12 +6,12 @@ package eventsource import ( "bytes" + "encoding/json" + "fmt" "io" "strings" "time" - - jsoniter "github.com/json-iterator/go" ) func wrapNewlines(w io.Writer, prefix []byte, value []byte) (sum int64, err error) { @@ -80,7 +80,7 @@ func (e *Event) WriteTo(w io.Writer) (int64, error) { data = []byte(v) default: var err error - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err = json.Marshal(e.Data) if err != nil { return sum, err diff --git a/modules/httplib/httplib.go b/modules/httplib/httplib.go index 5c8eac8b4283a..03d0ef80bd208 100644 --- a/modules/httplib/httplib.go +++ b/modules/httplib/httplib.go @@ -9,6 +9,8 @@ import ( "bytes" "context" "crypto/tls" + + "encoding/json" "encoding/xml" "io" "io/ioutil" @@ -23,8 +25,6 @@ import ( "strings" "sync" "time" - - jsoniter "github.com/json-iterator/go" ) var defaultSetting = Settings{false, "GiteaServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} @@ -443,7 +443,7 @@ func (r *Request) ToJSON(v interface{}) error { if err != nil { return err } - json := jsoniter.ConfigCompatibleWithStandardLibrary + err = json.Unmarshal(data, v) return err } diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go index 569917f151f49..1bdbdc003fdd8 100644 --- a/modules/indexer/code/elastic_search.go +++ b/modules/indexer/code/elastic_search.go @@ -7,6 +7,8 @@ package code import ( "bufio" "context" + "encoding/json" + "fmt" "io" "io/ioutil" @@ -24,7 +26,6 @@ import ( "code.gitea.io/gitea/modules/typesniffer" "github.com/go-enry/go-enry/v2" - jsoniter "github.com/json-iterator/go" "github.com/olivere/elastic/v7" ) @@ -321,7 +322,7 @@ func convertResult(searchResult *elastic.SearchResult, kw string, pageSize int) repoID, fileName := parseIndexerID(hit.Id) var res = make(map[string]interface{}) - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal(hit.Source, &res); err != nil { return 0, nil, nil, err } diff --git a/modules/json/json.go b/modules/json/json.go new file mode 100644 index 0000000000000..35e53d0975ab3 --- /dev/null +++ b/modules/json/json.go @@ -0,0 +1,123 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package json + +import ( + "bytes" + "encoding/json" + "io" + + jsoniter "github.com/json-iterator/go" +) + +// Encoder represents an encoder for json +type Encoder interface { + Encode(v interface{}) error +} + +// Decoder represents a decoder for json +type Decoder interface { + Decode(v interface{}) error +} + +// Interface represents an interface to handle json data +type Interface interface { + Marshal(v interface{}) ([]byte, error) + Unmarshal(data []byte, v interface{}) error + NewEncoder(writer io.Writer) Encoder + NewDecoder(reader io.Reader) Decoder + Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error +} + +var ( + // DefaultJSONHandler default json handler + DefaultJSONHandler Interface = JSONiter{jsoniter.ConfigCompatibleWithStandardLibrary} + + _ Interface = StdJSON{} + _ Interface = JSONiter{} +) + +// StdJSON implements Interface via encoding/json +type StdJSON struct{} + +// Marshal implements Interface +func (StdJSON) Marshal(v interface{}) ([]byte, error) { + return json.Marshal(v) +} + +// Unmarshal implements Interface +func (StdJSON) Unmarshal(data []byte, v interface{}) error { + return json.Unmarshal(data, v) +} + +// NewEncoder implements Interface +func (StdJSON) NewEncoder(writer io.Writer) Encoder { + return json.NewEncoder(writer) +} + +// NewDecoder implements Interface +func (StdJSON) NewDecoder(reader io.Reader) Decoder { + return json.NewDecoder(reader) +} + +// Indent implements Interface +func (StdJSON) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { + return json.Indent(dst, src, prefix, indent) +} + +// JSONiter implements Interface via jsoniter +type JSONiter struct { + jsoniter.API +} + +// Marshal implements Interface +func (j JSONiter) Marshal(v interface{}) ([]byte, error) { + return j.API.Marshal(v) +} + +// Unmarshal implements Interface +func (j JSONiter) Unmarshal(data []byte, v interface{}) error { + return j.API.Unmarshal(data, v) +} + +// NewEncoder implements Interface +func (j JSONiter) NewEncoder(writer io.Writer) Encoder { + return j.API.NewEncoder(writer) +} + +// NewDecoder implements Interface +func (j JSONiter) NewDecoder(reader io.Reader) Decoder { + return j.API.NewDecoder(reader) +} + +// Indent implements Interface, since jsoniter don't support Indent, just use encoding/json's +func (j JSONiter) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { + return json.Indent(dst, src, prefix, indent) +} + +// Marshal converts object as bytes +func Marshal(v interface{}) ([]byte, error) { + return DefaultJSONHandler.Marshal(v) +} + +// Unmarshal decodes object from bytes +func Unmarshal(data []byte, v interface{}) error { + return DefaultJSONHandler.Unmarshal(data, v) +} + +// NewEncoder creates an encoder to write objects to writer +func NewEncoder(writer io.Writer) Encoder { + return DefaultJSONHandler.NewEncoder(writer) +} + +// NewDecoder creates a decoder to read objects from reader +func NewDecoder(reader io.Reader) Decoder { + return DefaultJSONHandler.NewDecoder(reader) +} + +// Indent appends to dst an indented form of the JSON-encoded src. +func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { + return DefaultJSONHandler.Indent(dst, src, prefix, indent) +} diff --git a/modules/lfs/http_client.go b/modules/lfs/http_client.go index e799b80831ead..51843bcb98628 100644 --- a/modules/lfs/http_client.go +++ b/modules/lfs/http_client.go @@ -14,7 +14,6 @@ import ( "strings" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) diff --git a/modules/lfs/transferadapter.go b/modules/lfs/transferadapter.go index 8c40ab8c04469..e6f566652804e 100644 --- a/modules/lfs/transferadapter.go +++ b/modules/lfs/transferadapter.go @@ -13,7 +13,6 @@ import ( "net/http" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) diff --git a/modules/log/conn.go b/modules/log/conn.go index ad3d7f74e0cde..988bb3c3840a6 100644 --- a/modules/log/conn.go +++ b/modules/log/conn.go @@ -6,11 +6,10 @@ package log import ( + "encoding/json" "fmt" "io" "net" - - jsoniter "github.com/json-iterator/go" ) type connWriter struct { @@ -106,7 +105,7 @@ func NewConn() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConnLogger) Init(jsonconfig string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary + err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/log/console.go b/modules/log/console.go index 339b9ef3a4856..21ab81383728d 100644 --- a/modules/log/console.go +++ b/modules/log/console.go @@ -6,11 +6,10 @@ package log import ( + "encoding/json" "fmt" "io" "os" - - jsoniter "github.com/json-iterator/go" ) // CanColorStdout reports if we can color the Stdout @@ -52,7 +51,7 @@ func NewConsoleLogger() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConsoleLogger) Init(config string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary + err := json.Unmarshal([]byte(config), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/log/file.go b/modules/log/file.go index 79cbe740fdb8f..1bc3638944dbb 100644 --- a/modules/log/file.go +++ b/modules/log/file.go @@ -7,6 +7,8 @@ package log import ( "bufio" "compress/gzip" + "encoding/json" + "errors" "fmt" "os" @@ -16,7 +18,6 @@ import ( "time" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) // FileLogger implements LoggerProvider. @@ -101,7 +102,7 @@ func NewFileLogger() LoggerProvider { // "rotate":true // } func (log *FileLogger) Init(config string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(config), log); err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) } diff --git a/modules/log/level.go b/modules/log/level.go index 4b2d4ced4148e..892b24b8c7df1 100644 --- a/modules/log/level.go +++ b/modules/log/level.go @@ -6,11 +6,11 @@ package log import ( "bytes" + "encoding/json" + "fmt" "os" "strings" - - jsoniter "github.com/json-iterator/go" ) // Level is the level of the logger @@ -104,7 +104,7 @@ func FromString(level string) Level { // UnmarshalJSON takes text and turns it into a Level func (l *Level) UnmarshalJSON(b []byte) error { var tmp interface{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + err := json.Unmarshal(b, &tmp) if err != nil { fmt.Fprintf(os.Stderr, "Err: %v", err) diff --git a/modules/log/level_test.go b/modules/log/level_test.go index 2f37e407a110e..422b430688561 100644 --- a/modules/log/level_test.go +++ b/modules/log/level_test.go @@ -5,10 +5,10 @@ package log import ( + "encoding/json" "fmt" "testing" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -17,7 +17,7 @@ type testLevel struct { } func TestLevelMarshalUnmarshalJSON(t *testing.T) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + levelBytes, err := json.Marshal(testLevel{ Level: INFO, }) diff --git a/modules/log/smtp.go b/modules/log/smtp.go index 3f27b2c658a32..9f2326b9351c6 100644 --- a/modules/log/smtp.go +++ b/modules/log/smtp.go @@ -6,11 +6,10 @@ package log import ( + "encoding/json" "fmt" "net/smtp" "strings" - - jsoniter "github.com/json-iterator/go" ) type smtpWriter struct { @@ -57,7 +56,7 @@ func NewSMTPLogger() LoggerProvider { // "level":LevelError // } func (log *SMTPLogger) Init(jsonconfig string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary + err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 776bee6a0ca3e..98f66187b2ddd 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -5,6 +5,7 @@ package action import ( + "encoding/json" "fmt" "path" "strings" @@ -13,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/repository" - jsoniter "github.com/json-iterator/go" ) type actionNotifier struct { @@ -296,7 +296,7 @@ func (*actionNotifier) NotifyPullRevieweDismiss(doer *models.User, review *model } func (a *actionNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.Marshal(commits) if err != nil { log.Error("Marshal: %v", err) @@ -368,7 +368,7 @@ func (a *actionNotifier) NotifyDeleteRef(doer *models.User, repo *models.Reposit } func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.Marshal(commits) if err != nil { log.Error("json.Marshal: %v", err) diff --git a/modules/private/hook.go b/modules/private/hook.go index 79fae052dd765..61dafd2ca8290 100644 --- a/modules/private/hook.go +++ b/modules/private/hook.go @@ -6,15 +6,14 @@ package private import ( "context" - "encoding/json" "fmt" "net/http" "net/url" "strconv" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // Git environment variables @@ -88,7 +87,6 @@ func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOp ) req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, _ := json.Marshal(opts) req.Body(jsonBytes) req.SetTimeout(60*time.Second, time.Duration(60+len(opts.OldCommitIDs))*time.Second) @@ -115,7 +113,7 @@ func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookO req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") req.SetTimeout(60*time.Second, time.Duration(60+len(opts.OldCommitIDs))*time.Second) - json := jsoniter.ConfigCompatibleWithStandardLibrary + jsonBytes, _ := json.Marshal(opts) req.Body(jsonBytes) resp, err := req.Response() diff --git a/modules/private/internal.go b/modules/private/internal.go index 672ac74970edf..1589da76aa67b 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -7,13 +7,14 @@ package private import ( "context" "crypto/tls" + "fmt" "net" "net/http" "code.gitea.io/gitea/modules/httplib" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) func newRequest(ctx context.Context, url, method string) *httplib.Request { @@ -30,7 +31,7 @@ type Response struct { func decodeJSONError(resp *http.Response) *Response { var res Response - json := jsoniter.ConfigCompatibleWithStandardLibrary + err := json.NewDecoder(resp.Body).Decode(&res) if err != nil { res.Err = err.Error() diff --git a/modules/private/mail.go b/modules/private/mail.go index 4a5a3eedd794d..fa390a1962686 100644 --- a/modules/private/mail.go +++ b/modules/private/mail.go @@ -6,12 +6,13 @@ package private import ( "context" + "fmt" "io/ioutil" "net/http" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // Email structure holds a data for sending general emails @@ -33,7 +34,7 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (int, req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary + jsonBytes, _ := json.Marshal(Email{ Subject: subject, Message: message, diff --git a/modules/private/manager.go b/modules/private/manager.go index 0bcc3f8112484..2b34b82249efd 100644 --- a/modules/private/manager.go +++ b/modules/private/manager.go @@ -6,13 +6,14 @@ package private import ( "context" + "fmt" "net/http" "net/url" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // Shutdown calls the internal shutdown function @@ -66,7 +67,7 @@ func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) ( req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second) } req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary + jsonBytes, _ := json.Marshal(FlushOptions{ Timeout: timeout, NonBlocking: nonBlocking, @@ -153,7 +154,7 @@ func AddLogger(ctx context.Context, group, name, mode string, config map[string] req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary + jsonBytes, _ := json.Marshal(LoggerOptions{ Group: group, Name: name, diff --git a/modules/private/restore_repo.go b/modules/private/restore_repo.go index 66b60d8d124b2..a523ddc29456d 100644 --- a/modules/private/restore_repo.go +++ b/modules/private/restore_repo.go @@ -6,13 +6,14 @@ package private import ( "context" + "encoding/json" + "fmt" "io/ioutil" "net/http" "time" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // RestoreParams structure holds a data for restore repository @@ -30,7 +31,7 @@ func RestoreRepo(ctx context.Context, repoDir, ownerName, repoName string, units req := newInternalRequest(ctx, reqURL, "POST") req.SetTimeout(3*time.Second, 0) // since the request will spend much time, don't timeout req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary + jsonBytes, _ := json.Marshal(RestoreParams{ RepoDir: repoDir, OwnerName: ownerName, diff --git a/modules/private/serv.go b/modules/private/serv.go index 9643dad679a1b..d8a4774909d2f 100644 --- a/modules/private/serv.go +++ b/modules/private/serv.go @@ -6,13 +6,14 @@ package private import ( "context" + "fmt" "net/http" "net/url" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // KeyAndOwner is the response from ServNoCommand @@ -35,7 +36,7 @@ func ServNoCommand(ctx context.Context, keyID int64) (*models.PublicKey, *models } var keyAndOwner KeyAndOwner - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.NewDecoder(resp.Body).Decode(&keyAndOwner); err != nil { return nil, nil, err } @@ -91,7 +92,7 @@ func ServCommand(ctx context.Context, keyID int64, ownerName, repoName string, m return nil, err } defer resp.Body.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + if resp.StatusCode != http.StatusOK { var errServCommand ErrServCommand if err := json.NewDecoder(resp.Body).Decode(&errServCommand); err != nil { diff --git a/modules/queue/helper.go b/modules/queue/helper.go index e0368bce3a506..294184aaa81ce 100644 --- a/modules/queue/helper.go +++ b/modules/queue/helper.go @@ -5,9 +5,8 @@ package queue import ( + "encoding/json" "reflect" - - jsoniter "github.com/json-iterator/go" ) // Mappable represents an interface that can MapTo another interface @@ -20,7 +19,6 @@ type Mappable interface { // It will tolerate the cfg being passed as a []byte or string of a json representation of the // exemplar or the correct type of the exemplar itself func toConfig(exemplar, cfg interface{}) (interface{}, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary // First of all check if we've got the same type as the exemplar - if so it's all fine. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) { @@ -68,7 +66,7 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) { // unmarshalAs will attempt to unmarshal provided bytes as the provided exemplar func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + if exemplar != nil { t := reflect.TypeOf(exemplar) n := reflect.New(t) diff --git a/modules/queue/manager.go b/modules/queue/manager.go index a6d48575ab674..c6faa21acd5c0 100644 --- a/modules/queue/manager.go +++ b/modules/queue/manager.go @@ -6,6 +6,8 @@ package queue import ( "context" + "encoding/json" + "fmt" "reflect" "sort" @@ -13,7 +15,6 @@ import ( "time" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) var manager *Manager @@ -110,7 +111,6 @@ func (m *Manager) Add(managed interface{}, configuration, exemplar interface{}) int64 { - json := jsoniter.ConfigCompatibleWithStandardLibrary cfg, _ := json.Marshal(configuration) mq := &ManagedQueue{ Type: t, diff --git a/modules/queue/queue_bytefifo.go b/modules/queue/queue_bytefifo.go index 3ea61aad0e4c5..84f684b4327d7 100644 --- a/modules/queue/queue_bytefifo.go +++ b/modules/queue/queue_bytefifo.go @@ -6,12 +6,13 @@ package queue import ( "context" + "encoding/json" + "fmt" "sync" "time" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) // ByteFIFOQueueConfiguration is the configuration for a ByteFIFOQueue @@ -83,7 +84,7 @@ func (q *ByteFIFOQueue) PushFunc(data Data, fn func() error) error { if !assignableTo(data, q.exemplar) { return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name) } - json := jsoniter.ConfigCompatibleWithStandardLibrary + bs, err := json.Marshal(data) if err != nil { return err @@ -309,7 +310,7 @@ func (q *ByteFIFOUniqueQueue) Has(data Data) (bool, error) { if !assignableTo(data, q.exemplar) { return false, fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name) } - json := jsoniter.ConfigCompatibleWithStandardLibrary + bs, err := json.Marshal(data) if err != nil { return false, err diff --git a/modules/queue/queue_test.go b/modules/queue/queue_test.go index 89ce23ac4c5c7..3608f68d3d424 100644 --- a/modules/queue/queue_test.go +++ b/modules/queue/queue_test.go @@ -5,9 +5,9 @@ package queue import ( + "encoding/json" "testing" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -30,7 +30,6 @@ func TestToConfig(t *testing.T) { assert.NotEqual(t, cfg2, exemplar) assert.Equal(t, &cfg, &cfg2) - json := jsoniter.ConfigCompatibleWithStandardLibrary cfgString, err := json.Marshal(cfg) assert.NoError(t, err) diff --git a/modules/queue/setting.go b/modules/queue/setting.go index 9b2c31b78334d..9ee1af8c7d8d7 100644 --- a/modules/queue/setting.go +++ b/modules/queue/setting.go @@ -5,12 +5,12 @@ package queue import ( + "encoding/json" "fmt" "strings" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) func validType(t string) (Type, error) { @@ -28,7 +28,6 @@ func validType(t string) (Type, error) { func getQueueSettings(name string) (setting.QueueSettings, []byte) { q := setting.GetQueueSettings(name) - json := jsoniter.ConfigCompatibleWithStandardLibrary cfg, err := json.Marshal(q) if err != nil { log.Error("Unable to marshall generic options: %v Error: %v", q, err) diff --git a/modules/recaptcha/recaptcha.go b/modules/recaptcha/recaptcha.go index f5f4c7ef9c1cc..8057575ee1cd1 100644 --- a/modules/recaptcha/recaptcha.go +++ b/modules/recaptcha/recaptcha.go @@ -6,6 +6,8 @@ package recaptcha import ( "context" + "encoding/json" + "fmt" "io/ioutil" "net/http" @@ -14,7 +16,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) // Response is the structure of JSON returned from API @@ -51,7 +52,7 @@ func Verify(ctx context.Context, response string) (bool, error) { return false, fmt.Errorf("Failed to read CAPTCHA response: %s", err) } var jsonResponse Response - json := jsoniter.ConfigCompatibleWithStandardLibrary + err = json.Unmarshal(body, &jsonResponse) if err != nil { return false, fmt.Errorf("Failed to parse CAPTCHA response: %s", err) diff --git a/modules/session/virtual.go b/modules/session/virtual.go index 8f5c4a7e8942b..9da62348073f5 100644 --- a/modules/session/virtual.go +++ b/modules/session/virtual.go @@ -5,6 +5,7 @@ package session import ( + "encoding/json" "fmt" "sync" @@ -13,7 +14,6 @@ import ( memcache "gitea.com/go-chi/session/memcache" mysql "gitea.com/go-chi/session/mysql" postgres "gitea.com/go-chi/session/postgres" - jsoniter "github.com/json-iterator/go" ) // VirtualSessionProvider represents a shadowed session provider implementation. @@ -25,7 +25,7 @@ type VirtualSessionProvider struct { // Init initializes the cookie session provider with given root path. func (o *VirtualSessionProvider) Init(gclifetime int64, config string) error { var opts session.Options - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(config), &opts); err != nil { return err } diff --git a/modules/setting/log.go b/modules/setting/log.go index 0fb108c93dcf3..a2cafd067982c 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -5,6 +5,7 @@ package setting import ( + "encoding/json" "fmt" golog "log" "os" @@ -14,7 +15,6 @@ import ( "sync" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ini "gopkg.in/ini.v1" ) @@ -204,7 +204,6 @@ func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions logConfig["colorize"] = sec.Key("COLORIZE").MustBool(false) - json := jsoniter.ConfigCompatibleWithStandardLibrary byteConfig, err := json.Marshal(logConfig) if err != nil { log.Error("Failed to marshal log configuration: %v %v", logConfig, err) diff --git a/modules/setting/session.go b/modules/setting/session.go index bce73b51da647..38bcd4ac9f84b 100644 --- a/modules/setting/session.go +++ b/modules/setting/session.go @@ -5,13 +5,13 @@ package setting import ( + "encoding/json" "net/http" "path" "path/filepath" "strings" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) var ( @@ -66,7 +66,6 @@ func newSessionService() { SessionConfig.SameSite = http.SameSiteLaxMode } - json := jsoniter.ConfigCompatibleWithStandardLibrary shadowConfig, err := json.Marshal(SessionConfig) if err != nil { log.Fatal("Can't shadow session config: %v", err) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index e3da5796e4268..ee03b8e2e38b3 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -7,6 +7,8 @@ package setting import ( "encoding/base64" + "encoding/json" + "fmt" "io" "io/ioutil" @@ -28,7 +30,6 @@ import ( "code.gitea.io/gitea/modules/user" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" shellquote "github.com/kballard/go-shellquote" "github.com/unknwon/com" gossh "golang.org/x/crypto/ssh" @@ -1116,7 +1117,6 @@ func MakeManifestData(appName string, appURL string, absoluteAssetURL string) [] Icons []manifestIcon `json:"icons"` } - json := jsoniter.ConfigCompatibleWithStandardLibrary bytes, err := json.Marshal(&manifestJSON{ Name: appName, ShortName: appName, diff --git a/modules/setting/setting_test.go b/modules/setting/setting_test.go index 9bc77ab0bb470..f61dedd4069b3 100644 --- a/modules/setting/setting_test.go +++ b/modules/setting/setting_test.go @@ -5,9 +5,9 @@ package setting import ( + "encoding/json" "testing" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -28,6 +28,6 @@ func TestMakeAbsoluteAssetURL(t *testing.T) { func TestMakeManifestData(t *testing.T) { jsonBytes := MakeManifestData(`Example App '\"`, "https://example.com", "https://example.com/foo/bar") - json := jsoniter.ConfigCompatibleWithStandardLibrary + assert.True(t, json.Valid(jsonBytes)) } diff --git a/modules/storage/helper.go b/modules/storage/helper.go index 4d8ba8e6d9dd9..63c80495690a4 100644 --- a/modules/storage/helper.go +++ b/modules/storage/helper.go @@ -5,9 +5,8 @@ package storage import ( + "encoding/json" "reflect" - - jsoniter "github.com/json-iterator/go" ) // Mappable represents an interface that can MapTo another interface @@ -20,7 +19,6 @@ type Mappable interface { // It will tolerate the cfg being passed as a []byte or string of a json representation of the // exemplar or the correct type of the exemplar itself func toConfig(exemplar, cfg interface{}) (interface{}, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary // First of all check if we've got the same type as the exemplar - if so it's all fine. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) { diff --git a/modules/structs/hook.go b/modules/structs/hook.go index ad86cb35a4f18..e030a4527b48b 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -6,11 +6,10 @@ package structs import ( + "encoding/json" "errors" "strings" "time" - - jsoniter "github.com/json-iterator/go" ) var ( @@ -132,14 +131,14 @@ type CreatePayload struct { // JSONPayload return payload information func (p *CreatePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } // ParseCreateHook parses create event hook content. func ParseCreateHook(raw []byte) (*CreatePayload, error) { hook := new(CreatePayload) - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal(raw, hook); err != nil { return nil, err } @@ -183,7 +182,7 @@ type DeletePayload struct { // JSONPayload implements Payload func (p *DeletePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } @@ -203,7 +202,7 @@ type ForkPayload struct { // JSONPayload implements Payload func (p *ForkPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } @@ -230,7 +229,7 @@ type IssueCommentPayload struct { // JSONPayload implements Payload func (p *IssueCommentPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } @@ -261,7 +260,7 @@ type ReleasePayload struct { // JSONPayload implements Payload func (p *ReleasePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } @@ -287,14 +286,14 @@ type PushPayload struct { // JSONPayload FIXME func (p *PushPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } // ParsePushHook parses push event hook content. func ParsePushHook(raw []byte) (*PushPayload, error) { hook := new(PushPayload) - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal(raw, hook); err != nil { return nil, err } @@ -362,7 +361,7 @@ type IssuePayload struct { // JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces. func (p *IssuePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } @@ -398,7 +397,7 @@ type PullRequestPayload struct { // JSONPayload FIXME func (p *PullRequestPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } @@ -435,6 +434,6 @@ type RepositoryPayload struct { // JSONPayload JSON representation of the payload func (p *RepositoryPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.MarshalIndent(p, "", " ") } diff --git a/modules/structs/user.go b/modules/structs/user.go index a3c8f0c32a7b3..9e747deb6d59e 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -5,9 +5,8 @@ package structs import ( + "encoding/json" "time" - - jsoniter "github.com/json-iterator/go" ) // User represents a user @@ -56,7 +55,7 @@ type User struct { func (u User) MarshalJSON() ([]byte, error) { // Re-declaring User to avoid recursion type shadow User - json := jsoniter.ConfigCompatibleWithStandardLibrary + return json.Marshal(struct { shadow CompatUserName string `json:"username"` diff --git a/modules/task/migrate.go b/modules/task/migrate.go index d7655112d351c..4f22df979f127 100644 --- a/modules/task/migrate.go +++ b/modules/task/migrate.go @@ -6,6 +6,8 @@ package task import ( "context" + "encoding/json" + "errors" "fmt" "strings" @@ -20,7 +22,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) func handleCreateError(owner *models.User, err error) error { @@ -112,7 +113,7 @@ func runMigrateTask(t *models.Task) (err error) { Format: format, Args: args, } - json := jsoniter.ConfigCompatibleWithStandardLibrary + bs, _ := json.Marshal(message) t.Message = string(bs) _ = t.UpdateCols("message") diff --git a/modules/task/task.go b/modules/task/task.go index 1c0a87e1f61a4..b71447ae5a6ec 100644 --- a/modules/task/task.go +++ b/modules/task/task.go @@ -5,6 +5,7 @@ package task import ( + "encoding/json" "fmt" "code.gitea.io/gitea/models" @@ -18,7 +19,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) // taskQueue is a global queue of tasks @@ -86,7 +86,6 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models. } opts.AuthToken = "" - json := jsoniter.ConfigCompatibleWithStandardLibrary bs, err := json.Marshal(&opts) if err != nil { return nil, err diff --git a/modules/templates/helper.go b/modules/templates/helper.go index d4913f7c41770..3a9e30fdc5d6c 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -8,7 +8,7 @@ package templates import ( "bytes" "container/list" - "encoding/json" + "errors" "fmt" "html" @@ -37,6 +37,7 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/gitdiff" + "code.gitea.io/gitea/modules/json" "github.com/editorconfig/editorconfig-core-go/v2" jsoniter "github.com/json-iterator/go" ) @@ -847,7 +848,6 @@ func ActionContent2Commits(act Actioner) *repository.PushCommits { return push } - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil { log.Error("json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err) } diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 5f2be65a29d99..4cfa01fcbcf40 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -5,6 +5,7 @@ package utils import ( + "encoding/json" "fmt" "net/http" "strings" @@ -16,7 +17,6 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/services/webhook" - jsoniter "github.com/json-iterator/go" ) // GetOrgHook get an organization's webhook. If there is an error, write to @@ -147,7 +147,6 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID return nil, false } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.SlackMeta{ Channel: strings.TrimSpace(channel), Username: form.Config["username"], @@ -222,7 +221,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho if w.Type == models.SLACK { if channel, ok := form.Config["channel"]; ok { - json := jsoniter.ConfigCompatibleWithStandardLibrary + meta, err := json.Marshal(&webhook.SlackMeta{ Channel: channel, Username: form.Config["username"], diff --git a/routers/private/mail.go b/routers/private/mail.go index d7bd5155f65ae..6576297dd3f52 100644 --- a/routers/private/mail.go +++ b/routers/private/mail.go @@ -5,6 +5,7 @@ package private import ( + "encoding/json" "fmt" "net/http" "strconv" @@ -15,7 +16,6 @@ import ( "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/mailer" - jsoniter "github.com/json-iterator/go" ) // SendEmail pushes messages to mail queue @@ -32,7 +32,7 @@ func SendEmail(ctx *context.PrivateContext) { var mail private.Email rd := ctx.Req.Body defer rd.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.NewDecoder(rd).Decode(&mail); err != nil { log.Error("%v", err) ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/private/manager.go b/routers/private/manager.go index 7d010f3f8180c..03124321f4042 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -5,6 +5,7 @@ package private import ( + "encoding/json" "fmt" "net/http" @@ -15,7 +16,6 @@ import ( "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" - jsoniter "github.com/json-iterator/go" ) // FlushQueues flushes all the Queues @@ -130,7 +130,7 @@ func AddLogger(ctx *context.PrivateContext) { } bufferLen := setting.Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000) - json := jsoniter.ConfigCompatibleWithStandardLibrary + byteConfig, err := json.Marshal(opts.Config) if err != nil { log.Error("Failed to marshal log configuration: %v %v", opts.Config, err) diff --git a/routers/private/restore_repo.go b/routers/private/restore_repo.go index 36d17dd95c239..f6c5bf36c4e14 100644 --- a/routers/private/restore_repo.go +++ b/routers/private/restore_repo.go @@ -5,18 +5,18 @@ package private import ( + "encoding/json" "io/ioutil" "net/http" myCtx "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/private" - jsoniter "github.com/json-iterator/go" ) // RestoreRepo restore a repository from data func RestoreRepo(ctx *myCtx.PrivateContext) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + bs, err := ioutil.ReadAll(ctx.Req.Body) if err != nil { ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index c2d94ab9c9710..55549d5597755 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -6,6 +6,7 @@ package admin import ( + "encoding/json" "fmt" "net/http" "net/url" @@ -28,7 +29,6 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" - jsoniter "github.com/json-iterator/go" "gitea.com/go-chi/session" ) @@ -275,7 +275,7 @@ func Config(ctx *context.Context) { sessionCfg := setting.SessionConfig if sessionCfg.Provider == "VirtualSession" { var realSession session.Options - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(sessionCfg.ProviderConfig), &realSession); err != nil { log.Error("Unable to unmarshall session config for virtualed provider config: %s\nError: %v", sessionCfg.ProviderConfig, err) } diff --git a/routers/web/events/events.go b/routers/web/events/events.go index f9cc274851756..f854dfdb35b04 100644 --- a/routers/web/events/events.go +++ b/routers/web/events/events.go @@ -5,6 +5,7 @@ package events import ( + "encoding/json" "net/http" "time" @@ -16,7 +17,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/web/user" - jsoniter "github.com/json-iterator/go" ) // Events listens for events @@ -103,7 +103,7 @@ loop: log.Error("Unable to APIFormat stopwatches: %v", err) continue } - json := jsoniter.ConfigCompatibleWithStandardLibrary + dataBs, err := json.Marshal(apiSWs) if err != nil { log.Error("Unable to marshal stopwatches: %v", err) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 0f978c7b01c59..ba062e25413ce 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -5,6 +5,7 @@ package repo import ( + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -26,7 +27,6 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/services/forms" - jsoniter "github.com/json-iterator/go" ) const ( @@ -165,7 +165,7 @@ func GetEditorConfig(ctx *context.Context, treePath string) string { if err == nil { def, err := ec.GetDefinitionForFilename(treePath) if err == nil { - json := jsoniter.ConfigCompatibleWithStandardLibrary + jsonStr, _ := json.Marshal(def) return string(jsonStr) } diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 30fa98f809df5..e0a17d40a4150 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -6,6 +6,7 @@ package repo import ( + "encoding/json" "errors" "fmt" "net/http" @@ -23,7 +24,6 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/webhook" - jsoniter "github.com/json-iterator/go" ) const ( @@ -306,7 +306,6 @@ func DiscordHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.DiscordMeta{ Username: form.Username, IconURL: form.IconURL, @@ -402,7 +401,6 @@ func TelegramHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.TelegramMeta{ BotToken: form.BotToken, ChatID: form.ChatID, @@ -455,7 +453,6 @@ func MatrixHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.MatrixMeta{ HomeserverURL: form.HomeserverURL, Room: form.RoomID, @@ -560,7 +557,6 @@ func SlackHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.SlackMeta{ Channel: strings.TrimSpace(form.Channel), Username: form.Username, @@ -848,7 +844,6 @@ func SlackHooksEditPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.SlackMeta{ Channel: strings.TrimSpace(form.Channel), Username: form.Username, @@ -894,7 +889,6 @@ func DiscordHooksEditPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.DiscordMeta{ Username: form.Username, IconURL: form.IconURL, @@ -970,7 +964,7 @@ func TelegramHooksEditPost(ctx *context.Context) { ctx.HTML(http.StatusOK, orCtx.NewTemplate) return } - json := jsoniter.ConfigCompatibleWithStandardLibrary + meta, err := json.Marshal(&webhook.TelegramMeta{ BotToken: form.BotToken, ChatID: form.ChatID, @@ -1012,7 +1006,7 @@ func MatrixHooksEditPost(ctx *context.Context) { ctx.HTML(http.StatusOK, orCtx.NewTemplate) return } - json := jsoniter.ConfigCompatibleWithStandardLibrary + meta, err := json.Marshal(&webhook.MatrixMeta{ HomeserverURL: form.HomeserverURL, Room: form.RoomID, diff --git a/routers/web/user/home.go b/routers/web/user/home.go index d3fc36c73012e..57c94bb5c6b7e 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -7,6 +7,8 @@ package user import ( "bytes" + "encoding/json" + "fmt" "net/http" "regexp" @@ -26,7 +28,6 @@ import ( issue_service "code.gitea.io/gitea/services/issue" pull_service "code.gitea.io/gitea/services/pull" - jsoniter "github.com/json-iterator/go" "github.com/keybase/go-crypto/openpgp" "github.com/keybase/go-crypto/openpgp/armor" "xorm.io/builder" @@ -703,7 +704,7 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) { } // Convert []int64 to string - json := jsoniter.ConfigCompatibleWithStandardLibrary + reposParam, _ := json.Marshal(repoIDs) ctx.Data["ReposParam"] = string(reposParam) diff --git a/routers/web/user/task.go b/routers/web/user/task.go index 8e7b66ef9538e..e4764e456d06c 100644 --- a/routers/web/user/task.go +++ b/routers/web/user/task.go @@ -5,11 +5,11 @@ package user import ( + "encoding/json" "net/http" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - jsoniter "github.com/json-iterator/go" ) // TaskStatus returns task's status @@ -26,7 +26,7 @@ func TaskStatus(ctx *context.Context) { if task.Message != "" && task.Message[0] == '{' { // assume message is actually a translatable string - json := jsoniter.ConfigCompatibleWithStandardLibrary + var translatableMessage models.TranslatableMessage if err := json.Unmarshal([]byte(message), &translatableMessage); err != nil { translatableMessage = models.TranslatableMessage{ diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 2386552efec77..5e26dfa581d97 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -6,6 +6,7 @@ package gitdiff import ( + "encoding/json" "fmt" "html/template" "strconv" @@ -15,7 +16,6 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" dmp "github.com/sergi/go-diff/diffmatchpatch" "github.com/stretchr/testify/assert" "gopkg.in/ini.v1" @@ -297,7 +297,7 @@ index 6961180..9ba1a00 100644 t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr) return } - json := jsoniter.ConfigCompatibleWithStandardLibrary + gotMarshaled, _ := json.MarshalIndent(got, "", " ") if got.NumFiles != 1 { t.Errorf("ParsePath(%q) did not receive 1 file:\n%s", testcase.name, string(gotMarshaled)) diff --git a/services/lfs/locks.go b/services/lfs/locks.go index 20ba12e65bb20..641c1c595b4cc 100644 --- a/services/lfs/locks.go +++ b/services/lfs/locks.go @@ -12,11 +12,11 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/json" lfs_module "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *models.LFSLock, err error) { @@ -159,7 +159,7 @@ func PostLockHandler(ctx *context.Context) { var req api.LFSLockRequest bodyReader := ctx.Req.Body defer bodyReader.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + dec := json.NewDecoder(bodyReader) if err := dec.Decode(&req); err != nil { log.Warn("Failed to decode lock request as json. Error: %v", err) @@ -293,7 +293,7 @@ func UnLockHandler(ctx *context.Context) { var req api.LFSLockDeleteRequest bodyReader := ctx.Req.Body defer bodyReader.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + dec := json.NewDecoder(bodyReader) if err := dec.Decode(&req); err != nil { log.Warn("Failed to decode lock request as json. Error: %v", err) diff --git a/services/pull/pull.go b/services/pull/pull.go index 6b3acd2004254..a98782a00eda5 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -8,6 +8,8 @@ import ( "bufio" "bytes" "context" + "encoding/json" + "fmt" "regexp" "strings" @@ -20,7 +22,6 @@ import ( "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/setting" issue_service "code.gitea.io/gitea/services/issue" - jsoniter "github.com/json-iterator/go" ) // NewPullRequest creates new pull request with labels for repository. @@ -86,7 +87,6 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 data.CommitIDs = append(data.CommitIDs, e.Value.(*git.Commit).ID.String()) } - json := jsoniter.ConfigCompatibleWithStandardLibrary dataJSON, err := json.Marshal(data) if err != nil { return err diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 49e161ea57b5e..35f8eba79b8e1 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -5,6 +5,7 @@ package webhook import ( + "encoding/json" "fmt" "strings" @@ -12,7 +13,6 @@ import ( "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" dingtalk "github.com/lunny/dingtalk_webhook" ) @@ -27,7 +27,7 @@ var ( // JSONPayload Marshals the DingtalkPayload to json func (d *DingtalkPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.MarshalIndent(d, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/discord.go b/services/webhook/discord.go index ea3879f1980d0..696a2312937af 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -5,6 +5,7 @@ package webhook import ( + "encoding/json" "errors" "fmt" "strconv" @@ -15,7 +16,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -68,7 +68,7 @@ type ( // GetDiscordHook returns discord metadata func GetDiscordHook(w *models.Webhook) *DiscordMeta { s := &DiscordMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err) } @@ -99,7 +99,7 @@ var ( // JSONPayload Marshals the DiscordPayload to json func (d *DiscordPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.MarshalIndent(d, "", " ") if err != nil { return []byte{}, err @@ -250,7 +250,7 @@ func GetDiscordPayload(p api.Payloader, event models.HookEventType, meta string) s := new(DiscordPayload) discord := &DiscordMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(meta), &discord); err != nil { return s, errors.New("GetDiscordPayload meta json:" + err.Error()) } diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index b280e67759ce5..470f0f742c443 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -5,13 +5,13 @@ package webhook import ( + "encoding/json" "fmt" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -37,7 +37,7 @@ func newFeishuTextPayload(text string) *FeishuPayload { // JSONPayload Marshals the FeishuPayload to json func (f *FeishuPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.MarshalIndent(f, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 6fca67ca84f97..90636c879e3ad 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -6,6 +6,8 @@ package webhook import ( "crypto/sha1" + "encoding/json" + "errors" "fmt" "html" @@ -18,7 +20,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) const matrixPayloadSizeLimit = 1024 * 64 @@ -39,7 +40,7 @@ var messageTypeText = map[int]string{ // GetMatrixHook returns Matrix metadata func GetMatrixHook(w *models.Webhook) *MatrixMeta { s := &MatrixMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetMatrixHook(%d): %v", w.ID, err) } @@ -78,7 +79,7 @@ type MatrixPayloadSafe struct { // JSONPayload Marshals the MatrixPayloadUnsafe to json func (m *MatrixPayloadUnsafe) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.MarshalIndent(m, "", " ") if err != nil { return []byte{}, err @@ -228,7 +229,7 @@ func GetMatrixPayload(p api.Payloader, event models.HookEventType, meta string) s := new(MatrixPayloadUnsafe) matrix := &MatrixMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(meta), &matrix); err != nil { return s, errors.New("GetMatrixPayload meta json:" + err.Error()) } @@ -262,7 +263,7 @@ func getMessageBody(htmlText string) string { // The access_token is removed from t.PayloadContent func getMatrixHookRequest(w *models.Webhook, t *models.HookTask) (*http.Request, error) { payloadunsafe := MatrixPayloadUnsafe{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(t.PayloadContent), &payloadunsafe); err != nil { log.Error("Matrix Hook delivery failed: %v", err) return nil, err diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 035dbc1c4cf23..426cb808ac148 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -5,13 +5,13 @@ package webhook import ( + "encoding/json" "fmt" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -57,7 +57,7 @@ type ( // JSONPayload Marshals the MSTeamsPayload to json func (m *MSTeamsPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.MarshalIndent(m, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/slack.go b/services/webhook/slack.go index f522ca35f2f0c..30a05711e64b8 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -5,6 +5,7 @@ package webhook import ( + "encoding/json" "errors" "fmt" "strings" @@ -14,7 +15,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) // SlackMeta contains the slack metadata @@ -28,7 +28,7 @@ type SlackMeta struct { // GetSlackHook returns slack metadata func GetSlackHook(w *models.Webhook) *SlackMeta { s := &SlackMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetSlackHook(%d): %v", w.ID, err) } @@ -58,7 +58,7 @@ type SlackAttachment struct { // JSONPayload Marshals the SlackPayload to json func (s *SlackPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.MarshalIndent(s, "", " ") if err != nil { return []byte{}, err @@ -279,7 +279,7 @@ func GetSlackPayload(p api.Payloader, event models.HookEventType, meta string) ( s := new(SlackPayload) slack := &SlackMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(meta), &slack); err != nil { return s, errors.New("GetSlackPayload meta json:" + err.Error()) } diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 4c4230759d318..07107f6375d1d 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -5,6 +5,7 @@ package webhook import ( + "encoding/json" "fmt" "strings" @@ -13,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -34,7 +34,7 @@ type ( // GetTelegramHook returns telegram metadata func GetTelegramHook(w *models.Webhook) *TelegramMeta { s := &TelegramMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err) } @@ -50,7 +50,7 @@ func (t *TelegramPayload) JSONPayload() ([]byte, error) { t.ParseMode = "HTML" t.DisableWebPreview = true t.Message = markup.Sanitize(t.Message) - json := jsoniter.ConfigCompatibleWithStandardLibrary + data, err := json.MarshalIndent(t, "", " ") if err != nil { return []byte{}, err From 7ce53723e7639b84aec94322fa24ec6a8199a471 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Jul 2021 10:35:53 +0800 Subject: [PATCH 2/6] Fix import --- cmd/dump.go | 3 ++- cmd/serv.go | 3 ++- integrations/api_admin_test.go | 3 ++- .../api_helper_for_declarative_test.go | 3 ++- integrations/api_pull_review_test.go | 3 ++- integrations/api_repo_lfs_test.go | 3 ++- integrations/create_no_session_test.go | 3 ++- integrations/integration_test.go | 3 ++- integrations/oauth_test.go | 3 ++- integrations/repo_commits_test.go | 3 ++- integrations/testlogger.go | 3 ++- models/issue_comment.go | 3 ++- models/migrations/testlogger_test.go | 3 ++- models/migrations/v130.go | 2 +- models/migrations/v180.go | 2 +- models/repo_unit.go | 3 ++- models/task.go | 3 ++- models/user_heatmap_test.go | 3 ++- models/webhook.go | 3 ++- models/webhook_test.go | 3 ++- modules/cache/cache_twoqueue.go | 3 ++- modules/context/context.go | 3 ++- modules/eventsource/event.go | 3 ++- modules/httplib/httplib.go | 3 ++- modules/indexer/code/elastic_search.go | 3 ++- modules/json/json.go | 19 +++++++++++++++++++ modules/log/conn.go | 3 ++- modules/log/console.go | 3 ++- modules/log/file.go | 3 ++- modules/log/level.go | 3 ++- modules/log/level_test.go | 3 ++- modules/log/smtp.go | 3 ++- modules/notification/action/action.go | 3 ++- modules/private/restore_repo.go | 3 ++- modules/queue/helper.go | 3 ++- modules/queue/manager.go | 3 ++- modules/queue/queue_bytefifo.go | 3 ++- modules/queue/queue_test.go | 3 ++- modules/queue/setting.go | 3 ++- modules/recaptcha/recaptcha.go | 3 ++- modules/session/virtual.go | 3 ++- modules/setting/log.go | 3 ++- modules/setting/session.go | 3 ++- modules/setting/setting.go | 3 ++- modules/setting/setting_test.go | 3 ++- modules/storage/helper.go | 3 ++- modules/structs/hook.go | 3 ++- modules/structs/user.go | 3 ++- modules/task/migrate.go | 3 ++- modules/task/task.go | 3 ++- routers/api/v1/utils/hook.go | 3 ++- routers/private/mail.go | 3 ++- routers/private/manager.go | 3 ++- routers/private/restore_repo.go | 3 ++- routers/web/admin/admin.go | 3 ++- routers/web/events/events.go | 3 ++- routers/web/repo/editor.go | 3 ++- routers/web/repo/webhook.go | 3 ++- routers/web/user/home.go | 3 ++- routers/web/user/task.go | 3 ++- services/gitdiff/gitdiff_test.go | 3 ++- services/pull/pull.go | 3 ++- services/webhook/dingtalk.go | 3 ++- services/webhook/discord.go | 3 ++- services/webhook/feishu.go | 3 ++- services/webhook/matrix.go | 3 ++- services/webhook/msteams.go | 3 ++- services/webhook/slack.go | 3 ++- services/webhook/telegram.go | 3 ++- services/webhook/wechatwork.go | 3 ++- 70 files changed, 155 insertions(+), 69 deletions(-) diff --git a/cmd/dump.go b/cmd/dump.go index 326062c49533c..2cedd5aed7912 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -6,7 +6,6 @@ package cmd import ( - "encoding/json" "fmt" "io/ioutil" "os" @@ -15,6 +14,8 @@ import ( "strings" "time" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" diff --git a/cmd/serv.go b/cmd/serv.go index 6a924fa5c0892..e53e79f77554b 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -6,7 +6,6 @@ package cmd import ( - "encoding/json" "fmt" "net/http" "net/url" @@ -17,6 +16,8 @@ import ( "strings" "time" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/pprof" diff --git a/integrations/api_admin_test.go b/integrations/api_admin_test.go index 988d6b07b18d3..15e7acd654d63 100644 --- a/integrations/api_admin_test.go +++ b/integrations/api_admin_test.go @@ -5,11 +5,12 @@ package integrations import ( - "encoding/json" "fmt" "net/http" "testing" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" api "code.gitea.io/gitea/modules/structs" diff --git a/integrations/api_helper_for_declarative_test.go b/integrations/api_helper_for_declarative_test.go index 5a541bdec01e4..024b96a7caed4 100644 --- a/integrations/api_helper_for_declarative_test.go +++ b/integrations/api_helper_for_declarative_test.go @@ -6,7 +6,8 @@ package integrations import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "io/ioutil" diff --git a/integrations/api_pull_review_test.go b/integrations/api_pull_review_test.go index cc8590d5ea6d8..b0958699c3d45 100644 --- a/integrations/api_pull_review_test.go +++ b/integrations/api_pull_review_test.go @@ -5,11 +5,12 @@ package integrations import ( - "encoding/json" "fmt" "net/http" "testing" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/integrations/api_repo_lfs_test.go b/integrations/api_repo_lfs_test.go index 4b73ae7aa9e77..a6dbe6f2e1a0e 100644 --- a/integrations/api_repo_lfs_test.go +++ b/integrations/api_repo_lfs_test.go @@ -6,7 +6,8 @@ package integrations import ( "bytes" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "net/http" "path" diff --git a/integrations/create_no_session_test.go b/integrations/create_no_session_test.go index 4253713553c11..fb7bba9e8fceb 100644 --- a/integrations/create_no_session_test.go +++ b/integrations/create_no_session_test.go @@ -5,7 +5,6 @@ package integrations import ( - "encoding/json" "io/ioutil" "net/http" "net/http/httptest" @@ -13,6 +12,8 @@ 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" diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 1d160caf58ccc..3af8bb98b537a 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -8,7 +8,8 @@ import ( "bytes" "context" "database/sql" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "hash" diff --git a/integrations/oauth_test.go b/integrations/oauth_test.go index 20074f1a6a34a..99eddd77b0fb7 100644 --- a/integrations/oauth_test.go +++ b/integrations/oauth_test.go @@ -6,7 +6,8 @@ package integrations import ( "bytes" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "io/ioutil" "testing" diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index cf799e871a79d..f467f5955d171 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -5,12 +5,13 @@ package integrations import ( - "encoding/json" "net/http" "net/http/httptest" "path" "testing" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/integrations/testlogger.go b/integrations/testlogger.go index 6ee929741eb2b..d67c7316bd68a 100644 --- a/integrations/testlogger.go +++ b/integrations/testlogger.go @@ -6,7 +6,8 @@ package integrations import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "os" diff --git a/models/issue_comment.go b/models/issue_comment.go index 54be79a5559bc..5675b9cb6ef2d 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -8,7 +8,8 @@ package models import ( "container/list" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "regexp" diff --git a/models/migrations/testlogger_test.go b/models/migrations/testlogger_test.go index 5f477f6de0fa7..775fffb1928d0 100644 --- a/models/migrations/testlogger_test.go +++ b/models/migrations/testlogger_test.go @@ -6,7 +6,8 @@ package migrations import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "os" diff --git a/models/migrations/v130.go b/models/migrations/v130.go index f931869f06186..d57a7bb5b31d6 100644 --- a/models/migrations/v130.go +++ b/models/migrations/v130.go @@ -5,7 +5,7 @@ package migrations import ( - "encoding/json" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" diff --git a/models/migrations/v180.go b/models/migrations/v180.go index b71bc9fa0b571..4b6ccece2ec00 100644 --- a/models/migrations/v180.go +++ b/models/migrations/v180.go @@ -5,7 +5,7 @@ package migrations import ( - "encoding/json" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/util" diff --git a/models/repo_unit.go b/models/repo_unit.go index 2780690905e88..ac26dd5b4d8b2 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -5,9 +5,10 @@ package models import ( - "encoding/json" "fmt" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/timeutil" "xorm.io/xorm" diff --git a/models/task.go b/models/task.go index 607aed1db9f6e..7a3c870380104 100644 --- a/models/task.go +++ b/models/task.go @@ -5,9 +5,10 @@ package models import ( - "encoding/json" "fmt" + "code.gitea.io/gitea/modules/json" + migration "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go index bed2e9de396f1..dc1d56628e958 100644 --- a/models/user_heatmap_test.go +++ b/models/user_heatmap_test.go @@ -5,10 +5,11 @@ package models import ( - "encoding/json" "fmt" "testing" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) diff --git a/models/webhook.go b/models/webhook.go index a1b9633d73697..bbcb8c86d1884 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -7,7 +7,8 @@ package models import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "strings" diff --git a/models/webhook_test.go b/models/webhook_test.go index 3273b05f580fd..a56d07744a29a 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -6,7 +6,8 @@ package models import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "testing" "time" diff --git a/modules/cache/cache_twoqueue.go b/modules/cache/cache_twoqueue.go index 5af9fcebc109e..fc6064284b8ee 100644 --- a/modules/cache/cache_twoqueue.go +++ b/modules/cache/cache_twoqueue.go @@ -5,11 +5,12 @@ package cache import ( - "encoding/json" "strconv" "sync" "time" + "code.gitea.io/gitea/modules/json" + mc "gitea.com/go-chi/cache" lru "github.com/hashicorp/golang-lru" ) diff --git a/modules/context/context.go b/modules/context/context.go index 9adf4b8695110..6e26f8b2f035d 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -9,7 +9,8 @@ import ( "context" "crypto/sha256" "encoding/hex" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "html" diff --git a/modules/eventsource/event.go b/modules/eventsource/event.go index 1a37dce00b88a..fd957f639ed27 100644 --- a/modules/eventsource/event.go +++ b/modules/eventsource/event.go @@ -6,7 +6,8 @@ package eventsource import ( "bytes" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "io" diff --git a/modules/httplib/httplib.go b/modules/httplib/httplib.go index 03d0ef80bd208..fd380fe88fc29 100644 --- a/modules/httplib/httplib.go +++ b/modules/httplib/httplib.go @@ -10,7 +10,6 @@ import ( "context" "crypto/tls" - "encoding/json" "encoding/xml" "io" "io/ioutil" @@ -25,6 +24,8 @@ import ( "strings" "sync" "time" + + "code.gitea.io/gitea/modules/json" ) var defaultSetting = Settings{false, "GiteaServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go index 1bdbdc003fdd8..f88570e041019 100644 --- a/modules/indexer/code/elastic_search.go +++ b/modules/indexer/code/elastic_search.go @@ -7,7 +7,8 @@ package code import ( "bufio" "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "io" diff --git a/modules/json/json.go b/modules/json/json.go index 35e53d0975ab3..be42b6ae6c441 100644 --- a/modules/json/json.go +++ b/modules/json/json.go @@ -121,3 +121,22 @@ func NewDecoder(reader io.Reader) Decoder { func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { return DefaultJSONHandler.Indent(dst, src, prefix, indent) } + +// MarshalIndent copied from encoding/json +func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { + b, err := Marshal(v) + if err != nil { + return nil, err + } + var buf bytes.Buffer + err = Indent(&buf, b, prefix, indent) + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +// Valid proxy to json.Valid +func Valid(data []byte) bool { + return json.Valid(data) +} diff --git a/modules/log/conn.go b/modules/log/conn.go index 988bb3c3840a6..9360fddac1995 100644 --- a/modules/log/conn.go +++ b/modules/log/conn.go @@ -6,10 +6,11 @@ package log import ( - "encoding/json" "fmt" "io" "net" + + "code.gitea.io/gitea/modules/json" ) type connWriter struct { diff --git a/modules/log/console.go b/modules/log/console.go index 21ab81383728d..0acd216db2777 100644 --- a/modules/log/console.go +++ b/modules/log/console.go @@ -6,10 +6,11 @@ package log import ( - "encoding/json" "fmt" "io" "os" + + "code.gitea.io/gitea/modules/json" ) // CanColorStdout reports if we can color the Stdout diff --git a/modules/log/file.go b/modules/log/file.go index 1bc3638944dbb..d9a5da8ca4656 100644 --- a/modules/log/file.go +++ b/modules/log/file.go @@ -7,7 +7,8 @@ package log import ( "bufio" "compress/gzip" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "errors" "fmt" diff --git a/modules/log/level.go b/modules/log/level.go index 892b24b8c7df1..c5096051774b0 100644 --- a/modules/log/level.go +++ b/modules/log/level.go @@ -6,7 +6,8 @@ package log import ( "bytes" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "os" diff --git a/modules/log/level_test.go b/modules/log/level_test.go index 422b430688561..0f46579b6b8da 100644 --- a/modules/log/level_test.go +++ b/modules/log/level_test.go @@ -5,10 +5,11 @@ package log import ( - "encoding/json" "fmt" "testing" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) diff --git a/modules/log/smtp.go b/modules/log/smtp.go index 9f2326b9351c6..d41a0c376cfdd 100644 --- a/modules/log/smtp.go +++ b/modules/log/smtp.go @@ -6,10 +6,11 @@ package log import ( - "encoding/json" "fmt" "net/smtp" "strings" + + "code.gitea.io/gitea/modules/json" ) type smtpWriter struct { diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 98f66187b2ddd..db22b565468bd 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -5,11 +5,12 @@ package action import ( - "encoding/json" "fmt" "path" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/base" diff --git a/modules/private/restore_repo.go b/modules/private/restore_repo.go index a523ddc29456d..0ff23f298d0e4 100644 --- a/modules/private/restore_repo.go +++ b/modules/private/restore_repo.go @@ -6,7 +6,8 @@ package private import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "io/ioutil" diff --git a/modules/queue/helper.go b/modules/queue/helper.go index 294184aaa81ce..7d2bb016bcd3c 100644 --- a/modules/queue/helper.go +++ b/modules/queue/helper.go @@ -5,8 +5,9 @@ package queue import ( - "encoding/json" "reflect" + + "code.gitea.io/gitea/modules/json" ) // Mappable represents an interface that can MapTo another interface diff --git a/modules/queue/manager.go b/modules/queue/manager.go index c6faa21acd5c0..1c09b1272978d 100644 --- a/modules/queue/manager.go +++ b/modules/queue/manager.go @@ -6,7 +6,8 @@ package queue import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "reflect" diff --git a/modules/queue/queue_bytefifo.go b/modules/queue/queue_bytefifo.go index 84f684b4327d7..f47741efd6611 100644 --- a/modules/queue/queue_bytefifo.go +++ b/modules/queue/queue_bytefifo.go @@ -6,7 +6,8 @@ package queue import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "sync" diff --git a/modules/queue/queue_test.go b/modules/queue/queue_test.go index 3608f68d3d424..742f81c65eb69 100644 --- a/modules/queue/queue_test.go +++ b/modules/queue/queue_test.go @@ -5,9 +5,10 @@ package queue import ( - "encoding/json" "testing" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) diff --git a/modules/queue/setting.go b/modules/queue/setting.go index 9ee1af8c7d8d7..a32bee45c608c 100644 --- a/modules/queue/setting.go +++ b/modules/queue/setting.go @@ -5,10 +5,11 @@ package queue import ( - "encoding/json" "fmt" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) diff --git a/modules/recaptcha/recaptcha.go b/modules/recaptcha/recaptcha.go index 8057575ee1cd1..d88d4a6a4f4ef 100644 --- a/modules/recaptcha/recaptcha.go +++ b/modules/recaptcha/recaptcha.go @@ -6,7 +6,8 @@ package recaptcha import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "io/ioutil" diff --git a/modules/session/virtual.go b/modules/session/virtual.go index 9da62348073f5..59f6686cae5b8 100644 --- a/modules/session/virtual.go +++ b/modules/session/virtual.go @@ -5,10 +5,11 @@ package session import ( - "encoding/json" "fmt" "sync" + "code.gitea.io/gitea/modules/json" + "gitea.com/go-chi/session" couchbase "gitea.com/go-chi/session/couchbase" memcache "gitea.com/go-chi/session/memcache" diff --git a/modules/setting/log.go b/modules/setting/log.go index a2cafd067982c..d63b2e10177cf 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -5,7 +5,6 @@ package setting import ( - "encoding/json" "fmt" golog "log" "os" @@ -14,6 +13,8 @@ import ( "strings" "sync" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/log" ini "gopkg.in/ini.v1" diff --git a/modules/setting/session.go b/modules/setting/session.go index 38bcd4ac9f84b..78d4d49cef775 100644 --- a/modules/setting/session.go +++ b/modules/setting/session.go @@ -5,12 +5,13 @@ package setting import ( - "encoding/json" "net/http" "path" "path/filepath" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/log" ) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index ee03b8e2e38b3..536fa3d1ec8c5 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -7,7 +7,8 @@ package setting import ( "encoding/base64" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "io" diff --git a/modules/setting/setting_test.go b/modules/setting/setting_test.go index f61dedd4069b3..0a0caf5cc47d0 100644 --- a/modules/setting/setting_test.go +++ b/modules/setting/setting_test.go @@ -5,9 +5,10 @@ package setting import ( - "encoding/json" "testing" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) diff --git a/modules/storage/helper.go b/modules/storage/helper.go index 63c80495690a4..a9d95e40caf17 100644 --- a/modules/storage/helper.go +++ b/modules/storage/helper.go @@ -5,8 +5,9 @@ package storage import ( - "encoding/json" "reflect" + + "code.gitea.io/gitea/modules/json" ) // Mappable represents an interface that can MapTo another interface diff --git a/modules/structs/hook.go b/modules/structs/hook.go index e030a4527b48b..9a0ce12c82a1b 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -6,10 +6,11 @@ package structs import ( - "encoding/json" "errors" "strings" "time" + + "code.gitea.io/gitea/modules/json" ) var ( diff --git a/modules/structs/user.go b/modules/structs/user.go index 9e747deb6d59e..06d4d9f6cbd73 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -5,8 +5,9 @@ package structs import ( - "encoding/json" "time" + + "code.gitea.io/gitea/modules/json" ) // User represents a user diff --git a/modules/task/migrate.go b/modules/task/migrate.go index 4f22df979f127..a06fbd53d3968 100644 --- a/modules/task/migrate.go +++ b/modules/task/migrate.go @@ -6,7 +6,8 @@ package task import ( "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "errors" "fmt" diff --git a/modules/task/task.go b/modules/task/task.go index b71447ae5a6ec..69b97272183b1 100644 --- a/modules/task/task.go +++ b/modules/task/task.go @@ -5,9 +5,10 @@ package task import ( - "encoding/json" "fmt" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 4cfa01fcbcf40..1331756d3f229 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -5,11 +5,12 @@ package utils import ( - "encoding/json" "fmt" "net/http" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" diff --git a/routers/private/mail.go b/routers/private/mail.go index 6576297dd3f52..202ba2699e3dc 100644 --- a/routers/private/mail.go +++ b/routers/private/mail.go @@ -5,11 +5,12 @@ package private import ( - "encoding/json" "fmt" "net/http" "strconv" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" diff --git a/routers/private/manager.go b/routers/private/manager.go index 03124321f4042..bc42d29bf9655 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -5,10 +5,11 @@ package private import ( - "encoding/json" "fmt" "net/http" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" diff --git a/routers/private/restore_repo.go b/routers/private/restore_repo.go index f6c5bf36c4e14..0db0d26b4a0b7 100644 --- a/routers/private/restore_repo.go +++ b/routers/private/restore_repo.go @@ -5,10 +5,11 @@ package private import ( - "encoding/json" "io/ioutil" "net/http" + "code.gitea.io/gitea/modules/json" + myCtx "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/private" diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 55549d5597755..73b4e7a8e40b2 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -6,7 +6,6 @@ package admin import ( - "encoding/json" "fmt" "net/http" "net/url" @@ -16,6 +15,8 @@ import ( "strings" "time" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" diff --git a/routers/web/events/events.go b/routers/web/events/events.go index f854dfdb35b04..126a10cc2b926 100644 --- a/routers/web/events/events.go +++ b/routers/web/events/events.go @@ -5,10 +5,11 @@ package events import ( - "encoding/json" "net/http" "time" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index ba062e25413ce..490e1645222d0 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -5,13 +5,14 @@ package repo import ( - "encoding/json" "fmt" "io/ioutil" "net/http" "path" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/charset" diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index e0a17d40a4150..9830742b9b79a 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -6,13 +6,14 @@ package repo import ( - "encoding/json" "errors" "fmt" "net/http" "path" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 57c94bb5c6b7e..ad4e5edbadc9e 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -7,7 +7,8 @@ package user import ( "bytes" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "net/http" diff --git a/routers/web/user/task.go b/routers/web/user/task.go index e4764e456d06c..403a4d7f31b32 100644 --- a/routers/web/user/task.go +++ b/routers/web/user/task.go @@ -5,9 +5,10 @@ package user import ( - "encoding/json" "net/http" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" ) diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 5e26dfa581d97..03a63947ec9f6 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -6,13 +6,14 @@ package gitdiff import ( - "encoding/json" "fmt" "html/template" "strconv" "strings" "testing" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/setting" diff --git a/services/pull/pull.go b/services/pull/pull.go index a98782a00eda5..9ef0bcd314ffe 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -8,7 +8,8 @@ import ( "bufio" "bytes" "context" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "fmt" "regexp" diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 35f8eba79b8e1..4170c4ea9609a 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -5,10 +5,11 @@ package webhook import ( - "encoding/json" "fmt" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 696a2312937af..5b7de3ed8e22c 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -5,12 +5,13 @@ package webhook import ( - "encoding/json" "errors" "fmt" "strconv" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 470f0f742c443..278ae369a9eeb 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -5,10 +5,11 @@ package webhook import ( - "encoding/json" "fmt" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 90636c879e3ad..f93860e05911a 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -6,7 +6,8 @@ package webhook import ( "crypto/sha1" - "encoding/json" + + "code.gitea.io/gitea/modules/json" "errors" "fmt" diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 426cb808ac148..0f5dacc63516f 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -5,10 +5,11 @@ package webhook import ( - "encoding/json" "fmt" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 30a05711e64b8..0f2e3b3e4af59 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -5,11 +5,12 @@ package webhook import ( - "encoding/json" "errors" "fmt" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 07107f6375d1d..5b1030e95072c 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -5,10 +5,11 @@ package webhook import ( - "encoding/json" "fmt" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index 1e2d5f4a10975..0852e4e6831f8 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -5,10 +5,11 @@ package webhook import ( - "encoding/json" "fmt" "strings" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" From 0e961e9c08dac570f7a7c832d5b6b48cc59a7574 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Jul 2021 11:11:08 +0800 Subject: [PATCH 3/6] Fix import sequence --- cmd/dump.go | 4 +--- cmd/serv.go | 3 +-- integrations/api_admin_test.go | 4 +--- integrations/api_helper_for_declarative_test.go | 4 +--- integrations/api_pull_review_test.go | 5 ++--- integrations/api_repo_lfs_test.go | 4 +--- integrations/create_no_session_test.go | 1 - integrations/integration_test.go | 4 +--- integrations/oauth_test.go | 4 +--- integrations/repo_commits_test.go | 1 - integrations/testlogger.go | 4 +--- models/issue_comment.go | 4 +--- models/migrations/testlogger_test.go | 4 +--- models/migrations/v130.go | 2 -- models/migrations/v180.go | 2 -- models/repo_unit.go | 6 ------ models/task.go | 3 --- models/user_heatmap_test.go | 1 - models/webhook.go | 8 +------- models/webhook_test.go | 5 +---- modules/cache/cache_twoqueue.go | 1 - modules/context/context.go | 5 +---- modules/eventsource/event.go | 7 ++----- modules/httplib/httplib.go | 2 -- modules/indexer/code/elastic_search.go | 5 +---- modules/log/conn.go | 1 - modules/log/console.go | 1 - modules/log/file.go | 5 +---- modules/log/level.go | 6 ++---- modules/log/level_test.go | 1 - modules/log/smtp.go | 1 - modules/notification/action/action.go | 5 +---- modules/private/hook.go | 1 - modules/private/internal.go | 2 -- modules/private/mail.go | 2 -- modules/private/manager.go | 3 --- modules/private/restore_repo.go | 5 +---- modules/private/serv.go | 2 -- modules/queue/helper.go | 4 ---- modules/queue/manager.go | 4 +--- modules/queue/queue_bytefifo.go | 6 +----- modules/queue/queue_test.go | 1 - modules/queue/setting.go | 2 -- modules/recaptcha/recaptcha.go | 6 ++---- modules/session/virtual.go | 1 - modules/setting/log.go | 3 --- modules/setting/session.go | 2 -- modules/setting/setting.go | 4 +--- modules/setting/setting_test.go | 1 - modules/storage/helper.go | 2 -- modules/structs/hook.go | 11 ----------- modules/structs/user.go | 1 - modules/task/migrate.go | 5 +---- modules/task/task.go | 4 +--- modules/templates/helper.go | 7 ++----- routers/api/v1/utils/hook.go | 4 +--- routers/private/mail.go | 3 +-- routers/private/manager.go | 4 +--- routers/private/restore_repo.go | 4 +--- routers/web/admin/admin.go | 4 +--- routers/web/events/events.go | 4 +--- routers/web/repo/editor.go | 4 +--- routers/web/repo/webhook.go | 3 +-- routers/web/user/home.go | 5 +---- routers/web/user/task.go | 4 +--- services/gitdiff/gitdiff_test.go | 4 ++-- services/pull/pull.go | 4 +--- services/webhook/dingtalk.go | 4 +--- services/webhook/discord.go | 6 +----- services/webhook/feishu.go | 4 +--- services/webhook/matrix.go | 8 +------- services/webhook/msteams.go | 4 +--- services/webhook/slack.go | 6 +----- services/webhook/telegram.go | 5 +---- services/webhook/wechatwork.go | 3 +-- 75 files changed, 53 insertions(+), 226 deletions(-) diff --git a/cmd/dump.go b/cmd/dump.go index 2cedd5aed7912..9ce7c2636a0d2 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -14,9 +14,8 @@ import ( "strings" "time" - "code.gitea.io/gitea/modules/json" - "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" @@ -307,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 - if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil { return err } diff --git a/cmd/serv.go b/cmd/serv.go index e53e79f77554b..4479902ea1ef5 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -16,9 +16,8 @@ import ( "strings" "time" - "code.gitea.io/gitea/modules/json" - "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" diff --git a/integrations/api_admin_test.go b/integrations/api_admin_test.go index 15e7acd654d63..da6a89c9746e1 100644 --- a/integrations/api_admin_test.go +++ b/integrations/api_admin_test.go @@ -9,9 +9,8 @@ import ( "net/http" "testing" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -191,7 +190,6 @@ func TestAPIEditUser(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusUnprocessableEntity) errMap := make(map[string]interface{}) - json.Unmarshal(resp.Body.Bytes(), &errMap) assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string)) diff --git a/integrations/api_helper_for_declarative_test.go b/integrations/api_helper_for_declarative_test.go index 024b96a7caed4..5d44cde5bfd79 100644 --- a/integrations/api_helper_for_declarative_test.go +++ b/integrations/api_helper_for_declarative_test.go @@ -6,9 +6,6 @@ package integrations import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "io/ioutil" "net/http" @@ -17,6 +14,7 @@ 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" diff --git a/integrations/api_pull_review_test.go b/integrations/api_pull_review_test.go index b0958699c3d45..b0e380376bcb7 100644 --- a/integrations/api_pull_review_test.go +++ b/integrations/api_pull_review_test.go @@ -9,10 +9,10 @@ import ( "net/http" "testing" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" + "github.com/stretchr/testify/assert" ) @@ -190,7 +190,6 @@ func TestAPIPullReview(t *testing.T) { }) resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity) errMap := make(map[string]interface{}) - json.Unmarshal(resp.Body.Bytes(), &errMap) assert.EqualValues(t, "review event COMMENT requires a body or a comment", errMap["message"].(string)) diff --git a/integrations/api_repo_lfs_test.go b/integrations/api_repo_lfs_test.go index a6dbe6f2e1a0e..7aa172064e049 100644 --- a/integrations/api_repo_lfs_test.go +++ b/integrations/api_repo_lfs_test.go @@ -6,9 +6,6 @@ package integrations import ( "bytes" - - "code.gitea.io/gitea/modules/json" - "net/http" "path" "strconv" @@ -16,6 +13,7 @@ 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" diff --git a/integrations/create_no_session_test.go b/integrations/create_no_session_test.go index fb7bba9e8fceb..d6b363426821b 100644 --- a/integrations/create_no_session_test.go +++ b/integrations/create_no_session_test.go @@ -13,7 +13,6 @@ import ( "testing" "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers" diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 3af8bb98b537a..7ddcf3e1cde46 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -8,9 +8,6 @@ import ( "bytes" "context" "database/sql" - - "code.gitea.io/gitea/modules/json" - "fmt" "hash" "hash/fnv" @@ -31,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" diff --git a/integrations/oauth_test.go b/integrations/oauth_test.go index 99eddd77b0fb7..c97db777be1ac 100644 --- a/integrations/oauth_test.go +++ b/integrations/oauth_test.go @@ -6,12 +6,10 @@ package integrations import ( "bytes" - - "code.gitea.io/gitea/modules/json" - "io/ioutil" "testing" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index f467f5955d171..3e49a786a139f 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -11,7 +11,6 @@ import ( "testing" "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/integrations/testlogger.go b/integrations/testlogger.go index d67c7316bd68a..ca99dc09cbde0 100644 --- a/integrations/testlogger.go +++ b/integrations/testlogger.go @@ -6,9 +6,6 @@ package integrations import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "os" "runtime" @@ -17,6 +14,7 @@ import ( "testing" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" ) diff --git a/models/issue_comment.go b/models/issue_comment.go index 5675b9cb6ef2d..ae2dc0811fd08 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -8,9 +8,6 @@ package models import ( "container/list" - - "code.gitea.io/gitea/modules/json" - "fmt" "regexp" "strconv" @@ -18,6 +15,7 @@ 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" diff --git a/models/migrations/testlogger_test.go b/models/migrations/testlogger_test.go index 775fffb1928d0..6f4a91e1c2dc1 100644 --- a/models/migrations/testlogger_test.go +++ b/models/migrations/testlogger_test.go @@ -6,9 +6,6 @@ package migrations import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "os" "runtime" @@ -17,6 +14,7 @@ import ( "testing" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" ) diff --git a/models/migrations/v130.go b/models/migrations/v130.go index d57a7bb5b31d6..7ffed3fd17672 100644 --- a/models/migrations/v130.go +++ b/models/migrations/v130.go @@ -6,7 +6,6 @@ package migrations import ( "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/setting" "xorm.io/xorm" @@ -71,7 +70,6 @@ func expandWebhooks(x *xorm.Engine) error { for _, res := range results { var events HookEvent - if err = json.Unmarshal([]byte(res.Events), &events); err != nil { return err } diff --git a/models/migrations/v180.go b/models/migrations/v180.go index 4b6ccece2ec00..1b735c2035b44 100644 --- a/models/migrations/v180.go +++ b/models/migrations/v180.go @@ -6,7 +6,6 @@ package migrations import ( "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/util" "xorm.io/builder" @@ -106,7 +105,6 @@ func removeCredentials(payload string) (string, error) { } var opts MigrateOptions - err := json.Unmarshal([]byte(payload), &opts) if err != nil { return "", err diff --git a/models/repo_unit.go b/models/repo_unit.go index ac26dd5b4d8b2..2a20205fe4962 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -8,7 +8,6 @@ import ( "fmt" "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/timeutil" "xorm.io/xorm" @@ -34,7 +33,6 @@ func (cfg *UnitConfig) FromDB(bs []byte) error { // ToDB exports a UnitConfig to a serialized format. func (cfg *UnitConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) } @@ -50,7 +48,6 @@ func (cfg *ExternalWikiConfig) FromDB(bs []byte) error { // ToDB exports a ExternalWikiConfig to a serialized format. func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) } @@ -68,7 +65,6 @@ func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error { // ToDB exports a ExternalTrackerConfig to a serialized format. func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) } @@ -86,7 +82,6 @@ func (cfg *IssuesConfig) FromDB(bs []byte) error { // ToDB exports a IssuesConfig to a serialized format. func (cfg *IssuesConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) } @@ -110,7 +105,6 @@ func (cfg *PullRequestsConfig) FromDB(bs []byte) error { // ToDB exports a PullRequestsConfig to a serialized format. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) } diff --git a/models/task.go b/models/task.go index 7a3c870380104..e3c143127bdc7 100644 --- a/models/task.go +++ b/models/task.go @@ -8,7 +8,6 @@ import ( "fmt" "code.gitea.io/gitea/modules/json" - migration "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" @@ -115,7 +114,6 @@ func (task *Task) UpdateCols(cols ...string) error { func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) { if task.Type == structs.TaskTypeMigrateRepo { var opts migration.MigrateOptions - err := json.Unmarshal([]byte(task.PayloadContent), &opts) if err != nil { return nil, err @@ -191,7 +189,6 @@ func GetMigratingTaskByID(id, doerID int64) (*Task, *migration.MigrateOptions, e } var opts migration.MigrateOptions - if err := json.Unmarshal([]byte(task.PayloadContent), &opts); err != nil { return nil, nil, err } diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go index dc1d56628e958..bdf77666528c0 100644 --- a/models/user_heatmap_test.go +++ b/models/user_heatmap_test.go @@ -69,7 +69,6 @@ func TestGetUserHeatmapDataByUser(t *testing.T) { assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase %d", i)) // Test JSON rendering - jsonData, err := json.Marshal(heatmap) assert.NoError(t, err) assert.Equal(t, tc.JSONResult, string(jsonData)) diff --git a/models/webhook.go b/models/webhook.go index bbcb8c86d1884..138ba26bde090 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -7,13 +7,11 @@ package models import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "strings" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -162,7 +160,6 @@ type Webhook struct { // AfterLoad updates the webhook object upon setting a column func (w *Webhook) AfterLoad() { w.HookEvent = &HookEvent{} - if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { log.Error("Unmarshal[%d]: %v", w.ID, err) } @@ -175,7 +172,6 @@ func (w *Webhook) History(page int) ([]*HookTask, error) { // UpdateEvent handles conversion from HookEvent to Events. func (w *Webhook) UpdateEvent() error { - data, err := json.Marshal(w.HookEvent) w.Events = string(data) return err @@ -688,7 +684,6 @@ func (t *HookTask) AfterLoad() { } t.RequestInfo = &HookRequest{} - if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err) } @@ -702,7 +697,6 @@ func (t *HookTask) AfterLoad() { } func (t *HookTask) simpleMarshalJSON(v interface{}) string { - p, err := json.Marshal(v) if err != nil { log.Error("Marshal [%d]: %v", t.ID, err) diff --git a/models/webhook_test.go b/models/webhook_test.go index a56d07744a29a..84520666c0b22 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -6,12 +6,10 @@ package models import ( "context" - - "code.gitea.io/gitea/modules/json" - "testing" "time" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -60,7 +58,6 @@ func TestWebhook_UpdateEvent(t *testing.T) { assert.NoError(t, webhook.UpdateEvent()) assert.NotEmpty(t, webhook.Events) actualHookEvent := &HookEvent{} - assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent)) assert.Equal(t, *hookEvent, *actualHookEvent) } diff --git a/modules/cache/cache_twoqueue.go b/modules/cache/cache_twoqueue.go index fc6064284b8ee..275b73f068f29 100644 --- a/modules/cache/cache_twoqueue.go +++ b/modules/cache/cache_twoqueue.go @@ -178,7 +178,6 @@ func (c *TwoQueueCache) StartAndGC(opts mc.Options) error { size, err = strconv.Atoi(opts.AdapterConfig) } if err != nil { - if !json.Valid([]byte(opts.AdapterConfig)) { return err } diff --git a/modules/context/context.go b/modules/context/context.go index 6e26f8b2f035d..6973f0dddc183 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -9,9 +9,6 @@ import ( "context" "crypto/sha256" "encoding/hex" - - "code.gitea.io/gitea/modules/json" - "fmt" "html" "html/template" @@ -26,6 +23,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" mc "code.gitea.io/gitea/modules/cache" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/templates" @@ -410,7 +408,6 @@ func (ctx *Context) Error(status int, contents ...string) { func (ctx *Context) JSON(status int, content interface{}) { ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8") ctx.Resp.WriteHeader(status) - if err := json.NewEncoder(ctx.Resp).Encode(content); err != nil { ctx.ServerError("Render JSON failed", err) } diff --git a/modules/eventsource/event.go b/modules/eventsource/event.go index fd957f639ed27..8fc3221c181c9 100644 --- a/modules/eventsource/event.go +++ b/modules/eventsource/event.go @@ -6,13 +6,12 @@ package eventsource import ( "bytes" - - "code.gitea.io/gitea/modules/json" - "fmt" "io" "strings" "time" + + "code.gitea.io/gitea/modules/json" ) func wrapNewlines(w io.Writer, prefix []byte, value []byte) (sum int64, err error) { @@ -81,7 +80,6 @@ func (e *Event) WriteTo(w io.Writer) (int64, error) { data = []byte(v) default: var err error - data, err = json.Marshal(e.Data) if err != nil { return sum, err @@ -92,7 +90,6 @@ func (e *Event) WriteTo(w io.Writer) (int64, error) { if err != nil { return sum, err } - } n, err = wrapNewlines(w, []byte("id: "), []byte(e.ID)) diff --git a/modules/httplib/httplib.go b/modules/httplib/httplib.go index fd380fe88fc29..aecdd4346cdbb 100644 --- a/modules/httplib/httplib.go +++ b/modules/httplib/httplib.go @@ -9,7 +9,6 @@ import ( "bytes" "context" "crypto/tls" - "encoding/xml" "io" "io/ioutil" @@ -444,7 +443,6 @@ func (r *Request) ToJSON(v interface{}) error { if err != nil { return err } - err = json.Unmarshal(data, v) return err } diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go index f88570e041019..a7a243e24e13e 100644 --- a/modules/indexer/code/elastic_search.go +++ b/modules/indexer/code/elastic_search.go @@ -7,9 +7,6 @@ package code import ( "bufio" "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "io" "io/ioutil" @@ -21,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -323,7 +321,6 @@ func convertResult(searchResult *elastic.SearchResult, kw string, pageSize int) repoID, fileName := parseIndexerID(hit.Id) var res = make(map[string]interface{}) - if err := json.Unmarshal(hit.Source, &res); err != nil { return 0, nil, nil, err } diff --git a/modules/log/conn.go b/modules/log/conn.go index 9360fddac1995..3e32c81d7f958 100644 --- a/modules/log/conn.go +++ b/modules/log/conn.go @@ -106,7 +106,6 @@ func NewConn() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConnLogger) Init(jsonconfig string) error { - err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/log/console.go b/modules/log/console.go index 0acd216db2777..7ecdd5c3b8e79 100644 --- a/modules/log/console.go +++ b/modules/log/console.go @@ -52,7 +52,6 @@ func NewConsoleLogger() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConsoleLogger) Init(config string) error { - err := json.Unmarshal([]byte(config), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/log/file.go b/modules/log/file.go index d9a5da8ca4656..bc9d7417246cc 100644 --- a/modules/log/file.go +++ b/modules/log/file.go @@ -7,9 +7,6 @@ package log import ( "bufio" "compress/gzip" - - "code.gitea.io/gitea/modules/json" - "errors" "fmt" "os" @@ -18,6 +15,7 @@ import ( "sync" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/util" ) @@ -103,7 +101,6 @@ func NewFileLogger() LoggerProvider { // "rotate":true // } func (log *FileLogger) Init(config string) error { - if err := json.Unmarshal([]byte(config), log); err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) } diff --git a/modules/log/level.go b/modules/log/level.go index c5096051774b0..5964ed8f8b956 100644 --- a/modules/log/level.go +++ b/modules/log/level.go @@ -6,12 +6,11 @@ package log import ( "bytes" - - "code.gitea.io/gitea/modules/json" - "fmt" "os" "strings" + + "code.gitea.io/gitea/modules/json" ) // Level is the level of the logger @@ -105,7 +104,6 @@ func FromString(level string) Level { // UnmarshalJSON takes text and turns it into a Level func (l *Level) UnmarshalJSON(b []byte) error { var tmp interface{} - err := json.Unmarshal(b, &tmp) if err != nil { fmt.Fprintf(os.Stderr, "Err: %v", err) diff --git a/modules/log/level_test.go b/modules/log/level_test.go index 0f46579b6b8da..47f77738c5f14 100644 --- a/modules/log/level_test.go +++ b/modules/log/level_test.go @@ -18,7 +18,6 @@ type testLevel struct { } func TestLevelMarshalUnmarshalJSON(t *testing.T) { - levelBytes, err := json.Marshal(testLevel{ Level: INFO, }) diff --git a/modules/log/smtp.go b/modules/log/smtp.go index d41a0c376cfdd..60dced37067cc 100644 --- a/modules/log/smtp.go +++ b/modules/log/smtp.go @@ -57,7 +57,6 @@ func NewSMTPLogger() LoggerProvider { // "level":LevelError // } func (log *SMTPLogger) Init(jsonconfig string) error { - err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index db22b565468bd..772e7be13705d 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -9,9 +9,8 @@ import ( "path" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/repository" @@ -297,7 +296,6 @@ func (*actionNotifier) NotifyPullRevieweDismiss(doer *models.User, review *model } func (a *actionNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - data, err := json.Marshal(commits) if err != nil { log.Error("Marshal: %v", err) @@ -369,7 +367,6 @@ func (a *actionNotifier) NotifyDeleteRef(doer *models.User, repo *models.Reposit } func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - data, err := json.Marshal(commits) if err != nil { log.Error("json.Marshal: %v", err) diff --git a/modules/private/hook.go b/modules/private/hook.go index 61dafd2ca8290..9596f5f4dac0d 100644 --- a/modules/private/hook.go +++ b/modules/private/hook.go @@ -113,7 +113,6 @@ func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookO req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") req.SetTimeout(60*time.Second, time.Duration(60+len(opts.OldCommitIDs))*time.Second) - jsonBytes, _ := json.Marshal(opts) req.Body(jsonBytes) resp, err := req.Response() diff --git a/modules/private/internal.go b/modules/private/internal.go index 1589da76aa67b..f5b5db0ca1b50 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -7,7 +7,6 @@ package private import ( "context" "crypto/tls" - "fmt" "net" "net/http" @@ -31,7 +30,6 @@ type Response struct { func decodeJSONError(resp *http.Response) *Response { var res Response - err := json.NewDecoder(resp.Body).Decode(&res) if err != nil { res.Err = err.Error() diff --git a/modules/private/mail.go b/modules/private/mail.go index fa390a1962686..7e229396e31ee 100644 --- a/modules/private/mail.go +++ b/modules/private/mail.go @@ -6,7 +6,6 @@ package private import ( "context" - "fmt" "io/ioutil" "net/http" @@ -34,7 +33,6 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (int, req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - jsonBytes, _ := json.Marshal(Email{ Subject: subject, Message: message, diff --git a/modules/private/manager.go b/modules/private/manager.go index 2b34b82249efd..337b0dac64c94 100644 --- a/modules/private/manager.go +++ b/modules/private/manager.go @@ -6,7 +6,6 @@ package private import ( "context" - "fmt" "net/http" "net/url" @@ -67,7 +66,6 @@ func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) ( req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second) } req = req.Header("Content-Type", "application/json") - jsonBytes, _ := json.Marshal(FlushOptions{ Timeout: timeout, NonBlocking: nonBlocking, @@ -154,7 +152,6 @@ func AddLogger(ctx context.Context, group, name, mode string, config map[string] req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - jsonBytes, _ := json.Marshal(LoggerOptions{ Group: group, Name: name, diff --git a/modules/private/restore_repo.go b/modules/private/restore_repo.go index 0ff23f298d0e4..feb2e1d141a07 100644 --- a/modules/private/restore_repo.go +++ b/modules/private/restore_repo.go @@ -6,14 +6,12 @@ package private import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "io/ioutil" "net/http" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" ) @@ -32,7 +30,6 @@ func RestoreRepo(ctx context.Context, repoDir, ownerName, repoName string, units req := newInternalRequest(ctx, reqURL, "POST") req.SetTimeout(3*time.Second, 0) // since the request will spend much time, don't timeout req = req.Header("Content-Type", "application/json") - jsonBytes, _ := json.Marshal(RestoreParams{ RepoDir: repoDir, OwnerName: ownerName, diff --git a/modules/private/serv.go b/modules/private/serv.go index d8a4774909d2f..c378dc6dc7689 100644 --- a/modules/private/serv.go +++ b/modules/private/serv.go @@ -6,7 +6,6 @@ package private import ( "context" - "fmt" "net/http" "net/url" @@ -36,7 +35,6 @@ func ServNoCommand(ctx context.Context, keyID int64) (*models.PublicKey, *models } var keyAndOwner KeyAndOwner - if err := json.NewDecoder(resp.Body).Decode(&keyAndOwner); err != nil { return nil, nil, err } diff --git a/modules/queue/helper.go b/modules/queue/helper.go index 7d2bb016bcd3c..f1aba411a8564 100644 --- a/modules/queue/helper.go +++ b/modules/queue/helper.go @@ -20,7 +20,6 @@ type Mappable interface { // It will tolerate the cfg being passed as a []byte or string of a json representation of the // exemplar or the correct type of the exemplar itself func toConfig(exemplar, cfg interface{}) (interface{}, error) { - // First of all check if we've got the same type as the exemplar - if so it's all fine. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) { return cfg, nil @@ -47,7 +46,6 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) { if !ok { // hmm ... can we marshal it to json? var err error - configBytes, err = json.Marshal(cfg) ok = err == nil } @@ -67,7 +65,6 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) { // unmarshalAs will attempt to unmarshal provided bytes as the provided exemplar func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) { - if exemplar != nil { t := reflect.TypeOf(exemplar) n := reflect.New(t) @@ -77,7 +74,6 @@ func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) { } else { err = json.Unmarshal(bs, &data) } - return } diff --git a/modules/queue/manager.go b/modules/queue/manager.go index 1c09b1272978d..a88933191aabf 100644 --- a/modules/queue/manager.go +++ b/modules/queue/manager.go @@ -6,15 +6,13 @@ package queue import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "reflect" "sort" "sync" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" ) diff --git a/modules/queue/queue_bytefifo.go b/modules/queue/queue_bytefifo.go index f47741efd6611..edde47a62d627 100644 --- a/modules/queue/queue_bytefifo.go +++ b/modules/queue/queue_bytefifo.go @@ -6,13 +6,11 @@ package queue import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "sync" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" ) @@ -85,7 +83,6 @@ func (q *ByteFIFOQueue) PushFunc(data Data, fn func() error) error { if !assignableTo(data, q.exemplar) { return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name) } - bs, err := json.Marshal(data) if err != nil { return err @@ -311,7 +308,6 @@ func (q *ByteFIFOUniqueQueue) Has(data Data) (bool, error) { if !assignableTo(data, q.exemplar) { return false, fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name) } - bs, err := json.Marshal(data) if err != nil { return false, err diff --git a/modules/queue/queue_test.go b/modules/queue/queue_test.go index 742f81c65eb69..65fb816537ded 100644 --- a/modules/queue/queue_test.go +++ b/modules/queue/queue_test.go @@ -30,7 +30,6 @@ func TestToConfig(t *testing.T) { assert.True(t, ok) assert.NotEqual(t, cfg2, exemplar) assert.Equal(t, &cfg, &cfg2) - cfgString, err := json.Marshal(cfg) assert.NoError(t, err) diff --git a/modules/queue/setting.go b/modules/queue/setting.go index a32bee45c608c..0e6ff3a0a8f1f 100644 --- a/modules/queue/setting.go +++ b/modules/queue/setting.go @@ -9,7 +9,6 @@ import ( "strings" "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -28,7 +27,6 @@ func validType(t string) (Type, error) { func getQueueSettings(name string) (setting.QueueSettings, []byte) { q := setting.GetQueueSettings(name) - cfg, err := json.Marshal(q) if err != nil { log.Error("Unable to marshall generic options: %v Error: %v", q, err) diff --git a/modules/recaptcha/recaptcha.go b/modules/recaptcha/recaptcha.go index d88d4a6a4f4ef..e0f5ee2e0708e 100644 --- a/modules/recaptcha/recaptcha.go +++ b/modules/recaptcha/recaptcha.go @@ -6,15 +6,13 @@ package recaptcha import ( "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "io/ioutil" "net/http" "net/url" "strings" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" ) @@ -52,8 +50,8 @@ func Verify(ctx context.Context, response string) (bool, error) { if err != nil { return false, fmt.Errorf("Failed to read CAPTCHA response: %s", err) } - var jsonResponse Response + var jsonResponse Response err = json.Unmarshal(body, &jsonResponse) if err != nil { return false, fmt.Errorf("Failed to parse CAPTCHA response: %s", err) diff --git a/modules/session/virtual.go b/modules/session/virtual.go index 59f6686cae5b8..928dd497401af 100644 --- a/modules/session/virtual.go +++ b/modules/session/virtual.go @@ -26,7 +26,6 @@ type VirtualSessionProvider struct { // Init initializes the cookie session provider with given root path. func (o *VirtualSessionProvider) Init(gclifetime int64, config string) error { var opts session.Options - if err := json.Unmarshal([]byte(config), &opts); err != nil { return err } diff --git a/modules/setting/log.go b/modules/setting/log.go index d63b2e10177cf..ecf07114847cd 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -14,14 +14,12 @@ import ( "sync" "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/log" ini "gopkg.in/ini.v1" ) var filenameSuffix = "" - var descriptionLock = sync.RWMutex{} var logDescriptions = make(map[string]*LogDescription) @@ -204,7 +202,6 @@ func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions } logConfig["colorize"] = sec.Key("COLORIZE").MustBool(false) - byteConfig, err := json.Marshal(logConfig) if err != nil { log.Error("Failed to marshal log configuration: %v %v", logConfig, err) diff --git a/modules/setting/session.go b/modules/setting/session.go index 78d4d49cef775..9e6193e826286 100644 --- a/modules/setting/session.go +++ b/modules/setting/session.go @@ -11,7 +11,6 @@ import ( "strings" "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/log" ) @@ -66,7 +65,6 @@ func newSessionService() { default: SessionConfig.SameSite = http.SameSiteLaxMode } - shadowConfig, err := json.Marshal(SessionConfig) if err != nil { log.Fatal("Can't shadow session config: %v", err) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 536fa3d1ec8c5..593677344fd94 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -7,9 +7,6 @@ package setting import ( "encoding/base64" - - "code.gitea.io/gitea/modules/json" - "fmt" "io" "io/ioutil" @@ -27,6 +24,7 @@ import ( "time" "code.gitea.io/gitea/modules/generate" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/user" "code.gitea.io/gitea/modules/util" diff --git a/modules/setting/setting_test.go b/modules/setting/setting_test.go index 0a0caf5cc47d0..0e7f5a55ab129 100644 --- a/modules/setting/setting_test.go +++ b/modules/setting/setting_test.go @@ -29,6 +29,5 @@ func TestMakeAbsoluteAssetURL(t *testing.T) { func TestMakeManifestData(t *testing.T) { jsonBytes := MakeManifestData(`Example App '\"`, "https://example.com", "https://example.com/foo/bar") - assert.True(t, json.Valid(jsonBytes)) } diff --git a/modules/storage/helper.go b/modules/storage/helper.go index a9d95e40caf17..1a25716ccb766 100644 --- a/modules/storage/helper.go +++ b/modules/storage/helper.go @@ -20,7 +20,6 @@ type Mappable interface { // It will tolerate the cfg being passed as a []byte or string of a json representation of the // exemplar or the correct type of the exemplar itself func toConfig(exemplar, cfg interface{}) (interface{}, error) { - // First of all check if we've got the same type as the exemplar - if so it's all fine. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) { return cfg, nil @@ -47,7 +46,6 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) { if !ok { // hmm ... can we marshal it to json? var err error - configBytes, err = json.Marshal(cfg) ok = err == nil } diff --git a/modules/structs/hook.go b/modules/structs/hook.go index 9a0ce12c82a1b..163fb5c94d619 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -132,14 +132,12 @@ type CreatePayload struct { // JSONPayload return payload information func (p *CreatePayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } // ParseCreateHook parses create event hook content. func ParseCreateHook(raw []byte) (*CreatePayload, error) { hook := new(CreatePayload) - if err := json.Unmarshal(raw, hook); err != nil { return nil, err } @@ -183,7 +181,6 @@ type DeletePayload struct { // JSONPayload implements Payload func (p *DeletePayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } @@ -203,7 +200,6 @@ type ForkPayload struct { // JSONPayload implements Payload func (p *ForkPayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } @@ -230,7 +226,6 @@ type IssueCommentPayload struct { // JSONPayload implements Payload func (p *IssueCommentPayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } @@ -261,7 +256,6 @@ type ReleasePayload struct { // JSONPayload implements Payload func (p *ReleasePayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } @@ -287,14 +281,12 @@ type PushPayload struct { // JSONPayload FIXME func (p *PushPayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } // ParsePushHook parses push event hook content. func ParsePushHook(raw []byte) (*PushPayload, error) { hook := new(PushPayload) - if err := json.Unmarshal(raw, hook); err != nil { return nil, err } @@ -362,7 +354,6 @@ type IssuePayload struct { // JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces. func (p *IssuePayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } @@ -398,7 +389,6 @@ type PullRequestPayload struct { // JSONPayload FIXME func (p *PullRequestPayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } @@ -435,6 +425,5 @@ type RepositoryPayload struct { // JSONPayload JSON representation of the payload func (p *RepositoryPayload) JSONPayload() ([]byte, error) { - return json.MarshalIndent(p, "", " ") } diff --git a/modules/structs/user.go b/modules/structs/user.go index 06d4d9f6cbd73..431e230fac038 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -56,7 +56,6 @@ type User struct { func (u User) MarshalJSON() ([]byte, error) { // Re-declaring User to avoid recursion type shadow User - return json.Marshal(struct { shadow CompatUserName string `json:"username"` diff --git a/modules/task/migrate.go b/modules/task/migrate.go index a06fbd53d3968..481422403ba98 100644 --- a/modules/task/migrate.go +++ b/modules/task/migrate.go @@ -6,15 +6,13 @@ package task import ( "context" - - "code.gitea.io/gitea/modules/json" - "errors" "fmt" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" migration "code.gitea.io/gitea/modules/migrations/base" @@ -114,7 +112,6 @@ func runMigrateTask(t *models.Task) (err error) { Format: format, Args: args, } - bs, _ := json.Marshal(message) t.Message = string(bs) _ = t.UpdateCols("message") diff --git a/modules/task/task.go b/modules/task/task.go index 69b97272183b1..4e782869f93f5 100644 --- a/modules/task/task.go +++ b/modules/task/task.go @@ -7,10 +7,9 @@ package task import ( "fmt" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/queue" @@ -86,7 +85,6 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models. return nil, err } opts.AuthToken = "" - bs, err := json.Marshal(&opts) if err != nil { return nil, err diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 3a9e30fdc5d6c..1e8f00b669988 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -8,7 +8,6 @@ package templates import ( "bytes" "container/list" - "errors" "fmt" "html" @@ -28,6 +27,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" "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/repository" @@ -37,9 +37,7 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/gitdiff" - "code.gitea.io/gitea/modules/json" "github.com/editorconfig/editorconfig-core-go/v2" - jsoniter "github.com/json-iterator/go" ) // Used from static.go && dynamic.go @@ -47,7 +45,6 @@ var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}[\s]*$`) // NewFuncMap returns functions for injecting to templates func NewFuncMap() []template.FuncMap { - jsonED := jsoniter.ConfigCompatibleWithStandardLibrary return []template.FuncMap{map[string]interface{}{ "GoVer": func() string { return strings.Title(runtime.Version()) @@ -222,7 +219,7 @@ func NewFuncMap() []template.FuncMap { return fmt.Sprintf("%f", float64(adds)/(float64(adds)+float64(dels))*100) }, "Json": func(in interface{}) string { - out, err := jsonED.Marshal(in) + out, err := json.Marshal(in) if err != nil { return "" } diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 1331756d3f229..a257260cad113 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -9,11 +9,10 @@ import ( "net/http" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/utils" @@ -222,7 +221,6 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho if w.Type == models.SLACK { if channel, ok := form.Config["channel"]; ok { - meta, err := json.Marshal(&webhook.SlackMeta{ Channel: channel, Username: form.Config["username"], diff --git a/routers/private/mail.go b/routers/private/mail.go index 202ba2699e3dc..6b9100159a908 100644 --- a/routers/private/mail.go +++ b/routers/private/mail.go @@ -9,10 +9,9 @@ import ( "net/http" "strconv" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/setting" diff --git a/routers/private/manager.go b/routers/private/manager.go index bc42d29bf9655..2b4cfb8ef4e16 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -8,10 +8,9 @@ import ( "fmt" "net/http" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/queue" @@ -131,7 +130,6 @@ func AddLogger(ctx *context.PrivateContext) { } bufferLen := setting.Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000) - byteConfig, err := json.Marshal(opts.Config) if err != nil { log.Error("Failed to marshal log configuration: %v %v", opts.Config, err) diff --git a/routers/private/restore_repo.go b/routers/private/restore_repo.go index 0db0d26b4a0b7..87d60537cd8ad 100644 --- a/routers/private/restore_repo.go +++ b/routers/private/restore_repo.go @@ -8,16 +8,14 @@ import ( "io/ioutil" "net/http" - "code.gitea.io/gitea/modules/json" - myCtx "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/private" ) // RestoreRepo restore a repository from data func RestoreRepo(ctx *myCtx.PrivateContext) { - bs, err := ioutil.ReadAll(ctx.Req.Body) if err != nil { ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 73b4e7a8e40b2..125bef9b53ce9 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -15,13 +15,12 @@ import ( "strings" "time" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/cron" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" @@ -276,7 +275,6 @@ func Config(ctx *context.Context) { sessionCfg := setting.SessionConfig if sessionCfg.Provider == "VirtualSession" { var realSession session.Options - if err := json.Unmarshal([]byte(sessionCfg.ProviderConfig), &realSession); err != nil { log.Error("Unable to unmarshall session config for virtualed provider config: %s\nError: %v", sessionCfg.ProviderConfig, err) } diff --git a/routers/web/events/events.go b/routers/web/events/events.go index 126a10cc2b926..a630d9c224e8e 100644 --- a/routers/web/events/events.go +++ b/routers/web/events/events.go @@ -8,13 +8,12 @@ import ( "net/http" "time" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/eventsource" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/web/user" @@ -104,7 +103,6 @@ loop: log.Error("Unable to APIFormat stopwatches: %v", err) continue } - dataBs, err := json.Marshal(apiSWs) if err != nil { log.Error("Unable to marshal stopwatches: %v", err) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 490e1645222d0..792258da849d6 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -11,13 +11,12 @@ import ( "path" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/repofiles" repo_module "code.gitea.io/gitea/modules/repository" @@ -166,7 +165,6 @@ func GetEditorConfig(ctx *context.Context, treePath string) string { if err == nil { def, err := ec.GetDefinitionForFilename(treePath) if err == nil { - jsonStr, _ := json.Marshal(def) return string(jsonStr) } diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 9830742b9b79a..946801cb7649f 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -12,13 +12,12 @@ import ( "path" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" diff --git a/routers/web/user/home.go b/routers/web/user/home.go index ad4e5edbadc9e..fcf5295c9e613 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -7,9 +7,6 @@ package user import ( "bytes" - - "code.gitea.io/gitea/modules/json" - "fmt" "net/http" "regexp" @@ -21,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" issue_indexer "code.gitea.io/gitea/modules/indexer/issues" + "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" @@ -705,7 +703,6 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) { } // Convert []int64 to string - reposParam, _ := json.Marshal(repoIDs) ctx.Data["ReposParam"] = string(reposParam) diff --git a/routers/web/user/task.go b/routers/web/user/task.go index 403a4d7f31b32..c71d435233933 100644 --- a/routers/web/user/task.go +++ b/routers/web/user/task.go @@ -7,10 +7,9 @@ package user import ( "net/http" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" ) // TaskStatus returns task's status @@ -27,7 +26,6 @@ func TaskStatus(ctx *context.Context) { if task.Message != "" && task.Message[0] == '{' { // assume message is actually a translatable string - var translatableMessage models.TranslatableMessage if err := json.Unmarshal([]byte(message), &translatableMessage); err != nil { translatableMessage = models.TranslatableMessage{ diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 03a63947ec9f6..94ed1bc407ffe 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -12,11 +12,11 @@ import ( "strings" "testing" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/highlight" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" + dmp "github.com/sergi/go-diff/diffmatchpatch" "github.com/stretchr/testify/assert" "gopkg.in/ini.v1" diff --git a/services/pull/pull.go b/services/pull/pull.go index 9ef0bcd314ffe..6c108c224fd70 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -8,9 +8,6 @@ import ( "bufio" "bytes" "context" - - "code.gitea.io/gitea/modules/json" - "fmt" "regexp" "strings" @@ -19,6 +16,7 @@ import ( "code.gitea.io/gitea/models" "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/notification" "code.gitea.io/gitea/modules/setting" diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 4170c4ea9609a..3296b1114a10f 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -8,10 +8,9 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" dingtalk "github.com/lunny/dingtalk_webhook" @@ -28,7 +27,6 @@ var ( // JSONPayload Marshals the DingtalkPayload to json func (d *DingtalkPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(d, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 5b7de3ed8e22c..9a8f8a27a57d7 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -10,10 +10,9 @@ import ( "strconv" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -69,7 +68,6 @@ type ( // GetDiscordHook returns discord metadata func GetDiscordHook(w *models.Webhook) *DiscordMeta { s := &DiscordMeta{} - if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err) } @@ -100,7 +98,6 @@ var ( // JSONPayload Marshals the DiscordPayload to json func (d *DiscordPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(d, "", " ") if err != nil { return []byte{}, err @@ -251,7 +248,6 @@ func GetDiscordPayload(p api.Payloader, event models.HookEventType, meta string) s := new(DiscordPayload) discord := &DiscordMeta{} - if err := json.Unmarshal([]byte(meta), &discord); err != nil { return s, errors.New("GetDiscordPayload meta json:" + err.Error()) } diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 278ae369a9eeb..4428d54f92fe6 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -8,10 +8,9 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" ) @@ -38,7 +37,6 @@ func newFeishuTextPayload(text string) *FeishuPayload { // JSONPayload Marshals the FeishuPayload to json func (f *FeishuPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(f, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index f93860e05911a..10e0c8fd15543 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -6,9 +6,6 @@ package webhook import ( "crypto/sha1" - - "code.gitea.io/gitea/modules/json" - "errors" "fmt" "html" @@ -18,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -41,7 +39,6 @@ var messageTypeText = map[int]string{ // GetMatrixHook returns Matrix metadata func GetMatrixHook(w *models.Webhook) *MatrixMeta { s := &MatrixMeta{} - if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetMatrixHook(%d): %v", w.ID, err) } @@ -80,7 +77,6 @@ type MatrixPayloadSafe struct { // JSONPayload Marshals the MatrixPayloadUnsafe to json func (m *MatrixPayloadUnsafe) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(m, "", " ") if err != nil { return []byte{}, err @@ -230,7 +226,6 @@ func GetMatrixPayload(p api.Payloader, event models.HookEventType, meta string) s := new(MatrixPayloadUnsafe) matrix := &MatrixMeta{} - if err := json.Unmarshal([]byte(meta), &matrix); err != nil { return s, errors.New("GetMatrixPayload meta json:" + err.Error()) } @@ -264,7 +259,6 @@ func getMessageBody(htmlText string) string { // The access_token is removed from t.PayloadContent func getMatrixHookRequest(w *models.Webhook, t *models.HookTask) (*http.Request, error) { payloadunsafe := MatrixPayloadUnsafe{} - if err := json.Unmarshal([]byte(t.PayloadContent), &payloadunsafe); err != nil { log.Error("Matrix Hook delivery failed: %v", err) return nil, err diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 0f5dacc63516f..ed5e6590582c1 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -8,10 +8,9 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" ) @@ -58,7 +57,6 @@ type ( // JSONPayload Marshals the MSTeamsPayload to json func (m *MSTeamsPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(m, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 0f2e3b3e4af59..803b5de0b2ece 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -9,10 +9,9 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -29,7 +28,6 @@ type SlackMeta struct { // GetSlackHook returns slack metadata func GetSlackHook(w *models.Webhook) *SlackMeta { s := &SlackMeta{} - if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetSlackHook(%d): %v", w.ID, err) } @@ -59,7 +57,6 @@ type SlackAttachment struct { // JSONPayload Marshals the SlackPayload to json func (s *SlackPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(s, "", " ") if err != nil { return []byte{}, err @@ -280,7 +277,6 @@ func GetSlackPayload(p api.Payloader, event models.HookEventType, meta string) ( s := new(SlackPayload) slack := &SlackMeta{} - if err := json.Unmarshal([]byte(meta), &slack); err != nil { return s, errors.New("GetSlackPayload meta json:" + err.Error()) } diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 5b1030e95072c..c5a26f687dc4f 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -8,10 +8,9 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" api "code.gitea.io/gitea/modules/structs" @@ -35,7 +34,6 @@ type ( // GetTelegramHook returns telegram metadata func GetTelegramHook(w *models.Webhook) *TelegramMeta { s := &TelegramMeta{} - if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err) } @@ -51,7 +49,6 @@ func (t *TelegramPayload) JSONPayload() ([]byte, error) { t.ParseMode = "HTML" t.DisableWebPreview = true t.Message = markup.Sanitize(t.Message) - data, err := json.MarshalIndent(t, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index 0852e4e6831f8..08c77ff2a58c8 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -8,10 +8,9 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" ) From d32d021e7208f9c2f4b8bbe73abae5b44352091b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Jul 2021 11:17:04 +0800 Subject: [PATCH 4/6] Fix blank lines --- integrations/repo_commits_test.go | 1 - integrations/testlogger.go | 1 - models/migrations/testlogger_test.go | 1 - models/task.go | 1 - modules/lfs/http_client.go | 6 +++--- 5 files changed, 3 insertions(+), 7 deletions(-) diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index 3e49a786a139f..4be1e4ec8e47e 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -86,7 +86,6 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { } func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) { - var statuses []*api.CommitStatus assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses)) var status api.CombinedStatus diff --git a/integrations/testlogger.go b/integrations/testlogger.go index ca99dc09cbde0..9498ad655b331 100644 --- a/integrations/testlogger.go +++ b/integrations/testlogger.go @@ -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 { - err := json.Unmarshal([]byte(config), log) if err != nil { return err diff --git a/models/migrations/testlogger_test.go b/models/migrations/testlogger_test.go index 6f4a91e1c2dc1..5e3d880b54e30 100644 --- a/models/migrations/testlogger_test.go +++ b/models/migrations/testlogger_test.go @@ -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 { - err := json.Unmarshal([]byte(config), log) if err != nil { return err diff --git a/models/task.go b/models/task.go index e3c143127bdc7..7dfcaea2e3558 100644 --- a/models/task.go +++ b/models/task.go @@ -242,7 +242,6 @@ func FinishMigrateTask(task *Task) error { conf.AuthPasswordEncrypted = "" conf.AuthTokenEncrypted = "" conf.CloneAddrEncrypted = "" - confBytes, err := json.Marshal(conf) if err != nil { return err diff --git a/modules/lfs/http_client.go b/modules/lfs/http_client.go index 51843bcb98628..675b84b76e2fa 100644 --- a/modules/lfs/http_client.go +++ b/modules/lfs/http_client.go @@ -7,6 +7,7 @@ package lfs import ( "bytes" "context" + "encoding/json" "errors" "fmt" "net/http" @@ -14,7 +15,6 @@ import ( "strings" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) const batchSize = 20 @@ -68,7 +68,7 @@ func (c *HTTPClient) batch(ctx context.Context, operation string, objects []Poin request := &BatchRequest{operation, c.transferNames(), nil, objects} payload := new(bytes.Buffer) - err := jsoniter.NewEncoder(payload).Encode(request) + err := json.NewEncoder(payload).Encode(request) if err != nil { log.Error("Error encoding json: %v", err) return nil, err @@ -101,7 +101,7 @@ func (c *HTTPClient) batch(ctx context.Context, operation string, objects []Poin } var response BatchResponse - err = jsoniter.NewDecoder(res.Body).Decode(&response) + err = json.NewDecoder(res.Body).Decode(&response) if err != nil { log.Error("Error decoding json: %v", err) return nil, err From 0be76da15bec1996612d5d96000d42326765fb39 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Jul 2021 11:34:08 +0800 Subject: [PATCH 5/6] Fix blank lines --- integrations/lfs_getobject_test.go | 4 ++-- modules/lfs/http_client.go | 2 +- modules/lfs/http_client_test.go | 9 +++++---- modules/lfs/transferadapter.go | 6 +++--- modules/lfs/transferadapter_test.go | 7 ++++--- routers/web/user/oauth.go | 4 ++-- services/lfs/server.go | 8 ++++---- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/integrations/lfs_getobject_test.go b/integrations/lfs_getobject_test.go index c99500f469ea8..063f72b25ce24 100644 --- a/integrations/lfs_getobject_test.go +++ b/integrations/lfs_getobject_test.go @@ -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" ) @@ -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) } diff --git a/modules/lfs/http_client.go b/modules/lfs/http_client.go index 675b84b76e2fa..31c67903a83b1 100644 --- a/modules/lfs/http_client.go +++ b/modules/lfs/http_client.go @@ -7,13 +7,13 @@ package lfs import ( "bytes" "context" - "encoding/json" "errors" "fmt" "net/http" "net/url" "strings" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" ) diff --git a/modules/lfs/http_client_test.go b/modules/lfs/http_client_test.go index 0f633ede54cde..589773e45bf9c 100644 --- a/modules/lfs/http_client_test.go +++ b/modules/lfs/http_client_test.go @@ -13,7 +13,8 @@ import ( "strings" "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -146,7 +147,7 @@ func lfsTestRoundtripHandler(req *http.Request) *http.Response { } payload := new(bytes.Buffer) - jsoniter.NewEncoder(payload).Encode(batchResponse) + json.NewEncoder(payload).Encode(batchResponse) return &http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(payload)} } @@ -160,7 +161,7 @@ func TestHTTPClientDownload(t *testing.T) { assert.Equal(t, MediaType, req.Header.Get("Accept")) var batchRequest BatchRequest - err := jsoniter.NewDecoder(req.Body).Decode(&batchRequest) + err := json.NewDecoder(req.Body).Decode(&batchRequest) assert.NoError(t, err) assert.Equal(t, "download", batchRequest.Operation) @@ -267,7 +268,7 @@ func TestHTTPClientUpload(t *testing.T) { assert.Equal(t, MediaType, req.Header.Get("Accept")) var batchRequest BatchRequest - err := jsoniter.NewDecoder(req.Body).Decode(&batchRequest) + err := json.NewDecoder(req.Body).Decode(&batchRequest) assert.NoError(t, err) assert.Equal(t, "upload", batchRequest.Operation) diff --git a/modules/lfs/transferadapter.go b/modules/lfs/transferadapter.go index e6f566652804e..03f326f26c520 100644 --- a/modules/lfs/transferadapter.go +++ b/modules/lfs/transferadapter.go @@ -12,8 +12,8 @@ import ( "io" "net/http" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) // TransferAdapter represents an adapter for downloading/uploading LFS objects @@ -64,7 +64,7 @@ func (a *BasicTransferAdapter) Upload(ctx context.Context, l *Link, p Pointer, r // Verify calls the verify handler on the LFS server func (a *BasicTransferAdapter) Verify(ctx context.Context, l *Link, p Pointer) error { - b, err := jsoniter.Marshal(p) + b, err := json.Marshal(p) if err != nil { log.Error("Error encoding json: %v", err) return err @@ -127,7 +127,7 @@ func handleErrorResponse(resp *http.Response) error { func decodeReponseError(r io.Reader) (ErrorResponse, error) { var er ErrorResponse - err := jsoniter.NewDecoder(r).Decode(&er) + err := json.NewDecoder(r).Decode(&er) if err != nil { log.Error("Error decoding json: %v", err) } diff --git a/modules/lfs/transferadapter_test.go b/modules/lfs/transferadapter_test.go index 7dfdad417ea5b..9192b486ed1f9 100644 --- a/modules/lfs/transferadapter_test.go +++ b/modules/lfs/transferadapter_test.go @@ -13,7 +13,8 @@ import ( "strings" "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -49,7 +50,7 @@ func TestBasicTransferAdapter(t *testing.T) { assert.Equal(t, MediaType, req.Header.Get("Content-Type")) var vp Pointer - err := jsoniter.NewDecoder(req.Body).Decode(&vp) + err := json.NewDecoder(req.Body).Decode(&vp) assert.NoError(t, err) assert.Equal(t, p.Oid, vp.Oid) assert.Equal(t, p.Size, vp.Size) @@ -60,7 +61,7 @@ func TestBasicTransferAdapter(t *testing.T) { Message: "Object not found", } payload := new(bytes.Buffer) - jsoniter.NewEncoder(payload).Encode(er) + json.NewEncoder(payload).Encode(er) return &http.Response{StatusCode: http.StatusNotFound, Body: ioutil.NopCloser(payload)} } else { diff --git a/routers/web/user/oauth.go b/routers/web/user/oauth.go index 07fff8370cd22..18fc9434835fc 100644 --- a/routers/web/user/oauth.go +++ b/routers/web/user/oauth.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -25,7 +26,6 @@ import ( "gitea.com/go-chi/binding" "github.com/golang-jwt/jwt" - jsoniter "github.com/json-iterator/go" ) const ( @@ -507,7 +507,7 @@ func OIDCKeys(ctx *context.Context) { } ctx.Resp.Header().Set("Content-Type", "application/json") - enc := jsoniter.NewEncoder(ctx.Resp) + enc := json.NewEncoder(ctx.Resp) if err := enc.Encode(jwks); err != nil { log.Error("Failed to encode representation as json. Error: %v", err) } diff --git a/services/lfs/server.go b/services/lfs/server.go index 6b79a3a364024..0d357939d5ecc 100644 --- a/services/lfs/server.go +++ b/services/lfs/server.go @@ -17,12 +17,12 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" lfs_module "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "github.com/golang-jwt/jwt" - jsoniter "github.com/json-iterator/go" ) // requestContext contain variables from the HTTP request. @@ -243,7 +243,7 @@ func BatchHandler(ctx *context.Context) { ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType) - enc := jsoniter.NewEncoder(ctx.Resp) + enc := json.NewEncoder(ctx.Resp) if err := enc.Encode(respobj); err != nil { log.Error("Failed to encode representation as json. Error: %v", err) } @@ -336,7 +336,7 @@ func VerifyHandler(ctx *context.Context) { func decodeJSON(req *http.Request, v interface{}) error { defer req.Body.Close() - dec := jsoniter.NewDecoder(req.Body) + dec := json.NewDecoder(req.Body) return dec.Decode(v) } @@ -429,7 +429,7 @@ func writeStatusMessage(ctx *context.Context, status int, message string) { er := lfs_module.ErrorResponse{Message: message} - enc := jsoniter.NewEncoder(ctx.Resp) + enc := json.NewEncoder(ctx.Resp) if err := enc.Encode(er); err != nil { log.Error("Failed to encode error response as json. Error: %v", err) } From 8be348d7d33405db3aec967e99fb6a8087c95c1e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Jul 2021 23:36:04 +0800 Subject: [PATCH 6/6] Resolve conflict --- models/helper.go | 3 +-- models/migrations/v189.go | 6 +++--- models/migrations/v189_test.go | 7 ++++--- services/auth/source/ldap/source.go | 4 +--- services/auth/source/oauth2/source.go | 4 +--- services/auth/source/pam/source.go | 4 +--- services/auth/source/smtp/source.go | 4 +--- services/auth/source/sspi/source.go | 3 +-- 8 files changed, 13 insertions(+), 22 deletions(-) diff --git a/models/helper.go b/models/helper.go index 798fa3fef0dd4..c499b5512ddc7 100644 --- a/models/helper.go +++ b/models/helper.go @@ -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 { @@ -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 diff --git a/models/migrations/v189.go b/models/migrations/v189.go index 42b996353b74d..f136a89b4ef83 100644 --- a/models/migrations/v189.go +++ b/models/migrations/v189.go @@ -8,13 +8,13 @@ import ( "encoding/binary" "fmt" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "xorm.io/xorm" ) func unwrapLDAPSourceCfg(x *xorm.Engine) error { jsonUnmarshalHandleDoubleEncode := func(bs []byte, v interface{}) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal(bs, v) if err != nil { ok := true @@ -84,7 +84,7 @@ func unwrapLDAPSourceCfg(x *xorm.Engine) error { return fmt.Errorf("failed to unmarshal %s: %w", string(source.Cfg), err) } if wrapped.Source != nil && len(wrapped.Source) > 0 { - bs, err := jsoniter.Marshal(wrapped.Source) + bs, err := json.Marshal(wrapped.Source) if err != nil { return err } diff --git a/models/migrations/v189_test.go b/models/migrations/v189_test.go index f4fe6dec3f9ee..fcae5dcc73011 100644 --- a/models/migrations/v189_test.go +++ b/models/migrations/v189_test.go @@ -7,7 +7,8 @@ package migrations import ( "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -65,12 +66,12 @@ func Test_unwrapLDAPSourceCfg(t *testing.T) { converted := map[string]interface{}{} expected := map[string]interface{}{} - if err := jsoniter.Unmarshal([]byte(source.Cfg), &converted); err != nil { + if err := json.Unmarshal([]byte(source.Cfg), &converted); err != nil { assert.NoError(t, err) return } - if err := jsoniter.Unmarshal([]byte(source.Expected), &expected); err != nil { + if err := json.Unmarshal([]byte(source.Expected), &expected); err != nil { assert.NoError(t, err) return } diff --git a/services/auth/source/ldap/source.go b/services/auth/source/ldap/source.go index 87be0117ee5d8..79f118f784654 100644 --- a/services/auth/source/ldap/source.go +++ b/services/auth/source/ldap/source.go @@ -8,10 +8,9 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" - - jsoniter "github.com/json-iterator/go" ) // .____ ________ _____ __________ @@ -79,7 +78,6 @@ func (source *Source) ToDB() ([]byte, error) { return nil, err } source.BindPassword = "" - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/oauth2/source.go b/services/auth/source/oauth2/source.go index e9c49ef90b8ac..40d8973b4b2ce 100644 --- a/services/auth/source/oauth2/source.go +++ b/services/auth/source/oauth2/source.go @@ -6,8 +6,7 @@ package oauth2 import ( "code.gitea.io/gitea/models" - - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // ________ _____ __ .__ ________ @@ -37,7 +36,6 @@ func (source *Source) FromDB(bs []byte) error { // ToDB exports an SMTPConfig to a serialized format. func (source *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/pam/source.go b/services/auth/source/pam/source.go index b717ee6fe80fb..75aa99e45fd48 100644 --- a/services/auth/source/pam/source.go +++ b/services/auth/source/pam/source.go @@ -6,8 +6,7 @@ package pam import ( "code.gitea.io/gitea/models" - - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // __________ _____ _____ @@ -33,7 +32,6 @@ func (source *Source) FromDB(bs []byte) error { // ToDB exports a PAMConfig to a serialized format. func (source *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/smtp/source.go b/services/auth/source/smtp/source.go index 0f948d5381897..3d80aea68ac9a 100644 --- a/services/auth/source/smtp/source.go +++ b/services/auth/source/smtp/source.go @@ -6,8 +6,7 @@ package smtp import ( "code.gitea.io/gitea/models" - - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // _________ __________________________ @@ -37,7 +36,6 @@ func (source *Source) FromDB(bs []byte) error { // ToDB exports an SMTPConfig to a serialized format. func (source *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/sspi/source.go b/services/auth/source/sspi/source.go index e4be446f30dbe..58cb10de1df91 100644 --- a/services/auth/source/sspi/source.go +++ b/services/auth/source/sspi/source.go @@ -6,7 +6,7 @@ package sspi import ( "code.gitea.io/gitea/models" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // _________ ___________________.___ @@ -32,7 +32,6 @@ func (cfg *Source) FromDB(bs []byte) error { // ToDB exports an SSPIConfig to a serialized format. func (cfg *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(cfg) }