thunks.go 2.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package ipld

var Null Node = nullNode{}
var Undef Node = undefNode{}

type nullNode struct{}

func (nullNode) ReprKind() ReprKind {
	return ReprKind_Null
}
11
func (nullNode) LookupString(key string) (Node, error) {
12
	return nil, ErrWrongKind{MethodName: "LookupString", AppropriateKind: ReprKindSet_JustMap, ActualKind: ReprKind_Null}
13
}
14
func (nullNode) Lookup(key Node) (Node, error) {
15
	return nil, ErrWrongKind{MethodName: "Lookup", AppropriateKind: ReprKindSet_JustMap, ActualKind: ReprKind_Null}
16
}
17
func (nullNode) LookupIndex(idx int) (Node, error) {
18
	return nil, ErrWrongKind{MethodName: "LookupIndex", AppropriateKind: ReprKindSet_JustList, ActualKind: ReprKind_Null}
19 20
}
func (nullNode) MapIterator() MapIterator {
21
	return mapIteratorReject{ErrWrongKind{MethodName: "MapIterator", AppropriateKind: ReprKindSet_JustMap, ActualKind: ReprKind_Null}}
22 23
}
func (nullNode) ListIterator() ListIterator {
24
	return listIteratorReject{ErrWrongKind{MethodName: "ListIterator", AppropriateKind: ReprKindSet_JustList, ActualKind: ReprKind_Null}}
25 26 27 28 29 30 31 32 33 34 35
}
func (nullNode) Length() int {
	return -1
}
func (nullNode) IsUndefined() bool {
	return false
}
func (nullNode) IsNull() bool {
	return true
}
func (nullNode) AsBool() (bool, error) {
36
	return false, ErrWrongKind{MethodName: "AsBool", AppropriateKind: ReprKindSet_JustBool, ActualKind: ReprKind_Null}
37 38
}
func (nullNode) AsInt() (int, error) {
39
	return 0, ErrWrongKind{MethodName: "AsInt", AppropriateKind: ReprKindSet_JustInt, ActualKind: ReprKind_Null}
40 41
}
func (nullNode) AsFloat() (float64, error) {
42
	return 0, ErrWrongKind{MethodName: "AsFloat", AppropriateKind: ReprKindSet_JustFloat, ActualKind: ReprKind_Null}
43 44
}
func (nullNode) AsString() (string, error) {
45
	return "", ErrWrongKind{MethodName: "AsString", AppropriateKind: ReprKindSet_JustString, ActualKind: ReprKind_Null}
46 47
}
func (nullNode) AsBytes() ([]byte, error) {
48
	return nil, ErrWrongKind{MethodName: "AsBytes", AppropriateKind: ReprKindSet_JustBytes, ActualKind: ReprKind_Null}
49 50
}
func (nullNode) AsLink() (Link, error) {
51
	return nil, ErrWrongKind{MethodName: "AsLink", AppropriateKind: ReprKindSet_JustLink, ActualKind: ReprKind_Null}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}
func (nullNode) NodeBuilder() NodeBuilder {
	panic("cannot build null nodes")
}

type undefNode struct{ nullNode }

func (undefNode) IsUndefined() bool {
	return true
}
func (undefNode) IsNull() bool {
	return false
}

type mapIteratorReject struct{ err error }
type listIteratorReject struct{ err error }

func (itr mapIteratorReject) Next() (Node, Node, error) { return nil, nil, itr.err }
func (itr mapIteratorReject) Done() bool                { return false }

func (itr listIteratorReject) Next() (int, Node, error) { return -1, nil, itr.err }
func (itr listIteratorReject) Done() bool               { return false }