Migrate to use jsoniter instead of encoding/json (#14841)

* Migrate to use jsoniter

* fix tests

* update gitea.com/go-chi/binding

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
zeripath 2021-03-01 21:08:10 +00:00 committed by GitHub
parent 59fd641d1f
commit f0e15250b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
77 changed files with 264 additions and 82 deletions

View file

@ -8,7 +8,6 @@ package models
import (
"container/list"
"encoding/json"
"fmt"
"regexp"
"strconv"
@ -21,6 +20,7 @@ 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"
@ -655,6 +655,7 @@ func (c *Comment) LoadPushCommits() (err error) {
var data PushActionContent
json := jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal([]byte(c.Content), &data)
if err != nil {
return
@ -1241,6 +1242,8 @@ 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

View file

@ -7,7 +7,6 @@ package models
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/smtp"
@ -22,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
"xorm.io/xorm/convert"
@ -75,11 +75,13 @@ type LDAPConfig struct {
// FromDB fills up a LDAPConfig from serialized format.
func (cfg *LDAPConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a LDAPConfig to a serialized format.
func (cfg *LDAPConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -101,11 +103,13 @@ type SMTPConfig struct {
// FromDB fills up an SMTPConfig from serialized format.
func (cfg *SMTPConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, cfg)
}
// ToDB exports an SMTPConfig to a serialized format.
func (cfg *SMTPConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -116,11 +120,13 @@ type PAMConfig struct {
// FromDB fills up a PAMConfig from serialized format.
func (cfg *PAMConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a PAMConfig to a serialized format.
func (cfg *PAMConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -136,11 +142,13 @@ type OAuth2Config struct {
// FromDB fills up an OAuth2Config from serialized format.
func (cfg *OAuth2Config) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, cfg)
}
// ToDB exports an SMTPConfig to a serialized format.
func (cfg *OAuth2Config) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -155,11 +163,13 @@ type SSPIConfig struct {
// FromDB fills up an SSPIConfig from serialized format.
func (cfg *SSPIConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, cfg)
}
// ToDB exports an SSPIConfig to a serialized format.
func (cfg *SSPIConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}

View file

@ -5,9 +5,8 @@
package migrations
import (
"encoding/json"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
)
@ -72,6 +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
}

View file

@ -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"
)
@ -29,11 +29,13 @@ type UnitConfig struct {
// FromDB fills up a UnitConfig from serialized format.
func (cfg *UnitConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a UnitConfig to a serialized format.
func (cfg *UnitConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -44,11 +46,13 @@ type ExternalWikiConfig struct {
// FromDB fills up a ExternalWikiConfig from serialized format.
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a ExternalWikiConfig to a serialized format.
func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -61,11 +65,13 @@ type ExternalTrackerConfig struct {
// FromDB fills up a ExternalTrackerConfig from serialized format.
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a ExternalTrackerConfig to a serialized format.
func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -78,11 +84,13 @@ type IssuesConfig struct {
// FromDB fills up a IssuesConfig from serialized format.
func (cfg *IssuesConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a IssuesConfig to a serialized format.
func (cfg *IssuesConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -97,11 +105,13 @@ type PullRequestsConfig struct {
// FromDB fills up a PullRequestsConfig from serialized format.
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a PullRequestsConfig to a serialized format.
func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}

View file

@ -5,12 +5,12 @@
package models
import (
"encoding/json"
"fmt"
migration "code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"
"xorm.io/builder"
)
@ -105,6 +105,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
@ -162,6 +163,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
}

View file

@ -5,10 +5,10 @@
package models
import (
"encoding/json"
"fmt"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -56,6 +56,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
assert.Equal(t, tc.CountResult, len(heatmap), 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))

View file

@ -7,7 +7,6 @@ package models
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
@ -18,6 +17,7 @@ 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
@ -145,6 +145,8 @@ type Webhook struct {
// AfterLoad updates the webhook object upon setting a column
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)
}
@ -157,6 +159,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
@ -689,6 +692,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)
}
@ -702,6 +706,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)

View file

@ -6,12 +6,12 @@ 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,6 +58,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)
}