Skip to content

Commit

Permalink
Merge pull request #333 from moov-io/check-nil-on-fmt-stringer
Browse files Browse the repository at this point in the history
Check nil on fmt stringer
  • Loading branch information
kolaworld authored Oct 26, 2023
2 parents f9148cd + 3c23cdb commit 967cfe2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions log/model_valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"encoding/base64"
"fmt"
"reflect"
"strings"
"time"
)
Expand Down Expand Up @@ -93,6 +94,10 @@ func ByteBase64(b []byte) Valuer {
}

func Stringer(s fmt.Stringer) Valuer {
if v := reflect.ValueOf(s); v.Kind() == reflect.Pointer && v.IsNil() {
return &any{nil}
}

return &any{s.String()}
}

Expand Down
10 changes: 10 additions & 0 deletions log/model_valuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ func TestValuer_Stringer(t *testing.T) {
}).Log("log with .String() key/value pair")

require.Contains(t, out.String(), `mode=PRODUCTION`)

out, logger = log.NewBufferLogger()

var n *Mode

logger.With(log.Fields{
"mode": log.Stringer(n),
}).Log("log with nil .String() key/value pair")

require.Contains(t, out.String(), `mode=null`)
}

0 comments on commit 967cfe2

Please sign in to comment.