coding.go 2.29 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package merkledag

import (
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
4
	"fmt"
5

6
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
7 8 9

	pb "github.com/jbenet/go-ipfs/merkledag/internal/pb"
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12 13 14
)

// for now, we use a PBNode intermediate thing.
// because native go objects are nice.

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
15 16
// Unmarshal decodes raw data into a *Node instance.
// The conversion uses an intermediate PBNode.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
func (n *Node) Unmarshal(encoded []byte) error {
18
	var pbn pb.PBNode
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	if err := pbn.Unmarshal(encoded); err != nil {
		return fmt.Errorf("Unmarshal failed. %v", err)
	}

	pbnl := pbn.GetLinks()
	n.Links = make([]*Link, len(pbnl))
	for i, l := range pbnl {
		n.Links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()}
		h, err := mh.Cast(l.GetHash())
		if err != nil {
			return fmt.Errorf("Link hash is not valid multihash. %v", err)
		}
		n.Links[i].Hash = h
	}

	n.Data = pbn.GetData()
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
38 39
// MarshalTo encodes a *Node instance into a given byte slice.
// The conversion uses an intermediate PBNode.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
func (n *Node) MarshalTo(encoded []byte) error {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
41 42 43 44 45
	pbn := n.getPBNode()
	if _, err := pbn.MarshalTo(encoded); err != nil {
		return fmt.Errorf("Marshal failed. %v", err)
	}
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
48 49
// Marshal encodes a *Node instance into a new byte slice.
// The conversion uses an intermediate PBNode.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50
func (n *Node) Marshal() ([]byte, error) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
51 52 53 54 55 56
	pbn := n.getPBNode()
	data, err := pbn.Marshal()
	if err != nil {
		return data, fmt.Errorf("Marshal failed. %v", err)
	}
	return data, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58
}

59 60 61
func (n *Node) getPBNode() *pb.PBNode {
	pbn := &pb.PBNode{}
	pbn.Links = make([]*pb.PBLink, len(n.Links))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
62
	for i, l := range n.Links {
63
		pbn.Links[i] = &pb.PBLink{}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
64 65 66 67 68 69 70
		pbn.Links[i].Name = &l.Name
		pbn.Links[i].Tsize = &l.Size
		pbn.Links[i].Hash = []byte(l.Hash)
	}

	pbn.Data = n.Data
	return pbn
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
73 74
// Encoded returns the encoded raw data version of a Node instance.
// It may use a cached encoded version, unless the force flag is given.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75 76 77 78 79 80 81
func (n *Node) Encoded(force bool) ([]byte, error) {
	if n.encoded == nil || force {
		var err error
		n.encoded, err = n.Marshal()
		if err != nil {
			return []byte{}, err
		}
82
		n.cached = u.Hash(n.encoded)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84 85 86
	}

	return n.encoded, nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
88
// Decoded decodes raw data and returns a new Node instance.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89 90 91 92 93
func Decoded(encoded []byte) (*Node, error) {
	n := &Node{}
	err := n.Unmarshal(encoded)
	return n, err
}