msgpack-go

Message Pack Encoder/Decoder for Golang
git clone git://git.lair.cx/msgpack-go
Log | Files | Refs | LICENSE

uuid.go (706B)


      1 package msgpack
      2 
      3 import (
      4 	"errors"
      5 
      6 	"github.com/google/uuid"
      7 )
      8 
      9 const (
     10 	uuidType int8 = 127
     11 )
     12 
     13 // UUID wraps google's UUID and implements encoding/msgpack.Extension.
     14 type UUID struct {
     15 	uuid.UUID
     16 }
     17 
     18 var _ Extension = (*UUID)(nil)
     19 
     20 func (u *UUID) EncodeMsgpackExt(e *Encoder) {
     21 	e.Raw(u.UUID[:])
     22 }
     23 
     24 func (u *UUID) DecodeMsgpackExt(d *Decoder, typ int8, size int) error {
     25 	if typ != uuidType {
     26 		return errors.New("uuid: invalid type")
     27 	}
     28 	if size != 16 {
     29 		return errors.New("uuid: invalid uuid")
     30 	}
     31 
     32 	var p = make([]byte, 16)
     33 
     34 	err := d.Raw(p)
     35 	if err != nil {
     36 		return err
     37 	}
     38 
     39 	copy(u.UUID[:], p)
     40 	return nil
     41 }
     42 
     43 func (u *UUID) Type() int8 {
     44 	return uuidType
     45 }
     46 
     47 func (u *UUID) Size() int {
     48 	return 16
     49 }