Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Set nil pointer to string nil when converting key values. #230

Merged
merged 2 commits into from Jan 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion log/util.go
@@ -1,6 +1,9 @@
package log

import "fmt"
import (
"fmt"
"reflect"
)

// InterleavedKVToFields converts keyValues a la Span.LogKV() to a Field slice
// a la Span.LogFields().
Expand Down Expand Up @@ -46,6 +49,10 @@ func InterleavedKVToFields(keyValues ...interface{}) ([]Field, error) {
case float64:
fields[i] = Float64(key, typedVal)
default:
if typedVal == nil || (reflect.ValueOf(typedVal).Kind() == reflect.Ptr && reflect.ValueOf(typedVal).IsNil()) {
fields[i] = String(key, "nil")
continue
}
// When in doubt, coerce to a string
fields[i] = String(key, fmt.Sprint(typedVal))
}
Expand Down
86 changes: 86 additions & 0 deletions log/util_test.go
@@ -0,0 +1,86 @@
package log

import (
"errors"
"io"
"testing"

"github.com/stretchr/testify/assert"
)

var nilInterface io.Reader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be scoped inside the test function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure ! Will do


func TestInterleavedKVToFields(t *testing.T) {

tests := []struct {
name string
keyValues []interface{}
want []Field
wantErr bool
}{
{
"incorrect pair",
[]interface{}{"test"},
nil,
true,
},
{
"non string key",
[]interface{}{struct{}{}, "foo"},
nil,
true,
},
{
"happy path",
[]interface{}{
"bool", true,
"string", "string",
"int", int(1),
"int8", int8(2),
"int16", int16(3),
"int64", int64(4),
"uint", uint(5),
"uint64", uint64(6),
"uint8", uint8(7),
"uint16", uint16(8),
"uint32", uint32(9),
"float32", float32(10),
"float64", float64(11),
"int32", int32(12),
"stringer", errors.New("err"),
"nilInterface", nilInterface,
"nil", nil,
},
[]Field{
Bool("bool", true),
String("string", "string"),
Int("int", int(1)),
Int32("int8", int32(2)),
Int32("int16", int32(3)),
Int64("int64", int64(4)),
Uint64("uint", uint64(5)),
Uint64("uint64", uint64(6)),
Uint32("uint8", uint32(7)),
Uint32("uint16", uint32(8)),
Uint32("uint32", uint32(9)),
Float32("float32", float32(10)),
Float64("float64", float64(11)),
Int32("int32", int32(12)),
String("stringer", errors.New("err").Error()),
String("nilInterface", "nil"),
String("nil", "nil"),
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := InterleavedKVToFields(tt.keyValues...)
if (err != nil) != tt.wantErr {
t.Errorf("InterleavedKVToFields() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
}