unit.go 4.74 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1
package ld
2 3 4 5 6

var Null Node = nullNode{}

type nullNode struct{}

7 8
func (nullNode) Kind() Kind {
	return Kind_Null
9
}
10
func (nullNode) LookupByString(key string) (Node, error) {
11
	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupByString", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
12
}
13
func (nullNode) LookupByNode(key Node) (Node, error) {
14
	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupByNode", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
15
}
16
func (nullNode) LookupByIndex(idx int64) (Node, error) {
17
	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupByIndex", AppropriateKind: KindSet_JustList, ActualKind: Kind_Null}
18
}
19
func (nullNode) LookupBySegment(seg PathSegment) (Node, error) {
20
	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupBySegment", AppropriateKind: KindSet_Recursive, ActualKind: Kind_Null}
21 22 23 24 25 26 27
}
func (nullNode) MapIterator() MapIterator {
	return nil
}
func (nullNode) ListIterator() ListIterator {
	return nil
}
28
func (nullNode) Length() int64 {
29 30
	return -1
}
31
func (nullNode) IsAbsent() bool {
32 33 34 35 36 37
	return false
}
func (nullNode) IsNull() bool {
	return true
}
func (nullNode) AsBool() (bool, error) {
38
	return false, ErrWrongKind{TypeName: "null", MethodName: "AsBool", AppropriateKind: KindSet_JustBool, ActualKind: Kind_Null}
39
}
40
func (nullNode) AsInt() (int64, error) {
41
	return 0, ErrWrongKind{TypeName: "null", MethodName: "AsInt", AppropriateKind: KindSet_JustInt, ActualKind: Kind_Null}
42 43
}
func (nullNode) AsFloat() (float64, error) {
44
	return 0, ErrWrongKind{TypeName: "null", MethodName: "AsFloat", AppropriateKind: KindSet_JustFloat, ActualKind: Kind_Null}
45 46
}
func (nullNode) AsString() (string, error) {
47
	return "", ErrWrongKind{TypeName: "null", MethodName: "AsString", AppropriateKind: KindSet_JustString, ActualKind: Kind_Null}
48 49
}
func (nullNode) AsBytes() ([]byte, error) {
50
	return nil, ErrWrongKind{TypeName: "null", MethodName: "AsBytes", AppropriateKind: KindSet_JustBytes, ActualKind: Kind_Null}
51 52
}
func (nullNode) AsLink() (Link, error) {
53
	return nil, ErrWrongKind{TypeName: "null", MethodName: "AsLink", AppropriateKind: KindSet_JustLink, ActualKind: Kind_Null}
54
}
55 56
func (nullNode) Prototype() NodePrototype {
	return nullPrototype{}
57 58
}

59
type nullPrototype struct{}
60

61
func (nullPrototype) NewBuilder() NodeBuilder {
62 63 64
	panic("cannot build null nodes") // TODO: okay, fine, we could grind out a simple closing of the loop here.
}

65
var Absent Node = absentNode{}
66

67
type absentNode struct{}
68

69 70
func (absentNode) Kind() Kind {
	return Kind_Null
71
}
72
func (absentNode) LookupByString(key string) (Node, error) {
73
	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupByString", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
74
}
75
func (absentNode) LookupByNode(key Node) (Node, error) {
76
	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupByNode", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
77
}
78
func (absentNode) LookupByIndex(idx int64) (Node, error) {
79
	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupByIndex", AppropriateKind: KindSet_JustList, ActualKind: Kind_Null}
80
}
81
func (absentNode) LookupBySegment(seg PathSegment) (Node, error) {
82
	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupBySegment", AppropriateKind: KindSet_Recursive, ActualKind: Kind_Null}
83
}
84
func (absentNode) MapIterator() MapIterator {
85 86
	return nil
}
87
func (absentNode) ListIterator() ListIterator {
88 89
	return nil
}
90
func (absentNode) Length() int64 {
91 92
	return -1
}
93
func (absentNode) IsAbsent() bool {
94 95
	return true
}
96
func (absentNode) IsNull() bool {
97 98
	return false
}
99
func (absentNode) AsBool() (bool, error) {
100
	return false, ErrWrongKind{TypeName: "absent", MethodName: "AsBool", AppropriateKind: KindSet_JustBool, ActualKind: Kind_Null}
101
}
102
func (absentNode) AsInt() (int64, error) {
103
	return 0, ErrWrongKind{TypeName: "absent", MethodName: "AsInt", AppropriateKind: KindSet_JustInt, ActualKind: Kind_Null}
104
}
105
func (absentNode) AsFloat() (float64, error) {
106
	return 0, ErrWrongKind{TypeName: "absent", MethodName: "AsFloat", AppropriateKind: KindSet_JustFloat, ActualKind: Kind_Null}
107
}
108
func (absentNode) AsString() (string, error) {
109
	return "", ErrWrongKind{TypeName: "absent", MethodName: "AsString", AppropriateKind: KindSet_JustString, ActualKind: Kind_Null}
110
}
111
func (absentNode) AsBytes() ([]byte, error) {
112
	return nil, ErrWrongKind{TypeName: "absent", MethodName: "AsBytes", AppropriateKind: KindSet_JustBytes, ActualKind: Kind_Null}
113
}
114
func (absentNode) AsLink() (Link, error) {
115
	return nil, ErrWrongKind{TypeName: "absent", MethodName: "AsLink", AppropriateKind: KindSet_JustLink, ActualKind: Kind_Null}
116
}
117 118
func (absentNode) Prototype() NodePrototype {
	return absentPrototype{}
119 120
}

121
type absentPrototype struct{}
122

123 124
func (absentPrototype) NewBuilder() NodeBuilder {
	panic("cannot build absent nodes") // this definitely stays true.
125
}