Commit 301a925a authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

gofmt

parent dcdb1e32
package merkledag package merkledag
import ( import (
"fmt" "fmt"
mh "github.com/jbenet/go-multihash" mh "github.com/jbenet/go-multihash"
) )
// for now, we use a PBNode intermediate thing. // for now, we use a PBNode intermediate thing.
// because native go objects are nice. // because native go objects are nice.
func (n *Node) Unmarshal(encoded []byte) error { func (n *Node) Unmarshal(encoded []byte) error {
var pbn PBNode var pbn PBNode
if err := pbn.Unmarshal(encoded); err != nil { if err := pbn.Unmarshal(encoded); err != nil {
return fmt.Errorf("Unmarshal failed. %v", err) return fmt.Errorf("Unmarshal failed. %v", err)
} }
pbnl := pbn.GetLinks() pbnl := pbn.GetLinks()
n.Links = make([]*Link, len(pbnl)) n.Links = make([]*Link, len(pbnl))
for i, l := range(pbnl) { for i, l := range pbnl {
n.Links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()} n.Links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()}
h, err := mh.Cast(l.GetHash()) h, err := mh.Cast(l.GetHash())
if err != nil { if err != nil {
return fmt.Errorf("Link hash is not valid multihash. %v", err) return fmt.Errorf("Link hash is not valid multihash. %v", err)
} }
n.Links[i].Hash = h n.Links[i].Hash = h
} }
n.Data = pbn.GetData() n.Data = pbn.GetData()
return nil return nil
} }
func (n *Node) MarshalTo(encoded []byte) error { func (n *Node) MarshalTo(encoded []byte) error {
pbn := n.getPBNode() pbn := n.getPBNode()
if _, err := pbn.MarshalTo(encoded); err != nil { if _, err := pbn.MarshalTo(encoded); err != nil {
return fmt.Errorf("Marshal failed. %v", err) return fmt.Errorf("Marshal failed. %v", err)
} }
return nil return nil
} }
func (n *Node) Marshal() ([]byte, error) { func (n *Node) Marshal() ([]byte, error) {
pbn := n.getPBNode() pbn := n.getPBNode()
data, err := pbn.Marshal() data, err := pbn.Marshal()
if err != nil { if err != nil {
return data, fmt.Errorf("Marshal failed. %v", err) return data, fmt.Errorf("Marshal failed. %v", err)
} }
return data, nil return data, nil
} }
func (n *Node) getPBNode() *PBNode { func (n *Node) getPBNode() *PBNode {
pbn := &PBNode{} pbn := &PBNode{}
pbn.Links = make([]*PBLink, len(n.Links)) pbn.Links = make([]*PBLink, len(n.Links))
for i, l := range(n.Links) { for i, l := range n.Links {
pbn.Links[i] = &PBLink{} pbn.Links[i] = &PBLink{}
pbn.Links[i].Name = &l.Name pbn.Links[i].Name = &l.Name
pbn.Links[i].Tsize = &l.Size pbn.Links[i].Tsize = &l.Size
pbn.Links[i].Hash = []byte(l.Hash) pbn.Links[i].Hash = []byte(l.Hash)
} }
pbn.Data = n.Data pbn.Data = n.Data
return pbn return pbn
} }
package merkledag package merkledag
import ( import (
mh "github.com/jbenet/go-multihash" mh "github.com/jbenet/go-multihash"
) )
// A node in the IPFS Merkle DAG. // A node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links. // nodes have opaque data and a set of navigable links.
type Node struct { type Node struct {
Links []*Link Links []*Link
Data []byte Data []byte
} }
// An IPFS Merkle DAG Link // An IPFS Merkle DAG Link
type Link struct { type Link struct {
// utf string name. should be unique per object // utf string name. should be unique per object
Name string // utf8 Name string // utf8
// cumulative size of target object // cumulative size of target object
Size uint64 Size uint64
// multihash of the target object // multihash of the target object
Hash mh.Multihash Hash mh.Multihash
} }
type EncodedNode []byte type EncodedNode []byte
func (n *Node) Size() (uint64, error) { func (n *Node) Size() (uint64, error) {
d, err := n.Marshal() d, err := n.Marshal()
if err != nil { if err != nil {
return 0, err return 0, err
} }
s := uint64(len(d)) s := uint64(len(d))
for _, l := range(n.Links) { for _, l := range n.Links {
s += l.Size s += l.Size
} }
return s, nil return s, nil
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment