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

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

* Fix import

* Fix import sequence

* Fix blank lines

* Fix blank lines
This commit is contained in:
Lunny Xiao 2021-07-25 00:03:58 +08:00 committed by GitHub
parent e0f9635c06
commit 9f31f3aa8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
93 changed files with 272 additions and 264 deletions

View file

@ -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

View file

@ -15,13 +15,13 @@ import (
"unicode/utf8"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"
"xorm.io/builder"
"xorm.io/xorm"
@ -654,7 +654,6 @@ func (c *Comment) LoadPushCommits() (err error) {
var data PushActionContent
json := jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal([]byte(c.Content), &data)
if err != nil {
return
@ -1249,7 +1248,6 @@ func CreatePushPullComment(pusher *User, pr *PullRequest, oldCommitID, newCommit
ops.Issue = pr.Issue
json := jsoniter.ConfigCompatibleWithStandardLibrary
dataJSON, err := json.Marshal(data)
if err != nil {
return nil, err

View file

@ -14,9 +14,9 @@ import (
"testing"
"time"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
jsoniter "github.com/json-iterator/go"
)
var (
@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider {
// Init inits connection writer with json config.
// json config only need key "level".
func (log *TestLogger) Init(config string) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(config), log)
if err != nil {
return err

View file

@ -5,8 +5,8 @@
package migrations
import (
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
)
@ -70,7 +70,6 @@ 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,9 +5,9 @@
package migrations
import (
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/util"
jsoniter "github.com/json-iterator/go"
"xorm.io/builder"
"xorm.io/xorm"
)
@ -105,7 +105,6 @@ func removeCredentials(payload string) (string, error) {
}
var opts MigrateOptions
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(payload), &opts)
if err != nil {
return "", err

View file

@ -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
}

View file

@ -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
}

View file

@ -7,9 +7,9 @@ package models
import (
"fmt"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
"xorm.io/xorm/convert"
)
@ -33,7 +33,6 @@ 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 +48,6 @@ 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 +65,6 @@ 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 +82,6 @@ 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 +105,6 @@ 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)
}

View file

@ -7,13 +7,13 @@ package models
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"
"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,6 @@ 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 +189,6 @@ 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 +242,6 @@ func FinishMigrateTask(task *Task) error {
conf.AuthPasswordEncrypted = ""
conf.AuthTokenEncrypted = ""
conf.CloneAddrEncrypted = ""
json := jsoniter.ConfigCompatibleWithStandardLibrary
confBytes, err := json.Marshal(conf)
if err != nil {
return err

View file

@ -8,7 +8,8 @@ import (
"fmt"
"testing"
jsoniter "github.com/json-iterator/go"
"code.gitea.io/gitea/modules/json"
"github.com/stretchr/testify/assert"
)
@ -68,7 +69,6 @@ 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))

View file

@ -11,13 +11,13 @@ import (
"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"
"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
@ -160,8 +160,6 @@ 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)
}
@ -174,7 +172,6 @@ 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 +684,6 @@ 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 +697,6 @@ 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

@ -9,9 +9,9 @@ import (
"testing"
"time"
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -58,7 +58,6 @@ 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)
}