Skip to content

fix!: handle sql/driver.Valuer types properly in slogjson #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slog

import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -70,6 +71,14 @@ func marshalList(rv reflect.Value) []byte {
}

func encode(v interface{}) []byte {
if vr, ok := v.(driver.Valuer); ok {
var err error
v, err = vr.Value()
if err != nil {
return encodeJSON(fmt.Sprintf("error calling Value: %v", err))
}
}

switch v := v.(type) {
case json.Marshaler:
return encodeJSON(v)
Expand Down
6 changes: 3 additions & 3 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func TestMap(t *testing.T) {
{
"msg": "wrap1",
"fun": "cdr.dev/slog_test.TestMap.func2",
"loc": "`+mapTestFile+`:41"
"loc": "`+mapTestFile+`:41"
},
{
"msg": "wrap2",
"fun": "cdr.dev/slog_test.TestMap.func2",
"loc": "`+mapTestFile+`:42"
"loc": "`+mapTestFile+`:42"
},
"EOF"
],
Expand All @@ -93,7 +93,7 @@ func TestMap(t *testing.T) {
{
"msg": "failed to marshal to JSON",
"fun": "cdr.dev/slog.encodeJSON",
"loc": "`+mapTestFile+`:131"
"loc": "`+mapTestFile+`:140"
},
"json: error calling MarshalJSON for type slog_test.complexJSON: json: unsupported type: complex128"
],
Expand Down
29 changes: 28 additions & 1 deletion sloggers/slogjson/slogjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slogjson_test
import (
"bytes"
"context"
"database/sql"
"fmt"
"runtime"
"testing"
Expand Down Expand Up @@ -33,7 +34,33 @@ func TestMake(t *testing.T) {
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))

j := entryjson.Filter(b.String(), "ts")
exp := fmt.Sprintf(`{"level":"ERROR","msg":"line1\n\nline2","caller":"%v:33","func":"cdr.dev/slog/sloggers/slogjson_test.TestMake","logger_names":["named"],"trace":"%v","span":"%v","fields":{"wowow":"me\nyou"}}
exp := fmt.Sprintf(`{"level":"ERROR","msg":"line1\n\nline2","caller":"%v:34","func":"cdr.dev/slog/sloggers/slogjson_test.TestMake","logger_names":["named"],"trace":"%v","span":"%v","fields":{"wowow":"me\nyou"}}
`, slogjsonTestFile, span.SpanContext().TraceID().String(), span.SpanContext().SpanID().String())
assert.Equal(t, "entry", exp, j)
}

func TestNoDriverValue(t *testing.T) {
t.Parallel()

b := &bytes.Buffer{}
l := slog.Make(slogjson.Sink(b))
l = l.Named("named")
validField := sql.NullString{
String: "cat",
Valid: true,
}
invalidField := sql.NullString{
String: "dog",
Valid: false,
}
validInt := sql.NullInt64{
Int64: 42,
Valid: true,
}
l.Error(bg, "error!", slog.F("inval", invalidField), slog.F("val", validField), slog.F("int", validInt))

j := entryjson.Filter(b.String(), "ts")
exp := fmt.Sprintf(`{"level":"ERROR","msg":"error!","caller":"%v:60","func":"cdr.dev/slog/sloggers/slogjson_test.TestNoDriverValue","logger_names":["named"],"fields":{"inval":null,"val":"cat","int":42}}
`, slogjsonTestFile)
assert.Equal(t, "entry", exp, j)
}