Commit 919e159c authored by Eric Myhre's avatar Eric Myhre

Consistency pass on method order and fullness.

ipldfree and ipldbound and fluent and all the interfaces now have all
the isNull methods, asBytes, setBytes, etc.  (Previously this was a
little swiss-cheesy.)

SMOP.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent caaf28f2
......@@ -17,9 +17,12 @@ import (
type Node interface {
TraverseField(path string) Node
TraverseIndex(idx int) Node
IsNull() bool
AsBool() bool
AsString() string
AsInt() int
AsFloat() float64
AsString() string
AsBytes() []byte
AsLink() cid.Cid
GetError() error
}
......@@ -64,6 +67,12 @@ func (n node) TraverseIndex(idx int) Node {
}
return node{v, nil}
}
func (n node) IsNull() bool {
if n.err != nil {
panic(Error{n.err})
}
return n.n.IsNull()
}
func (n node) AsBool() bool {
if n.err != nil {
panic(Error{n.err})
......@@ -74,6 +83,26 @@ func (n node) AsBool() bool {
}
return v
}
func (n node) AsInt() int {
if n.err != nil {
panic(Error{n.err})
}
v, err := n.n.AsInt()
if err != nil {
panic(Error{err})
}
return v
}
func (n node) AsFloat() float64 {
if n.err != nil {
panic(Error{n.err})
}
v, err := n.n.AsFloat()
if err != nil {
panic(Error{err})
}
return v
}
func (n node) AsString() string {
if n.err != nil {
panic(Error{n.err})
......@@ -84,11 +113,11 @@ func (n node) AsString() string {
}
return v
}
func (n node) AsInt() int {
func (n node) AsBytes() []byte {
if n.err != nil {
panic(Error{n.err})
}
v, err := n.n.AsInt()
v, err := n.n.AsBytes()
if err != nil {
panic(Error{err})
}
......
......@@ -83,11 +83,19 @@ func (n Node) AsBool() (v bool, _ error) {
reflect.ValueOf(&v).Elem().Set(n.bound)
return
}
func (n Node) AsInt() (v int, _ error) {
reflect.ValueOf(&v).Elem().Set(n.bound)
return
}
func (n Node) AsFloat() (v float64, _ error) {
reflect.ValueOf(&v).Elem().Set(n.bound)
return
}
func (n Node) AsString() (v string, _ error) {
reflect.ValueOf(&v).Elem().Set(n.bound)
return
}
func (n Node) AsInt() (v int, _ error) {
func (n Node) AsBytes() (v []byte, _ error) {
reflect.ValueOf(&v).Elem().Set(n.bound)
return
}
......
......@@ -33,9 +33,9 @@ type Node struct {
_map2 map[int]ipld.Node // Value union. Only one of these has meaning, depending on the value of 'Type'.
_arr []ipld.Node // 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'.
_str string // Value union. Only one of these has meaning, depending on the value of 'Type'.
_int int // 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'.
_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'.
_link cid.Cid // Value union. Only one of these has meaning, depending on the value of 'Type'.
}
......@@ -50,11 +50,17 @@ func (n *Node) IsNull() bool {
func (n *Node) AsBool() (v bool, _ error) {
return n._bool, expectTyp(ipld.ReprKind_Bool, n.kind)
}
func (n *Node) AsInt() (v int, _ error) {
return n._int, expectTyp(ipld.ReprKind_Int, n.kind)
}
func (n *Node) AsFloat() (v float64, _ error) {
return n._float, expectTyp(ipld.ReprKind_Float, n.kind)
}
func (n *Node) AsString() (v string, _ error) {
return n._str, expectTyp(ipld.ReprKind_String, n.kind)
}
func (n *Node) AsInt() (v int, _ error) {
return n._int, expectTyp(ipld.ReprKind_Int, n.kind)
func (n *Node) AsBytes() (v []byte, _ error) {
return n._bytes, expectTyp(ipld.ReprKind_Bytes, n.kind)
}
func (n *Node) AsLink() (v cid.Cid, _ error) {
return n._link, expectTyp(ipld.ReprKind_Link, n.kind)
......
......@@ -5,6 +5,13 @@ import (
"github.com/ipld/go-ipld-prime"
)
var (
_ ipld.MutableNode = &Node{}
)
func (n *Node) SetNull() {
n.coerceType(ipld.ReprKind_Null)
}
func (n *Node) SetField(k string, v ipld.Node) {
n.coerceType(ipld.ReprKind_Map)
n._map[k] = v
......@@ -54,14 +61,6 @@ func (n *Node) SetBool(v bool) {
n.coerceType(ipld.ReprKind_Bool)
n._bool = v
}
func (n *Node) SetString(v string) {
n.coerceType(ipld.ReprKind_String)
n._str = v
}
func (n *Node) SetBytes(v []byte) {
n.coerceType(ipld.ReprKind_Bytes)
n._bytes = v
}
func (n *Node) SetInt(v int) {
n.coerceType(ipld.ReprKind_Int)
n._int = v
......@@ -70,6 +69,14 @@ func (n *Node) SetFloat(v float64) {
n.coerceType(ipld.ReprKind_Float)
n._float = v
}
func (n *Node) SetString(v string) {
n.coerceType(ipld.ReprKind_String)
n._str = v
}
func (n *Node) SetBytes(v []byte) {
n.coerceType(ipld.ReprKind_Bytes)
n._bytes = v
}
func (n *Node) SetLink(v cid.Cid) {
n.coerceType(ipld.ReprKind_Link)
n._link = v
......
......@@ -11,13 +11,13 @@ type ReprKind uint8
const (
ReprKind_Invalid = 0
ReprKind_Bool = 'b'
ReprKind_String = 's'
ReprKind_Bytes = 'x'
ReprKind_Int = 'i'
ReprKind_Float = 'f'
ReprKind_Map = '{'
ReprKind_List = '['
ReprKind_Null = '0'
ReprKind_Bool = 'b'
ReprKind_Int = 'i'
ReprKind_Float = 'f'
ReprKind_String = 's'
ReprKind_Bytes = 'x'
ReprKind_Link = '/'
)
......@@ -32,12 +32,16 @@ type Node interface {
//
// REVIEW: unsure if this should use ReprKind_Invalid or another enum.
// Since ReprKind_Invalid is returned for UnionStyle_Kinded, confusing.
// (But since this is only relevant for `typed.Node`, we can make that
// choice locally to that package.)
//IsUndefined() bool
IsNull() bool
AsBool() (bool, error)
AsString() (string, error)
AsInt() (int, error)
AsFloat() (float64, error)
AsString() (string, error)
AsBytes() ([]byte, error)
AsLink() (cid.Cid, error)
}
......@@ -57,8 +61,27 @@ type MutableNode interface {
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.)
SetNull()
SetBool(v bool)
SetString(v string)
SetInt(v int)
SetFloat(v float64)
SetString(v string)
SetBytes(v []byte)
SetLink(v cid.Cid)
}
// REVIEW: having a an immediate-mode Keys() method rather than iterator
// might actually be a bad idea. We're aiming to reuse this interface
// for *advanced layouts as well*, and those can be *large*.
//
// Similar goes for AsBytes().
//
// Probable solution is having both immediate and iterator return methods.
// Returning a reader for bytes when you know you want a slice already
// is going to be high friction without purpose in many common uses.
//
// Unclear what SetByteStream() would look like for advanced layouts.
// One could try to encapsulate the chunking entirely within the advlay
// node impl... but would it be graceful? Not sure. Maybe. Hopefully!
// Yes? The advlay impl would still tend to use SetBytes for the raw
// data model layer nodes its composing, so overall, it shakes out nicely.
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