13 func abs(i int64) int64 {
20 // Sizer is an interface implemented
21 // by types that can estimate their
22 // size when MessagePack encoded.
23 // This interface is optional, but
24 // encoding/marshaling implementations
25 // may use this as a way to pre-allocate
26 // memory for serialization.
27 type Sizer interface {
32 // Nowhere is an io.Writer to nowhere
33 Nowhere io.Writer = nwhere{}
35 btsType = reflect.TypeOf(([]byte)(nil))
36 writerPool = sync.Pool{
37 New: func() interface{} {
38 return &Writer{buf: make([]byte, 2048)}
43 func popWriter(w io.Writer) *Writer {
44 wr := writerPool.Get().(*Writer)
49 func pushWriter(wr *Writer) {
55 // freeW frees a writer for use
56 // by other processes. It is not necessary
57 // to call freeW on a writer. However, maintaining
58 // a reference to a *Writer after calling freeW on
59 // it will cause undefined behavior.
60 func freeW(w *Writer) { pushWriter(w) }
62 // Require ensures that cap(old)-len(old) >= extra
63 func Require(old []byte, extra int) []byte {
64 if cap(old)-len(old) >= extra {
68 return make([]byte, 0, extra)
70 n := make([]byte, len(old), cap(old)-len(old)+extra)
78 func (n nwhere) Write(p []byte) (int, error) { return len(p), nil }
80 // Marshaler is the interface implemented
81 // by types that know how to marshal themselves
82 // as MessagePack. MarshalMsg appends the marshalled
83 // form of the object to the provided
84 // byte slice, returning the extended
85 // slice and any errors encountered.
86 type Marshaler interface {
87 MarshalMsg([]byte) ([]byte, error)
90 // Encodable is the interface implemented
91 // by types that know how to write themselves
92 // as MessagePack using a *msgp.Writer.
93 type Encodable interface {
94 EncodeMsg(*Writer) error
97 // Writer is a buffered writer
98 // that can be used to write
99 // MessagePack objects to an io.Writer.
100 // You must call *Writer.Flush() in order
101 // to flush all of the buffered data
102 // to the underlying writer.
109 // NewWriter returns a new *Writer.
110 func NewWriter(w io.Writer) *Writer {
111 if wr, ok := w.(*Writer); ok {
117 // NewWriterSize returns a writer with a custom buffer size.
118 func NewWriterSize(w io.Writer, sz int) *Writer {
119 // we must be able to require() 18
120 // contiguous bytes, so that is the
121 // practical minimum buffer size
128 buf: make([]byte, sz),
132 // Encode encodes an Encodable to an io.Writer.
133 func Encode(w io.Writer, e Encodable) error {
135 err := e.EncodeMsg(wr)
143 func (mw *Writer) flush() error {
147 n, err := mw.w.Write(mw.buf[:mw.wloc])
150 mw.wloc = copy(mw.buf, mw.buf[n:mw.wloc])
158 // Flush flushes all of the buffered
159 // data to the underlying writer.
160 func (mw *Writer) Flush() error { return mw.flush() }
162 // Buffered returns the number bytes in the write buffer
163 func (mw *Writer) Buffered() int { return len(mw.buf) - mw.wloc }
165 func (mw *Writer) avail() int { return len(mw.buf) - mw.wloc }
167 func (mw *Writer) bufsize() int { return len(mw.buf) }
169 // NOTE: this should only be called with
170 // a number that is guaranteed to be less than
171 // len(mw.buf). typically, it is called with a constant.
173 // NOTE: this is a hot code path
174 func (mw *Writer) require(n int) (int, error) {
178 if err := mw.flush(); err != nil {
187 // push one byte onto the buffer
189 // NOTE: this is a hot code path
190 func (mw *Writer) push(b byte) error {
191 if mw.wloc == len(mw.buf) {
192 if err := mw.flush(); err != nil {
201 func (mw *Writer) prefix8(b byte, u uint8) error {
203 if len(mw.buf)-mw.wloc < need {
204 if err := mw.flush(); err != nil {
208 prefixu8(mw.buf[mw.wloc:], b, u)
213 func (mw *Writer) prefix16(b byte, u uint16) error {
215 if len(mw.buf)-mw.wloc < need {
216 if err := mw.flush(); err != nil {
220 prefixu16(mw.buf[mw.wloc:], b, u)
225 func (mw *Writer) prefix32(b byte, u uint32) error {
227 if len(mw.buf)-mw.wloc < need {
228 if err := mw.flush(); err != nil {
232 prefixu32(mw.buf[mw.wloc:], b, u)
237 func (mw *Writer) prefix64(b byte, u uint64) error {
239 if len(mw.buf)-mw.wloc < need {
240 if err := mw.flush(); err != nil {
244 prefixu64(mw.buf[mw.wloc:], b, u)
249 // Write implements io.Writer, and writes
250 // data directly to the buffer.
251 func (mw *Writer) Write(p []byte) (int, error) {
254 if err := mw.flush(); err != nil {
261 mw.wloc += copy(mw.buf[mw.wloc:], p)
265 // implements io.WriteString
266 func (mw *Writer) writeString(s string) error {
269 if err := mw.flush(); err != nil {
273 _, err := io.WriteString(mw.w, s)
277 mw.wloc += copy(mw.buf[mw.wloc:], s)
281 // Reset changes the underlying writer used by the Writer
282 func (mw *Writer) Reset(w io.Writer) {
283 mw.buf = mw.buf[:cap(mw.buf)]
288 // WriteMapHeader writes a map header of the given
289 // size to the writer
290 func (mw *Writer) WriteMapHeader(sz uint32) error {
293 return mw.push(wfixmap(uint8(sz)))
294 case sz < math.MaxUint16:
295 return mw.prefix16(mmap16, uint16(sz))
297 return mw.prefix32(mmap32, sz)
301 // WriteArrayHeader writes an array header of the
302 // given size to the writer
303 func (mw *Writer) WriteArrayHeader(sz uint32) error {
306 return mw.push(wfixarray(uint8(sz)))
307 case sz < math.MaxUint16:
308 return mw.prefix16(marray16, uint16(sz))
310 return mw.prefix32(marray32, sz)
314 // WriteNil writes a nil byte to the buffer
315 func (mw *Writer) WriteNil() error {
319 // WriteFloat64 writes a float64 to the writer
320 func (mw *Writer) WriteFloat64(f float64) error {
321 return mw.prefix64(mfloat64, math.Float64bits(f))
324 // WriteFloat32 writes a float32 to the writer
325 func (mw *Writer) WriteFloat32(f float32) error {
326 return mw.prefix32(mfloat32, math.Float32bits(f))
329 // WriteInt64 writes an int64 to the writer
330 func (mw *Writer) WriteInt64(i int64) error {
333 case i < 0 && i > -32:
334 return mw.push(wnfixint(int8(i)))
335 case i >= 0 && i < 128:
336 return mw.push(wfixint(uint8(i)))
337 case a < math.MaxInt8:
338 return mw.prefix8(mint8, uint8(i))
339 case a < math.MaxInt16:
340 return mw.prefix16(mint16, uint16(i))
341 case a < math.MaxInt32:
342 return mw.prefix32(mint32, uint32(i))
344 return mw.prefix64(mint64, uint64(i))
348 // WriteInt8 writes an int8 to the writer
349 func (mw *Writer) WriteInt8(i int8) error { return mw.WriteInt64(int64(i)) }
351 // WriteInt16 writes an int16 to the writer
352 func (mw *Writer) WriteInt16(i int16) error { return mw.WriteInt64(int64(i)) }
354 // WriteInt32 writes an int32 to the writer
355 func (mw *Writer) WriteInt32(i int32) error { return mw.WriteInt64(int64(i)) }
357 // WriteInt writes an int to the writer
358 func (mw *Writer) WriteInt(i int) error { return mw.WriteInt64(int64(i)) }
360 // WriteUint64 writes a uint64 to the writer
361 func (mw *Writer) WriteUint64(u uint64) error {
364 return mw.push(wfixint(uint8(u)))
365 case u < math.MaxUint8:
366 return mw.prefix8(muint8, uint8(u))
367 case u < math.MaxUint16:
368 return mw.prefix16(muint16, uint16(u))
369 case u < math.MaxUint32:
370 return mw.prefix32(muint32, uint32(u))
372 return mw.prefix64(muint64, u)
376 // WriteByte is analagous to WriteUint8
377 func (mw *Writer) WriteByte(u byte) error { return mw.WriteUint8(uint8(u)) }
379 // WriteUint8 writes a uint8 to the writer
380 func (mw *Writer) WriteUint8(u uint8) error { return mw.WriteUint64(uint64(u)) }
382 // WriteUint16 writes a uint16 to the writer
383 func (mw *Writer) WriteUint16(u uint16) error { return mw.WriteUint64(uint64(u)) }
385 // WriteUint32 writes a uint32 to the writer
386 func (mw *Writer) WriteUint32(u uint32) error { return mw.WriteUint64(uint64(u)) }
388 // WriteUint writes a uint to the writer
389 func (mw *Writer) WriteUint(u uint) error { return mw.WriteUint64(uint64(u)) }
391 // WriteBytes writes binary as 'bin' to the writer
392 func (mw *Writer) WriteBytes(b []byte) error {
396 case sz < math.MaxUint8:
397 err = mw.prefix8(mbin8, uint8(sz))
398 case sz < math.MaxUint16:
399 err = mw.prefix16(mbin16, uint16(sz))
401 err = mw.prefix32(mbin32, sz)
410 // WriteBool writes a bool to the writer
411 func (mw *Writer) WriteBool(b bool) error {
413 return mw.push(mtrue)
415 return mw.push(mfalse)
418 // WriteString writes a messagepack string to the writer.
419 // (This is NOT an implementation of io.StringWriter)
420 func (mw *Writer) WriteString(s string) error {
425 err = mw.push(wfixstr(uint8(sz)))
426 case sz < math.MaxUint8:
427 err = mw.prefix8(mstr8, uint8(sz))
428 case sz < math.MaxUint16:
429 err = mw.prefix16(mstr16, uint16(sz))
431 err = mw.prefix32(mstr32, sz)
436 return mw.writeString(s)
439 // WriteComplex64 writes a complex64 to the writer
440 func (mw *Writer) WriteComplex64(f complex64) error {
441 o, err := mw.require(10)
446 mw.buf[o+1] = Complex64Extension
447 big.PutUint32(mw.buf[o+2:], math.Float32bits(real(f)))
448 big.PutUint32(mw.buf[o+6:], math.Float32bits(imag(f)))
452 // WriteComplex128 writes a complex128 to the writer
453 func (mw *Writer) WriteComplex128(f complex128) error {
454 o, err := mw.require(18)
458 mw.buf[o] = mfixext16
459 mw.buf[o+1] = Complex128Extension
460 big.PutUint64(mw.buf[o+2:], math.Float64bits(real(f)))
461 big.PutUint64(mw.buf[o+10:], math.Float64bits(imag(f)))
465 // WriteMapStrStr writes a map[string]string to the writer
466 func (mw *Writer) WriteMapStrStr(mp map[string]string) (err error) {
467 err = mw.WriteMapHeader(uint32(len(mp)))
471 for key, val := range mp {
472 err = mw.WriteString(key)
476 err = mw.WriteString(val)
484 // WriteMapStrIntf writes a map[string]interface to the writer
485 func (mw *Writer) WriteMapStrIntf(mp map[string]interface{}) (err error) {
486 err = mw.WriteMapHeader(uint32(len(mp)))
490 for key, val := range mp {
491 err = mw.WriteString(key)
495 err = mw.WriteIntf(val)
503 // WriteTime writes a time.Time object to the wire.
505 // Time is encoded as Unix time, which means that
506 // location (time zone) data is removed from the object.
507 // The encoded object itself is 12 bytes: 8 bytes for
508 // a big-endian 64-bit integer denoting seconds
509 // elapsed since "zero" Unix time, followed by 4 bytes
510 // for a big-endian 32-bit signed integer denoting
511 // the nanosecond offset of the time. This encoding
512 // is intended to ease portability accross languages.
513 // (Note that this is *not* the standard time.Time
514 // binary encoding, because its implementation relies
515 // heavily on the internal representation used by the
517 func (mw *Writer) WriteTime(t time.Time) error {
519 o, err := mw.require(15)
525 mw.buf[o+2] = TimeExtension
526 putUnix(mw.buf[o+3:], t.Unix(), int32(t.Nanosecond()))
530 // WriteIntf writes the concrete type of 'v'.
531 // WriteIntf will error if 'v' is not one of the following:
532 // - A bool, float, string, []byte, int, uint, or complex
533 // - A map of supported types (with string keys)
534 // - An array or slice of supported types
535 // - A pointer to a supported type
536 // - A type that satisfies the msgp.Encodable interface
537 // - A type that satisfies the msgp.Extension interface
538 func (mw *Writer) WriteIntf(v interface{}) error {
542 switch v := v.(type) {
544 // preferred interfaces
547 return v.EncodeMsg(mw)
549 return mw.WriteExtension(v)
554 return mw.WriteBool(v)
556 return mw.WriteFloat32(v)
558 return mw.WriteFloat64(v)
560 return mw.WriteComplex64(v)
562 return mw.WriteComplex128(v)
564 return mw.WriteUint8(v)
566 return mw.WriteUint16(v)
568 return mw.WriteUint32(v)
570 return mw.WriteUint64(v)
572 return mw.WriteUint(v)
574 return mw.WriteInt8(v)
576 return mw.WriteInt16(v)
578 return mw.WriteInt32(v)
580 return mw.WriteInt64(v)
582 return mw.WriteInt(v)
584 return mw.WriteString(v)
586 return mw.WriteBytes(v)
587 case map[string]string:
588 return mw.WriteMapStrStr(v)
589 case map[string]interface{}:
590 return mw.WriteMapStrIntf(v)
592 return mw.WriteTime(v)
595 val := reflect.ValueOf(v)
596 if !isSupported(val.Kind()) || !val.IsValid() {
597 return fmt.Errorf("msgp: type %s not supported", val)
605 return mw.WriteIntf(val.Elem().Interface())
607 return mw.writeSlice(val)
609 return mw.writeMap(val)
611 return &ErrUnsupportedType{val.Type()}
614 func (mw *Writer) writeMap(v reflect.Value) (err error) {
615 if v.Elem().Kind() != reflect.String {
616 return errors.New("msgp: map keys must be strings")
619 err = mw.WriteMapHeader(uint32(len(ks)))
623 for _, key := range ks {
624 val := v.MapIndex(key)
625 err = mw.WriteString(key.String())
629 err = mw.WriteIntf(val.Interface())
637 func (mw *Writer) writeSlice(v reflect.Value) (err error) {
639 if v.Type().ConvertibleTo(btsType) {
640 return mw.WriteBytes(v.Bytes())
643 sz := uint32(v.Len())
644 err = mw.WriteArrayHeader(sz)
648 for i := uint32(0); i < sz; i++ {
649 err = mw.WriteIntf(v.Index(int(i)).Interface())
657 func (mw *Writer) writeStruct(v reflect.Value) error {
658 if enc, ok := v.Interface().(Encodable); ok {
659 return enc.EncodeMsg(mw)
661 return fmt.Errorf("msgp: unsupported type: %s", v.Type())
664 func (mw *Writer) writeVal(v reflect.Value) error {
665 if !isSupported(v.Kind()) {
666 return fmt.Errorf("msgp: msgp/enc: type %q not supported", v.Type())
669 // shortcut for nil values
675 return mw.WriteBool(v.Bool())
677 case reflect.Float32, reflect.Float64:
678 return mw.WriteFloat64(v.Float())
680 case reflect.Complex64, reflect.Complex128:
681 return mw.WriteComplex128(v.Complex())
683 case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
684 return mw.WriteInt64(v.Int())
686 case reflect.Interface, reflect.Ptr:
690 return mw.writeVal(v.Elem())
693 return mw.writeMap(v)
695 case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
696 return mw.WriteUint64(v.Uint())
699 return mw.WriteString(v.String())
701 case reflect.Slice, reflect.Array:
702 return mw.writeSlice(v)
705 return mw.writeStruct(v)
708 return fmt.Errorf("msgp: msgp/enc: type %q not supported", v.Type())
711 // is the reflect.Kind encodable?
712 func isSupported(k reflect.Kind) bool {
714 case reflect.Func, reflect.Chan, reflect.Invalid, reflect.UnsafePointer:
721 // GuessSize guesses the size of the underlying
722 // value of 'i'. If the underlying value is not
723 // a simple builtin (or []byte), GuessSize defaults
725 func GuessSize(i interface{}) int {
730 switch i := i.(type) {
734 return ExtensionPrefixSize + i.Len()
739 case uint8, uint16, uint32, uint64, uint:
741 case int8, int16, int32, int64, int:
744 return BytesPrefixSize + len(i)
746 return StringPrefixSize + len(i)
750 return Complex128Size
753 case map[string]interface{}:
755 for key, val := range i {
756 s += StringPrefixSize + len(key) + GuessSize(val)
759 case map[string]string:
761 for key, val := range i {
762 s += 2*StringPrefixSize + len(key) + len(val)