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

@@ -6,7 +6,6 @@
package gitdiff
import (
"encoding/json"
"fmt"
"html/template"
"strconv"
@@ -17,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/git"
"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"
@@ -298,7 +298,8 @@ index 6961180..9ba1a00 100644
t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr)
return
}
gotMarshaled, _ := json.MarshalIndent(got, " ", " ")
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))
return

View File

@@ -8,7 +8,6 @@ import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strings"
@@ -21,6 +20,7 @@ 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,6 +86,7 @@ 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

View File

@@ -5,7 +5,6 @@
package webhook
import (
"encoding/json"
"fmt"
"strings"
@@ -13,6 +12,7 @@ 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"
)
@@ -30,6 +30,7 @@ func (d *DingtalkPayload) SetSecret(_ string) {}
// 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

View File

@@ -5,7 +5,6 @@
package webhook
import (
"encoding/json"
"errors"
"fmt"
"strconv"
@@ -16,6 +15,7 @@ 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,6 +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)
}
@@ -101,6 +102,7 @@ func (d *DiscordPayload) SetSecret(_ string) {}
// 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
@@ -407,6 +409,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())
}

View File

@@ -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 (
@@ -40,6 +40,7 @@ func (f *FeishuPayload) SetSecret(_ string) {}
// 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

View File

@@ -6,7 +6,6 @@ package webhook
import (
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"html"
@@ -19,6 +18,7 @@ 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,6 +39,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)
}
@@ -80,6 +81,7 @@ func (m *MatrixPayloadUnsafe) SetSecret(_ string) {}
// 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
@@ -229,6 +231,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,6 +265,7 @@ func getMessageBody(htmlText string) string {
// The access_token is removed from t.PayloadContent
func getMatrixHookRequest(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

View File

@@ -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 (
@@ -60,6 +60,7 @@ func (m *MSTeamsPayload) SetSecret(_ string) {}
// 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

View File

@@ -5,7 +5,6 @@
package webhook
import (
"encoding/json"
"errors"
"fmt"
"strings"
@@ -15,6 +14,7 @@ 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,6 +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)
}
@@ -60,6 +61,7 @@ func (s *SlackPayload) SetSecret(_ string) {}
// 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
@@ -320,6 +322,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())
}

View File

@@ -5,7 +5,6 @@
package webhook
import (
"encoding/json"
"fmt"
"strings"
@@ -14,6 +13,7 @@ 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,6 +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)
}
@@ -52,6 +53,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