node.go 1.97 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2
package ipld

3 4
import "github.com/ipfs/go-cid"

Eric Myhre's avatar
Eric Myhre committed
5
type Node interface {
6 7 8 9 10
	// Kind returns a value from the ReprKind enum describing what the
	// essential serializable kind of this node is (map, list, int, etc).
	// Most other handling of a node requires first switching upon the kind.
	Kind() ReprKind

11
	// GetField resolves a path in the the object and returns
12 13
	// either a primitive (e.g. string, int, etc), a link (type CID),
	// or another Node.
Eric Myhre's avatar
Eric Myhre committed
14
	TraverseField(path string) (Node, error)
15 16 17 18 19

	// 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.
Eric Myhre's avatar
Eric Myhre committed
20
	TraverseIndex(idx int) (Node, error)
Eric Myhre's avatar
Eric Myhre committed
21

22 23 24 25 26 27 28
	// Keys returns instructions for traversing the node.
	// If the node kind is a map, the keys slice has content;
	// if it's a list, the length int will be positive
	// (and if it's a zero length list, there's not to traverse, right?);
	// and if it's a primitive type the returned values are nil and zero.
	Keys() ([]string, int)

Eric Myhre's avatar
Eric Myhre committed
29 30 31 32
	AsBool() (bool, error)
	AsString() (string, error)
	AsInt() (int, error)
	AsLink() (cid.Cid, error)
Eric Myhre's avatar
Eric Myhre committed
33
}
34 35

type SerializableNode interface {
36
	CID() cid.Cid
37 38
}

39 40 41 42 43 44 45 46
// MutableNode is an interface which allows setting its value.
// MutableNode can be all the same kinds as Node can, and can also
// be freely coerced between kinds.
//
// Using a method which coerces a Mutable node to a new kind can
// discard data; the SetField and SetIndex methods are concatenative,
// but using any of the Set{Scalar} methods will result in any map or
// array content being discarded(!).
47
type MutableNode interface {
48 49 50 51 52 53 54
	Node
	SetField(k string, v Node) // SetField coerces the node to a map kind and sets a key:val pair.
	SetIndex(k int, v Node)    // SetIndex coerces the node to an array kind and sets an index:val pair.  (It will implicitly increase the size to include the index.)
	SetBool(v bool)
	SetString(v string)
	SetInt(v int)
	SetLink(v cid.Cid)
55
}