Add support for pointer values in Fields

Fixes #46
This commit is contained in:
Olivier Poitrey 2018-03-25 20:18:47 -07:00
parent 24cc441b11
commit d0ca9bbceb
2 changed files with 58 additions and 0 deletions

View File

@ -54,6 +54,38 @@ func appendFields(dst []byte, fields map[string]interface{}) []byte {
dst = json.AppendTime(dst, val, TimeFieldFormat) dst = json.AppendTime(dst, val, TimeFieldFormat)
case time.Duration: case time.Duration:
dst = json.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger) dst = json.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
case *string:
dst = json.AppendString(dst, *val)
case *bool:
dst = json.AppendBool(dst, *val)
case *int:
dst = json.AppendInt(dst, *val)
case *int8:
dst = json.AppendInt8(dst, *val)
case *int16:
dst = json.AppendInt16(dst, *val)
case *int32:
dst = json.AppendInt32(dst, *val)
case *int64:
dst = json.AppendInt64(dst, *val)
case *uint:
dst = json.AppendUint(dst, *val)
case *uint8:
dst = json.AppendUint8(dst, *val)
case *uint16:
dst = json.AppendUint16(dst, *val)
case *uint32:
dst = json.AppendUint32(dst, *val)
case *uint64:
dst = json.AppendUint64(dst, *val)
case *float32:
dst = json.AppendFloat32(dst, *val)
case *float64:
dst = json.AppendFloat64(dst, *val)
case *time.Time:
dst = json.AppendTime(dst, *val, TimeFieldFormat)
case *time.Duration:
dst = json.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger)
case []string: case []string:
dst = json.AppendStrings(dst, val) dst = json.AppendStrings(dst, val)
case []bool: case []bool:

View File

@ -135,6 +135,32 @@ func TestFieldsMap(t *testing.T) {
} }
} }
func TestFieldsMapPnt(t *testing.T) {
out := &bytes.Buffer{}
log := New(out)
log.Log().Fields(map[string]interface{}{
"string": new(string),
"bool": new(bool),
"int": new(int),
"int8": new(int8),
"int16": new(int16),
"int32": new(int32),
"int64": new(int64),
"uint": new(uint),
"uint8": new(uint8),
"uint16": new(uint16),
"uint32": new(uint32),
"uint64": new(uint64),
"float32": new(float32),
"float64": new(float64),
"dur": new(time.Duration),
"time": new(time.Time),
}).Msg("")
if got, want := out.String(), `{"bool":false,"dur":0,"float32":0,"float64":0,"int":0,"int16":0,"int32":0,"int64":0,"int8":0,"string":"","time":"0001-01-01T00:00:00Z","uint":0,"uint16":0,"uint32":0,"uint64":0,"uint8":0}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
func TestFields(t *testing.T) { func TestFields(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
log := New(out) log := New(out)