fastpastebin/vendor/github.com/rs/zerolog/fields.go

243 lines
5.4 KiB
Go
Raw Normal View History

2018-04-30 18:42:17 +05:00
package zerolog
import (
2019-03-07 07:56:50 +05:00
"net"
2018-04-30 18:42:17 +05:00
"sort"
"time"
)
func appendFields(dst []byte, fields map[string]interface{}) []byte {
keys := make([]string, 0, len(fields))
for key := range fields {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
2019-03-07 07:56:50 +05:00
dst = enc.AppendKey(dst, key)
val := fields[key]
if val, ok := val.(LogObjectMarshaler); ok {
e := newEvent(nil, 0)
e.buf = e.buf[:0]
e.appendObject(val)
dst = append(dst, e.buf...)
putEvent(e)
continue
}
switch val := val.(type) {
2018-04-30 18:42:17 +05:00
case string:
2019-03-07 07:56:50 +05:00
dst = enc.AppendString(dst, val)
2018-04-30 18:42:17 +05:00
case []byte:
2019-03-07 07:56:50 +05:00
dst = enc.AppendBytes(dst, val)
2018-04-30 18:42:17 +05:00
case error:
2019-03-07 07:56:50 +05:00
marshaled := ErrorMarshalFunc(val)
switch m := marshaled.(type) {
case LogObjectMarshaler:
e := newEvent(nil, 0)
e.buf = e.buf[:0]
e.appendObject(m)
dst = append(dst, e.buf...)
putEvent(e)
case error:
dst = enc.AppendString(dst, m.Error())
case string:
dst = enc.AppendString(dst, m)
default:
dst = enc.AppendInterface(dst, m)
}
2018-04-30 18:42:17 +05:00
case []error:
2019-03-07 07:56:50 +05:00
dst = enc.AppendArrayStart(dst)
for i, err := range val {
marshaled := ErrorMarshalFunc(err)
switch m := marshaled.(type) {
case LogObjectMarshaler:
e := newEvent(nil, 0)
e.buf = e.buf[:0]
e.appendObject(m)
dst = append(dst, e.buf...)
putEvent(e)
case error:
dst = enc.AppendString(dst, m.Error())
case string:
dst = enc.AppendString(dst, m)
default:
dst = enc.AppendInterface(dst, m)
}
if i < (len(val) - 1) {
enc.AppendArrayDelim(dst)
}
}
dst = enc.AppendArrayEnd(dst)
2018-04-30 18:42:17 +05:00
case bool:
2019-03-07 07:56:50 +05:00
dst = enc.AppendBool(dst, val)
2018-04-30 18:42:17 +05:00
case int:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInt(dst, val)
2018-04-30 18:42:17 +05:00
case int8:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInt8(dst, val)
2018-04-30 18:42:17 +05:00
case int16:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInt16(dst, val)
2018-04-30 18:42:17 +05:00
case int32:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInt32(dst, val)
2018-04-30 18:42:17 +05:00
case int64:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInt64(dst, val)
2018-04-30 18:42:17 +05:00
case uint:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUint(dst, val)
2018-04-30 18:42:17 +05:00
case uint8:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUint8(dst, val)
2018-04-30 18:42:17 +05:00
case uint16:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUint16(dst, val)
2018-04-30 18:42:17 +05:00
case uint32:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUint32(dst, val)
2018-04-30 18:42:17 +05:00
case uint64:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUint64(dst, val)
2018-04-30 18:42:17 +05:00
case float32:
2019-03-07 07:56:50 +05:00
dst = enc.AppendFloat32(dst, val)
2018-04-30 18:42:17 +05:00
case float64:
2019-03-07 07:56:50 +05:00
dst = enc.AppendFloat64(dst, val)
2018-04-30 18:42:17 +05:00
case time.Time:
2019-03-07 07:56:50 +05:00
dst = enc.AppendTime(dst, val, TimeFieldFormat)
2018-04-30 18:42:17 +05:00
case time.Duration:
2019-03-07 07:56:50 +05:00
dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
2018-04-30 18:42:17 +05:00
case *string:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendString(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *bool:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendBool(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *int:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendInt(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *int8:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendInt8(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *int16:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendInt16(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *int32:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendInt32(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *int64:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendInt64(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *uint:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendUint(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *uint8:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendUint8(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *uint16:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendUint16(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *uint32:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendUint32(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *uint64:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendUint64(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *float32:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendFloat32(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *float64:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendFloat64(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *time.Time:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendTime(dst, *val, TimeFieldFormat)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case *time.Duration:
2019-03-07 07:56:50 +05:00
if val != nil {
dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger)
} else {
dst = enc.AppendNil(dst)
}
2018-04-30 18:42:17 +05:00
case []string:
2019-03-07 07:56:50 +05:00
dst = enc.AppendStrings(dst, val)
2018-04-30 18:42:17 +05:00
case []bool:
2019-03-07 07:56:50 +05:00
dst = enc.AppendBools(dst, val)
2018-04-30 18:42:17 +05:00
case []int:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInts(dst, val)
2018-04-30 18:42:17 +05:00
case []int8:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInts8(dst, val)
2018-04-30 18:42:17 +05:00
case []int16:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInts16(dst, val)
2018-04-30 18:42:17 +05:00
case []int32:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInts32(dst, val)
2018-04-30 18:42:17 +05:00
case []int64:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInts64(dst, val)
2018-04-30 18:42:17 +05:00
case []uint:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUints(dst, val)
2018-04-30 18:42:17 +05:00
// case []uint8:
2019-03-07 07:56:50 +05:00
// dst = enc.AppendUints8(dst, val)
2018-04-30 18:42:17 +05:00
case []uint16:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUints16(dst, val)
2018-04-30 18:42:17 +05:00
case []uint32:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUints32(dst, val)
2018-04-30 18:42:17 +05:00
case []uint64:
2019-03-07 07:56:50 +05:00
dst = enc.AppendUints64(dst, val)
2018-04-30 18:42:17 +05:00
case []float32:
2019-03-07 07:56:50 +05:00
dst = enc.AppendFloats32(dst, val)
2018-04-30 18:42:17 +05:00
case []float64:
2019-03-07 07:56:50 +05:00
dst = enc.AppendFloats64(dst, val)
2018-04-30 18:42:17 +05:00
case []time.Time:
2019-03-07 07:56:50 +05:00
dst = enc.AppendTimes(dst, val, TimeFieldFormat)
2018-04-30 18:42:17 +05:00
case []time.Duration:
2019-03-07 07:56:50 +05:00
dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
2018-04-30 18:42:17 +05:00
case nil:
2019-03-07 07:56:50 +05:00
dst = enc.AppendNil(dst)
case net.IP:
dst = enc.AppendIPAddr(dst, val)
case net.IPNet:
dst = enc.AppendIPPrefix(dst, val)
case net.HardwareAddr:
dst = enc.AppendMACAddr(dst, val)
2018-04-30 18:42:17 +05:00
default:
2019-03-07 07:56:50 +05:00
dst = enc.AppendInterface(dst, val)
2018-04-30 18:42:17 +05:00
}
}
return dst
}