From 555f724dafa4706054fbf89548f467f85db3544b Mon Sep 17 00:00:00 2001 From: James Littlejohn Date: Thu, 22 Aug 2024 19:17:43 +1000 Subject: [PATCH] fix: Fix issues identified by golangci-lint --- array.go | 15 +++----- encoder.go | 2 +- encoder_bench_test.go | 9 ----- encoder_test.go | 81 ++++++++++++++++++++++++------------------- error.go | 2 +- object.go | 14 ++++---- pool.go | 4 --- 7 files changed, 57 insertions(+), 70 deletions(-) diff --git a/array.go b/array.go index 9aada37..3495530 100644 --- a/array.go +++ b/array.go @@ -94,9 +94,7 @@ func (e *prettyConsoleEncoder) AppendUint64(u uint64) { func (e *prettyConsoleEncoder) AppendDuration(duration time.Duration) { e.addSeparator() cur := e.buf.Len() - if durationEncoder := e.cfg.EncodeDuration; e != nil { - durationEncoder(duration, e) - } + e.cfg.EncodeDuration(duration, e) if cur == e.buf.Len() { // User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep // JSON valid. @@ -110,9 +108,7 @@ func (e *prettyConsoleEncoder) AppendDuration(duration time.Duration) { func (e *prettyConsoleEncoder) AppendTime(t time.Time) { e.addSeparator() cur := e.buf.Len() - if timeEncoder := e.cfg.EncodeTime; e != nil { - timeEncoder(t, e) - } + e.cfg.EncodeTime(t, e) if cur == e.buf.Len() { // User-supplied EncodeTime is a no-op. Fall back to RFC3339 e.buf.AppendTime(t, time.RFC3339) @@ -189,11 +185,8 @@ func (e *prettyConsoleEncoder) AppendReflected(value interface{}) error { indent: enc.namespaceIndent, lineEnding: []byte(e.cfg.LineEnding), } - - if reflectedEncoder := e.cfg.NewReflectedEncoder(iw); e != nil { - if err := reflectedEncoder.Encode(value); err != nil { - return err - } + if err := e.cfg.NewReflectedEncoder(iw).Encode(value); err != nil { + return err } if l-enc.buf.Len() == 0 { // User-supplied reflectedEncoder is a no-op. Fall back to dd diff --git a/encoder.go b/encoder.go index b84e0e3..6be3b73 100644 --- a/encoder.go +++ b/encoder.go @@ -264,7 +264,7 @@ func (e *prettyConsoleEncoder) appendSafeByte(s []byte) { i++ continue } - e.buf.Write(s[i : i+size]) + _, _ = e.buf.Write(s[i : i+size]) // Explicitly ignore errors i += size } } diff --git a/encoder_bench_test.go b/encoder_bench_test.go index b590bc6..ab02fea 100644 --- a/encoder_bench_test.go +++ b/encoder_bench_test.go @@ -283,15 +283,6 @@ func newBenchmarkPrettyZapLogger(lvl zapcore.Level) *zap.Logger { )) } -func newSampledLogger(lvl zapcore.Level) *zap.Logger { - return zap.New(zapcore.NewSamplerWithOptions( - newZapLogger(zap.DebugLevel).Core(), - 100*time.Millisecond, - 10, // first - 10, // thereafter - )) -} - func fakeFields() []zap.Field { return []zap.Field{ zap.Int("int", _tenInts[0]), diff --git a/encoder_test.go b/encoder_test.go index 57b0b16..efa151b 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -198,39 +198,52 @@ func TestEncodeEntry(t *testing.T) { }, { desc: "Errors", - // 4:33PM ERR > test message named_stracktrace=.TestEncodeEntry\n\t/:\ntesting.tRunner\n\t/: + // 4:33PM ERR > test message named_stracktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry\n\t/:\ntesting.tRunner\n\t/: // ↳ error=something \nwent wrong // ↳ nested.cause=error with stacktrace - // .cause.cause=error with 2 causes - // .cause.cause.0=cause 1 - // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry - // /: - // testing.tRunner - // /: - // runtime.goexit - // /: - // .cause.1.cause=deeper error with two causes - // .cause.cause.0=deeper cause 1 - // .cause.1=deeper cause 2 - // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry - // /: - // testing.tRunner - // /: - // runtime.goexit - // /: - // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry - // /: - // testing.tRunner - // /: - // runtime.goexit - // /: - // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry - // /: - // testing.tRunner - // /: - // runtime.goexit - // /: - expected: "\x1b[90m4:33PM\x1b[0m\x1b[31m \x1b[0m\x1b[31mERR\x1b[0m\x1b[31m \x1b[0m\x1b[1m\x1b[31m>\x1b[0m\x1b[0m\x1b[31m \x1b[0mtest message\x1b[31m \x1b[0m\x1b[31mnamed_stracktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/:\x1b[31m\\n\x1b[0mtesting.tRunner\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/:\n\x1b[31m ↳ error\x1b[0m\x1b[31m=\x1b[0msomething \x1b[31m\\n\x1b[0mwent wrong\n\x1b[31m ↳ nested\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with stacktrace\n \x1b[31m.cause\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with 2 causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mcause 1\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n \x1b[31m.cause.1\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0mdeeper error with two causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mdeeper cause 1\n \x1b[31m.cause.1\x1b[0m\x1b[31m=\x1b[0mdeeper cause 2\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n\x1b[31m ↳ nil_panic_PANIC_DISPLAYING_ERROR\x1b[0m\x1b[31m=\x1b[0mPANIC=runtime error: invalid memory address or nil pointer dereference\n\x1b[31m ↳ normal_panic\x1b[0m\n\x1b[31m ↳ stack\x1b[0m\x1b[31m=\x1b[0man error with a stacktrace has occurred\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n\x1b[31m ↳ \x1b[0m\x1b[31mstacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n", + // .cause.cause=error with 2 causes + // .cause.cause.0=cause 1 + // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry + // /: + // testing.tRunner + // /: + // runtime.goexit + // /: + // .cause.1.cause=deeper error with two causes + // .cause.cause.0=deeper cause 1 + // .cause.1=deeper cause 2 + // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry + // /: + // testing.tRunner + // /: + // runtime.goexit + // /: + // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry + // /: + // testing.tRunner + // /: + // runtime.goexit + // /: + // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry + // /: + // testing.tRunner + // /: + // runtime.goexit + // /: + // ↳ nil_panic_PANIC_DISPLAYING_ERROR=PANIC=Panic! + // ↳ normal_panic + // ↳ stack=an error with a stacktrace has occurred + // .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry + // /: + // testing.tRunner + // /: + // runtime.goexit + // /: + // ↳ stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry + // /: + // testing.tRunner + // /: + expected: "\x1b[90m4:33PM\x1b[0m\x1b[31m \x1b[0m\x1b[31mERR\x1b[0m\x1b[31m \x1b[0m\x1b[1m\x1b[31m>\x1b[0m\x1b[0m\x1b[31m \x1b[0mtest message\x1b[31m \x1b[0m\x1b[31mnamed_stracktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/:\x1b[31m\\n\x1b[0mtesting.tRunner\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/:\n\x1b[31m ↳ error\x1b[0m\x1b[31m=\x1b[0msomething \x1b[31m\\n\x1b[0mwent wrong\n\x1b[31m ↳ nested\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with stacktrace\n \x1b[31m.cause\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with 2 causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mcause 1\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n \x1b[31m.cause.1\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0mdeeper error with two causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mdeeper cause 1\n \x1b[31m.cause.1\x1b[0m\x1b[31m=\x1b[0mdeeper cause 2\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n\x1b[31m ↳ nil_panic_PANIC_DISPLAYING_ERROR\x1b[0m\x1b[31m=\x1b[0mPANIC=Panic!\n\x1b[31m ↳ normal_panic\x1b[0m\n\x1b[31m ↳ stack\x1b[0m\x1b[31m=\x1b[0man error with a stacktrace has occurred\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n runtime.goexit\n \t/:\n\x1b[31m ↳ \x1b[0m\x1b[31mstacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/:\n testing.tRunner\n \t/:\n", ent: zapcore.Entry{ Level: zapcore.ErrorLevel, Message: "test message", @@ -341,11 +354,7 @@ func (t testStableMap) MarshalLogObject(encoder zapcore.ObjectEncoder) error { type testPanicError string func (t *testPanicError) Error() string { - if t == nil { - return string(*t) - } else { - panic(*t) - } + panic("Panic!") } type nilCauseError struct{} diff --git a/error.go b/error.go index 522f47a..047e6b9 100644 --- a/error.go +++ b/error.go @@ -47,7 +47,7 @@ func (e *prettyConsoleEncoder) encodeError(key string, err error) (retErr error) } enc.addSafeString("") } - e.buf.Write(enc.buf.Bytes()) + _, _ = e.buf.Write(enc.buf.Bytes()) // Explicitly ignore errors putPrettyConsoleEncoder(enc) e.inList = true diff --git a/object.go b/object.go index 2df61b4..dd71687 100644 --- a/object.go +++ b/object.go @@ -116,12 +116,12 @@ func (e *prettyConsoleEncoder) AddReflected(key string, value interface{}) error switch v := value.(type) { case formattedString: - iw.Write([]byte(v)) + if _, err := iw.Write([]byte(v)); err != nil { + return err + } default: - if reflectedEncoder := e.cfg.NewReflectedEncoder(iw); e != nil { - if err := reflectedEncoder.Encode(value); err != nil { - return err - } + if err := e.cfg.NewReflectedEncoder(iw).Encode(value); err != nil { + return err } if l-enc.buf.Len() == 0 { // User-supplied reflectedEncoder is a no-op. Fall back to dd @@ -183,9 +183,7 @@ func (e *prettyConsoleEncoder) AddDuration(key string, value time.Duration) { cur := e.buf.Len() // Both of these append, and we're at the first element of the sublist e.inList = false - if durationEncoder := e.cfg.EncodeDuration; e != nil { - durationEncoder(value, e) - } + e.cfg.EncodeDuration(value, e) if cur == e.buf.Len() { // User-supplied EncodeDuration is a no-op. Fall back to Go format e.buf.AppendString(value.String()) diff --git a/pool.go b/pool.go index 7ca978d..0e6d5fd 100644 --- a/pool.go +++ b/pool.go @@ -21,10 +21,6 @@ func getRecordingEncoder() *recordingEncoder { return _recorderPool.Get().(*recordingEncoder) } -func putRecordingEncoder() *recordingEncoder { - return _recorderPool.Get().(*recordingEncoder) -} - func getPrettyConsoleEncoder() *prettyConsoleEncoder { return _prettyConsolePool.Get().(*prettyConsoleEncoder) }