diff options
author | zeripath <art27@cantab.net> | 2021-03-01 21:08:10 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-01 22:08:10 +0100 |
commit | f0e15250b9e322cc7731ba026d12387c2b549a42 (patch) | |
tree | f13d46119077ba924d620ef172b91daa315bda0a /modules/log | |
parent | 59fd641d1fb021e35aea7f9f4a1916cc11ef5c51 (diff) | |
download | gitea-f0e15250b9e322cc7731ba026d12387c2b549a42.tar.gz gitea-f0e15250b9e322cc7731ba026d12387c2b549a42.zip |
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>
Diffstat (limited to 'modules/log')
-rw-r--r-- | modules/log/conn.go | 7 | ||||
-rw-r--r-- | modules/log/conn_test.go | 3 | ||||
-rw-r--r-- | modules/log/console.go | 7 | ||||
-rw-r--r-- | modules/log/console_test.go | 3 | ||||
-rw-r--r-- | modules/log/file.go | 5 | ||||
-rw-r--r-- | modules/log/level.go | 4 | ||||
-rw-r--r-- | modules/log/level_test.go | 3 | ||||
-rw-r--r-- | modules/log/smtp.go | 7 |
8 files changed, 27 insertions, 12 deletions
diff --git a/modules/log/conn.go b/modules/log/conn.go index 1abe44c1d4..ad3d7f74e0 100644 --- a/modules/log/conn.go +++ b/modules/log/conn.go @@ -6,9 +6,11 @@ package log import ( - "encoding/json" + "fmt" "io" "net" + + jsoniter "github.com/json-iterator/go" ) type connWriter struct { @@ -104,9 +106,10 @@ func NewConn() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConnLogger) Init(jsonconfig string) error { + json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { - return err + return fmt.Errorf("Unable to parse JSON: %v", err) } log.NewWriterLogger(&connWriter{ ReconnectOnMsg: log.ReconnectOnMsg, diff --git a/modules/log/conn_test.go b/modules/log/conn_test.go index 0f35f02a2d..dc5de732f4 100644 --- a/modules/log/conn_test.go +++ b/modules/log/conn_test.go @@ -98,7 +98,8 @@ func TestConnLoggerBadConfig(t *testing.T) { logger := NewConn() err := logger.Init("{") - assert.Equal(t, "unexpected end of JSON input", err.Error()) + assert.Error(t, err) + assert.Contains(t, err.Error(), "Unable to parse JSON") logger.Close() } diff --git a/modules/log/console.go b/modules/log/console.go index a805021f0b..339b9ef3a4 100644 --- a/modules/log/console.go +++ b/modules/log/console.go @@ -6,9 +6,11 @@ package log import ( - "encoding/json" + "fmt" "io" "os" + + jsoniter "github.com/json-iterator/go" ) // CanColorStdout reports if we can color the Stdout @@ -50,9 +52,10 @@ func NewConsoleLogger() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConsoleLogger) Init(config string) error { + json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(config), log) if err != nil { - return err + return fmt.Errorf("Unable to parse JSON: %v", err) } if log.Stderr { log.NewWriterLogger(&nopWriteCloser{ diff --git a/modules/log/console_test.go b/modules/log/console_test.go index a028b5b875..e7ed07123b 100644 --- a/modules/log/console_test.go +++ b/modules/log/console_test.go @@ -17,7 +17,8 @@ func TestConsoleLoggerBadConfig(t *testing.T) { logger := NewConsoleLogger() err := logger.Init("{") - assert.Equal(t, "unexpected end of JSON input", err.Error()) + assert.Error(t, err) + assert.Contains(t, err.Error(), "Unable to parse JSON") logger.Close() } diff --git a/modules/log/file.go b/modules/log/file.go index c9b5d47c0b..d5b38d4e01 100644 --- a/modules/log/file.go +++ b/modules/log/file.go @@ -7,7 +7,6 @@ package log import ( "bufio" "compress/gzip" - "encoding/json" "errors" "fmt" "os" @@ -17,6 +16,7 @@ import ( "time" "code.gitea.io/gitea/modules/util" + jsoniter "github.com/json-iterator/go" ) // FileLogger implements LoggerProvider. @@ -101,8 +101,9 @@ func NewFileLogger() LoggerProvider { // "rotate":true // } func (log *FileLogger) Init(config string) error { + json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(config), log); err != nil { - return err + return fmt.Errorf("Unable to parse JSON: %v", err) } if len(log.Filename) == 0 { return errors.New("config must have filename") diff --git a/modules/log/level.go b/modules/log/level.go index ab231bd1bd..4b2d4ced41 100644 --- a/modules/log/level.go +++ b/modules/log/level.go @@ -6,10 +6,11 @@ package log import ( "bytes" - "encoding/json" "fmt" "os" "strings" + + jsoniter "github.com/json-iterator/go" ) // Level is the level of the logger @@ -103,6 +104,7 @@ func FromString(level string) Level { // UnmarshalJSON takes text and turns it into a Level func (l *Level) UnmarshalJSON(b []byte) error { var tmp interface{} + json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal(b, &tmp) if err != nil { fmt.Fprintf(os.Stderr, "Err: %v", err) diff --git a/modules/log/level_test.go b/modules/log/level_test.go index 40f6310bfd..2f37e407a1 100644 --- a/modules/log/level_test.go +++ b/modules/log/level_test.go @@ -5,10 +5,10 @@ package log import ( - "encoding/json" "fmt" "testing" + jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -17,6 +17,7 @@ type testLevel struct { } func TestLevelMarshalUnmarshalJSON(t *testing.T) { + json := jsoniter.ConfigCompatibleWithStandardLibrary levelBytes, err := json.Marshal(testLevel{ Level: INFO, }) diff --git a/modules/log/smtp.go b/modules/log/smtp.go index edf4943619..3f27b2c658 100644 --- a/modules/log/smtp.go +++ b/modules/log/smtp.go @@ -6,9 +6,11 @@ package log import ( - "encoding/json" + "fmt" "net/smtp" "strings" + + jsoniter "github.com/json-iterator/go" ) type smtpWriter struct { @@ -55,9 +57,10 @@ func NewSMTPLogger() LoggerProvider { // "level":LevelError // } func (log *SMTPLogger) Init(jsonconfig string) error { + json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { - return err + return fmt.Errorf("Unable to parse JSON: %v", err) } log.NewWriterLogger(&smtpWriter{ owner: log, |