Commit 7782d061 authored by Eric Myhre's avatar Eric Myhre

Define MutableNode; ipldfree.Node implements it.

(Mostly; all the scalars work; the composites are todo's.)

Heading in the direction of being able to construct stuff for testing.
Almost there.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent 6e449c0a
package ipldfree
import (
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
)
func (n *Node) SetField(k string, v ipld.Node) {
n.coerceType(tMap)
//TODO
}
func (n *Node) SetIndex(k int, v ipld.Node) {
n.coerceType(tArr)
//TODO
}
func (n *Node) SetBool(v bool) {
n.coerceType(tBool)
n._bool = v
}
func (n *Node) SetString(v string) {
n.coerceType(tStr)
n._str = v
}
func (n *Node) SetInt(v int) {
n.coerceType(tInt)
n._int = v
}
func (n *Node) SetFloat(v float64) {
n.coerceType(tFloat)
n._float = v
}
func (n *Node) SetBytes(v []byte) {
n.coerceType(tBytes)
n._bytes = v
}
func (n *Node) SetLink(v cid.Cid) {
n.coerceType(tLink)
n._link = v
}
func (n *Node) coerceType(newKind typ) {
// Clear previous data, if relevant.
// Don't bother with zeroing finite-size scalars.
switch n.typ {
case tMap:
switch newKind {
case tMap:
return
default:
n._map = nil
}
case tArr:
switch newKind {
case tArr:
return
default:
n._arr = nil
}
case tStr:
switch newKind {
case tStr:
return
default:
n._str = ""
}
case tBytes:
switch newKind {
case tBytes:
return
default:
n._bytes = nil
}
case tLink:
switch newKind {
case tLink:
return
default:
n._link = cid.Undef
}
}
// Set new type union marker.
// Initialize empty value if necessary (maps).
n.typ = newKind
switch newKind {
case tMap:
n._map = make(map[string]*Node)
}
// You'll still want to set the value itself after this.
}
...@@ -24,6 +24,20 @@ type SerializableNode interface { ...@@ -24,6 +24,20 @@ type SerializableNode interface {
CID() cid.Cid CID() cid.Cid
} }
// 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(!).
type MutableNode interface { type MutableNode interface {
// FUTURE: setter methods go here 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)
} }
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