cbor

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2018 License: MIT Imports: 8 Imported by: 0

README

Reference: CBOR Encoding is described in RFC7049 https://tools.ietf.org/html/rfc7049

Tests and benchmark:

sprint @ cbor>go test -v -benchmem -bench=.                                                                               
=== RUN   TestDecodeInteger                                                                                               
--- PASS: TestDecodeInteger (0.00s)                                                                                       
=== RUN   TestDecodeString                                                                                                
--- PASS: TestDecodeString (0.00s)                                                                                        
=== RUN   TestDecodeArray                                                                                                 
--- PASS: TestDecodeArray (0.00s)                                                                                         
=== RUN   TestDecodeMap                                                                                                   
--- PASS: TestDecodeMap (0.00s)                                                                                           
=== RUN   TestDecodeBool                                                                                                  
--- PASS: TestDecodeBool (0.00s)                                                                                          
=== RUN   TestDecodeFloat                                                                                                 
--- PASS: TestDecodeFloat (0.00s)                                                                                         
=== RUN   TestDecodeTimestamp                                                                                             
--- PASS: TestDecodeTimestamp (0.00s)                                                                                     
=== RUN   TestDecodeCbor2Json                                                                                             
--- PASS: TestDecodeCbor2Json (0.00s)                                                                                     
=== RUN   TestAppendString                                                                                                
--- PASS: TestAppendString (0.00s)                                                                                        
=== RUN   TestAppendBytes                                                                                                 
--- PASS: TestAppendBytes (0.00s)                                                                                         
=== RUN   TestAppendTimeNow                                                                                               
--- PASS: TestAppendTimeNow (0.00s)                                                                                       
=== RUN   TestAppendTimePastPresentInteger                                                                                
--- PASS: TestAppendTimePastPresentInteger (0.00s)                                                                        
=== RUN   TestAppendTimePastPresentFloat                                                                                  
--- PASS: TestAppendTimePastPresentFloat (0.00s)                                                                          
=== RUN   TestAppendNull                                                                                                  
--- PASS: TestAppendNull (0.00s)                                                                                          
=== RUN   TestAppendBool                                                                                                  
--- PASS: TestAppendBool (0.00s)                                                                                          
=== RUN   TestAppendBoolArray
--- PASS: TestAppendBoolArray (0.00s)
=== RUN   TestAppendInt
--- PASS: TestAppendInt (0.00s)
=== RUN   TestAppendIntArray
--- PASS: TestAppendIntArray (0.00s)
=== RUN   TestAppendFloat32
--- PASS: TestAppendFloat32 (0.00s)
goos: linux
goarch: amd64
pkg: github.com/toravir/zerolog/internal/cbor
BenchmarkAppendString/MultiBytesLast-4          30000000                43.3 ns/op             0 B/op          0 allocs/op
BenchmarkAppendString/NoEncoding-4              30000000                48.2 ns/op             0 B/op          0 allocs/op
BenchmarkAppendString/EncodingFirst-4           30000000                48.2 ns/op             0 B/op          0 allocs/op
BenchmarkAppendString/EncodingMiddle-4          30000000                41.7 ns/op             0 B/op          0 allocs/op
BenchmarkAppendString/EncodingLast-4            30000000                51.8 ns/op             0 B/op          0 allocs/op
BenchmarkAppendString/MultiBytesFirst-4         50000000                38.0 ns/op             0 B/op          0 allocs/op
BenchmarkAppendString/MultiBytesMiddle-4        50000000                38.0 ns/op             0 B/op          0 allocs/op
BenchmarkAppendTime/Integer-4                   50000000                39.6 ns/op             0 B/op          0 allocs/op
BenchmarkAppendTime/Float-4                     30000000                56.1 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/uint8-4                      50000000                29.1 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/uint16-4                     50000000                30.3 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/uint32-4                     50000000                37.1 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/int8-4                       100000000               21.5 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/int16-4                      50000000                25.8 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/int32-4                      50000000                26.7 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/int-Positive-4               100000000               21.5 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/int-Negative-4               100000000               20.7 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/uint64-4                     50000000                36.7 ns/op             0 B/op          0 allocs/op
BenchmarkAppendInt/int64-4                      30000000                39.6 ns/op             0 B/op          0 allocs/op
BenchmarkAppendFloat/Float32-4                  50000000                23.9 ns/op             0 B/op          0 allocs/op
BenchmarkAppendFloat/Float64-4                  50000000                32.8 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/toravir/zerolog/internal/cbor        34.969s
sprint @ cbor>

Documentation

Overview

Package cbor provides primitives for storing different data in the CBOR (binary) format. CBOR is defined in RFC7049.

Index

Constants

This section is empty.

Variables

View Source
var IntegerTimeFieldFormat = time.RFC3339

IntegerTimeFieldFormat indicates the format of timestamp decoded from an integer (time in seconds).

View Source
var NanoTimeFieldFormat = time.RFC3339Nano

NanoTimeFieldFormat indicates the format of timestamp decoded from a float value (time in seconds and nano seconds).

Functions

func AppendArrayDelim

func AppendArrayDelim(dst []byte) []byte

AppendArrayDelim adds markers to indicate end of a particular array element.

func AppendArrayEnd

func AppendArrayEnd(dst []byte) []byte

AppendArrayEnd adds markers to indicate the end of an array.

func AppendArrayStart

func AppendArrayStart(dst []byte) []byte

AppendArrayStart adds markers to indicate the start of an array.

func AppendBeginMarker

func AppendBeginMarker(dst []byte) []byte

AppendBeginMarker inserts a map start into the dst byte array.

func AppendBool

func AppendBool(dst []byte, val bool) []byte

AppendBool encodes and inserts a boolean value into the dst byte array.

func AppendBools

func AppendBools(dst []byte, vals []bool) []byte

AppendBools encodes and inserts an array of boolean values into the dst byte array.

func AppendBytes

func AppendBytes(dst, s []byte) []byte

AppendBytes encodes and adds an array of bytes to the dst byte array.

func AppendDuration

func AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []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 AppendDurations

func AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte

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 AppendEmbeddedJSON

func AppendEmbeddedJSON(dst, s []byte) []byte

AppendEmbeddedJSON adds a tag and embeds input JSON as such.

func AppendEndMarker

func AppendEndMarker(dst []byte) []byte

AppendEndMarker inserts a map end into the dst byte array.

func AppendError

func AppendError(dst []byte, err error) []byte

AppendError adds the Error to the log message if error is NOT nil

func AppendErrors

func AppendErrors(dst []byte, errs []error) []byte

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 AppendFloat32

func AppendFloat32(dst []byte, val float32) []byte

AppendFloat32 encodes and inserts a single precision float value into the dst byte array.

func AppendFloat64

func AppendFloat64(dst []byte, val float64) []byte

AppendFloat64 encodes and inserts a double precision float value into the dst byte array.

func AppendFloats32

func AppendFloats32(dst []byte, vals []float32) []byte

AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array.

func AppendFloats64

func AppendFloats64(dst []byte, vals []float64) []byte

AppendFloats64 encodes and inserts an array of double precision float values into the dst byte array.

func AppendHex

func AppendHex(dst []byte, val []byte) []byte

func AppendInt

func AppendInt(dst []byte, val int) []byte

AppendInt encodes and inserts an integer value into the dst byte array.

func AppendInt16

func AppendInt16(dst []byte, val int16) []byte

AppendInt16 encodes and inserts a int16 value into the dst byte array.

func AppendInt32

func AppendInt32(dst []byte, val int32) []byte

AppendInt32 encodes and inserts a int32 value into the dst byte array.

func AppendInt64

func AppendInt64(dst []byte, val int64) []byte

AppendInt64 encodes and inserts a int64 value into the dst byte array.

func AppendInt8

func AppendInt8(dst []byte, val int8) []byte

AppendInt8 encodes and inserts an int8 value into the dst byte array.

func AppendInterface

func AppendInterface(dst []byte, i interface{}) []byte

AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst.

func AppendInts

func AppendInts(dst []byte, vals []int) []byte

AppendInts encodes and inserts an array of integer values into the dst byte array.

func AppendInts16

func AppendInts16(dst []byte, vals []int16) []byte

AppendInts16 encodes and inserts an array of int16 values into the dst byte array.

func AppendInts32

func AppendInts32(dst []byte, vals []int32) []byte

AppendInts32 encodes and inserts an array of int32 values into the dst byte array.

func AppendInts64

func AppendInts64(dst []byte, vals []int64) []byte

AppendInts64 encodes and inserts an array of int64 values into the dst byte array.

func AppendInts8

func AppendInts8(dst []byte, vals []int8) []byte

AppendInts8 encodes and inserts an array of integer values into the dst byte array.

func AppendKey

func AppendKey(dst []byte, key string) []byte

AppendKey adds a key (string) to the binary encoded log message

func AppendNull

func AppendNull(dst []byte) []byte

AppendNull inserts a 'Nil' object into the dst byte array.

func AppendObjectData

func AppendObjectData(dst []byte, o []byte) []byte

AppendObjectData takes an object in form of a byte array and appends to dst.

func AppendString

func AppendString(dst []byte, s string) []byte

AppendString encodes and adds a string to the dst byte array.

func AppendStrings

func AppendStrings(dst []byte, vals []string) []byte

AppendStrings encodes and adds an array of strings to the dst byte array.

func AppendTime

func AppendTime(dst []byte, t time.Time, unused string) []byte

AppendTime encodes and adds a timestamp to the dst byte array.

func AppendTimes

func AppendTimes(dst []byte, vals []time.Time, unused string) []byte

AppendTimes encodes and adds an array of timestamps to the dst byte array.

func AppendUint

func AppendUint(dst []byte, val uint) []byte

AppendUint encodes and inserts an unsigned integer value into the dst byte array.

func AppendUint16

func AppendUint16(dst []byte, val uint16) []byte

AppendUint16 encodes and inserts a uint16 value into the dst byte array.

func AppendUint32

func AppendUint32(dst []byte, val uint32) []byte

AppendUint32 encodes and inserts a uint32 value into the dst byte array.

func AppendUint64

func AppendUint64(dst []byte, val uint64) []byte

AppendUint64 encodes and inserts a uint64 value into the dst byte array.

func AppendUint8

func AppendUint8(dst []byte, val uint8) []byte

AppendUint8 encodes and inserts a unsigned int8 value into the dst byte array.

func AppendUints

func AppendUints(dst []byte, vals []uint) []byte

AppendUints encodes and inserts an array of unsigned integer values into the dst byte array.

func AppendUints16

func AppendUints16(dst []byte, vals []uint16) []byte

AppendUints16 encodes and inserts an array of uint16 values into the dst byte array.

func AppendUints32

func AppendUints32(dst []byte, vals []uint32) []byte

AppendUints32 encodes and inserts an array of uint32 values into the dst byte array.

func AppendUints64

func AppendUints64(dst []byte, vals []uint64) []byte

AppendUints64 encodes and inserts an array of uint64 values into the dst byte array.

func AppendUints8

func AppendUints8(dst []byte, vals []uint8) []byte

AppendUints8 encodes and inserts an array of uint8 values into the dst byte array.

func Cbor2JsonManyObjects

func Cbor2JsonManyObjects(src []byte, dst io.Writer) (uint, error)

Cbor2JsonManyObjects decodes all the CBOR Objects present in the source byte array. It keeps on decoding until it runs out of bytes. Decoded string is written to the dst. At the end of every CBOR Object newline is written to the output stream. Returns the number of bytes decoded and if any error was encountered.

func Cbor2JsonOneObject

func Cbor2JsonOneObject(src []byte, dst io.Writer) (uint, error)

Cbor2JsonOneObject takes in byte array and decodes ONE CBOR Object usually a MAP. Use this when only ONE CBOR object needs decoding. Decoded string is written to the dst. Returns the bytes decoded and if any error was encountered.

func DecodeIfBinaryToBytes

func DecodeIfBinaryToBytes(in []byte) []byte

DecodeIfBinaryToBytes checks if the input is a binary format, if so, it will decode all Objects and return the decoded string as byte array.

func DecodeIfBinaryToString

func DecodeIfBinaryToString(in []byte) string

DecodeIfBinaryToString converts a binary formatted log msg to a JSON formatted String Log message - suitable for printing to Console/Syslog.

func DecodeObjectToStr

func DecodeObjectToStr(in []byte) string

DecodeObjectToStr checks if the input is a binary format, if so, it will decode a single Object and return the decoded string.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL