node.go 1.42 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
	// either a primitive (e.g. string, int, etc), a link (type CID),
	// or another Node.
Eric Myhre's avatar
Eric Myhre committed
9
	TraverseField(path string) (Node, error)
10 11 12 13 14

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

Eric Myhre's avatar
Eric Myhre committed
17 18 19 20
	AsBool() (bool, error)
	AsString() (string, error)
	AsInt() (int, error)
	AsLink() (cid.Cid, error)
Eric Myhre's avatar
Eric Myhre committed
21
}
22 23

type SerializableNode interface {
24
	CID() cid.Cid
25 26
}

27 28 29 30 31 32 33 34
// 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(!).
35
type MutableNode interface {
36 37 38 39 40 41 42
	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)
43
}