From ea1184be2bbeb1b1b747028ec12d115985632214 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Thu, 10 May 2018 15:01:41 -0700 Subject: [PATCH] Get back some ns by removing the extra inferance added by binary support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit benchstat old new name old time/op new time/op delta LogEmpty-8 15.2ns ±14% 13.4ns ± 3% -12.11% (p=0.008 n=5+5) Disabled-8 2.50ns ± 1% 2.28ns ± 6% -8.81% (p=0.008 n=5+5) Info-8 44.4ns ± 1% 36.4ns ± 4% -17.99% (p=0.008 n=5+5) ContextFields-8 47.6ns ± 1% 39.4ns ± 7% -17.30% (p=0.008 n=5+5) ContextAppend-8 18.9ns ± 4% 15.2ns ± 4% -19.68% (p=0.008 n=5+5) LogFields-8 181ns ± 2% 173ns ± 2% -4.63% (p=0.008 n=5+5) LogArrayObject-8 530ns ± 3% 487ns ± 3% -8.11% (p=0.008 n=5+5) LogFieldType/Int-8 29.5ns ± 3% 28.8ns ± 2% ~ (p=0.167 n=5+5) LogFieldType/Interface-8 180ns ± 7% 175ns ± 4% ~ (p=0.579 n=5+5) LogFieldType/Interface(Object)-8 87.8ns ± 3% 80.5ns ± 1% -8.29% (p=0.008 n=5+5) LogFieldType/Object-8 83.7ns ± 2% 77.2ns ± 3% -7.76% (p=0.008 n=5+5) LogFieldType/Bools-8 34.6ns ± 3% 32.3ns ± 6% -6.64% (p=0.032 n=5+5) LogFieldType/Float-8 43.0ns ± 4% 40.5ns ± 4% -5.86% (p=0.016 n=5+5) LogFieldType/Str-8 29.8ns ± 2% 26.5ns ± 5% -11.01% (p=0.008 n=5+5) LogFieldType/Err-8 32.8ns ± 2% 29.8ns ± 4% -9.21% (p=0.008 n=5+5) LogFieldType/Durs-8 309ns ± 3% 304ns ± 3% ~ (p=0.238 n=5+5) LogFieldType/Floats-8 175ns ± 2% 174ns ± 3% ~ (p=0.968 n=5+5) LogFieldType/Strs-8 51.0ns ± 3% 48.4ns ± 6% -5.06% (p=0.032 n=5+5) LogFieldType/Dur-8 44.5ns ± 3% 41.3ns ± 3% -7.11% (p=0.008 n=5+5) LogFieldType/Interface(Objects)-8 758ns ± 3% 760ns ± 6% ~ (p=1.000 n=5+5) LogFieldType/Interfaces-8 772ns ± 5% 762ns ± 4% ~ (p=0.794 n=5+5) LogFieldType/Bool-8 28.0ns ± 6% 26.5ns ± 9% ~ (p=0.143 n=5+5) LogFieldType/Ints-8 49.6ns ± 2% 46.2ns ± 2% -6.70% (p=0.008 n=5+5) LogFieldType/Errs-8 46.5ns ±11% 40.9ns ± 4% -11.92% (p=0.008 n=5+5) LogFieldType/Time-8 115ns ± 3% 113ns ± 3% ~ (p=0.167 n=5+5) LogFieldType/Times-8 810ns ± 1% 811ns ± 3% ~ (p=0.889 n=5+5) ContextFieldType/Errs-8 158ns ± 6% 156ns ±12% ~ (p=1.000 n=5+5) ContextFieldType/Times-8 165ns ±11% 173ns ± 9% ~ (p=0.651 n=5+5) ContextFieldType/Interface-8 289ns ±13% 287ns ±11% ~ (p=0.690 n=5+5) ContextFieldType/Interface(Object)-8 285ns ±12% 297ns ± 6% ~ (p=0.238 n=5+5) ContextFieldType/Interface(Objects)-8 941ns ± 6% 941ns ± 5% ~ (p=1.000 n=5+5) ContextFieldType/Object-8 201ns ± 5% 210ns ±12% ~ (p=0.262 n=5+5) ContextFieldType/Ints-8 173ns ±10% 165ns ± 9% ~ (p=0.198 n=5+5) ContextFieldType/Floats-8 297ns ± 6% 292ns ± 7% ~ (p=0.579 n=5+5) ContextFieldType/Timestamp-8 174ns ± 9% 174ns ±11% ~ (p=0.810 n=5+5) ContextFieldType/Durs-8 445ns ± 9% 425ns ± 3% ~ (p=0.151 n=5+5) ContextFieldType/Interfaces-8 944ns ± 6% 876ns ±10% ~ (p=0.095 n=5+5) ContextFieldType/Strs-8 179ns ±11% 165ns ±13% ~ (p=0.135 n=5+5) ContextFieldType/Dur-8 158ns ± 8% 160ns ±19% ~ (p=1.000 n=5+5) ContextFieldType/Time-8 152ns ±15% 148ns ±14% ~ (p=0.952 n=5+5) ContextFieldType/Str-8 146ns ±12% 147ns ±16% ~ (p=0.841 n=5+5) ContextFieldType/Err-8 138ns ±12% 145ns ±17% ~ (p=0.595 n=5+5) ContextFieldType/Int-8 145ns ±10% 146ns ±13% ~ (p=0.873 n=5+5) ContextFieldType/Float-8 181ns ± 9% 162ns ±12% ~ (p=0.151 n=5+5) ContextFieldType/Bool-8 153ns ±10% 131ns ±19% ~ (p=0.063 n=5+5) ContextFieldType/Bools-8 149ns ±11% 160ns ±16% ~ (p=0.500 n=5+5) --- array.go | 96 ++++++++-------- context.go | 92 ++++++++-------- encoder.go | 58 ++++++++++ encoder_cbor.go | 204 +--------------------------------- encoder_json.go | 205 +---------------------------------- event.go | 110 +++++++++---------- fields.go | 114 +++++++++---------- internal/cbor/base.go | 28 ++--- internal/cbor/string.go | 10 +- internal/cbor/string_test.go | 10 +- internal/cbor/time.go | 26 ++--- internal/cbor/time_test.go | 8 +- internal/cbor/types.go | 198 +++++++++++++++++---------------- internal/cbor/types_test.go | 44 ++++---- internal/json/base.go | 16 +-- internal/json/bytes.go | 4 +- internal/json/bytes_test.go | 28 ++--- internal/json/string.go | 8 +- internal/json/string_test.go | 4 +- internal/json/time.go | 16 +-- internal/json/types.go | 124 +++++++++++++-------- internal/json/types_test.go | 36 +++--- log.go | 2 +- 23 files changed, 578 insertions(+), 863 deletions(-) create mode 100644 encoder.go diff --git a/array.go b/array.go index 66c2a4f..47402ec 100644 --- a/array.go +++ b/array.go @@ -33,163 +33,163 @@ func (*Array) MarshalZerologArray(*Array) { } func (a *Array) write(dst []byte) []byte { - dst = appendArrayStart(dst) + dst = enc.AppendArrayStart(dst) if len(a.buf) > 0 { dst = append(append(dst, a.buf...)) } - dst = appendArrayEnd(dst) + dst = enc.AppendArrayEnd(dst) arrayPool.Put(a) return dst } // Object marshals an object that implement the LogObjectMarshaler -// interface and append it to the array. +// interface and enc.Append it to the array. func (a *Array) Object(obj LogObjectMarshaler) *Array { e := Dict() obj.MarshalZerologObject(e) - e.buf = appendEndMarker(e.buf) - a.buf = append(appendArrayDelim(a.buf), e.buf...) + e.buf = enc.AppendEndMarker(e.buf) + a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...) eventPool.Put(e) return a } -// Str append the val as a string to the array. +// Str enc.Append the val as a string to the array. func (a *Array) Str(val string) *Array { - a.buf = appendString(appendArrayDelim(a.buf), val) + a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val) return a } -// Bytes append the val as a string to the array. +// Bytes enc.Append the val as a string to the array. func (a *Array) Bytes(val []byte) *Array { - a.buf = appendBytes(appendArrayDelim(a.buf), val) + a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val) return a } -// Hex append the val as a hex string to the array. +// Hex enc.Append the val as a hex string to the array. func (a *Array) Hex(val []byte) *Array { - a.buf = appendHex(appendArrayDelim(a.buf), val) + a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val) return a } -// Err append the err as a string to the array. +// Err enc.Append the err as a string to the array. func (a *Array) Err(err error) *Array { - a.buf = appendError(appendArrayDelim(a.buf), err) + a.buf = enc.AppendError(enc.AppendArrayDelim(a.buf), err) return a } -// Bool append the val as a bool to the array. +// Bool enc.Append the val as a bool to the array. func (a *Array) Bool(b bool) *Array { - a.buf = appendBool(appendArrayDelim(a.buf), b) + a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b) return a } -// Int append i as a int to the array. +// Int enc.Append i as a int to the array. func (a *Array) Int(i int) *Array { - a.buf = appendInt(appendArrayDelim(a.buf), i) + a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i) return a } -// Int8 append i as a int8 to the array. +// Int8 enc.Append i as a int8 to the array. func (a *Array) Int8(i int8) *Array { - a.buf = appendInt8(appendArrayDelim(a.buf), i) + a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i) return a } -// Int16 append i as a int16 to the array. +// Int16 enc.Append i as a int16 to the array. func (a *Array) Int16(i int16) *Array { - a.buf = appendInt16(appendArrayDelim(a.buf), i) + a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i) return a } -// Int32 append i as a int32 to the array. +// Int32 enc.Append i as a int32 to the array. func (a *Array) Int32(i int32) *Array { - a.buf = appendInt32(appendArrayDelim(a.buf), i) + a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i) return a } -// Int64 append i as a int64 to the array. +// Int64 enc.Append i as a int64 to the array. func (a *Array) Int64(i int64) *Array { - a.buf = appendInt64(appendArrayDelim(a.buf), i) + a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i) return a } -// Uint append i as a uint to the array. +// Uint enc.Append i as a uint to the array. func (a *Array) Uint(i uint) *Array { - a.buf = appendUint(appendArrayDelim(a.buf), i) + a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i) return a } -// Uint8 append i as a uint8 to the array. +// Uint8 enc.Append i as a uint8 to the array. func (a *Array) Uint8(i uint8) *Array { - a.buf = appendUint8(appendArrayDelim(a.buf), i) + a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i) return a } -// Uint16 append i as a uint16 to the array. +// Uint16 enc.Append i as a uint16 to the array. func (a *Array) Uint16(i uint16) *Array { - a.buf = appendUint16(appendArrayDelim(a.buf), i) + a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i) return a } -// Uint32 append i as a uint32 to the array. +// Uint32 enc.Append i as a uint32 to the array. func (a *Array) Uint32(i uint32) *Array { - a.buf = appendUint32(appendArrayDelim(a.buf), i) + a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i) return a } -// Uint64 append i as a uint64 to the array. +// Uint64 enc.Append i as a uint64 to the array. func (a *Array) Uint64(i uint64) *Array { - a.buf = appendUint64(appendArrayDelim(a.buf), i) + a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i) return a } -// Float32 append f as a float32 to the array. +// Float32 enc.Append f as a float32 to the array. func (a *Array) Float32(f float32) *Array { - a.buf = appendFloat32(appendArrayDelim(a.buf), f) + a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f) return a } -// Float64 append f as a float64 to the array. +// Float64 enc.Append f as a float64 to the array. func (a *Array) Float64(f float64) *Array { - a.buf = appendFloat64(appendArrayDelim(a.buf), f) + a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f) return a } -// Time append t formated as string using zerolog.TimeFieldFormat. +// Time enc.Append t formated as string using zerolog.TimeFieldFormat. func (a *Array) Time(t time.Time) *Array { - a.buf = appendTime(appendArrayDelim(a.buf), t, TimeFieldFormat) + a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat) return a } -// Dur append d to the array. +// Dur enc.Append d to the array. func (a *Array) Dur(d time.Duration) *Array { - a.buf = appendDuration(appendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger) + a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger) return a } -// Interface append i marshaled using reflection. +// Interface enc.Append i marshaled using reflection. func (a *Array) Interface(i interface{}) *Array { if obj, ok := i.(LogObjectMarshaler); ok { return a.Object(obj) } - a.buf = appendInterface(appendArrayDelim(a.buf), i) + a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), i) return a } // IPAddr adds IPv4 or IPv6 address to the array func (a *Array) IPAddr(ip net.IP) *Array { - a.buf = appendIPAddr(appendArrayDelim(a.buf), ip) + a.buf = enc.AppendIPAddr(enc.AppendArrayDelim(a.buf), ip) return a } // IPPrefix adds IPv4 or IPv6 Prefix (IP + mask) to the array func (a *Array) IPPrefix(pfx net.IPNet) *Array { - a.buf = appendIPPrefix(appendArrayDelim(a.buf), pfx) + a.buf = enc.AppendIPPrefix(enc.AppendArrayDelim(a.buf), pfx) return a } // MACAddr adds a MAC (Ethernet) address to the array func (a *Array) MACAddr(ha net.HardwareAddr) *Array { - a.buf = appendMACAddr(appendArrayDelim(a.buf), ha) + a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha) return a } diff --git a/context.go b/context.go index 146a5df..d9f9c22 100644 --- a/context.go +++ b/context.go @@ -24,8 +24,8 @@ func (c Context) Fields(fields map[string]interface{}) Context { // Dict adds the field key with the dict to the logger context. func (c Context) Dict(key string, dict *Event) Context { - dict.buf = appendEndMarker(dict.buf) - c.l.context = append(appendKey(c.l.context, key), dict.buf...) + dict.buf = enc.AppendEndMarker(dict.buf) + c.l.context = append(enc.AppendKey(c.l.context, key), dict.buf...) eventPool.Put(dict) return c } @@ -34,7 +34,7 @@ func (c Context) Dict(key string, dict *Event) Context { // Use zerolog.Arr() to create the array or pass a type that // implement the LogArrayMarshaler interface. func (c Context) Array(key string, arr LogArrayMarshaler) Context { - c.l.context = appendKey(c.l.context, key) + c.l.context = enc.AppendKey(c.l.context, key) if arr, ok := arr.(*Array); ok { c.l.context = arr.write(c.l.context) return c @@ -54,32 +54,32 @@ func (c Context) Array(key string, arr LogArrayMarshaler) Context { func (c Context) Object(key string, obj LogObjectMarshaler) Context { e := newEvent(levelWriterAdapter{ioutil.Discard}, 0) e.Object(key, obj) - c.l.context = appendObjectData(c.l.context, e.buf) + c.l.context = enc.AppendObjectData(c.l.context, e.buf) eventPool.Put(e) return c } // Str adds the field key with val as a string to the logger context. func (c Context) Str(key, val string) Context { - c.l.context = appendString(appendKey(c.l.context, key), val) + c.l.context = enc.AppendString(enc.AppendKey(c.l.context, key), val) return c } // Strs adds the field key with val as a string to the logger context. func (c Context) Strs(key string, vals []string) Context { - c.l.context = appendStrings(appendKey(c.l.context, key), vals) + c.l.context = enc.AppendStrings(enc.AppendKey(c.l.context, key), vals) return c } // Bytes adds the field key with val as a []byte to the logger context. func (c Context) Bytes(key string, val []byte) Context { - c.l.context = appendBytes(appendKey(c.l.context, key), val) + c.l.context = enc.AppendBytes(enc.AppendKey(c.l.context, key), val) return c } // Hex adds the field key with val as a hex string to the logger context. func (c Context) Hex(key string, val []byte) Context { - c.l.context = appendHex(appendKey(c.l.context, key), val) + c.l.context = enc.AppendHex(enc.AppendKey(c.l.context, key), val) return c } @@ -88,21 +88,21 @@ func (c Context) Hex(key string, val []byte) Context { // No sanity check is performed on b; it must not contain carriage returns and // be valid JSON. func (c Context) RawJSON(key string, b []byte) Context { - c.l.context = appendJSON(appendKey(c.l.context, key), b) + c.l.context = appendJSON(enc.AppendKey(c.l.context, key), b) return c } // AnErr adds the field key with err as a string to the logger context. func (c Context) AnErr(key string, err error) Context { if err != nil { - c.l.context = appendError(appendKey(c.l.context, key), err) + c.l.context = enc.AppendError(enc.AppendKey(c.l.context, key), err) } return c } // Errs adds the field key with errs as an array of strings to the logger context. func (c Context) Errs(key string, errs []error) Context { - c.l.context = appendErrors(appendKey(c.l.context, key), errs) + c.l.context = enc.AppendErrors(enc.AppendKey(c.l.context, key), errs) return c } @@ -110,164 +110,164 @@ func (c Context) Errs(key string, errs []error) Context { // To customize the key name, change zerolog.ErrorFieldName. func (c Context) Err(err error) Context { if err != nil { - c.l.context = appendError(appendKey(c.l.context, ErrorFieldName), err) + c.l.context = enc.AppendError(enc.AppendKey(c.l.context, ErrorFieldName), err) } return c } // Bool adds the field key with val as a bool to the logger context. func (c Context) Bool(key string, b bool) Context { - c.l.context = appendBool(appendKey(c.l.context, key), b) + c.l.context = enc.AppendBool(enc.AppendKey(c.l.context, key), b) return c } // Bools adds the field key with val as a []bool to the logger context. func (c Context) Bools(key string, b []bool) Context { - c.l.context = appendBools(appendKey(c.l.context, key), b) + c.l.context = enc.AppendBools(enc.AppendKey(c.l.context, key), b) return c } // Int adds the field key with i as a int to the logger context. func (c Context) Int(key string, i int) Context { - c.l.context = appendInt(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInt(enc.AppendKey(c.l.context, key), i) return c } // Ints adds the field key with i as a []int to the logger context. func (c Context) Ints(key string, i []int) Context { - c.l.context = appendInts(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInts(enc.AppendKey(c.l.context, key), i) return c } // Int8 adds the field key with i as a int8 to the logger context. func (c Context) Int8(key string, i int8) Context { - c.l.context = appendInt8(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInt8(enc.AppendKey(c.l.context, key), i) return c } // Ints8 adds the field key with i as a []int8 to the logger context. func (c Context) Ints8(key string, i []int8) Context { - c.l.context = appendInts8(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInts8(enc.AppendKey(c.l.context, key), i) return c } // Int16 adds the field key with i as a int16 to the logger context. func (c Context) Int16(key string, i int16) Context { - c.l.context = appendInt16(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInt16(enc.AppendKey(c.l.context, key), i) return c } // Ints16 adds the field key with i as a []int16 to the logger context. func (c Context) Ints16(key string, i []int16) Context { - c.l.context = appendInts16(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInts16(enc.AppendKey(c.l.context, key), i) return c } // Int32 adds the field key with i as a int32 to the logger context. func (c Context) Int32(key string, i int32) Context { - c.l.context = appendInt32(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInt32(enc.AppendKey(c.l.context, key), i) return c } // Ints32 adds the field key with i as a []int32 to the logger context. func (c Context) Ints32(key string, i []int32) Context { - c.l.context = appendInts32(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInts32(enc.AppendKey(c.l.context, key), i) return c } // Int64 adds the field key with i as a int64 to the logger context. func (c Context) Int64(key string, i int64) Context { - c.l.context = appendInt64(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInt64(enc.AppendKey(c.l.context, key), i) return c } // Ints64 adds the field key with i as a []int64 to the logger context. func (c Context) Ints64(key string, i []int64) Context { - c.l.context = appendInts64(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInts64(enc.AppendKey(c.l.context, key), i) return c } // Uint adds the field key with i as a uint to the logger context. func (c Context) Uint(key string, i uint) Context { - c.l.context = appendUint(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUint(enc.AppendKey(c.l.context, key), i) return c } // Uints adds the field key with i as a []uint to the logger context. func (c Context) Uints(key string, i []uint) Context { - c.l.context = appendUints(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUints(enc.AppendKey(c.l.context, key), i) return c } // Uint8 adds the field key with i as a uint8 to the logger context. func (c Context) Uint8(key string, i uint8) Context { - c.l.context = appendUint8(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUint8(enc.AppendKey(c.l.context, key), i) return c } // Uints8 adds the field key with i as a []uint8 to the logger context. func (c Context) Uints8(key string, i []uint8) Context { - c.l.context = appendUints8(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUints8(enc.AppendKey(c.l.context, key), i) return c } // Uint16 adds the field key with i as a uint16 to the logger context. func (c Context) Uint16(key string, i uint16) Context { - c.l.context = appendUint16(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUint16(enc.AppendKey(c.l.context, key), i) return c } // Uints16 adds the field key with i as a []uint16 to the logger context. func (c Context) Uints16(key string, i []uint16) Context { - c.l.context = appendUints16(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUints16(enc.AppendKey(c.l.context, key), i) return c } // Uint32 adds the field key with i as a uint32 to the logger context. func (c Context) Uint32(key string, i uint32) Context { - c.l.context = appendUint32(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUint32(enc.AppendKey(c.l.context, key), i) return c } // Uints32 adds the field key with i as a []uint32 to the logger context. func (c Context) Uints32(key string, i []uint32) Context { - c.l.context = appendUints32(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUints32(enc.AppendKey(c.l.context, key), i) return c } // Uint64 adds the field key with i as a uint64 to the logger context. func (c Context) Uint64(key string, i uint64) Context { - c.l.context = appendUint64(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUint64(enc.AppendKey(c.l.context, key), i) return c } // Uints64 adds the field key with i as a []uint64 to the logger context. func (c Context) Uints64(key string, i []uint64) Context { - c.l.context = appendUints64(appendKey(c.l.context, key), i) + c.l.context = enc.AppendUints64(enc.AppendKey(c.l.context, key), i) return c } // Float32 adds the field key with f as a float32 to the logger context. func (c Context) Float32(key string, f float32) Context { - c.l.context = appendFloat32(appendKey(c.l.context, key), f) + c.l.context = enc.AppendFloat32(enc.AppendKey(c.l.context, key), f) return c } // Floats32 adds the field key with f as a []float32 to the logger context. func (c Context) Floats32(key string, f []float32) Context { - c.l.context = appendFloats32(appendKey(c.l.context, key), f) + c.l.context = enc.AppendFloats32(enc.AppendKey(c.l.context, key), f) return c } // Float64 adds the field key with f as a float64 to the logger context. func (c Context) Float64(key string, f float64) Context { - c.l.context = appendFloat64(appendKey(c.l.context, key), f) + c.l.context = enc.AppendFloat64(enc.AppendKey(c.l.context, key), f) return c } // Floats64 adds the field key with f as a []float64 to the logger context. func (c Context) Floats64(key string, f []float64) Context { - c.l.context = appendFloats64(appendKey(c.l.context, key), f) + c.l.context = enc.AppendFloats64(enc.AppendKey(c.l.context, key), f) return c } @@ -290,31 +290,31 @@ func (c Context) Timestamp() Context { // Time adds the field key with t formated as string using zerolog.TimeFieldFormat. func (c Context) Time(key string, t time.Time) Context { - c.l.context = appendTime(appendKey(c.l.context, key), t, TimeFieldFormat) + c.l.context = enc.AppendTime(enc.AppendKey(c.l.context, key), t, TimeFieldFormat) return c } // Times adds the field key with t formated as string using zerolog.TimeFieldFormat. func (c Context) Times(key string, t []time.Time) Context { - c.l.context = appendTimes(appendKey(c.l.context, key), t, TimeFieldFormat) + c.l.context = enc.AppendTimes(enc.AppendKey(c.l.context, key), t, TimeFieldFormat) return c } // Dur adds the fields key with d divided by unit and stored as a float. func (c Context) Dur(key string, d time.Duration) Context { - c.l.context = appendDuration(appendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger) + c.l.context = enc.AppendDuration(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger) return c } // Durs adds the fields key with d divided by unit and stored as a float. func (c Context) Durs(key string, d []time.Duration) Context { - c.l.context = appendDurations(appendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger) + c.l.context = enc.AppendDurations(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger) return c } // Interface adds the field key with obj marshaled using reflection. func (c Context) Interface(key string, i interface{}) Context { - c.l.context = appendInterface(appendKey(c.l.context, key), i) + c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), i) return c } @@ -334,18 +334,18 @@ func (c Context) Caller() Context { // IPAddr adds IPv4 or IPv6 Address to the context func (c Context) IPAddr(key string, ip net.IP) Context { - c.l.context = appendIPAddr(appendKey(c.l.context, key), ip) + c.l.context = enc.AppendIPAddr(enc.AppendKey(c.l.context, key), ip) return c } // IPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the context func (c Context) IPPrefix(key string, pfx net.IPNet) Context { - c.l.context = appendIPPrefix(appendKey(c.l.context, key), pfx) + c.l.context = enc.AppendIPPrefix(enc.AppendKey(c.l.context, key), pfx) return c } // MACAddr adds MAC address to the context func (c Context) MACAddr(key string, ha net.HardwareAddr) Context { - c.l.context = appendMACAddr(appendKey(c.l.context, key), ha) + c.l.context = enc.AppendMACAddr(enc.AppendKey(c.l.context, key), ha) return c } diff --git a/encoder.go b/encoder.go new file mode 100644 index 0000000..4bbe27a --- /dev/null +++ b/encoder.go @@ -0,0 +1,58 @@ +package zerolog + +import ( + "net" + "time" +) + +type encoder interface { + AppendArrayDelim(dst []byte) []byte + AppendArrayEnd(dst []byte) []byte + AppendArrayStart(dst []byte) []byte + AppendBeginMarker(dst []byte) []byte + AppendBool(dst []byte, val bool) []byte + AppendBools(dst []byte, vals []bool) []byte + AppendBytes(dst, s []byte) []byte + AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte + AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte + AppendEndMarker(dst []byte) []byte + AppendError(dst []byte, err error) []byte + AppendErrors(dst []byte, errs []error) []byte + AppendFloat32(dst []byte, val float32) []byte + AppendFloat64(dst []byte, val float64) []byte + AppendFloats32(dst []byte, vals []float32) []byte + AppendFloats64(dst []byte, vals []float64) []byte + AppendHex(dst, s []byte) []byte + AppendIPAddr(dst []byte, ip net.IP) []byte + AppendIPPrefix(dst []byte, pfx net.IPNet) []byte + AppendInt(dst []byte, val int) []byte + AppendInt16(dst []byte, val int16) []byte + AppendInt32(dst []byte, val int32) []byte + AppendInt64(dst []byte, val int64) []byte + AppendInt8(dst []byte, val int8) []byte + AppendInterface(dst []byte, i interface{}) []byte + AppendInts(dst []byte, vals []int) []byte + AppendInts16(dst []byte, vals []int16) []byte + AppendInts32(dst []byte, vals []int32) []byte + AppendInts64(dst []byte, vals []int64) []byte + AppendInts8(dst []byte, vals []int8) []byte + AppendKey(dst []byte, key string) []byte + AppendLineBreak(dst []byte) []byte + AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte + AppendNil(dst []byte) []byte + AppendObjectData(dst []byte, o []byte) []byte + AppendString(dst []byte, s string) []byte + AppendStrings(dst []byte, vals []string) []byte + AppendTime(dst []byte, t time.Time, format string) []byte + AppendTimes(dst []byte, vals []time.Time, format string) []byte + AppendUint(dst []byte, val uint) []byte + AppendUint16(dst []byte, val uint16) []byte + AppendUint32(dst []byte, val uint32) []byte + AppendUint64(dst []byte, val uint64) []byte + AppendUint8(dst []byte, val uint8) []byte + AppendUints(dst []byte, vals []uint) []byte + AppendUints16(dst []byte, vals []uint16) []byte + AppendUints32(dst []byte, vals []uint32) []byte + AppendUints64(dst []byte, vals []uint64) []byte + AppendUints8(dst []byte, vals []uint8) []byte +} diff --git a/encoder_cbor.go b/encoder_cbor.go index d345c8f..f8d3fe9 100644 --- a/encoder_cbor.go +++ b/encoder_cbor.go @@ -5,203 +5,19 @@ package zerolog // This file contains bindings to do binary encoding. import ( - "net" - "time" - "github.com/rs/zerolog/internal/cbor" ) -func appendInterface(dst []byte, i interface{}) []byte { - return cbor.AppendInterface(dst, i) -} +var ( + _ encoder = (*cbor.Encoder)(nil) -func appendKey(dst []byte, s string) []byte { - return cbor.AppendKey(dst, s) -} - -func appendFloats64(dst []byte, f []float64) []byte { - return cbor.AppendFloats64(dst, f) -} - -func appendFloat64(dst []byte, f float64) []byte { - return cbor.AppendFloat64(dst, f) -} - -func appendFloats32(dst []byte, f []float32) []byte { - return cbor.AppendFloats32(dst, f) -} - -func appendFloat32(dst []byte, f float32) []byte { - return cbor.AppendFloat32(dst, f) -} - -func appendUints64(dst []byte, i []uint64) []byte { - return cbor.AppendUints64(dst, i) -} - -func appendUint64(dst []byte, i uint64) []byte { - return cbor.AppendUint64(dst, i) -} - -func appendUints32(dst []byte, i []uint32) []byte { - return cbor.AppendUints32(dst, i) -} - -func appendUint32(dst []byte, i uint32) []byte { - return cbor.AppendUint32(dst, i) -} - -func appendUints16(dst []byte, i []uint16) []byte { - return cbor.AppendUints16(dst, i) -} - -func appendUint16(dst []byte, i uint16) []byte { - return cbor.AppendUint16(dst, i) -} - -func appendUints8(dst []byte, i []uint8) []byte { - return cbor.AppendUints8(dst, i) -} - -func appendUint8(dst []byte, i uint8) []byte { - return cbor.AppendUint8(dst, i) -} - -func appendUints(dst []byte, i []uint) []byte { - return cbor.AppendUints(dst, i) -} - -func appendUint(dst []byte, i uint) []byte { - return cbor.AppendUint(dst, i) -} - -func appendInts64(dst []byte, i []int64) []byte { - return cbor.AppendInts64(dst, i) -} - -func appendInt64(dst []byte, i int64) []byte { - return cbor.AppendInt64(dst, i) -} - -func appendInts32(dst []byte, i []int32) []byte { - return cbor.AppendInts32(dst, i) -} - -func appendInt32(dst []byte, i int32) []byte { - return cbor.AppendInt32(dst, i) -} - -func appendInts16(dst []byte, i []int16) []byte { - return cbor.AppendInts16(dst, i) -} - -func appendInt16(dst []byte, i int16) []byte { - return cbor.AppendInt16(dst, i) -} - -func appendInts8(dst []byte, i []int8) []byte { - return cbor.AppendInts8(dst, i) -} - -func appendInt8(dst []byte, i int8) []byte { - return cbor.AppendInt8(dst, i) -} - -func appendInts(dst []byte, i []int) []byte { - return cbor.AppendInts(dst, i) -} - -func appendInt(dst []byte, i int) []byte { - return cbor.AppendInt(dst, i) -} - -func appendBools(dst []byte, b []bool) []byte { - return cbor.AppendBools(dst, b) -} - -func appendBool(dst []byte, b bool) []byte { - return cbor.AppendBool(dst, b) -} - -func appendError(dst []byte, e error) []byte { - return cbor.AppendError(dst, e) -} - -func appendErrors(dst []byte, e []error) []byte { - return cbor.AppendErrors(dst, e) -} - -func appendString(dst []byte, s string) []byte { - return cbor.AppendString(dst, s) -} - -func appendStrings(dst []byte, s []string) []byte { - return cbor.AppendStrings(dst, s) -} - -func appendDuration(dst []byte, t time.Duration, d time.Duration, fmt bool) []byte { - return cbor.AppendDuration(dst, t, d, fmt) -} - -func appendDurations(dst []byte, t []time.Duration, d time.Duration, fmt bool) []byte { - return cbor.AppendDurations(dst, t, d, fmt) -} - -func appendTimes(dst []byte, t []time.Time, fmt string) []byte { - return cbor.AppendTimes(dst, t, fmt) -} - -func appendTime(dst []byte, t time.Time, fmt string) []byte { - return cbor.AppendTime(dst, t, fmt) -} - -func appendEndMarker(dst []byte) []byte { - return cbor.AppendEndMarker(dst) -} - -func appendLineBreak(dst []byte) []byte { - // No line breaks needed in binary format. - return dst -} - -func appendBeginMarker(dst []byte) []byte { - return cbor.AppendBeginMarker(dst) -} - -func appendBytes(dst []byte, b []byte) []byte { - return cbor.AppendBytes(dst, b) -} - -func appendArrayStart(dst []byte) []byte { - return cbor.AppendArrayStart(dst) -} - -func appendArrayEnd(dst []byte) []byte { - return cbor.AppendArrayEnd(dst) -} - -func appendArrayDelim(dst []byte) []byte { - return cbor.AppendArrayDelim(dst) -} - -func appendObjectData(dst []byte, src []byte) []byte { - // Map begin character is present in the src, which - // should not be copied when appending to existing data. - return cbor.AppendObjectData(dst, src[1:]) -} + enc = cbor.Encoder{} +) func appendJSON(dst []byte, j []byte) []byte { return cbor.AppendEmbeddedJSON(dst, j) } -func appendNil(dst []byte) []byte { - return cbor.AppendNull(dst) -} - -func appendHex(dst []byte, val []byte) []byte { - return cbor.AppendHex(dst, val) -} - // decodeIfBinaryToString - converts a binary formatted log msg to a // JSON formatted String Log message. func decodeIfBinaryToString(in []byte) string { @@ -212,18 +28,6 @@ func decodeObjectToStr(in []byte) string { return cbor.DecodeObjectToStr(in) } -func appendIPAddr(dst []byte, ip net.IP) []byte { - return cbor.AppendIPAddr(dst, ip) -} - -func appendIPPrefix(dst []byte, pfx net.IPNet) []byte { - return cbor.AppendIPPrefix(dst, pfx) -} - -func appendMACAddr(dst []byte, ha net.HardwareAddr) []byte { - return cbor.AppendMACAddr(dst, ha) -} - // decodeIfBinaryToBytes - converts a binary formatted log msg to a // JSON formatted Bytes Log message. func decodeIfBinaryToBytes(in []byte) []byte { diff --git a/encoder_json.go b/encoder_json.go index df15c7a..fe580f5 100644 --- a/encoder_json.go +++ b/encoder_json.go @@ -6,200 +6,19 @@ package zerolog // JSON encoded byte stream. import ( - "net" - "strconv" - "time" - "github.com/rs/zerolog/internal/json" ) -func appendInterface(dst []byte, i interface{}) []byte { - return json.AppendInterface(dst, i) -} +var ( + _ encoder = (*json.Encoder)(nil) -func appendKey(dst []byte, s string) []byte { - return json.AppendKey(dst, s) -} - -func appendFloats64(dst []byte, f []float64) []byte { - return json.AppendFloats64(dst, f) -} - -func appendFloat64(dst []byte, f float64) []byte { - return json.AppendFloat64(dst, f) -} - -func appendFloats32(dst []byte, f []float32) []byte { - return json.AppendFloats32(dst, f) -} - -func appendFloat32(dst []byte, f float32) []byte { - return json.AppendFloat32(dst, f) -} - -func appendUints64(dst []byte, i []uint64) []byte { - return json.AppendUints64(dst, i) -} - -func appendUint64(dst []byte, i uint64) []byte { - return strconv.AppendUint(dst, uint64(i), 10) -} - -func appendUints32(dst []byte, i []uint32) []byte { - return json.AppendUints32(dst, i) -} - -func appendUint32(dst []byte, i uint32) []byte { - return strconv.AppendUint(dst, uint64(i), 10) -} - -func appendUints16(dst []byte, i []uint16) []byte { - return json.AppendUints16(dst, i) -} - -func appendUint16(dst []byte, i uint16) []byte { - return strconv.AppendUint(dst, uint64(i), 10) -} - -func appendUints8(dst []byte, i []uint8) []byte { - return json.AppendUints8(dst, i) -} - -func appendUint8(dst []byte, i uint8) []byte { - return strconv.AppendUint(dst, uint64(i), 10) -} - -func appendUints(dst []byte, i []uint) []byte { - return json.AppendUints(dst, i) -} - -func appendUint(dst []byte, i uint) []byte { - return strconv.AppendUint(dst, uint64(i), 10) -} - -func appendInts64(dst []byte, i []int64) []byte { - return json.AppendInts64(dst, i) -} - -func appendInt64(dst []byte, i int64) []byte { - return strconv.AppendInt(dst, int64(i), 10) -} - -func appendInts32(dst []byte, i []int32) []byte { - return json.AppendInts32(dst, i) -} - -func appendInt32(dst []byte, i int32) []byte { - return strconv.AppendInt(dst, int64(i), 10) -} - -func appendInts16(dst []byte, i []int16) []byte { - return json.AppendInts16(dst, i) -} - -func appendInt16(dst []byte, i int16) []byte { - return strconv.AppendInt(dst, int64(i), 10) -} - -func appendInts8(dst []byte, i []int8) []byte { - return json.AppendInts8(dst, i) -} - -func appendInt8(dst []byte, i int8) []byte { - return strconv.AppendInt(dst, int64(i), 10) -} - -func appendInts(dst []byte, i []int) []byte { - return json.AppendInts(dst, i) -} - -func appendInt(dst []byte, i int) []byte { - return strconv.AppendInt(dst, int64(i), 10) -} - -func appendBools(dst []byte, b []bool) []byte { - return json.AppendBools(dst, b) -} - -func appendBool(dst []byte, b bool) []byte { - return strconv.AppendBool(dst, b) -} - -func appendError(dst []byte, e error) []byte { - return json.AppendError(dst, e) -} - -func appendErrors(dst []byte, e []error) []byte { - return json.AppendErrors(dst, e) -} - -func appendString(dst []byte, s string) []byte { - return json.AppendString(dst, s) -} - -func appendStrings(dst []byte, s []string) []byte { - return json.AppendStrings(dst, s) -} - -func appendDuration(dst []byte, t time.Duration, d time.Duration, fmt bool) []byte { - return json.AppendDuration(dst, t, d, fmt) -} - -func appendDurations(dst []byte, t []time.Duration, d time.Duration, fmt bool) []byte { - return json.AppendDurations(dst, t, d, fmt) -} - -func appendTimes(dst []byte, t []time.Time, fmt string) []byte { - return json.AppendTimes(dst, t, fmt) -} - -func appendTime(dst []byte, t time.Time, fmt string) []byte { - return json.AppendTime(dst, t, fmt) -} - -func appendEndMarker(dst []byte) []byte { - return append(dst, '}') -} - -func appendLineBreak(dst []byte) []byte { - return append(dst, '\n') -} - -func appendBeginMarker(dst []byte) []byte { - return append(dst, '{') -} - -func appendBytes(dst []byte, b []byte) []byte { - return json.AppendBytes(dst, b) -} - -func appendArrayStart(dst []byte) []byte { - return append(dst, '[') -} - -func appendArrayEnd(dst []byte) []byte { - return append(dst, ']') -} - -func appendArrayDelim(dst []byte) []byte { - if len(dst) > 0 { - return append(dst, ',') - } - return dst -} - -func appendObjectData(dst []byte, src []byte) []byte { - return json.AppendObjectData(dst, src) -} + enc = json.Encoder{} +) func appendJSON(dst []byte, j []byte) []byte { return append(dst, j...) } -func appendNil(dst []byte) []byte { - return append(dst, "null"...) -} - func decodeIfBinaryToString(in []byte) string { return string(in) } @@ -211,19 +30,3 @@ func decodeObjectToStr(in []byte) string { func decodeIfBinaryToBytes(in []byte) []byte { return in } - -func appendIPAddr(dst []byte, ip net.IP) []byte { - return json.AppendIPAddr(dst, ip) -} - -func appendIPPrefix(dst []byte, pfx net.IPNet) []byte { - return json.AppendIPPrefix(dst, pfx) -} - -func appendMACAddr(dst []byte, ha net.HardwareAddr) []byte { - return json.AppendMACAddr(dst, ha) -} - -func appendHex(in []byte, val []byte) []byte { - return json.AppendHex(in, val) -} diff --git a/event.go b/event.go index 7481a40..85e90ee 100644 --- a/event.go +++ b/event.go @@ -45,7 +45,7 @@ func newEvent(w LevelWriter, level Level) *Event { e := eventPool.Get().(*Event) e.buf = e.buf[:0] e.h = e.h[:0] - e.buf = appendBeginMarker(e.buf) + e.buf = enc.AppendBeginMarker(e.buf) e.w = w e.level = level return e @@ -55,8 +55,8 @@ func (e *Event) write() (err error) { if e == nil { return nil } - e.buf = appendEndMarker(e.buf) - e.buf = appendLineBreak(e.buf) + e.buf = enc.AppendEndMarker(e.buf) + e.buf = enc.AppendLineBreak(e.buf) if e.w != nil { _, err = e.w.WriteLevel(e.level, e.buf) } @@ -95,7 +95,7 @@ func (e *Event) Msg(msg string) { } } if msg != "" { - e.buf = appendString(appendKey(e.buf, MessageFieldName), msg) + e.buf = enc.AppendString(enc.AppendKey(e.buf, MessageFieldName), msg) } if e.done != nil { defer e.done(msg) @@ -131,8 +131,8 @@ func (e *Event) Dict(key string, dict *Event) *Event { if e == nil { return e } - dict.buf = appendEndMarker(dict.buf) - e.buf = append(appendKey(e.buf, key), dict.buf...) + dict.buf = enc.AppendEndMarker(dict.buf) + e.buf = append(enc.AppendKey(e.buf, key), dict.buf...) eventPool.Put(dict) return e } @@ -151,7 +151,7 @@ func (e *Event) Array(key string, arr LogArrayMarshaler) *Event { if e == nil { return e } - e.buf = appendKey(e.buf, key) + e.buf = enc.AppendKey(e.buf, key) var a *Array if aa, ok := arr.(*Array); ok { a = aa @@ -164,9 +164,9 @@ func (e *Event) Array(key string, arr LogArrayMarshaler) *Event { } func (e *Event) appendObject(obj LogObjectMarshaler) { - e.buf = appendBeginMarker(e.buf) + e.buf = enc.AppendBeginMarker(e.buf) obj.MarshalZerologObject(e) - e.buf = appendEndMarker(e.buf) + e.buf = enc.AppendEndMarker(e.buf) } // Object marshals an object that implement the LogObjectMarshaler interface. @@ -174,7 +174,7 @@ func (e *Event) Object(key string, obj LogObjectMarshaler) *Event { if e == nil { return e } - e.buf = appendKey(e.buf, key) + e.buf = enc.AppendKey(e.buf, key) e.appendObject(obj) return e } @@ -184,7 +184,7 @@ func (e *Event) Str(key, val string) *Event { if e == nil { return e } - e.buf = appendString(appendKey(e.buf, key), val) + e.buf = enc.AppendString(enc.AppendKey(e.buf, key), val) return e } @@ -193,7 +193,7 @@ func (e *Event) Strs(key string, vals []string) *Event { if e == nil { return e } - e.buf = appendStrings(appendKey(e.buf, key), vals) + e.buf = enc.AppendStrings(enc.AppendKey(e.buf, key), vals) return e } @@ -205,7 +205,7 @@ func (e *Event) Bytes(key string, val []byte) *Event { if e == nil { return e } - e.buf = appendBytes(appendKey(e.buf, key), val) + e.buf = enc.AppendBytes(enc.AppendKey(e.buf, key), val) return e } @@ -214,7 +214,7 @@ func (e *Event) Hex(key string, val []byte) *Event { if e == nil { return e } - e.buf = appendHex(appendKey(e.buf, key), val) + e.buf = enc.AppendHex(enc.AppendKey(e.buf, key), val) return e } @@ -226,7 +226,7 @@ func (e *Event) RawJSON(key string, b []byte) *Event { if e == nil { return e } - e.buf = appendJSON(appendKey(e.buf, key), b) + e.buf = appendJSON(enc.AppendKey(e.buf, key), b) return e } @@ -237,7 +237,7 @@ func (e *Event) AnErr(key string, err error) *Event { return e } if err != nil { - e.buf = appendError(appendKey(e.buf, key), err) + e.buf = enc.AppendError(enc.AppendKey(e.buf, key), err) } return e } @@ -248,7 +248,7 @@ func (e *Event) Errs(key string, errs []error) *Event { if e == nil { return e } - e.buf = appendErrors(appendKey(e.buf, key), errs) + e.buf = enc.AppendErrors(enc.AppendKey(e.buf, key), errs) return e } @@ -260,7 +260,7 @@ func (e *Event) Err(err error) *Event { return e } if err != nil { - e.buf = appendError(appendKey(e.buf, ErrorFieldName), err) + e.buf = enc.AppendError(enc.AppendKey(e.buf, ErrorFieldName), err) } return e } @@ -270,7 +270,7 @@ func (e *Event) Bool(key string, b bool) *Event { if e == nil { return e } - e.buf = appendBool(appendKey(e.buf, key), b) + e.buf = enc.AppendBool(enc.AppendKey(e.buf, key), b) return e } @@ -279,7 +279,7 @@ func (e *Event) Bools(key string, b []bool) *Event { if e == nil { return e } - e.buf = appendBools(appendKey(e.buf, key), b) + e.buf = enc.AppendBools(enc.AppendKey(e.buf, key), b) return e } @@ -288,7 +288,7 @@ func (e *Event) Int(key string, i int) *Event { if e == nil { return e } - e.buf = appendInt(appendKey(e.buf, key), i) + e.buf = enc.AppendInt(enc.AppendKey(e.buf, key), i) return e } @@ -297,7 +297,7 @@ func (e *Event) Ints(key string, i []int) *Event { if e == nil { return e } - e.buf = appendInts(appendKey(e.buf, key), i) + e.buf = enc.AppendInts(enc.AppendKey(e.buf, key), i) return e } @@ -306,7 +306,7 @@ func (e *Event) Int8(key string, i int8) *Event { if e == nil { return e } - e.buf = appendInt8(appendKey(e.buf, key), i) + e.buf = enc.AppendInt8(enc.AppendKey(e.buf, key), i) return e } @@ -315,7 +315,7 @@ func (e *Event) Ints8(key string, i []int8) *Event { if e == nil { return e } - e.buf = appendInts8(appendKey(e.buf, key), i) + e.buf = enc.AppendInts8(enc.AppendKey(e.buf, key), i) return e } @@ -324,7 +324,7 @@ func (e *Event) Int16(key string, i int16) *Event { if e == nil { return e } - e.buf = appendInt16(appendKey(e.buf, key), i) + e.buf = enc.AppendInt16(enc.AppendKey(e.buf, key), i) return e } @@ -333,7 +333,7 @@ func (e *Event) Ints16(key string, i []int16) *Event { if e == nil { return e } - e.buf = appendInts16(appendKey(e.buf, key), i) + e.buf = enc.AppendInts16(enc.AppendKey(e.buf, key), i) return e } @@ -342,7 +342,7 @@ func (e *Event) Int32(key string, i int32) *Event { if e == nil { return e } - e.buf = appendInt32(appendKey(e.buf, key), i) + e.buf = enc.AppendInt32(enc.AppendKey(e.buf, key), i) return e } @@ -351,7 +351,7 @@ func (e *Event) Ints32(key string, i []int32) *Event { if e == nil { return e } - e.buf = appendInts32(appendKey(e.buf, key), i) + e.buf = enc.AppendInts32(enc.AppendKey(e.buf, key), i) return e } @@ -360,7 +360,7 @@ func (e *Event) Int64(key string, i int64) *Event { if e == nil { return e } - e.buf = appendInt64(appendKey(e.buf, key), i) + e.buf = enc.AppendInt64(enc.AppendKey(e.buf, key), i) return e } @@ -369,7 +369,7 @@ func (e *Event) Ints64(key string, i []int64) *Event { if e == nil { return e } - e.buf = appendInts64(appendKey(e.buf, key), i) + e.buf = enc.AppendInts64(enc.AppendKey(e.buf, key), i) return e } @@ -378,7 +378,7 @@ func (e *Event) Uint(key string, i uint) *Event { if e == nil { return e } - e.buf = appendUint(appendKey(e.buf, key), i) + e.buf = enc.AppendUint(enc.AppendKey(e.buf, key), i) return e } @@ -387,7 +387,7 @@ func (e *Event) Uints(key string, i []uint) *Event { if e == nil { return e } - e.buf = appendUints(appendKey(e.buf, key), i) + e.buf = enc.AppendUints(enc.AppendKey(e.buf, key), i) return e } @@ -396,7 +396,7 @@ func (e *Event) Uint8(key string, i uint8) *Event { if e == nil { return e } - e.buf = appendUint8(appendKey(e.buf, key), i) + e.buf = enc.AppendUint8(enc.AppendKey(e.buf, key), i) return e } @@ -405,7 +405,7 @@ func (e *Event) Uints8(key string, i []uint8) *Event { if e == nil { return e } - e.buf = appendUints8(appendKey(e.buf, key), i) + e.buf = enc.AppendUints8(enc.AppendKey(e.buf, key), i) return e } @@ -414,7 +414,7 @@ func (e *Event) Uint16(key string, i uint16) *Event { if e == nil { return e } - e.buf = appendUint16(appendKey(e.buf, key), i) + e.buf = enc.AppendUint16(enc.AppendKey(e.buf, key), i) return e } @@ -423,7 +423,7 @@ func (e *Event) Uints16(key string, i []uint16) *Event { if e == nil { return e } - e.buf = appendUints16(appendKey(e.buf, key), i) + e.buf = enc.AppendUints16(enc.AppendKey(e.buf, key), i) return e } @@ -432,7 +432,7 @@ func (e *Event) Uint32(key string, i uint32) *Event { if e == nil { return e } - e.buf = appendUint32(appendKey(e.buf, key), i) + e.buf = enc.AppendUint32(enc.AppendKey(e.buf, key), i) return e } @@ -441,7 +441,7 @@ func (e *Event) Uints32(key string, i []uint32) *Event { if e == nil { return e } - e.buf = appendUints32(appendKey(e.buf, key), i) + e.buf = enc.AppendUints32(enc.AppendKey(e.buf, key), i) return e } @@ -450,7 +450,7 @@ func (e *Event) Uint64(key string, i uint64) *Event { if e == nil { return e } - e.buf = appendUint64(appendKey(e.buf, key), i) + e.buf = enc.AppendUint64(enc.AppendKey(e.buf, key), i) return e } @@ -459,7 +459,7 @@ func (e *Event) Uints64(key string, i []uint64) *Event { if e == nil { return e } - e.buf = appendUints64(appendKey(e.buf, key), i) + e.buf = enc.AppendUints64(enc.AppendKey(e.buf, key), i) return e } @@ -468,7 +468,7 @@ func (e *Event) Float32(key string, f float32) *Event { if e == nil { return e } - e.buf = appendFloat32(appendKey(e.buf, key), f) + e.buf = enc.AppendFloat32(enc.AppendKey(e.buf, key), f) return e } @@ -477,7 +477,7 @@ func (e *Event) Floats32(key string, f []float32) *Event { if e == nil { return e } - e.buf = appendFloats32(appendKey(e.buf, key), f) + e.buf = enc.AppendFloats32(enc.AppendKey(e.buf, key), f) return e } @@ -486,7 +486,7 @@ func (e *Event) Float64(key string, f float64) *Event { if e == nil { return e } - e.buf = appendFloat64(appendKey(e.buf, key), f) + e.buf = enc.AppendFloat64(enc.AppendKey(e.buf, key), f) return e } @@ -495,7 +495,7 @@ func (e *Event) Floats64(key string, f []float64) *Event { if e == nil { return e } - e.buf = appendFloats64(appendKey(e.buf, key), f) + e.buf = enc.AppendFloats64(enc.AppendKey(e.buf, key), f) return e } @@ -508,7 +508,7 @@ func (e *Event) Timestamp() *Event { if e == nil { return e } - e.buf = appendTime(appendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat) + e.buf = enc.AppendTime(enc.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat) return e } @@ -517,7 +517,7 @@ func (e *Event) Time(key string, t time.Time) *Event { if e == nil { return e } - e.buf = appendTime(appendKey(e.buf, key), t, TimeFieldFormat) + e.buf = enc.AppendTime(enc.AppendKey(e.buf, key), t, TimeFieldFormat) return e } @@ -526,7 +526,7 @@ func (e *Event) Times(key string, t []time.Time) *Event { if e == nil { return e } - e.buf = appendTimes(appendKey(e.buf, key), t, TimeFieldFormat) + e.buf = enc.AppendTimes(enc.AppendKey(e.buf, key), t, TimeFieldFormat) return e } @@ -537,7 +537,7 @@ func (e *Event) Dur(key string, d time.Duration) *Event { if e == nil { return e } - e.buf = appendDuration(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) + e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) return e } @@ -548,7 +548,7 @@ func (e *Event) Durs(key string, d []time.Duration) *Event { if e == nil { return e } - e.buf = appendDurations(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) + e.buf = enc.AppendDurations(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) return e } @@ -563,7 +563,7 @@ func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event { if t.After(start) { d = t.Sub(start) } - e.buf = appendDuration(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) + e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) return e } @@ -575,7 +575,7 @@ func (e *Event) Interface(key string, i interface{}) *Event { if obj, ok := i.(LogObjectMarshaler); ok { return e.Object(key, obj) } - e.buf = appendInterface(appendKey(e.buf, key), i) + e.buf = enc.AppendInterface(enc.AppendKey(e.buf, key), i) return e } @@ -592,7 +592,7 @@ func (e *Event) caller(skip int) *Event { if !ok { return e } - e.buf = appendString(appendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line)) + e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line)) return e } @@ -601,7 +601,7 @@ func (e *Event) IPAddr(key string, ip net.IP) *Event { if e == nil { return e } - e.buf = appendIPAddr(appendKey(e.buf, key), ip) + e.buf = enc.AppendIPAddr(enc.AppendKey(e.buf, key), ip) return e } @@ -610,7 +610,7 @@ func (e *Event) IPPrefix(key string, pfx net.IPNet) *Event { if e == nil { return e } - e.buf = appendIPPrefix(appendKey(e.buf, key), pfx) + e.buf = enc.AppendIPPrefix(enc.AppendKey(e.buf, key), pfx) return e } @@ -619,6 +619,6 @@ func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event { if e == nil { return e } - e.buf = appendMACAddr(appendKey(e.buf, key), ha) + e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha) return e } diff --git a/fields.go b/fields.go index 8d8a62b..25f0d47 100644 --- a/fields.go +++ b/fields.go @@ -13,7 +13,7 @@ func appendFields(dst []byte, fields map[string]interface{}) []byte { } sort.Strings(keys) for _, key := range keys { - dst = appendKey(dst, key) + dst = enc.AppendKey(dst, key) val := fields[key] if val, ok := val.(LogObjectMarshaler); ok { e := newEvent(nil, 0) @@ -25,117 +25,117 @@ func appendFields(dst []byte, fields map[string]interface{}) []byte { } switch val := val.(type) { case string: - dst = appendString(dst, val) + dst = enc.AppendString(dst, val) case []byte: - dst = appendBytes(dst, val) + dst = enc.AppendBytes(dst, val) case error: - dst = appendError(dst, val) + dst = enc.AppendError(dst, val) case []error: - dst = appendErrors(dst, val) + dst = enc.AppendErrors(dst, val) case bool: - dst = appendBool(dst, val) + dst = enc.AppendBool(dst, val) case int: - dst = appendInt(dst, val) + dst = enc.AppendInt(dst, val) case int8: - dst = appendInt8(dst, val) + dst = enc.AppendInt8(dst, val) case int16: - dst = appendInt16(dst, val) + dst = enc.AppendInt16(dst, val) case int32: - dst = appendInt32(dst, val) + dst = enc.AppendInt32(dst, val) case int64: - dst = appendInt64(dst, val) + dst = enc.AppendInt64(dst, val) case uint: - dst = appendUint(dst, val) + dst = enc.AppendUint(dst, val) case uint8: - dst = appendUint8(dst, val) + dst = enc.AppendUint8(dst, val) case uint16: - dst = appendUint16(dst, val) + dst = enc.AppendUint16(dst, val) case uint32: - dst = appendUint32(dst, val) + dst = enc.AppendUint32(dst, val) case uint64: - dst = appendUint64(dst, val) + dst = enc.AppendUint64(dst, val) case float32: - dst = appendFloat32(dst, val) + dst = enc.AppendFloat32(dst, val) case float64: - dst = appendFloat64(dst, val) + dst = enc.AppendFloat64(dst, val) case time.Time: - dst = appendTime(dst, val, TimeFieldFormat) + dst = enc.AppendTime(dst, val, TimeFieldFormat) case time.Duration: - dst = appendDuration(dst, val, DurationFieldUnit, DurationFieldInteger) + dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger) case *string: - dst = appendString(dst, *val) + dst = enc.AppendString(dst, *val) case *bool: - dst = appendBool(dst, *val) + dst = enc.AppendBool(dst, *val) case *int: - dst = appendInt(dst, *val) + dst = enc.AppendInt(dst, *val) case *int8: - dst = appendInt8(dst, *val) + dst = enc.AppendInt8(dst, *val) case *int16: - dst = appendInt16(dst, *val) + dst = enc.AppendInt16(dst, *val) case *int32: - dst = appendInt32(dst, *val) + dst = enc.AppendInt32(dst, *val) case *int64: - dst = appendInt64(dst, *val) + dst = enc.AppendInt64(dst, *val) case *uint: - dst = appendUint(dst, *val) + dst = enc.AppendUint(dst, *val) case *uint8: - dst = appendUint8(dst, *val) + dst = enc.AppendUint8(dst, *val) case *uint16: - dst = appendUint16(dst, *val) + dst = enc.AppendUint16(dst, *val) case *uint32: - dst = appendUint32(dst, *val) + dst = enc.AppendUint32(dst, *val) case *uint64: - dst = appendUint64(dst, *val) + dst = enc.AppendUint64(dst, *val) case *float32: - dst = appendFloat32(dst, *val) + dst = enc.AppendFloat32(dst, *val) case *float64: - dst = appendFloat64(dst, *val) + dst = enc.AppendFloat64(dst, *val) case *time.Time: - dst = appendTime(dst, *val, TimeFieldFormat) + dst = enc.AppendTime(dst, *val, TimeFieldFormat) case *time.Duration: - dst = appendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger) + dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger) case []string: - dst = appendStrings(dst, val) + dst = enc.AppendStrings(dst, val) case []bool: - dst = appendBools(dst, val) + dst = enc.AppendBools(dst, val) case []int: - dst = appendInts(dst, val) + dst = enc.AppendInts(dst, val) case []int8: - dst = appendInts8(dst, val) + dst = enc.AppendInts8(dst, val) case []int16: - dst = appendInts16(dst, val) + dst = enc.AppendInts16(dst, val) case []int32: - dst = appendInts32(dst, val) + dst = enc.AppendInts32(dst, val) case []int64: - dst = appendInts64(dst, val) + dst = enc.AppendInts64(dst, val) case []uint: - dst = appendUints(dst, val) + dst = enc.AppendUints(dst, val) // case []uint8: - // dst = appendUints8(dst, val) + // dst = enc.AppendUints8(dst, val) case []uint16: - dst = appendUints16(dst, val) + dst = enc.AppendUints16(dst, val) case []uint32: - dst = appendUints32(dst, val) + dst = enc.AppendUints32(dst, val) case []uint64: - dst = appendUints64(dst, val) + dst = enc.AppendUints64(dst, val) case []float32: - dst = appendFloats32(dst, val) + dst = enc.AppendFloats32(dst, val) case []float64: - dst = appendFloats64(dst, val) + dst = enc.AppendFloats64(dst, val) case []time.Time: - dst = appendTimes(dst, val, TimeFieldFormat) + dst = enc.AppendTimes(dst, val, TimeFieldFormat) case []time.Duration: - dst = appendDurations(dst, val, DurationFieldUnit, DurationFieldInteger) + dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger) case nil: - dst = appendNil(dst) + dst = enc.AppendNil(dst) case net.IP: - dst = appendIPAddr(dst, val) + dst = enc.AppendIPAddr(dst, val) case net.IPNet: - dst = appendIPPrefix(dst, val) + dst = enc.AppendIPPrefix(dst, val) case net.HardwareAddr: - dst = appendMACAddr(dst, val) + dst = enc.AppendMACAddr(dst, val) default: - dst = appendInterface(dst, val) + dst = enc.AppendInterface(dst, val) } } return dst diff --git a/internal/cbor/base.go b/internal/cbor/base.go index 6096183..1b13278 100644 --- a/internal/cbor/base.go +++ b/internal/cbor/base.go @@ -1,43 +1,45 @@ package cbor +type Encoder struct{} + // AppendKey adds a key (string) to the binary encoded log message -func AppendKey(dst []byte, key string) []byte { +func (e Encoder) AppendKey(dst []byte, key string) []byte { if len(dst) < 1 { - dst = AppendBeginMarker(dst) + dst = e.AppendBeginMarker(dst) } - return AppendString(dst, key) + return e.AppendString(dst, key) } // AppendError adds the Error to the log message if error is NOT nil -func AppendError(dst []byte, err error) []byte { +func (e Encoder) AppendError(dst []byte, err error) []byte { if err == nil { return append(dst, `null`...) } - return AppendString(dst, err.Error()) + return e.AppendString(dst, err.Error()) } // AppendErrors when given an array of errors, // adds them to the log message if a specific error is nil, then // Nil is added, or else the error string is added. -func AppendErrors(dst []byte, errs []error) []byte { +func (e Encoder) AppendErrors(dst []byte, errs []error) []byte { if len(errs) == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } - dst = AppendArrayStart(dst) + dst = e.AppendArrayStart(dst) if errs[0] != nil { - dst = AppendString(dst, errs[0].Error()) + dst = e.AppendString(dst, errs[0].Error()) } else { - dst = AppendNull(dst) + dst = e.AppendNil(dst) } if len(errs) > 1 { for _, err := range errs[1:] { if err == nil { - dst = AppendNull(dst) + dst = e.AppendNil(dst) continue } - dst = AppendString(dst, err.Error()) + dst = e.AppendString(dst, err.Error()) } } - dst = AppendArrayEnd(dst) + dst = e.AppendArrayEnd(dst) return dst } diff --git a/internal/cbor/string.go b/internal/cbor/string.go index 8b3ab96..681640a 100644 --- a/internal/cbor/string.go +++ b/internal/cbor/string.go @@ -1,7 +1,7 @@ package cbor // AppendStrings encodes and adds an array of strings to the dst byte array. -func AppendStrings(dst []byte, vals []string) []byte { +func (e Encoder) AppendStrings(dst []byte, vals []string) []byte { major := majorTypeArray l := len(vals) if l <= additionalMax { @@ -11,13 +11,13 @@ func AppendStrings(dst []byte, vals []string) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendString(dst, v) + dst = e.AppendString(dst, v) } return dst } // AppendString encodes and adds a string to the dst byte array. -func AppendString(dst []byte, s string) []byte { +func (Encoder) AppendString(dst []byte, s string) []byte { major := majorTypeUtf8String l := len(s) @@ -31,7 +31,7 @@ func AppendString(dst []byte, s string) []byte { } // AppendBytes encodes and adds an array of bytes to the dst byte array. -func AppendBytes(dst, s []byte) []byte { +func (Encoder) AppendBytes(dst, s []byte) []byte { major := majorTypeByteString l := len(s) @@ -45,7 +45,7 @@ func AppendBytes(dst, s []byte) []byte { } // AppendEmbeddedJSON adds a tag and embeds input JSON as such. -func AppendEmbeddedJSON(dst, s []byte) []byte { +func (Encoder) AppendEmbeddedJSON(dst, s []byte) []byte { major := majorTypeTags minor := additionalTypeEmbeddedJSON diff --git a/internal/cbor/string_test.go b/internal/cbor/string_test.go index 809b2a3..0e3e9ab 100644 --- a/internal/cbor/string_test.go +++ b/internal/cbor/string_test.go @@ -59,7 +59,7 @@ var encodeByteTests = []struct { func TestAppendString(t *testing.T) { for _, tt := range encodeStringTests { - b := AppendString([]byte{}, tt.plain) + b := enc.AppendString([]byte{}, tt.plain) if got, want := string(b), tt.binary; got != want { t.Errorf("appendString(%q) = %#q, want %#q", tt.plain, got, want) } @@ -72,7 +72,7 @@ func TestAppendString(t *testing.T) { } inp := buffer.String() want := "\x7a\x00\x01\x11\x70" + inp - b := AppendString([]byte{}, inp) + b := enc.AppendString([]byte{}, inp) if got := string(b); got != want { t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want) } @@ -80,7 +80,7 @@ func TestAppendString(t *testing.T) { func TestAppendBytes(t *testing.T) { for _, tt := range encodeByteTests { - b := AppendBytes([]byte{}, tt.plain) + b := enc.AppendBytes([]byte{}, tt.plain) if got, want := string(b), tt.binary; got != want { t.Errorf("appendString(%q) = %#q, want %#q", tt.plain, got, want) } @@ -92,7 +92,7 @@ func TestAppendBytes(t *testing.T) { inp = append(inp, byte('a')) } want := "\x5a\x00\x01\x11\x70" + string(inp) - b := AppendBytes([]byte{}, inp) + b := enc.AppendBytes([]byte{}, inp) if got := string(b); got != want { t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want) } @@ -111,7 +111,7 @@ func BenchmarkAppendString(b *testing.B) { b.Run(name, func(b *testing.B) { buf := make([]byte, 0, 120) for i := 0; i < b.N; i++ { - _ = AppendString(buf, str) + _ = enc.AppendString(buf, str) } }) } diff --git a/internal/cbor/time.go b/internal/cbor/time.go index c8513f2..12f6a1d 100644 --- a/internal/cbor/time.go +++ b/internal/cbor/time.go @@ -21,7 +21,7 @@ func appendIntegerTimestamp(dst []byte, t time.Time) []byte { return dst } -func appendFloatTimestamp(dst []byte, t time.Time) []byte { +func (e Encoder) appendFloatTimestamp(dst []byte, t time.Time) []byte { major := majorTypeTags minor := additionalTypeTimestamp dst = append(dst, byte(major|minor)) @@ -29,24 +29,24 @@ func appendFloatTimestamp(dst []byte, t time.Time) []byte { nanos := t.Nanosecond() var val float64 val = float64(secs)*1.0 + float64(nanos)*1E-9 - return AppendFloat64(dst, val) + return e.AppendFloat64(dst, val) } // AppendTime encodes and adds a timestamp to the dst byte array. -func AppendTime(dst []byte, t time.Time, unused string) []byte { +func (e Encoder) AppendTime(dst []byte, t time.Time, unused string) []byte { utc := t.UTC() if utc.Nanosecond() == 0 { return appendIntegerTimestamp(dst, utc) } - return appendFloatTimestamp(dst, utc) + return e.appendFloatTimestamp(dst, utc) } // AppendTimes encodes and adds an array of timestamps to the dst byte array. -func AppendTimes(dst []byte, vals []time.Time, unused string) []byte { +func (e Encoder) AppendTimes(dst []byte, vals []time.Time, unused string) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -56,7 +56,7 @@ func AppendTimes(dst []byte, vals []time.Time, unused string) []byte { } for _, t := range vals { - dst = AppendTime(dst, t, unused) + dst = e.AppendTime(dst, t, unused) } return dst } @@ -64,21 +64,21 @@ func AppendTimes(dst []byte, vals []time.Time, unused string) []byte { // AppendDuration encodes and adds a duration to the dst byte array. // useInt field indicates whether to store the duration as seconds (integer) or // as seconds+nanoseconds (float). -func AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte { +func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte { if useInt { - return AppendInt64(dst, int64(d/unit)) + return e.AppendInt64(dst, int64(d/unit)) } - return AppendFloat64(dst, float64(d)/float64(unit)) + return e.AppendFloat64(dst, float64(d)/float64(unit)) } // AppendDurations encodes and adds an array of durations to the dst byte array. // useInt field indicates whether to store the duration as seconds (integer) or // as seconds+nanoseconds (float). -func AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte { +func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -87,7 +87,7 @@ func AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useIn dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, d := range vals { - dst = AppendDuration(dst, d, unit, useInt) + dst = e.AppendDuration(dst, d, unit, useInt) } return dst } diff --git a/internal/cbor/time_test.go b/internal/cbor/time_test.go index 507a6ba..d285e35 100644 --- a/internal/cbor/time_test.go +++ b/internal/cbor/time_test.go @@ -10,7 +10,7 @@ import ( func TestAppendTimeNow(t *testing.T) { tm := time.Now() - s := AppendTime([]byte{}, tm, "unused") + s := enc.AppendTime([]byte{}, tm, "unused") got := string(s) tm1 := float64(tm.Unix()) + float64(tm.Nanosecond())*1E-9 @@ -43,7 +43,7 @@ func TestAppendTimePastPresentInteger(t *testing.T) { fmt.Println("Cannot parse input", tt.txt, ".. Skipping!", err) continue } - b := AppendTime([]byte{}, tin, "unused") + b := enc.AppendTime([]byte{}, tin, "unused") if got, want := string(b), tt.binary; got != want { t.Errorf("appendString(%s) = 0x%s, want 0x%s", tt.txt, hex.EncodeToString(b), @@ -68,7 +68,7 @@ func TestAppendTimePastPresentFloat(t *testing.T) { fmt.Println("Cannot parse input", tt.rfcStr, ".. Skipping!") continue } - b := AppendTime([]byte{}, tin, "unused") + b := enc.AppendTime([]byte{}, tin, "unused") if got, want := string(b), tt.out; got != want { t.Errorf("appendString(%s) = 0x%s, want 0x%s", tt.rfcStr, hex.EncodeToString(b), @@ -92,7 +92,7 @@ func BenchmarkAppendTime(b *testing.B) { b.Run(name, func(b *testing.B) { buf := make([]byte, 0, 100) for i := 0; i < b.N; i++ { - _ = AppendTime(buf, t, "unused") + _ = enc.AppendTime(buf, t, "unused") } }) } diff --git a/internal/cbor/types.go b/internal/cbor/types.go index 9b1216d..2a8a716 100644 --- a/internal/cbor/types.go +++ b/internal/cbor/types.go @@ -7,23 +7,50 @@ import ( "net" ) -// AppendNull inserts a 'Nil' object into the dst byte array. -func AppendNull(dst []byte) []byte { +// AppendNil inserts a 'Nil' object into the dst byte array. +func (Encoder) AppendNil(dst []byte) []byte { return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeNull)) } // AppendBeginMarker inserts a map start into the dst byte array. -func AppendBeginMarker(dst []byte) []byte { +func (Encoder) AppendBeginMarker(dst []byte) []byte { return append(dst, byte(majorTypeMap|additionalTypeInfiniteCount)) } // AppendEndMarker inserts a map end into the dst byte array. -func AppendEndMarker(dst []byte) []byte { +func (Encoder) AppendEndMarker(dst []byte) []byte { return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak)) } +// AppendObjectData takes an object in form of a byte array and appends to dst. +func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { + return append(dst, o...) +} + +// AppendArrayStart adds markers to indicate the start of an array. +func (Encoder) AppendArrayStart(dst []byte) []byte { + return append(dst, byte(majorTypeArray|additionalTypeInfiniteCount)) +} + +// AppendArrayEnd adds markers to indicate the end of an array. +func (Encoder) AppendArrayEnd(dst []byte) []byte { + return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak)) +} + +// AppendArrayDelim adds markers to indicate end of a particular array element. +func (Encoder) AppendArrayDelim(dst []byte) []byte { + //No delimiters needed in cbor + return dst +} + +// AppendLineBreak is a noop that keep API compat with json encoder. +func (Encoder) AppendLineBreak(dst []byte) []byte { + // No line breaks needed in binary format. + return dst +} + // AppendBool encodes and inserts a boolean value into the dst byte array. -func AppendBool(dst []byte, val bool) []byte { +func (Encoder) AppendBool(dst []byte, val bool) []byte { b := additionalTypeBoolFalse if val { b = additionalTypeBoolTrue @@ -32,11 +59,11 @@ func AppendBool(dst []byte, val bool) []byte { } // AppendBools encodes and inserts an array of boolean values into the dst byte array. -func AppendBools(dst []byte, vals []bool) []byte { +func (e Encoder) AppendBools(dst []byte, vals []bool) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -45,13 +72,13 @@ func AppendBools(dst []byte, vals []bool) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendBool(dst, v) + dst = e.AppendBool(dst, v) } return dst } // AppendInt encodes and inserts an integer value into the dst byte array. -func AppendInt(dst []byte, val int) []byte { +func (Encoder) AppendInt(dst []byte, val int) []byte { major := majorTypeUnsignedInt contentVal := val if val < 0 { @@ -68,11 +95,11 @@ func AppendInt(dst []byte, val int) []byte { } // AppendInts encodes and inserts an array of integer values into the dst byte array. -func AppendInts(dst []byte, vals []int) []byte { +func (e Encoder) AppendInts(dst []byte, vals []int) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -81,22 +108,22 @@ func AppendInts(dst []byte, vals []int) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendInt(dst, v) + dst = e.AppendInt(dst, v) } return dst } // AppendInt8 encodes and inserts an int8 value into the dst byte array. -func AppendInt8(dst []byte, val int8) []byte { - return AppendInt(dst, int(val)) +func (e Encoder) AppendInt8(dst []byte, val int8) []byte { + return e.AppendInt(dst, int(val)) } // AppendInts8 encodes and inserts an array of integer values into the dst byte array. -func AppendInts8(dst []byte, vals []int8) []byte { +func (e Encoder) AppendInts8(dst []byte, vals []int8) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -105,22 +132,22 @@ func AppendInts8(dst []byte, vals []int8) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendInt(dst, int(v)) + dst = e.AppendInt(dst, int(v)) } return dst } // AppendInt16 encodes and inserts a int16 value into the dst byte array. -func AppendInt16(dst []byte, val int16) []byte { - return AppendInt(dst, int(val)) +func (e Encoder) AppendInt16(dst []byte, val int16) []byte { + return e.AppendInt(dst, int(val)) } // AppendInts16 encodes and inserts an array of int16 values into the dst byte array. -func AppendInts16(dst []byte, vals []int16) []byte { +func (e Encoder) AppendInts16(dst []byte, vals []int16) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -129,22 +156,22 @@ func AppendInts16(dst []byte, vals []int16) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendInt(dst, int(v)) + dst = e.AppendInt(dst, int(v)) } return dst } // AppendInt32 encodes and inserts a int32 value into the dst byte array. -func AppendInt32(dst []byte, val int32) []byte { - return AppendInt(dst, int(val)) +func (e Encoder) AppendInt32(dst []byte, val int32) []byte { + return e.AppendInt(dst, int(val)) } // AppendInts32 encodes and inserts an array of int32 values into the dst byte array. -func AppendInts32(dst []byte, vals []int32) []byte { +func (e Encoder) AppendInts32(dst []byte, vals []int32) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -153,13 +180,13 @@ func AppendInts32(dst []byte, vals []int32) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendInt(dst, int(v)) + dst = e.AppendInt(dst, int(v)) } return dst } // AppendInt64 encodes and inserts a int64 value into the dst byte array. -func AppendInt64(dst []byte, val int64) []byte { +func (Encoder) AppendInt64(dst []byte, val int64) []byte { major := majorTypeUnsignedInt contentVal := val if val < 0 { @@ -176,11 +203,11 @@ func AppendInt64(dst []byte, val int64) []byte { } // AppendInts64 encodes and inserts an array of int64 values into the dst byte array. -func AppendInts64(dst []byte, vals []int64) []byte { +func (e Encoder) AppendInts64(dst []byte, vals []int64) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -189,22 +216,22 @@ func AppendInts64(dst []byte, vals []int64) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendInt64(dst, v) + dst = e.AppendInt64(dst, v) } return dst } // AppendUint encodes and inserts an unsigned integer value into the dst byte array. -func AppendUint(dst []byte, val uint) []byte { - return AppendInt64(dst, int64(val)) +func (e Encoder) AppendUint(dst []byte, val uint) []byte { + return e.AppendInt64(dst, int64(val)) } // AppendUints encodes and inserts an array of unsigned integer values into the dst byte array. -func AppendUints(dst []byte, vals []uint) []byte { +func (e Encoder) AppendUints(dst []byte, vals []uint) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -213,22 +240,22 @@ func AppendUints(dst []byte, vals []uint) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendUint(dst, v) + dst = e.AppendUint(dst, v) } return dst } // AppendUint8 encodes and inserts a unsigned int8 value into the dst byte array. -func AppendUint8(dst []byte, val uint8) []byte { - return AppendUint(dst, uint(val)) +func (e Encoder) AppendUint8(dst []byte, val uint8) []byte { + return e.AppendUint(dst, uint(val)) } // AppendUints8 encodes and inserts an array of uint8 values into the dst byte array. -func AppendUints8(dst []byte, vals []uint8) []byte { +func (e Encoder) AppendUints8(dst []byte, vals []uint8) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -237,22 +264,22 @@ func AppendUints8(dst []byte, vals []uint8) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendUint8(dst, v) + dst = e.AppendUint8(dst, v) } return dst } // AppendUint16 encodes and inserts a uint16 value into the dst byte array. -func AppendUint16(dst []byte, val uint16) []byte { - return AppendUint(dst, uint(val)) +func (e Encoder) AppendUint16(dst []byte, val uint16) []byte { + return e.AppendUint(dst, uint(val)) } // AppendUints16 encodes and inserts an array of uint16 values into the dst byte array. -func AppendUints16(dst []byte, vals []uint16) []byte { +func (e Encoder) AppendUints16(dst []byte, vals []uint16) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -261,22 +288,22 @@ func AppendUints16(dst []byte, vals []uint16) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendUint16(dst, v) + dst = e.AppendUint16(dst, v) } return dst } // AppendUint32 encodes and inserts a uint32 value into the dst byte array. -func AppendUint32(dst []byte, val uint32) []byte { - return AppendUint(dst, uint(val)) +func (e Encoder) AppendUint32(dst []byte, val uint32) []byte { + return e.AppendUint(dst, uint(val)) } // AppendUints32 encodes and inserts an array of uint32 values into the dst byte array. -func AppendUints32(dst []byte, vals []uint32) []byte { +func (e Encoder) AppendUints32(dst []byte, vals []uint32) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -285,13 +312,13 @@ func AppendUints32(dst []byte, vals []uint32) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendUint32(dst, v) + dst = e.AppendUint32(dst, v) } return dst } // AppendUint64 encodes and inserts a uint64 value into the dst byte array. -func AppendUint64(dst []byte, val uint64) []byte { +func (Encoder) AppendUint64(dst []byte, val uint64) []byte { major := majorTypeUnsignedInt contentVal := val if contentVal <= additionalMax { @@ -304,11 +331,11 @@ func AppendUint64(dst []byte, val uint64) []byte { } // AppendUints64 encodes and inserts an array of uint64 values into the dst byte array. -func AppendUints64(dst []byte, vals []uint64) []byte { +func (e Encoder) AppendUints64(dst []byte, vals []uint64) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -317,13 +344,13 @@ func AppendUints64(dst []byte, vals []uint64) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendUint64(dst, v) + dst = e.AppendUint64(dst, v) } return dst } // AppendFloat32 encodes and inserts a single precision float value into the dst byte array. -func AppendFloat32(dst []byte, val float32) []byte { +func (Encoder) AppendFloat32(dst []byte, val float32) []byte { switch { case math.IsNaN(float64(val)): return append(dst, "\xfa\x7f\xc0\x00\x00"...) @@ -343,11 +370,11 @@ func AppendFloat32(dst []byte, val float32) []byte { } // AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array. -func AppendFloats32(dst []byte, vals []float32) []byte { +func (e Encoder) AppendFloats32(dst []byte, vals []float32) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -356,13 +383,13 @@ func AppendFloats32(dst []byte, vals []float32) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendFloat32(dst, v) + dst = e.AppendFloat32(dst, v) } return dst } // AppendFloat64 encodes and inserts a double precision float value into the dst byte array. -func AppendFloat64(dst []byte, val float64) []byte { +func (Encoder) AppendFloat64(dst []byte, val float64) []byte { switch { case math.IsNaN(val): return append(dst, "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00"...) @@ -383,11 +410,11 @@ func AppendFloat64(dst []byte, val float64) []byte { } // AppendFloats64 encodes and inserts an array of double precision float values into the dst byte array. -func AppendFloats64(dst []byte, vals []float64) []byte { +func (e Encoder) AppendFloats64(dst []byte, vals []float64) []byte { major := majorTypeArray l := len(vals) if l == 0 { - return AppendArrayEnd(AppendArrayStart(dst)) + return e.AppendArrayEnd(e.AppendArrayStart(dst)) } if l <= additionalMax { lb := byte(l) @@ -396,51 +423,30 @@ func AppendFloats64(dst []byte, vals []float64) []byte { dst = appendCborTypePrefix(dst, major, uint64(l)) } for _, v := range vals { - dst = AppendFloat64(dst, v) + dst = e.AppendFloat64(dst, v) } return dst } // AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst. -func AppendInterface(dst []byte, i interface{}) []byte { +func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte { marshaled, err := json.Marshal(i) if err != nil { - return AppendString(dst, fmt.Sprintf("marshaling error: %v", err)) + return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err)) } - return AppendEmbeddedJSON(dst, marshaled) -} - -// AppendObjectData takes an object in form of a byte array and appends to dst. -func AppendObjectData(dst []byte, o []byte) []byte { - return append(dst, o...) -} - -// AppendArrayStart adds markers to indicate the start of an array. -func AppendArrayStart(dst []byte) []byte { - return append(dst, byte(majorTypeArray|additionalTypeInfiniteCount)) -} - -// AppendArrayEnd adds markers to indicate the end of an array. -func AppendArrayEnd(dst []byte) []byte { - return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak)) -} - -// AppendArrayDelim adds markers to indicate end of a particular array element. -func AppendArrayDelim(dst []byte) []byte { - //No delimiters needed in cbor - return dst + return e.AppendEmbeddedJSON(dst, marshaled) } // AppendIPAddr encodes and inserts an IP Address (IPv4 or IPv6). -func AppendIPAddr(dst []byte, ip net.IP) []byte { +func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte { dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) dst = append(dst, byte(additionalTypeTagNetworkAddr>>8)) dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff)) - return AppendBytes(dst, ip) + return e.AppendBytes(dst, ip) } // AppendIPPrefix encodes and inserts an IP Address Prefix (Address + Mask Length). -func AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { +func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) dst = append(dst, byte(additionalTypeTagNetworkPrefix>>8)) dst = append(dst, byte(additionalTypeTagNetworkPrefix&0xff)) @@ -448,23 +454,23 @@ func AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { // Prefix is a tuple (aka MAP of 1 pair of elements) - // first element is prefix, second is mask length. dst = append(dst, byte(majorTypeMap|0x1)) - dst = AppendBytes(dst, pfx.IP) + dst = e.AppendBytes(dst, pfx.IP) maskLen, _ := pfx.Mask.Size() - return AppendUint8(dst, uint8(maskLen)) + return e.AppendUint8(dst, uint8(maskLen)) } // AppendMACAddr encodes and inserts an Hardware (MAC) address. -func AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte { +func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte { dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) dst = append(dst, byte(additionalTypeTagNetworkAddr>>8)) dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff)) - return AppendBytes(dst, ha) + return e.AppendBytes(dst, ha) } // AppendHex adds a TAG and inserts a hex bytes as a string. -func AppendHex(dst []byte, val []byte) []byte { +func (e Encoder) AppendHex(dst []byte, val []byte) []byte { dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) dst = append(dst, byte(additionalTypeTagHexString>>8)) dst = append(dst, byte(additionalTypeTagHexString&0xff)) - return AppendBytes(dst, val) + return e.AppendBytes(dst, val) } diff --git a/internal/cbor/types_test.go b/internal/cbor/types_test.go index b3a1094..9c31429 100644 --- a/internal/cbor/types_test.go +++ b/internal/cbor/types_test.go @@ -6,8 +6,10 @@ import ( "testing" ) -func TestAppendNull(t *testing.T) { - s := AppendNull([]byte{}) +var enc = Encoder{} + +func TestAppendNil(t *testing.T) { + s := enc.AppendNil([]byte{}) got := string(s) want := "\xf6" if got != want { @@ -27,7 +29,7 @@ var booleanTestCases = []struct { func TestAppendBool(t *testing.T) { for _, tc := range booleanTestCases { - s := AppendBool([]byte{}, tc.val) + s := enc.AppendBool([]byte{}, tc.val) got := string(s) if got != tc.binary { t.Errorf("AppendBool(%s)=0x%s, want: 0x%s", @@ -48,7 +50,7 @@ var booleanArrayTestCases = []struct { func TestAppendBoolArray(t *testing.T) { for _, tc := range booleanArrayTestCases { - s := AppendBools([]byte{}, tc.val) + s := enc.AppendBools([]byte{}, tc.val) got := string(s) if got != tc.binary { t.Errorf("AppendBools(%s)=0x%s, want: 0x%s", @@ -123,7 +125,7 @@ var integerTestCases = []struct { func TestAppendInt(t *testing.T) { for _, tc := range integerTestCases { - s := AppendInt([]byte{}, tc.val) + s := enc.AppendInt([]byte{}, tc.val) got := string(s) if got != tc.binary { t.Errorf("AppendInt(0x%x)=0x%s, want: 0x%s", @@ -148,7 +150,7 @@ var integerArrayTestCases = []struct { func TestAppendIntArray(t *testing.T) { for _, tc := range integerArrayTestCases { - s := AppendInts([]byte{}, tc.val) + s := enc.AppendInts([]byte{}, tc.val) got := string(s) if got != tc.binary { t.Errorf("AppendInts(%s)=0x%s, want: 0x%s", @@ -173,7 +175,7 @@ var float32TestCases = []struct { func TestAppendFloat32(t *testing.T) { for _, tc := range float32TestCases { - s := AppendFloat32([]byte{}, tc.val) + s := enc.AppendFloat32([]byte{}, tc.val) got := string(s) if got != tc.binary { t.Errorf("AppendFloat32(%f)=0x%s, want: 0x%s", @@ -196,7 +198,7 @@ var ipAddrTestCases = []struct { func TestAppendNetworkAddr(t *testing.T) { for _, tc := range ipAddrTestCases { - s := AppendIPAddr([]byte{}, tc.ipaddr) + s := enc.AppendIPAddr([]byte{}, tc.ipaddr) got := string(s) if got != tc.binary { t.Errorf("AppendIPAddr(%s)=0x%s, want: 0x%s", @@ -217,7 +219,7 @@ var macAddrTestCases = []struct { func TestAppendMacAddr(t *testing.T) { for _, tc := range macAddrTestCases { - s := AppendMACAddr([]byte{}, tc.macaddr) + s := enc.AppendMACAddr([]byte{}, tc.macaddr) got := string(s) if got != tc.binary { t.Errorf("AppendMACAddr(%s)=0x%s, want: 0x%s", @@ -239,7 +241,7 @@ var IPPrefixTestCases = []struct { func TestAppendIPPrefix(t *testing.T) { for _, tc := range IPPrefixTestCases { - s := AppendIPPrefix([]byte{}, tc.pfx) + s := enc.AppendIPPrefix([]byte{}, tc.pfx) got := string(s) if got != tc.binary { t.Errorf("AppendIPPrefix(%s)=0x%s, want: 0x%s", @@ -272,23 +274,23 @@ func BenchmarkAppendInt(b *testing.B) { for i := 0; i < b.N; i++ { switch str.sz { case 0: - _ = AppendInt(buf, int(str.val)) + _ = enc.AppendInt(buf, int(str.val)) case 1: - _ = AppendUint8(buf, uint8(str.val)) + _ = enc.AppendUint8(buf, uint8(str.val)) case 2: - _ = AppendUint16(buf, uint16(str.val)) + _ = enc.AppendUint16(buf, uint16(str.val)) case 4: - _ = AppendUint32(buf, uint32(str.val)) + _ = enc.AppendUint32(buf, uint32(str.val)) case 8: - _ = AppendUint64(buf, uint64(str.val)) + _ = enc.AppendUint64(buf, uint64(str.val)) case 21: - _ = AppendInt8(buf, int8(str.val)) + _ = enc.AppendInt8(buf, int8(str.val)) case 22: - _ = AppendInt16(buf, int16(str.val)) + _ = enc.AppendInt16(buf, int16(str.val)) case 23: - _ = AppendInt32(buf, int32(str.val)) + _ = enc.AppendInt32(buf, int32(str.val)) case 24: - _ = AppendInt64(buf, int64(str.val)) + _ = enc.AppendInt64(buf, int64(str.val)) } } }) @@ -310,9 +312,9 @@ func BenchmarkAppendFloat(b *testing.B) { for i := 0; i < b.N; i++ { switch str.sz { case 4: - _ = AppendFloat32(buf, float32(str.val)) + _ = enc.AppendFloat32(buf, float32(str.val)) case 8: - _ = AppendFloat64(buf, str.val) + _ = enc.AppendFloat64(buf, str.val) } } }) diff --git a/internal/json/base.go b/internal/json/base.go index 722bf76..bad1c74 100644 --- a/internal/json/base.go +++ b/internal/json/base.go @@ -1,32 +1,34 @@ package json +type Encoder struct{} + // AppendKey appends a new key to the output JSON. -func AppendKey(dst []byte, key string) []byte { +func (e Encoder) AppendKey(dst []byte, key string) []byte { if len(dst) > 1 && dst[len(dst)-1] != '{' { dst = append(dst, ',') } - dst = AppendString(dst, key) + dst = e.AppendString(dst, key) return append(dst, ':') } // AppendError encodes the error string to json and appends // the encoded string to the input byte slice. -func AppendError(dst []byte, err error) []byte { +func (e Encoder) AppendError(dst []byte, err error) []byte { if err == nil { return append(dst, `null`...) } - return AppendString(dst, err.Error()) + return e.AppendString(dst, err.Error()) } // AppendErrors encodes the error strings to json and // appends the encoded string list to the input byte slice. -func AppendErrors(dst []byte, errs []error) []byte { +func (e Encoder) AppendErrors(dst []byte, errs []error) []byte { if len(errs) == 0 { return append(dst, '[', ']') } dst = append(dst, '[') if errs[0] != nil { - dst = AppendString(dst, errs[0].Error()) + dst = e.AppendString(dst, errs[0].Error()) } else { dst = append(dst, "null"...) } @@ -36,7 +38,7 @@ func AppendErrors(dst []byte, errs []error) []byte { dst = append(dst, ",null"...) continue } - dst = AppendString(append(dst, ','), err.Error()) + dst = e.AppendString(append(dst, ','), err.Error()) } } dst = append(dst, ']') diff --git a/internal/json/bytes.go b/internal/json/bytes.go index 8f7d5fe..de64120 100644 --- a/internal/json/bytes.go +++ b/internal/json/bytes.go @@ -3,7 +3,7 @@ package json import "unicode/utf8" // AppendBytes is a mirror of appendString with []byte arg -func AppendBytes(dst, s []byte) []byte { +func (Encoder) AppendBytes(dst, s []byte) []byte { dst = append(dst, '"') for i := 0; i < len(s); i++ { if !noEscapeTable[s[i]] { @@ -20,7 +20,7 @@ func AppendBytes(dst, s []byte) []byte { // // The operation loops though each byte and encodes it as hex using // the hex lookup table. -func AppendHex(dst, s []byte) []byte { +func (Encoder) AppendHex(dst, s []byte) []byte { dst = append(dst, '"') for _, v := range s { dst = append(dst, hex[v>>4], hex[v&0x0f]) diff --git a/internal/json/bytes_test.go b/internal/json/bytes_test.go index e33c1e0..d1a370a 100644 --- a/internal/json/bytes_test.go +++ b/internal/json/bytes_test.go @@ -5,9 +5,11 @@ import ( "unicode" ) +var enc = Encoder{} + func TestAppendBytes(t *testing.T) { for _, tt := range encodeStringTests { - b := AppendBytes([]byte{}, []byte(tt.in)) + b := enc.AppendBytes([]byte{}, []byte(tt.in)) if got, want := string(b), tt.out; got != want { t.Errorf("appendBytes(%q) = %#q, want %#q", tt.in, got, want) } @@ -16,7 +18,7 @@ func TestAppendBytes(t *testing.T) { func TestAppendHex(t *testing.T) { for _, tt := range encodeHexTests { - b := AppendHex([]byte{}, []byte{tt.in}) + b := enc.AppendHex([]byte{}, []byte{tt.in}) if got, want := string(b), tt.out; got != want { t.Errorf("appendHex(%x) = %s, want %s", tt.in, got, want) } @@ -32,31 +34,31 @@ func TestStringBytes(t *testing.T) { } s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too - enc := string(AppendString([]byte{}, s)) - encBytes := string(AppendBytes([]byte{}, []byte(s))) + encStr := string(enc.AppendString([]byte{}, s)) + encBytes := string(enc.AppendBytes([]byte{}, []byte(s))) - if enc != encBytes { + if encStr != encBytes { i := 0 - for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] { + for i < len(encStr) && i < len(encBytes) && encStr[i] == encBytes[i] { i++ } - enc = enc[i:] + encStr = encStr[i:] encBytes = encBytes[i:] i = 0 - for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] { + for i < len(encStr) && i < len(encBytes) && encStr[len(encStr)-i-1] == encBytes[len(encBytes)-i-1] { i++ } - enc = enc[:len(enc)-i] + encStr = encStr[:len(encStr)-i] encBytes = encBytes[:len(encBytes)-i] - if len(enc) > 20 { - enc = enc[:20] + "..." + if len(encStr) > 20 { + encStr = encStr[:20] + "..." } if len(encBytes) > 20 { encBytes = encBytes[:20] + "..." } - t.Errorf("encodings differ at %#q vs %#q", enc, encBytes) + t.Errorf("encodings differ at %#q vs %#q", encStr, encBytes) } } @@ -75,7 +77,7 @@ func BenchmarkAppendBytes(b *testing.B) { b.Run(name, func(b *testing.B) { buf := make([]byte, 0, 100) for i := 0; i < b.N; i++ { - _ = AppendBytes(buf, byt) + _ = enc.AppendBytes(buf, byt) } }) } diff --git a/internal/json/string.go b/internal/json/string.go index bb606f0..815906f 100644 --- a/internal/json/string.go +++ b/internal/json/string.go @@ -14,15 +14,15 @@ func init() { // AppendStrings encodes the input strings to json and // appends the encoded string list to the input byte slice. -func AppendStrings(dst []byte, vals []string) []byte { +func (e Encoder) AppendStrings(dst []byte, vals []string) []byte { if len(vals) == 0 { return append(dst, '[', ']') } dst = append(dst, '[') - dst = AppendString(dst, vals[0]) + dst = e.AppendString(dst, vals[0]) if len(vals) > 1 { for _, val := range vals[1:] { - dst = AppendString(append(dst, ','), val) + dst = e.AppendString(append(dst, ','), val) } } dst = append(dst, ']') @@ -38,7 +38,7 @@ func AppendStrings(dst []byte, vals []string) []byte { // entirety to the byte slice. // If we encounter a byte that does need encoding, switch up // the operation and perform a byte-by-byte read-encode-append. -func AppendString(dst []byte, s string) []byte { +func (Encoder) AppendString(dst []byte, s string) []byte { // Start with a double quote. dst = append(dst, '"') // Loop through each character in the string. diff --git a/internal/json/string_test.go b/internal/json/string_test.go index a30b124..10c1313 100644 --- a/internal/json/string_test.go +++ b/internal/json/string_test.go @@ -65,7 +65,7 @@ var encodeHexTests = []struct { func TestAppendString(t *testing.T) { for _, tt := range encodeStringTests { - b := AppendString([]byte{}, tt.in) + b := enc.AppendString([]byte{}, tt.in) if got, want := string(b), tt.out; got != want { t.Errorf("appendString(%q) = %#q, want %#q", tt.in, got, want) } @@ -86,7 +86,7 @@ func BenchmarkAppendString(b *testing.B) { b.Run(name, func(b *testing.B) { buf := make([]byte, 0, 100) for i := 0; i < b.N; i++ { - _ = AppendString(buf, str) + _ = enc.AppendString(buf, str) } }) } diff --git a/internal/json/time.go b/internal/json/time.go index 6007d58..739afff 100644 --- a/internal/json/time.go +++ b/internal/json/time.go @@ -7,16 +7,16 @@ import ( // AppendTime formats the input time with the given format // and appends the encoded string to the input byte slice. -func AppendTime(dst []byte, t time.Time, format string) []byte { +func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte { if format == "" { - return AppendInt64(dst, t.Unix()) + return e.AppendInt64(dst, t.Unix()) } return append(t.AppendFormat(append(dst, '"'), format), '"') } // AppendTimes converts the input times with the given format // and appends the encoded string list to the input byte slice. -func AppendTimes(dst []byte, vals []time.Time, format string) []byte { +func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte { if format == "" { return appendUnixTimes(dst, vals) } @@ -51,24 +51,24 @@ func appendUnixTimes(dst []byte, vals []time.Time) []byte { // AppendDuration formats the input duration with the given unit & format // and appends the encoded string to the input byte slice. -func AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte { +func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte { if useInt { return strconv.AppendInt(dst, int64(d/unit), 10) } - return AppendFloat64(dst, float64(d)/float64(unit)) + return e.AppendFloat64(dst, float64(d)/float64(unit)) } // AppendDurations formats the input durations with the given unit & format // and appends the encoded string list to the input byte slice. -func AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte { +func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte { if len(vals) == 0 { return append(dst, '[', ']') } dst = append(dst, '[') - dst = AppendDuration(dst, vals[0], unit, useInt) + dst = e.AppendDuration(dst, vals[0], unit, useInt) if len(vals) > 1 { for _, d := range vals[1:] { - dst = AppendDuration(append(dst, ','), d, unit, useInt) + dst = e.AppendDuration(append(dst, ','), d, unit, useInt) } } dst = append(dst, ']') diff --git a/internal/json/types.go b/internal/json/types.go index 1beff1f..f343c86 100644 --- a/internal/json/types.go +++ b/internal/json/types.go @@ -8,15 +8,53 @@ import ( "strconv" ) +// AppendNil inserts a 'Nil' object into the dst byte array. +func (Encoder) AppendNil(dst []byte) []byte { + return append(dst, "null"...) +} + +// AppendBeginMarker inserts a map start into the dst byte array. +func (Encoder) AppendBeginMarker(dst []byte) []byte { + return append(dst, '{') +} + +// AppendEndMarker inserts a map end into the dst byte array. +func (Encoder) AppendEndMarker(dst []byte) []byte { + return append(dst, '}') +} + +// AppendLineBreak appends a line break. +func (Encoder) AppendLineBreak(dst []byte) []byte { + return append(dst, '\n') +} + +// AppendArrayStart adds markers to indicate the start of an array. +func (Encoder) AppendArrayStart(dst []byte) []byte { + return append(dst, '[') +} + +// AppendArrayEnd adds markers to indicate the end of an array. +func (Encoder) AppendArrayEnd(dst []byte) []byte { + return append(dst, ']') +} + +// AppendArrayDelim adds markers to indicate end of a particular array element. +func (Encoder) AppendArrayDelim(dst []byte) []byte { + if len(dst) > 0 { + return append(dst, ',') + } + return dst +} + // AppendBool converts the input bool to a string and // appends the encoded string to the input byte slice. -func AppendBool(dst []byte, val bool) []byte { +func (Encoder) AppendBool(dst []byte, val bool) []byte { return strconv.AppendBool(dst, val) } // AppendBools encodes the input bools to json and // appends the encoded string list to the input byte slice. -func AppendBools(dst []byte, vals []bool) []byte { +func (Encoder) AppendBools(dst []byte, vals []bool) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -33,13 +71,13 @@ func AppendBools(dst []byte, vals []bool) []byte { // AppendInt converts the input int to a string and // appends the encoded string to the input byte slice. -func AppendInt(dst []byte, val int) []byte { +func (Encoder) AppendInt(dst []byte, val int) []byte { return strconv.AppendInt(dst, int64(val), 10) } // AppendInts encodes the input ints to json and // appends the encoded string list to the input byte slice. -func AppendInts(dst []byte, vals []int) []byte { +func (Encoder) AppendInts(dst []byte, vals []int) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -56,13 +94,13 @@ func AppendInts(dst []byte, vals []int) []byte { // AppendInt8 converts the input []int8 to a string and // appends the encoded string to the input byte slice. -func AppendInt8(dst []byte, val int8) []byte { +func (Encoder) AppendInt8(dst []byte, val int8) []byte { return strconv.AppendInt(dst, int64(val), 10) } // AppendInts8 encodes the input int8s to json and // appends the encoded string list to the input byte slice. -func AppendInts8(dst []byte, vals []int8) []byte { +func (Encoder) AppendInts8(dst []byte, vals []int8) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -79,13 +117,13 @@ func AppendInts8(dst []byte, vals []int8) []byte { // AppendInt16 converts the input int16 to a string and // appends the encoded string to the input byte slice. -func AppendInt16(dst []byte, val int16) []byte { +func (Encoder) AppendInt16(dst []byte, val int16) []byte { return strconv.AppendInt(dst, int64(val), 10) } // AppendInts16 encodes the input int16s to json and // appends the encoded string list to the input byte slice. -func AppendInts16(dst []byte, vals []int16) []byte { +func (Encoder) AppendInts16(dst []byte, vals []int16) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -102,13 +140,13 @@ func AppendInts16(dst []byte, vals []int16) []byte { // AppendInt32 converts the input int32 to a string and // appends the encoded string to the input byte slice. -func AppendInt32(dst []byte, val int32) []byte { +func (Encoder) AppendInt32(dst []byte, val int32) []byte { return strconv.AppendInt(dst, int64(val), 10) } // AppendInts32 encodes the input int32s to json and // appends the encoded string list to the input byte slice. -func AppendInts32(dst []byte, vals []int32) []byte { +func (Encoder) AppendInts32(dst []byte, vals []int32) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -125,13 +163,13 @@ func AppendInts32(dst []byte, vals []int32) []byte { // AppendInt64 converts the input int64 to a string and // appends the encoded string to the input byte slice. -func AppendInt64(dst []byte, val int64) []byte { +func (Encoder) AppendInt64(dst []byte, val int64) []byte { return strconv.AppendInt(dst, val, 10) } // AppendInts64 encodes the input int64s to json and // appends the encoded string list to the input byte slice. -func AppendInts64(dst []byte, vals []int64) []byte { +func (Encoder) AppendInts64(dst []byte, vals []int64) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -148,13 +186,13 @@ func AppendInts64(dst []byte, vals []int64) []byte { // AppendUint converts the input uint to a string and // appends the encoded string to the input byte slice. -func AppendUint(dst []byte, val uint) []byte { +func (Encoder) AppendUint(dst []byte, val uint) []byte { return strconv.AppendUint(dst, uint64(val), 10) } // AppendUints encodes the input uints to json and // appends the encoded string list to the input byte slice. -func AppendUints(dst []byte, vals []uint) []byte { +func (Encoder) AppendUints(dst []byte, vals []uint) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -171,13 +209,13 @@ func AppendUints(dst []byte, vals []uint) []byte { // AppendUint8 converts the input uint8 to a string and // appends the encoded string to the input byte slice. -func AppendUint8(dst []byte, val uint8) []byte { +func (Encoder) AppendUint8(dst []byte, val uint8) []byte { return strconv.AppendUint(dst, uint64(val), 10) } // AppendUints8 encodes the input uint8s to json and // appends the encoded string list to the input byte slice. -func AppendUints8(dst []byte, vals []uint8) []byte { +func (Encoder) AppendUints8(dst []byte, vals []uint8) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -194,13 +232,13 @@ func AppendUints8(dst []byte, vals []uint8) []byte { // AppendUint16 converts the input uint16 to a string and // appends the encoded string to the input byte slice. -func AppendUint16(dst []byte, val uint16) []byte { +func (Encoder) AppendUint16(dst []byte, val uint16) []byte { return strconv.AppendUint(dst, uint64(val), 10) } // AppendUints16 encodes the input uint16s to json and // appends the encoded string list to the input byte slice. -func AppendUints16(dst []byte, vals []uint16) []byte { +func (Encoder) AppendUints16(dst []byte, vals []uint16) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -217,13 +255,13 @@ func AppendUints16(dst []byte, vals []uint16) []byte { // AppendUint32 converts the input uint32 to a string and // appends the encoded string to the input byte slice. -func AppendUint32(dst []byte, val uint32) []byte { +func (Encoder) AppendUint32(dst []byte, val uint32) []byte { return strconv.AppendUint(dst, uint64(val), 10) } // AppendUints32 encodes the input uint32s to json and // appends the encoded string list to the input byte slice. -func AppendUints32(dst []byte, vals []uint32) []byte { +func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -240,13 +278,13 @@ func AppendUints32(dst []byte, vals []uint32) []byte { // AppendUint64 converts the input uint64 to a string and // appends the encoded string to the input byte slice. -func AppendUint64(dst []byte, val uint64) []byte { +func (Encoder) AppendUint64(dst []byte, val uint64) []byte { return strconv.AppendUint(dst, uint64(val), 10) } // AppendUints64 encodes the input uint64s to json and // appends the encoded string list to the input byte slice. -func AppendUints64(dst []byte, vals []uint64) []byte { +func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte { if len(vals) == 0 { return append(dst, '[', ']') } @@ -261,9 +299,7 @@ func AppendUints64(dst []byte, vals []uint64) []byte { return dst } -// AppendFloat converts the input float to a string and -// appends the encoded string to the input byte slice. -func AppendFloat(dst []byte, val float64, bitSize int) []byte { +func appendFloat(dst []byte, val float64, bitSize int) []byte { // JSON does not permit NaN or Infinity. A typical JSON encoder would fail // with an error, but a logging library wants the data to get thru so we // make a tradeoff and store those types as string. @@ -280,21 +316,21 @@ func AppendFloat(dst []byte, val float64, bitSize int) []byte { // AppendFloat32 converts the input float32 to a string and // appends the encoded string to the input byte slice. -func AppendFloat32(dst []byte, val float32) []byte { - return AppendFloat(dst, float64(val), 32) +func (Encoder) AppendFloat32(dst []byte, val float32) []byte { + return appendFloat(dst, float64(val), 32) } // AppendFloats32 encodes the input float32s to json and // appends the encoded string list to the input byte slice. -func AppendFloats32(dst []byte, vals []float32) []byte { +func (Encoder) AppendFloats32(dst []byte, vals []float32) []byte { if len(vals) == 0 { return append(dst, '[', ']') } dst = append(dst, '[') - dst = AppendFloat(dst, float64(vals[0]), 32) + dst = appendFloat(dst, float64(vals[0]), 32) if len(vals) > 1 { for _, val := range vals[1:] { - dst = AppendFloat(append(dst, ','), float64(val), 32) + dst = appendFloat(append(dst, ','), float64(val), 32) } } dst = append(dst, ']') @@ -303,21 +339,21 @@ func AppendFloats32(dst []byte, vals []float32) []byte { // AppendFloat64 converts the input float64 to a string and // appends the encoded string to the input byte slice. -func AppendFloat64(dst []byte, val float64) []byte { - return AppendFloat(dst, val, 64) +func (Encoder) AppendFloat64(dst []byte, val float64) []byte { + return appendFloat(dst, val, 64) } // AppendFloats64 encodes the input float64s to json and // appends the encoded string list to the input byte slice. -func AppendFloats64(dst []byte, vals []float64) []byte { +func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte { if len(vals) == 0 { return append(dst, '[', ']') } dst = append(dst, '[') - dst = AppendFloat(dst, vals[0], 32) + dst = appendFloat(dst, vals[0], 32) if len(vals) > 1 { for _, val := range vals[1:] { - dst = AppendFloat(append(dst, ','), val, 64) + dst = appendFloat(append(dst, ','), val, 64) } } dst = append(dst, ']') @@ -326,17 +362,17 @@ func AppendFloats64(dst []byte, vals []float64) []byte { // AppendInterface marshals the input interface to a string and // appends the encoded string to the input byte slice. -func AppendInterface(dst []byte, i interface{}) []byte { +func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte { marshaled, err := json.Marshal(i) if err != nil { - return AppendString(dst, fmt.Sprintf("marshaling error: %v", err)) + return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err)) } return append(dst, marshaled...) } // AppendObjectData takes in an object that is already in a byte array // and adds it to the dst. -func AppendObjectData(dst []byte, o []byte) []byte { +func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { // Two conditions we want to put a ',' between existing content and // new content: // 1. new content starts with '{' - which shd be dropped OR @@ -350,17 +386,17 @@ func AppendObjectData(dst []byte, o []byte) []byte { } // AppendIPAddr adds IPv4 or IPv6 address to dst. -func AppendIPAddr(dst []byte, ip net.IP) []byte { - return AppendString(dst, ip.String()) +func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte { + return e.AppendString(dst, ip.String()) } // AppendIPPrefix adds IPv4 or IPv6 Prefix (address & mask) to dst. -func AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { - return AppendString(dst, pfx.String()) +func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { + return e.AppendString(dst, pfx.String()) } // AppendMACAddr adds MAC address to dst. -func AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte { - return AppendString(dst, ha.String()) +func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte { + return e.AppendString(dst, ha.String()) } diff --git a/internal/json/types_test.go b/internal/json/types_test.go index 719649d..f396299 100644 --- a/internal/json/types_test.go +++ b/internal/json/types_test.go @@ -9,18 +9,18 @@ import ( func TestAppendType(t *testing.T) { w := map[string]func(interface{}) []byte{ - "AppendInt": func(v interface{}) []byte { return AppendInt([]byte{}, v.(int)) }, - "AppendInt8": func(v interface{}) []byte { return AppendInt8([]byte{}, v.(int8)) }, - "AppendInt16": func(v interface{}) []byte { return AppendInt16([]byte{}, v.(int16)) }, - "AppendInt32": func(v interface{}) []byte { return AppendInt32([]byte{}, v.(int32)) }, - "AppendInt64": func(v interface{}) []byte { return AppendInt64([]byte{}, v.(int64)) }, - "AppendUint": func(v interface{}) []byte { return AppendUint([]byte{}, v.(uint)) }, - "AppendUint8": func(v interface{}) []byte { return AppendUint8([]byte{}, v.(uint8)) }, - "AppendUint16": func(v interface{}) []byte { return AppendUint16([]byte{}, v.(uint16)) }, - "AppendUint32": func(v interface{}) []byte { return AppendUint32([]byte{}, v.(uint32)) }, - "AppendUint64": func(v interface{}) []byte { return AppendUint64([]byte{}, v.(uint64)) }, - "AppendFloat32": func(v interface{}) []byte { return AppendFloat32([]byte{}, v.(float32)) }, - "AppendFloat64": func(v interface{}) []byte { return AppendFloat64([]byte{}, v.(float64)) }, + "AppendInt": func(v interface{}) []byte { return enc.AppendInt([]byte{}, v.(int)) }, + "AppendInt8": func(v interface{}) []byte { return enc.AppendInt8([]byte{}, v.(int8)) }, + "AppendInt16": func(v interface{}) []byte { return enc.AppendInt16([]byte{}, v.(int16)) }, + "AppendInt32": func(v interface{}) []byte { return enc.AppendInt32([]byte{}, v.(int32)) }, + "AppendInt64": func(v interface{}) []byte { return enc.AppendInt64([]byte{}, v.(int64)) }, + "AppendUint": func(v interface{}) []byte { return enc.AppendUint([]byte{}, v.(uint)) }, + "AppendUint8": func(v interface{}) []byte { return enc.AppendUint8([]byte{}, v.(uint8)) }, + "AppendUint16": func(v interface{}) []byte { return enc.AppendUint16([]byte{}, v.(uint16)) }, + "AppendUint32": func(v interface{}) []byte { return enc.AppendUint32([]byte{}, v.(uint32)) }, + "AppendUint64": func(v interface{}) []byte { return enc.AppendUint64([]byte{}, v.(uint64)) }, + "AppendFloat32": func(v interface{}) []byte { return enc.AppendFloat32([]byte{}, v.(float32)) }, + "AppendFloat64": func(v interface{}) []byte { return enc.AppendFloat64([]byte{}, v.(float64)) }, } tests := []struct { name string @@ -74,7 +74,7 @@ func Test_appendMAC(t *testing.T) { for _, tt := range MACtests { t.Run("MAC", func(t *testing.T) { ha, _ := net.ParseMAC(tt.input) - if got := AppendMACAddr([]byte{}, ha); !reflect.DeepEqual(got, tt.want) { + if got := enc.AppendMACAddr([]byte{}, ha); !reflect.DeepEqual(got, tt.want) { t.Errorf("appendMACAddr() = %s, want %s", got, tt.want) } }) @@ -92,7 +92,7 @@ func Test_appendIP(t *testing.T) { for _, tt := range IPv4tests { t.Run("IPv4", func(t *testing.T) { - if got := AppendIPAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { + if got := enc.AppendIPAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { t.Errorf("appendIPAddr() = %s, want %s", got, tt.want) } }) @@ -107,7 +107,7 @@ func Test_appendIP(t *testing.T) { } for _, tt := range IPv6tests { t.Run("IPv6", func(t *testing.T) { - if got := AppendIPAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { + if got := enc.AppendIPAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { t.Errorf("appendIPAddr() = %s, want %s", got, tt.want) } }) @@ -124,7 +124,7 @@ func Test_appendIPPrefix(t *testing.T) { } for _, tt := range IPv4Prefixtests { t.Run("IPv4", func(t *testing.T) { - if got := AppendIPPrefix([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { + if got := enc.AppendIPPrefix([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { t.Errorf("appendIPPrefix() = %s, want %s", got, tt.want) } }) @@ -141,7 +141,7 @@ func Test_appendIPPrefix(t *testing.T) { } for _, tt := range IPv6Prefixtests { t.Run("IPv6", func(t *testing.T) { - if got := AppendIPPrefix([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { + if got := enc.AppendIPPrefix([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { t.Errorf("appendIPPrefix() = %s, want %s", got, tt.want) } }) @@ -159,7 +159,7 @@ func Test_appendMac(t *testing.T) { for _, tt := range MACtests { t.Run("MAC", func(t *testing.T) { - if got := AppendMACAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { + if got := enc.AppendMACAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) { t.Errorf("appendMAC() = %s, want %s", got, tt.want) } }) diff --git a/log.go b/log.go index 528ac77..ed7223f 100644 --- a/log.go +++ b/log.go @@ -381,7 +381,7 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event { e.Str(LevelFieldName, level.String()) } if l.context != nil && len(l.context) > 0 { - e.buf = appendObjectData(e.buf, l.context) + e.buf = enc.AppendObjectData(e.buf, l.context) } return e }