-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathbytes.go
157 lines (141 loc) · 4.24 KB
/
bytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package basicnode
import (
"bytes"
"io"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/mixins"
)
var (
_ datamodel.Node = plainBytes(nil)
_ datamodel.NodePrototype = Prototype__Bytes{}
_ datamodel.NodeBuilder = &plainBytes__Builder{}
_ datamodel.NodeAssembler = &plainBytes__Assembler{}
)
func NewBytes(value []byte) datamodel.Node {
v := plainBytes(value)
return &v
}
// plainBytes is a simple boxed byte slice that complies with datamodel.Node.
type plainBytes []byte
// -- Node interface methods -->
func (plainBytes) Kind() datamodel.Kind {
return datamodel.Kind_Bytes
}
func (plainBytes) LookupByString(string) (datamodel.Node, error) {
return mixins.Bytes{TypeName: "bytes"}.LookupByString("")
}
func (plainBytes) LookupByNode(key datamodel.Node) (datamodel.Node, error) {
return mixins.Bytes{TypeName: "bytes"}.LookupByNode(nil)
}
func (plainBytes) LookupByIndex(idx int64) (datamodel.Node, error) {
return mixins.Bytes{TypeName: "bytes"}.LookupByIndex(0)
}
func (plainBytes) LookupBySegment(seg datamodel.PathSegment) (datamodel.Node, error) {
return mixins.Bytes{TypeName: "bytes"}.LookupBySegment(seg)
}
func (plainBytes) MapIterator() datamodel.MapIterator {
return nil
}
func (plainBytes) ListIterator() datamodel.ListIterator {
return nil
}
func (plainBytes) Length() int64 {
return -1
}
func (plainBytes) IsAbsent() bool {
return false
}
func (plainBytes) IsNull() bool {
return false
}
func (plainBytes) AsBool() (bool, error) {
return mixins.Bytes{TypeName: "bytes"}.AsBool()
}
func (plainBytes) AsInt() (int64, error) {
return mixins.Bytes{TypeName: "bytes"}.AsInt()
}
func (plainBytes) AsFloat() (float64, error) {
return mixins.Bytes{TypeName: "bytes"}.AsFloat()
}
func (plainBytes) AsString() (string, error) {
return mixins.Bytes{TypeName: "bytes"}.AsString()
}
func (n plainBytes) AsBytes() ([]byte, error) {
return []byte(n), nil
}
func (plainBytes) AsLink() (datamodel.Link, error) {
return mixins.Bytes{TypeName: "bytes"}.AsLink()
}
func (plainBytes) Prototype() datamodel.NodePrototype {
return Prototype__Bytes{}
}
func (n plainBytes) AsLargeBytes() (io.ReadSeeker, error) {
return bytes.NewReader(n), nil
}
// -- NodePrototype -->
type Prototype__Bytes struct{}
func (Prototype__Bytes) NewBuilder() datamodel.NodeBuilder {
var w plainBytes
return &plainBytes__Builder{plainBytes__Assembler{w: &w}}
}
// -- NodeBuilder -->
type plainBytes__Builder struct {
plainBytes__Assembler
}
func (nb *plainBytes__Builder) Build() datamodel.Node {
return nb.w
}
func (nb *plainBytes__Builder) Reset() {
var w plainBytes
*nb = plainBytes__Builder{plainBytes__Assembler{w: &w}}
}
// -- NodeAssembler -->
type plainBytes__Assembler struct {
w datamodel.Node
}
func (plainBytes__Assembler) BeginMap(sizeHint int64) (datamodel.MapAssembler, error) {
return mixins.BytesAssembler{TypeName: "bytes"}.BeginMap(0)
}
func (plainBytes__Assembler) BeginList(sizeHint int64) (datamodel.ListAssembler, error) {
return mixins.BytesAssembler{TypeName: "bytes"}.BeginList(0)
}
func (plainBytes__Assembler) AssignNull() error {
return mixins.BytesAssembler{TypeName: "bytes"}.AssignNull()
}
func (plainBytes__Assembler) AssignBool(bool) error {
return mixins.BytesAssembler{TypeName: "bytes"}.AssignBool(false)
}
func (plainBytes__Assembler) AssignInt(int64) error {
return mixins.BytesAssembler{TypeName: "bytes"}.AssignInt(0)
}
func (plainBytes__Assembler) AssignFloat(float64) error {
return mixins.BytesAssembler{TypeName: "bytes"}.AssignFloat(0)
}
func (plainBytes__Assembler) AssignString(string) error {
return mixins.BytesAssembler{TypeName: "bytes"}.AssignString("")
}
func (na *plainBytes__Assembler) AssignBytes(v []byte) error {
na.w = datamodel.Node(plainBytes(v))
return nil
}
func (plainBytes__Assembler) AssignLink(datamodel.Link) error {
return mixins.BytesAssembler{TypeName: "bytes"}.AssignLink(nil)
}
func (na *plainBytes__Assembler) AssignNode(v datamodel.Node) error {
if lb, ok := v.(datamodel.LargeBytesNode); ok {
lbn, err := lb.AsLargeBytes()
if err == nil {
na.w = streamBytes{lbn}
return nil
}
}
if v2, err := v.AsBytes(); err != nil {
return err
} else {
na.w = plainBytes(v2)
return nil
}
}
func (plainBytes__Assembler) Prototype() datamodel.NodePrototype {
return Prototype__Bytes{}
}