Skip to content

Commit

Permalink
Merge pull request #45 from ipfs/feat/allocate-less
Browse files Browse the repository at this point in the history
attempt to allocate a bit less
  • Loading branch information
Stebalien committed Sep 25, 2018
2 parents e251301 + d404342 commit 969ab0f
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cbornode
import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
"strconv"
Expand Down Expand Up @@ -68,11 +67,7 @@ func decodeBlock(block blocks.Block) (*Node, error) {
}

func newObject(block blocks.Block, m interface{}) (*Node, error) {
tree, err := compTree(m)
if err != nil {
return nil, err
}
links, err := compLinks(m)
tree, links, err := compute(m)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -290,41 +285,28 @@ func (n *Node) Tree(path string, depth int) []string {
return out
}

func compTree(obj interface{}) ([]string, error) {
var out []string
err := traverse(obj, "", func(name string, val interface{}) error {
func compute(obj interface{}) (tree []string, links []*node.Link, err error) {
err = traverse(obj, "", func(name string, val interface{}) error {
if name != "" {
out = append(out, name[1:])
tree = append(tree, name[1:])
}
if lnk, ok := val.(cid.Cid); ok {
links = append(links, &node.Link{Cid: lnk})
}
return nil
})
if err != nil {
return nil, err
return nil, nil, err
}

return out, nil
return tree, links, nil
}

// Links lists all known links of the Node.
func (n *Node) Links() []*node.Link {
return n.links
}

func compLinks(obj interface{}) ([]*node.Link, error) {
var out []*node.Link
err := traverse(obj, "", func(name string, val interface{}) error {
if lnk, ok := val.(cid.Cid); ok {
out = append(out, &node.Link{Cid: lnk})
}
return nil
})
if err != nil {
return nil, err
}

return out, nil
}

func traverse(obj interface{}, cur string, cb func(string, interface{}) error) error {
if err := cb(cur, obj); err != nil {
return err
Expand Down Expand Up @@ -353,7 +335,7 @@ func traverse(obj interface{}, cur string, cb func(string, interface{}) error) e
return nil
case []interface{}:
for i, v := range obj {
this := fmt.Sprintf("%s/%d", cur, i)
this := cur + "/" + strconv.Itoa(i)
if err := traverse(v, this, cb); err != nil {
return err
}
Expand Down

0 comments on commit 969ab0f

Please sign in to comment.