1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

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
77 changed files with 264 additions and 82 deletions

View File

@@ -5,8 +5,9 @@
package queue
import (
"encoding/json"
"reflect"
jsoniter "github.com/json-iterator/go"
)
// Mappable represents an interface that can MapTo another interface
@@ -19,6 +20,7 @@ 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)) {
@@ -66,6 +68,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)

View File

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

View File

@@ -6,12 +6,12 @@ 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
@@ -71,6 +71,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
@@ -229,6 +230,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

View File

@@ -5,9 +5,9 @@
package queue
import (
"encoding/json"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@@ -30,6 +30,7 @@ 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)

View File

@@ -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,6 +28,7 @@ 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)