node.go 2.26 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
	// GetField resolves a path in the the object and returns
7 8 9 10 11 12 13 14
	// either a primitive (e.g. string, int, etc), a link (type CID),
	// or another Node.
	//
	// If a Node is returned, it will be a unrooted node -- that is,
	// it can be used to view the fields below it, but since it was not
	// originally stored as a full node, you cannot immediately take
	// a link to it for embedding in other objects (you'd have to make
	// a new RootNode with the same content first, then store that).
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	//
	// A variety of GetField{Foo} methods exists to yield specific types,
	// and will panic upon encountering unexpected types.
	GetField(path string) (interface{}, error)
	GetFieldBool(path string) (bool, error)     // See GetField docs.
	GetFieldString(path string) (string, error) // See GetField docs.
	GetFieldInt(path string) (int, error)       // See GetField docs.
	GetFieldLink(path string) (cid.Cid, error)  // See GetField docs.

	// 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.
	//
	// A variety of GetIndex{Foo} methods exists to yield specific types,
	// and will panic upon encountering unexpected types.
	GetIndex(idx int) (interface{}, error)
	GetIndexBool(idx int) (bool, error)     // See GetIndex docs.
	GetIndexString(idx int) (string, error) // See GetIndex docs.
	GetIndexInt(idx int) (int, error)       // See GetIndex docs.
	GetIndexLink(idx int) (cid.Cid, error)  // See GetIndex docs.
Eric Myhre's avatar
Eric Myhre committed
36

37 38 39 40 41 42 43 44
	// REVIEW this whole interface is still *very* unfriendly to chaining.
	// Friendly would be returning `(Node)` at all times, and having final
	// dereferencing options on leaf nodes, and treating it as a Maybe at all
	// other times.  Multireturn methods are antithetical to graceful fluent
	// chaining in golang, syntactically.
	// Panics might also be a viable option.  I suspect we do quite usually
	// want to do large amounts of these traversal operations, and bailing at
	// any point is desired to be terse.  We could provide a thunkwrapper.
Eric Myhre's avatar
Eric Myhre committed
45
}
46 47

type SerializableNode interface {
48
	CID() cid.Cid
49 50 51
}

type MutableNode interface {
52
	// FUTURE: setter methods go here
53
}