int.go 3.46 KB
Newer Older
1 2 3 4
package impls

import (
	ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution"
5
	"github.com/ipld/go-ipld-prime/_rsrch/nodesolution/impls/mixins"
6 7
)

8 9 10 11 12 13 14
var (
	_ ipld.Node          = plainInt(0)
	_ ipld.NodeStyle     = Style__Int{}
	_ ipld.NodeBuilder   = &plainInt__Builder{}
	_ ipld.NodeAssembler = &plainInt__Assembler{}
)

15 16 17 18 19 20 21
func Int(value int) ipld.Node {
	return plainInt(value)
}

// plainInt is a simple boxed int that complies with ipld.Node.
type plainInt int

22 23
// -- Node interface methods -->

24 25 26 27
func (plainInt) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Int
}
func (plainInt) LookupString(string) (ipld.Node, error) {
28
	return mixins.Int{"int"}.LookupString("")
29 30
}
func (plainInt) Lookup(key ipld.Node) (ipld.Node, error) {
31
	return mixins.Int{"int"}.Lookup(nil)
32 33
}
func (plainInt) LookupIndex(idx int) (ipld.Node, error) {
34
	return mixins.Int{"int"}.LookupIndex(0)
35 36
}
func (plainInt) LookupSegment(seg ipld.PathSegment) (ipld.Node, error) {
37
	return mixins.Int{"int"}.LookupSegment(seg)
38 39
}
func (plainInt) MapIterator() ipld.MapIterator {
40
	return nil
41 42
}
func (plainInt) ListIterator() ipld.ListIterator {
43
	return nil
44 45 46 47 48 49 50 51 52 53 54
}
func (plainInt) Length() int {
	return -1
}
func (plainInt) IsUndefined() bool {
	return false
}
func (plainInt) IsNull() bool {
	return false
}
func (plainInt) AsBool() (bool, error) {
55
	return mixins.Int{"int"}.AsBool()
56 57 58 59 60
}
func (n plainInt) AsInt() (int, error) {
	return int(n), nil
}
func (plainInt) AsFloat() (float64, error) {
61
	return mixins.Int{"int"}.AsFloat()
62 63
}
func (plainInt) AsString() (string, error) {
64
	return mixins.Int{"int"}.AsString()
65 66
}
func (plainInt) AsBytes() ([]byte, error) {
67
	return mixins.Int{"int"}.AsBytes()
68 69
}
func (plainInt) AsLink() (ipld.Link, error) {
70
	return mixins.Int{"int"}.AsLink()
71 72
}
func (plainInt) Style() ipld.NodeStyle {
Eric Myhre's avatar
Eric Myhre committed
73
	return Style__Int{}
74
}
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

// -- NodeStyle -->

type Style__Int struct{}

func (Style__Int) NewBuilder() ipld.NodeBuilder {
	var w plainInt
	return &plainInt__Builder{plainInt__Assembler{w: &w}}
}

// -- NodeBuilder -->

type plainInt__Builder struct {
	plainInt__Assembler
}

91 92
func (nb *plainInt__Builder) Build() ipld.Node {
	return nb.w
93 94 95 96 97 98 99 100 101 102 103 104
}
func (nb *plainInt__Builder) Reset() {
	var w plainInt
	*nb = plainInt__Builder{plainInt__Assembler{w: &w}}
}

// -- NodeAssembler -->

type plainInt__Assembler struct {
	w *plainInt
}

Eric Myhre's avatar
Eric Myhre committed
105 106 107 108 109 110 111 112 113 114 115 116
func (plainInt__Assembler) BeginMap(sizeHint int) (ipld.MapNodeAssembler, error) {
	return mixins.IntAssembler{"int"}.BeginMap(0)
}
func (plainInt__Assembler) BeginList(sizeHint int) (ipld.ListNodeAssembler, error) {
	return mixins.IntAssembler{"int"}.BeginList(0)
}
func (plainInt__Assembler) AssignNull() error {
	return mixins.IntAssembler{"int"}.AssignNull()
}
func (plainInt__Assembler) AssignBool(bool) error {
	return mixins.IntAssembler{"int"}.AssignBool(false)
}
117 118 119 120
func (na *plainInt__Assembler) AssignInt(v int) error {
	*na.w = plainInt(v)
	return nil
}
Eric Myhre's avatar
Eric Myhre committed
121 122 123 124 125 126 127 128 129
func (plainInt__Assembler) AssignFloat(float64) error {
	return mixins.IntAssembler{"int"}.AssignFloat(0)
}
func (plainInt__Assembler) AssignString(string) error {
	return mixins.IntAssembler{"int"}.AssignString("")
}
func (plainInt__Assembler) AssignBytes([]byte) error {
	return mixins.IntAssembler{"int"}.AssignBytes(nil)
}
Eric Myhre's avatar
Eric Myhre committed
130
func (na *plainInt__Assembler) AssignNode(v ipld.Node) error {
131 132 133 134 135 136 137
	if s, err := v.AsInt(); err != nil {
		return err
	} else {
		*na.w = plainInt(s)
		return nil
	}
}
138 139 140
func (plainInt__Assembler) AssignLink(ipld.Link) error {
	return mixins.IntAssembler{"int"}.AssignLink(nil)
}
141 142 143
func (plainInt__Assembler) Style() ipld.NodeStyle {
	return Style__Int{}
}