bool.go 3.7 KB
Newer Older
1
package basicnode
Eric Myhre's avatar
Eric Myhre committed
2 3

import (
tavit ohanian's avatar
tavit ohanian committed
4
	ld "gitlab.dms3.io/ld/go-ld-prime"
tavit ohanian's avatar
tavit ohanian committed
5
	"gitlab.dms3.io/ld/go-ld-prime/node/mixins"
Eric Myhre's avatar
Eric Myhre committed
6 7 8
)

var (
tavit ohanian's avatar
tavit ohanian committed
9 10 11 12
	_ ld.Node          = plainBool(false)
	_ ld.NodePrototype = Prototype__Bool{}
	_ ld.NodeBuilder   = &plainBool__Builder{}
	_ ld.NodeAssembler = &plainBool__Assembler{}
Eric Myhre's avatar
Eric Myhre committed
13 14
)

tavit ohanian's avatar
tavit ohanian committed
15
func NewBool(value bool) ld.Node {
16 17
	v := plainBool(value)
	return &v
Eric Myhre's avatar
Eric Myhre committed
18 19
}

tavit ohanian's avatar
tavit ohanian committed
20
// plainBool is a simple boxed boolean that complies with ld.Node.
Eric Myhre's avatar
Eric Myhre committed
21 22 23 24
type plainBool bool

// -- Node interface methods -->

tavit ohanian's avatar
tavit ohanian committed
25 26
func (plainBool) Kind() ld.Kind {
	return ld.Kind_Bool
Eric Myhre's avatar
Eric Myhre committed
27
}
tavit ohanian's avatar
tavit ohanian committed
28
func (plainBool) LookupByString(string) (ld.Node, error) {
29
	return mixins.Bool{TypeName: "bool"}.LookupByString("")
Eric Myhre's avatar
Eric Myhre committed
30
}
tavit ohanian's avatar
tavit ohanian committed
31
func (plainBool) LookupByNode(key ld.Node) (ld.Node, error) {
32
	return mixins.Bool{TypeName: "bool"}.LookupByNode(nil)
Eric Myhre's avatar
Eric Myhre committed
33
}
tavit ohanian's avatar
tavit ohanian committed
34
func (plainBool) LookupByIndex(idx int64) (ld.Node, error) {
35
	return mixins.Bool{TypeName: "bool"}.LookupByIndex(0)
Eric Myhre's avatar
Eric Myhre committed
36
}
tavit ohanian's avatar
tavit ohanian committed
37
func (plainBool) LookupBySegment(seg ld.PathSegment) (ld.Node, error) {
38
	return mixins.Bool{TypeName: "bool"}.LookupBySegment(seg)
Eric Myhre's avatar
Eric Myhre committed
39
}
tavit ohanian's avatar
tavit ohanian committed
40
func (plainBool) MapIterator() ld.MapIterator {
Eric Myhre's avatar
Eric Myhre committed
41 42
	return nil
}
tavit ohanian's avatar
tavit ohanian committed
43
func (plainBool) ListIterator() ld.ListIterator {
Eric Myhre's avatar
Eric Myhre committed
44 45
	return nil
}
46
func (plainBool) Length() int64 {
Eric Myhre's avatar
Eric Myhre committed
47 48
	return -1
}
49
func (plainBool) IsAbsent() bool {
Eric Myhre's avatar
Eric Myhre committed
50 51 52 53 54 55 56 57
	return false
}
func (plainBool) IsNull() bool {
	return false
}
func (n plainBool) AsBool() (bool, error) {
	return bool(n), nil
}
58
func (plainBool) AsInt() (int64, error) {
59
	return mixins.Bool{TypeName: "bool"}.AsInt()
Eric Myhre's avatar
Eric Myhre committed
60 61
}
func (plainBool) AsFloat() (float64, error) {
62
	return mixins.Bool{TypeName: "bool"}.AsFloat()
Eric Myhre's avatar
Eric Myhre committed
63 64
}
func (plainBool) AsString() (string, error) {
65
	return mixins.Bool{TypeName: "bool"}.AsString()
Eric Myhre's avatar
Eric Myhre committed
66 67
}
func (plainBool) AsBytes() ([]byte, error) {
68
	return mixins.Bool{TypeName: "bool"}.AsBytes()
Eric Myhre's avatar
Eric Myhre committed
69
}
tavit ohanian's avatar
tavit ohanian committed
70
func (plainBool) AsLink() (ld.Link, error) {
71
	return mixins.Bool{TypeName: "bool"}.AsLink()
Eric Myhre's avatar
Eric Myhre committed
72
}
tavit ohanian's avatar
tavit ohanian committed
73
func (plainBool) Prototype() ld.NodePrototype {
74
	return Prototype__Bool{}
Eric Myhre's avatar
Eric Myhre committed
75 76
}

77
// -- NodePrototype -->
Eric Myhre's avatar
Eric Myhre committed
78

79
type Prototype__Bool struct{}
Eric Myhre's avatar
Eric Myhre committed
80

tavit ohanian's avatar
tavit ohanian committed
81
func (Prototype__Bool) NewBuilder() ld.NodeBuilder {
Eric Myhre's avatar
Eric Myhre committed
82 83 84 85 86 87 88 89 90 91
	var w plainBool
	return &plainBool__Builder{plainBool__Assembler{w: &w}}
}

// -- NodeBuilder -->

type plainBool__Builder struct {
	plainBool__Assembler
}

tavit ohanian's avatar
tavit ohanian committed
92
func (nb *plainBool__Builder) Build() ld.Node {
Eric Myhre's avatar
Eric Myhre committed
93 94 95 96 97 98 99 100 101 102 103 104 105
	return nb.w
}
func (nb *plainBool__Builder) Reset() {
	var w plainBool
	*nb = plainBool__Builder{plainBool__Assembler{w: &w}}
}

// -- NodeAssembler -->

type plainBool__Assembler struct {
	w *plainBool
}

tavit ohanian's avatar
tavit ohanian committed
106
func (plainBool__Assembler) BeginMap(sizeHint int64) (ld.MapAssembler, error) {
107
	return mixins.BoolAssembler{TypeName: "bool"}.BeginMap(0)
Eric Myhre's avatar
Eric Myhre committed
108
}
tavit ohanian's avatar
tavit ohanian committed
109
func (plainBool__Assembler) BeginList(sizeHint int64) (ld.ListAssembler, error) {
110
	return mixins.BoolAssembler{TypeName: "bool"}.BeginList(0)
Eric Myhre's avatar
Eric Myhre committed
111 112
}
func (plainBool__Assembler) AssignNull() error {
113
	return mixins.BoolAssembler{TypeName: "bool"}.AssignNull()
Eric Myhre's avatar
Eric Myhre committed
114 115 116 117 118
}
func (na *plainBool__Assembler) AssignBool(v bool) error {
	*na.w = plainBool(v)
	return nil
}
119
func (plainBool__Assembler) AssignInt(int64) error {
120
	return mixins.BoolAssembler{TypeName: "bool"}.AssignInt(0)
Eric Myhre's avatar
Eric Myhre committed
121 122
}
func (plainBool__Assembler) AssignFloat(float64) error {
123
	return mixins.BoolAssembler{TypeName: "bool"}.AssignFloat(0)
Eric Myhre's avatar
Eric Myhre committed
124 125
}
func (plainBool__Assembler) AssignString(string) error {
126
	return mixins.BoolAssembler{TypeName: "bool"}.AssignString("")
Eric Myhre's avatar
Eric Myhre committed
127 128
}
func (plainBool__Assembler) AssignBytes([]byte) error {
129
	return mixins.BoolAssembler{TypeName: "bool"}.AssignBytes(nil)
Eric Myhre's avatar
Eric Myhre committed
130
}
tavit ohanian's avatar
tavit ohanian committed
131
func (plainBool__Assembler) AssignLink(ld.Link) error {
132
	return mixins.BoolAssembler{TypeName: "bool"}.AssignLink(nil)
Eric Myhre's avatar
Eric Myhre committed
133
}
tavit ohanian's avatar
tavit ohanian committed
134
func (na *plainBool__Assembler) AssignNode(v ld.Node) error {
Eric Myhre's avatar
Eric Myhre committed
135 136 137 138 139 140 141
	if v2, err := v.AsBool(); err != nil {
		return err
	} else {
		*na.w = plainBool(v2)
		return nil
	}
}
tavit ohanian's avatar
tavit ohanian committed
142
func (plainBool__Assembler) Prototype() ld.NodePrototype {
143
	return Prototype__Bool{}
Eric Myhre's avatar
Eric Myhre committed
144
}