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:
2021-07-25 00:03:58 +08:00
committed by GitHub
parent e0f9635c06
commit 9f31f3aa8a
93 changed files with 272 additions and 264 deletions

View File

@ -7,7 +7,7 @@ package queue
import (
"reflect"
jsoniter "github.com/json-iterator/go"
"code.gitea.io/gitea/modules/json"
)
// Mappable represents an interface that can MapTo another interface
@ -20,8 +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) {
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)) {
return cfg, nil
@ -48,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
}
@ -68,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) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
if exemplar != nil {
t := reflect.TypeOf(exemplar)
n := reflect.New(t)
@ -78,7 +74,6 @@ func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) {
} else {
err = json.Unmarshal(bs, &data)
}
return
}

View File

@ -12,8 +12,8 @@ import (
"sync"
"time"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
jsoniter "github.com/json-iterator/go"
)
var manager *Manager
@ -110,7 +110,6 @@ func (m *Manager) Add(managed interface{},
configuration,
exemplar interface{}) int64 {
json := jsoniter.ConfigCompatibleWithStandardLibrary
cfg, _ := json.Marshal(configuration)
mq := &ManagedQueue{
Type: t,

View File

@ -10,8 +10,8 @@ import (
"sync"
"time"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
jsoniter "github.com/json-iterator/go"
)
// ByteFIFOQueueConfiguration is the configuration for a ByteFIFOQueue
@ -83,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)
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
bs, err := json.Marshal(data)
if err != nil {
return err
@ -309,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)
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
bs, err := json.Marshal(data)
if err != nil {
return false, err

View File

@ -7,7 +7,8 @@ package queue
import (
"testing"
jsoniter "github.com/json-iterator/go"
"code.gitea.io/gitea/modules/json"
"github.com/stretchr/testify/assert"
)
@ -29,8 +30,6 @@ func TestToConfig(t *testing.T) {
assert.True(t, ok)
assert.NotEqual(t, cfg2, exemplar)
assert.Equal(t, &cfg, &cfg2)
json := jsoniter.ConfigCompatibleWithStandardLibrary
cfgString, err := json.Marshal(cfg)
assert.NoError(t, err)

View File

@ -8,9 +8,9 @@ import (
"fmt"
"strings"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
)
func validType(t string) (Type, error) {
@ -27,8 +27,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)