Beispiel #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
}
Beispiel #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)
}
Beispiel #3
0
func (c *decoder) Decode(v interface{}) error {
	nv, ok := v.(*ipld.Node)
	if !ok {
		return errors.New("must decode to *ipld.Node")
	}

	if err := mc.ConsumeHeader(c.r, c.c.Header()); err != nil {
		return err
	}

	var pbn PBNode
	if err := c.pbd.Decode(&pbn); err != nil {
		return err
	}

	pb2ldNode(&pbn, nv)
	return nil
}
Beispiel #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)
}
Beispiel #5
0
func TestPBDecode(t *testing.T) {
	c := mcproto.Multicodec(&PBNode{})
	buf := bytes.NewBuffer(testfile)
	dec := c.Decoder(buf)

	// pass the /mdagv1
	if err := mc.ConsumeHeader(buf, Header); err != nil {
		t.Fatal("failed to consume header", err)
	}

	var pbn PBNode
	if err := dec.Decode(&pbn); err != nil {
		t.Fatal("failed to decode", err)
	}

	if len(pbn.Links) < 7 {
		t.Fatal("incorrect number of links")
	}
	if len(pbn.Data) == 0 {
		t.Error("should have some data")
	}

	findLink := func(s string) *PBLink {
		for _, l := range pbn.Links {
			if *l.Name == s {
				return l
			}
		}
		return nil
	}

	makefile := findLink("Makefile")
	if makefile == nil {
		t.Error("did not find Makefile")
	} else {
		if *makefile.Tsize < 700 || *makefile.Tsize > 4096 {
			t.Error("makefile incorrect size")
		}
	}
}
Beispiel #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")
}
Beispiel #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)
}