Example #1
0
func (c *decoder) Decode(v interface{}) error {
	var w io.Writer
	switch v := v.(type) {
	case []byte:
		w = &bufWriter{B: v}
	case io.Writer:
		w = v
	default:
		return mc.ErrType
	}

	if c.mc {
		// if multicodec, consume the header first
		if err := mc.ConsumeHeader(c.r, Header); err != nil {
			return err
		}
	}

	r, err := msgio.LimitedReader(c.r)
	if err != nil {
		return err
	}

	_, err = io.Copy(w, r)
	return err
}
Example #2
0
func (c *decoder) Decode(v interface{}) error {
	// if multicodec, consume the header first
	if c.mc {
		if err := mc.ConsumeHeader(c.r, Header); err != nil {
			return err
		}
	}
	return c.dec.Decode(v)
}
Example #3
0
func TestBinaryEncoding(t *testing.T) {
	buf := bytes.Buffer{}
	data := []byte("Is Awesome")

	Multicodec().Encoder(&buf).Encode(data)

	err := mc.ConsumeHeader(&buf, Header)
	if err != nil {
		t.Fatal(err)
	}

	dataOut := make([]byte, len(data))
	buf.Read(dataOut)

	if !bytes.Equal(data, dataOut) {
		t.Fatalf("dataOut(%v) is not eqal to data(%v)", dataOut, data)
	}
}
Example #4
0
func (c *decoder) Decode(v interface{}) error {
	r := c.r

	if c.c.mc {
		// if multicodec, consume the header first
		if err := mc.ConsumeHeader(c.r, c.c.Header()); err != nil {
			return err
		}
	}
	if c.c.msgio {
		// need to make a new one per read.
		var err error
		r, err = msgio.LimitedReader(c.r)
		if err != nil {
			return err
		}
	}

	return json.NewDecoder(r).Decode(v)
}
Example #5
0
func TestHexEncoding(t *testing.T) {
	buf := bytes.Buffer{}
	data := []byte("ffff")

	err := Multicodec().Encoder(&buf).Encode([]byte{255, 255})
	if err != nil {
		t.Fatal(err)
	}

	err = mc.ConsumeHeader(&buf, Multicodec().Header())
	if err != nil {
		t.Fatal(err)
	}

	dataOut := make([]byte, len(data))
	buf.Read(dataOut)

	if !bytes.Equal(data, dataOut) {
		t.Fatalf("dataOut(%v) is not eqal to data(%v)", dataOut, data)
	}
}
Example #6
0
func (c *decoder) Decode(v interface{}) error {
	pbv, ok := v.(proto.Message)
	if !ok {
		return ErrNotProtobuf
	}

	if c.c.mc {
		// if multicodec, consume the header first
		if err := mc.ConsumeHeader(c.r, c.c.Header()); err != nil {
			return err
		}
	}

	if c.c.msgio {
		msg, err := msgio.NewReader(c.r).ReadMsg()
		if err != nil {
			return err
		}
		return proto.Unmarshal(msg, pbv)
	}

	return errors.New("protobuf without msgio not supported yet")
}
Example #7
0
func (c *decoder) Decode(v interface{}) error {
	if c.c.Wrap { // read multicodec header.
		if err := mc.ConsumeHeader(c.r, c.c.Header()); err != nil {
			return err
		}
	}

	// get next header, to select codec
	hdr, err := mc.ReadHeader(c.r)
	if err != nil {
		return err
	}

	// "unwind" the read as subc consumes header
	r := mc.WrapHeaderReader(hdr, c.r)

	subc := CodecWithHeader(hdr, c.c.Codecs)
	if subc == nil {
		return fmt.Errorf("no codec for %s", hdr)
	}

	c.c.Last = subc
	return subc.Decoder(r).Decode(v)
}