MM-22051: Remove ToJson methods from network writes (#17999)

We replace the double conversion of
[]byte to string, with a direct write
to http.ResponseWriter.

https://mattermost.atlassian.net/browse/MM-22051

Tried using gofmt -r, but it only accepts Go
expressions. So had to resort to an ugly sed replace

sed -E -i 's/w.Write\(\[\]byte\((.*).ToJson\(\)\)\)/if err := json.NewEncoder\(w\).Encode\(\1\); err != nil { mlog.Warn\("Error while writing response", mlog.Err\(err\)\)}/g' *.go

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2021-07-26 13:41:02 +05:30
коммит произвёл GitHub
родитель ae7119ddf5
Коммит 23800326a0
33 изменённых файлов: 578 добавлений и 181 удалений

Просмотреть файл

@@ -4,12 +4,14 @@
package api4
import (
"encoding/json"
"net/http"
"reflect"
"github.com/mattermost/mattermost-server/v6/audit"
"github.com/mattermost/mattermost-server/v6/config"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
"github.com/mattermost/mattermost-server/v6/utils"
)
@@ -26,7 +28,9 @@ func localGetConfig(c *Context, w http.ResponseWriter, r *http.Request) {
cfg := c.App.GetSanitizedConfig()
auditRec.Success()
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Write([]byte(cfg.ToJson()))
if err := json.NewEncoder(w).Encode(cfg); err != nil {
mlog.Warn("Error while writing response", mlog.Err(err))
}
}
func localUpdateConfig(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -76,7 +80,9 @@ func localUpdateConfig(c *Context, w http.ResponseWriter, r *http.Request) {
c.LogAudit("updateConfig")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Write([]byte(newCfg.ToJson()))
if err := json.NewEncoder(w).Encode(newCfg); err != nil {
mlog.Warn("Error while writing response", mlog.Err(err))
}
}
func localPatchConfig(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -129,5 +135,7 @@ func localPatchConfig(c *Context, w http.ResponseWriter, r *http.Request) {
auditRec.Success()
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Write([]byte(c.App.GetSanitizedConfig().ToJson()))
if err := json.NewEncoder(w).Encode(c.App.GetSanitizedConfig()); err != nil {
mlog.Warn("Error while writing response", mlog.Err(err))
}
}