Commit ec0ac6d2 authored by Eric Myhre's avatar Eric Myhre

Clarify behavior of traverse on an absent field.

It should return an error explicitly.

A pair of "(nil, nil)" reponses would also be an unambiguous option for
representing an absent member, but seems more likely to create pitfalls
than save a significant amount of code for callers.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent 302da4f4
......@@ -30,7 +30,6 @@ type Node struct {
kind ipld.ReprKind
_map map[string]ipld.Node // Value union. Only one of these has meaning, depending on the value of 'Type'.
_map2 map[int]ipld.Node // Value union. Only one of these has meaning, depending on the value of 'Type'.
_arr []ipld.Node // Value union. Only one of these has meaning, depending on the value of 'Type'.
_bool bool // Value union. Only one of these has meaning, depending on the value of 'Type'.
_int int // Value union. Only one of these has meaning, depending on the value of 'Type'.
......@@ -78,20 +77,11 @@ func (n *Node) TraverseField(pth string) (ipld.Node, error) {
case ipld.ReprKind_Null:
return nil, fmt.Errorf("cannot traverse terminals")
case ipld.ReprKind_Map:
switch {
case n._map != nil:
v, _ := n._map[pth]
return v, nil
case n._map2 != nil:
i, err := strconv.Atoi(pth)
if err != nil {
return nil, fmt.Errorf("404")
}
v, _ := n._map2[i]
return v, nil
default:
panic("unreachable")
v, exists := n._map[pth]
if !exists {
return nil, fmt.Errorf("404")
}
return v, nil
case ipld.ReprKind_List:
i, err := strconv.Atoi(pth)
if err != nil {
......
......@@ -15,12 +15,22 @@ type Node interface {
// GetField resolves a path in the the object and returns
// either a primitive (e.g. string, int, etc), a link (type CID),
// or another Node.
TraverseField(path string) (Node, error)
//
// If the Kind of this Node is not ReprKind_Map, a nil node and an error
// will be returned.
//
// If the key does not exist, a nil node and an error will be returned.
TraverseField(key string) (Node, error)
// GetIndex is the equivalent of GetField but for indexing into an array
// (or a numerically-keyed map). Like GetField, it returns
// either a primitive (e.g. string, int, etc), a link (type CID),
// or another Node.
//
// If the Kind of this Node is not ReprKind_List, a nil node and an error
// will be returned.
//
// If idx is out of range, a nil node and an error will be returned.
TraverseIndex(idx int) (Node, error)
// Keys returns instructions for traversing the node.
......
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