freeNode.go 2.06 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
package ipldfree

import (
	"github.com/ipld/go-ipld-prime"
)

var (
	_ ipld.Node = &Node{}
)

/*
	Node has some internal

	This implementation of `ipld.Node` is pretty comparable to `ipldbind.Node`,
	but is somewhat simpler in implementation because values of this type can
	only be produced by its own builder patterns (and thus it requires much
	less reflection and in particular does not depend on refmt for object mapping).

	The "zero" value of this struct is interpreted as an empty map.

	This binding does not provide a serialization valid for hashing; to
	compute a CID, you'll have to convert to another kind of node.
	If you're not sure which kind serializable node to use, try `ipldcbor.Node`.
*/
type Node struct {
	typ typ

	_map   map[string]interface{} // Value union.  Only one of these has meaning, depending on the value of 'Type'.
	_map2  map[int]interface{}    // Value union.  Only one of these has meaning, depending on the value of 'Type'.
	_arr   []interface{}          // Value union.  Only one of these has meaning, depending on the value of 'Type'.
	_str   string                 // Value union.  Only one of these has meaning, depending on the value of 'Type'.
	_bytes []byte                 // 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   int64                  // Value union.  Only one of these has meaning, depending on the value of 'Type'.
	_uint  uint64                 // Value union.  Only one of these has meaning, depending on the value of 'Type'.
	_float float64                // Value union.  Only one of these has meaning, depending on the value of 'Type'.

}

type typ struct{ t byte }

var (
	tZero  = typ{} // treat as tMap
	tMap   = typ{'{'}
	tArr   = typ{'['}
	tStr   = typ{'s'}
	tBytes = typ{'x'}
	tBool  = typ{'b'}
	tInt   = typ{'i'}
	tUint  = typ{'u'}
	tFloat = typ{'f'}
)

func (n *Node) GetField(pth []string) (v interface{}, _ error) {
	return v, traverse(n.bound, pth, n.atlas, reflect.ValueOf(v))
}