mirror of
https://github.com/go-gitea/gitea.git
synced 2025-06-21 05:28:00 +02:00
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:
@ -8,10 +8,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/secret"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// .____ ________ _____ __________
|
||||
@ -79,7 +78,6 @@ func (source *Source) ToDB() ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
source.BindPassword = ""
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.Marshal(source)
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,7 @@ package oauth2
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
)
|
||||
|
||||
// ________ _____ __ .__ ________
|
||||
@ -37,7 +36,6 @@ func (source *Source) FromDB(bs []byte) error {
|
||||
|
||||
// ToDB exports an SMTPConfig to a serialized format.
|
||||
func (source *Source) ToDB() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.Marshal(source)
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,7 @@ package pam
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
)
|
||||
|
||||
// __________ _____ _____
|
||||
@ -33,7 +32,6 @@ func (source *Source) FromDB(bs []byte) error {
|
||||
|
||||
// ToDB exports a PAMConfig to a serialized format.
|
||||
func (source *Source) ToDB() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.Marshal(source)
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,7 @@ package smtp
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
)
|
||||
|
||||
// _________ __________________________
|
||||
@ -37,7 +36,6 @@ func (source *Source) FromDB(bs []byte) error {
|
||||
|
||||
// ToDB exports an SMTPConfig to a serialized format.
|
||||
func (source *Source) ToDB() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.Marshal(source)
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ package sspi
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
)
|
||||
|
||||
// _________ ___________________.___
|
||||
@ -32,7 +32,6 @@ func (cfg *Source) FromDB(bs []byte) error {
|
||||
|
||||
// ToDB exports an SSPIConfig to a serialized format.
|
||||
func (cfg *Source) ToDB() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.Marshal(cfg)
|
||||
}
|
||||
|
||||
|
@ -14,8 +14,9 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/highlight"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"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"
|
||||
@ -297,7 +298,7 @@ index 6961180..9ba1a00 100644
|
||||
t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr)
|
||||
return
|
||||
}
|
||||
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))
|
||||
|
@ -12,11 +12,11 @@ import (
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
lfs_module "code.gitea.io/gitea/modules/lfs"
|
||||
"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"
|
||||
)
|
||||
|
||||
func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *models.LFSLock, err error) {
|
||||
@ -159,7 +159,7 @@ func PostLockHandler(ctx *context.Context) {
|
||||
var req api.LFSLockRequest
|
||||
bodyReader := ctx.Req.Body
|
||||
defer bodyReader.Close()
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
dec := json.NewDecoder(bodyReader)
|
||||
if err := dec.Decode(&req); err != nil {
|
||||
log.Warn("Failed to decode lock request as json. Error: %v", err)
|
||||
@ -293,7 +293,7 @@ func UnLockHandler(ctx *context.Context) {
|
||||
var req api.LFSLockDeleteRequest
|
||||
bodyReader := ctx.Req.Body
|
||||
defer bodyReader.Close()
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
dec := json.NewDecoder(bodyReader)
|
||||
if err := dec.Decode(&req); err != nil {
|
||||
log.Warn("Failed to decode lock request as json. Error: %v", err)
|
||||
|
@ -17,12 +17,12 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
lfs_module "code.gitea.io/gitea/modules/lfs"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// requestContext contain variables from the HTTP request.
|
||||
@ -243,7 +243,7 @@ func BatchHandler(ctx *context.Context) {
|
||||
|
||||
ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
|
||||
|
||||
enc := jsoniter.NewEncoder(ctx.Resp)
|
||||
enc := json.NewEncoder(ctx.Resp)
|
||||
if err := enc.Encode(respobj); err != nil {
|
||||
log.Error("Failed to encode representation as json. Error: %v", err)
|
||||
}
|
||||
@ -336,7 +336,7 @@ func VerifyHandler(ctx *context.Context) {
|
||||
func decodeJSON(req *http.Request, v interface{}) error {
|
||||
defer req.Body.Close()
|
||||
|
||||
dec := jsoniter.NewDecoder(req.Body)
|
||||
dec := json.NewDecoder(req.Body)
|
||||
return dec.Decode(v)
|
||||
}
|
||||
|
||||
@ -429,7 +429,7 @@ func writeStatusMessage(ctx *context.Context, status int, message string) {
|
||||
|
||||
er := lfs_module.ErrorResponse{Message: message}
|
||||
|
||||
enc := jsoniter.NewEncoder(ctx.Resp)
|
||||
enc := json.NewEncoder(ctx.Resp)
|
||||
if err := enc.Encode(er); err != nil {
|
||||
log.Error("Failed to encode error response as json. Error: %v", err)
|
||||
}
|
||||
|
@ -16,11 +16,11 @@ import (
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"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,7 +86,6 @@ 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
|
||||
|
@ -10,9 +10,9 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
dingtalk "github.com/lunny/dingtalk_webhook"
|
||||
)
|
||||
|
||||
@ -27,7 +27,6 @@ var (
|
||||
|
||||
// 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
|
||||
|
@ -12,10 +12,10 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"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"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -68,7 +68,6 @@ 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)
|
||||
}
|
||||
@ -99,7 +98,6 @@ var (
|
||||
|
||||
// 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
|
||||
@ -250,7 +248,6 @@ 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())
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -37,7 +37,6 @@ func newFeishuTextPayload(text string) *FeishuPayload {
|
||||
|
||||
// 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
|
||||
|
@ -15,10 +15,10 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"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"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
const matrixPayloadSizeLimit = 1024 * 64
|
||||
@ -39,7 +39,6 @@ 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)
|
||||
}
|
||||
@ -78,7 +77,6 @@ type MatrixPayloadSafe struct {
|
||||
|
||||
// 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
|
||||
@ -228,7 +226,6 @@ 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,7 +259,6 @@ func getMessageBody(htmlText string) string {
|
||||
// The access_token is removed from t.PayloadContent
|
||||
func getMatrixHookRequest(w *models.Webhook, 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
|
||||
|
@ -10,8 +10,8 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -57,7 +57,6 @@ type (
|
||||
|
||||
// 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
|
||||
|
@ -11,10 +11,10 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"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"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// SlackMeta contains the slack metadata
|
||||
@ -28,7 +28,6 @@ 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)
|
||||
}
|
||||
@ -58,7 +57,6 @@ type SlackAttachment struct {
|
||||
|
||||
// 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
|
||||
@ -279,7 +277,6 @@ 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())
|
||||
}
|
||||
|
@ -10,10 +10,10 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"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,7 +34,6 @@ 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)
|
||||
}
|
||||
@ -50,7 +49,6 @@ 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
|
||||
|
@ -5,12 +5,12 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
|
Reference in New Issue
Block a user