Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to allocate a bit less #45

Merged
merged 1 commit into from
Sep 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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