Commit e997142e authored by Eric Myhre's avatar Eric Myhre

Draft ipldfree.Node.

Some comments on the interface as well.  Almost certain that traversal
should be pulled off the node interface itself.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent 21a9fa21
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))
}
...@@ -11,6 +11,16 @@ type Node interface { ...@@ -11,6 +11,16 @@ type Node interface {
// a link to it for embedding in other objects (you'd have to make // 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). // a new RootNode with the same content first, then store that).
GetField(path []string) (interface{}, error) GetField(path []string) (interface{}, error)
// Distinctly suspecting a GetField(string)(iface,err) API would be better.
// And a GetIndex(int)(iface,err) for arrays and int-keyed maps.
// Much easier to code.
// And traversals are apparently going to require a type schema parameter!
// Main counterargument: might be more efficient to let nodes take part,
// so they can do work without generating intermediate nodes.
// That logic applies to some but not all impls. e.g. freenode always
// already has the intermediates. ipldcbor.Node prob will too.
// Hrm. I guess ipldbind.Node is the only one that can get usefully fancy there.
} }
type SerializableNode interface { type SerializableNode interface {
......
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