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

import (
4 5
	ipld "github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/node/mixins"
Eric Myhre's avatar
Eric Myhre committed
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
)

var (
	_ ipld.Node          = &plainList{}
	_ ipld.NodeStyle     = Style__List{}
	_ ipld.NodeBuilder   = &plainList__Builder{}
	_ ipld.NodeAssembler = &plainList__Assembler{}
)

// plainList is a concrete type that provides a list-kind ipld.Node.
// It can contain any kind of value.
// plainList is also embedded in the 'any' struct and usable from there.
type plainList struct {
	x []ipld.Node
}

// -- Node interface methods -->

func (plainList) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_List
}
func (plainList) LookupString(string) (ipld.Node, error) {
	return mixins.List{"list"}.LookupString("")
}
func (plainList) Lookup(ipld.Node) (ipld.Node, error) {
	return mixins.List{"list"}.Lookup(nil)
}
func (n *plainList) LookupIndex(idx int) (ipld.Node, error) {
	if n.Length() <= idx {
		return nil, ipld.ErrNotExists{ipld.PathSegmentOfInt(idx)}
	}
	return n.x[idx], nil
}
func (n *plainList) LookupSegment(seg ipld.PathSegment) (ipld.Node, error) {
	idx, err := seg.Index()
	if err != nil {
		panic("todo name this kind of error")
	}
	return n.LookupIndex(idx)
}
func (plainList) MapIterator() ipld.MapIterator {
	return nil
}
func (n *plainList) ListIterator() ipld.ListIterator {
	return &plainList_ListIterator{n, 0}
}
func (n *plainList) Length() int {
	return len(n.x)
}
func (plainList) IsUndefined() bool {
	return false
}
func (plainList) IsNull() bool {
	return false
}
func (plainList) AsBool() (bool, error) {
	return mixins.List{"list"}.AsBool()
}
func (plainList) AsInt() (int, error) {
	return mixins.List{"list"}.AsInt()
}
func (plainList) AsFloat() (float64, error) {
	return mixins.List{"list"}.AsFloat()
}
func (plainList) AsString() (string, error) {
	return mixins.List{"list"}.AsString()
}
func (plainList) AsBytes() ([]byte, error) {
	return mixins.List{"list"}.AsBytes()
}
func (plainList) AsLink() (ipld.Link, error) {
	return mixins.List{"list"}.AsLink()
}
func (plainList) Style() ipld.NodeStyle {
	return Style__List{}
}

type plainList_ListIterator struct {
	n   *plainList
	idx int
}

func (itr *plainList_ListIterator) Next() (idx int, v ipld.Node, _ error) {
	if itr.Done() {
		return -1, nil, ipld.ErrIteratorOverread{}
	}
	v = itr.n.x[itr.idx]
	idx = itr.idx
	itr.idx++
	return
}
func (itr *plainList_ListIterator) Done() bool {
	return itr.idx >= len(itr.n.x)
}

// -- NodeStyle -->

type Style__List struct{}

func (Style__List) NewBuilder() ipld.NodeBuilder {
	return &plainList__Builder{plainList__Assembler{w: &plainList{}}}
}

// -- NodeBuilder -->

type plainList__Builder struct {
	plainList__Assembler
}

func (nb *plainList__Builder) Build() ipld.Node {
	if nb.state != laState_finished {
		panic("invalid state: assembler must be 'finished' before Build can be called!")
	}
	return nb.w
}
func (nb *plainList__Builder) Reset() {
	*nb = plainList__Builder{}
	nb.w = &plainList{}
}

// -- NodeAssembler -->

type plainList__Assembler struct {
	w *plainList

	va plainList__ValueAssembler

	state laState
}
type plainList__ValueAssembler struct {
	la *plainList__Assembler
}

// laState is an enum of the state machine for a list assembler.
// (this might be something to export reusably, but it's also very much an impl detail that need not be seen, so, dubious.)
// it's similar to maState for maps, but has fewer states because we never have keys to assemble.
type laState uint8

const (
	laState_initial  laState = iota // also the 'expect value or finish' state
	laState_midValue                // waiting for a 'finished' state in the ValueAssembler.
	laState_finished                // 'w' will also be nil, but this is a politer statement
)

func (plainList__Assembler) BeginMap(sizeHint int) (ipld.MapNodeAssembler, error) {
	return mixins.ListAssembler{"list"}.BeginMap(0)
}
func (na *plainList__Assembler) BeginList(sizeHint int) (ipld.ListNodeAssembler, error) {
154 155 156
	if sizeHint < 0 {
		sizeHint = 0
	}
Eric Myhre's avatar
Eric Myhre committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	// Allocate storage space.
	na.w.x = make([]ipld.Node, 0, sizeHint)
	// That's it; return self as the ListNodeAssembler.  We already have all the right methods on this structure.
	return na, nil
}
func (plainList__Assembler) AssignNull() error {
	return mixins.ListAssembler{"list"}.AssignNull()
}
func (plainList__Assembler) AssignBool(bool) error {
	return mixins.ListAssembler{"list"}.AssignBool(false)
}
func (plainList__Assembler) AssignInt(int) error {
	return mixins.ListAssembler{"list"}.AssignInt(0)
}
func (plainList__Assembler) AssignFloat(float64) error {
	return mixins.ListAssembler{"list"}.AssignFloat(0)
}
func (plainList__Assembler) AssignString(string) error {
	return mixins.ListAssembler{"list"}.AssignString("")
}
func (plainList__Assembler) AssignBytes([]byte) error {
	return mixins.ListAssembler{"list"}.AssignBytes(nil)
}
func (plainList__Assembler) AssignLink(ipld.Link) error {
	return mixins.ListAssembler{"list"}.AssignLink(nil)
}
func (na *plainList__Assembler) AssignNode(v ipld.Node) error {
	// todo: apply a generic 'copy' function.
	// todo: probably can also shortcut to copying na.t and na.m if it's our same concrete type?
	//  (can't quite just `na.w = v`, because we don't have 'freeze' features, and we don't wanna open door to mutation of 'v'.)
	//   (wait... actually, probably we can?  'Assign' is a "finish" method.  we can&should invalidate the wip pointer here.)
	panic("later")
}
func (plainList__Assembler) Style() ipld.NodeStyle {
	return Style__List{}
}

// -- ListNodeAssembler -->

// AssembleValue is part of conforming to ListNodeAssembler, which we do on
// plainList__Assembler so that BeginList can just return a retyped pointer rather than new object.
func (la *plainList__Assembler) AssembleValue() ipld.NodeAssembler {
	// Sanity check, then update, assembler state.
	if la.state != laState_initial {
		panic("misuse")
	}
	la.state = laState_midValue
	// Make value assembler valid by giving it pointer back to whole 'la'; yield it.
	la.va.la = la
	return &la.va
}

// Finish is part of conforming to ListNodeAssembler, which we do on
// plainList__Assembler so that BeginList can just return a retyped pointer rather than new object.
func (la *plainList__Assembler) Finish() error {
	// Sanity check, then update, assembler state.
	if la.state != laState_initial {
		panic("misuse")
	}
	la.state = laState_finished
	// validators could run and report errors promptly, if this type had any.
	return nil
}
220 221 222
func (plainList__Assembler) ValueStyle() ipld.NodeStyle {
	return Style__Any{}
}
Eric Myhre's avatar
Eric Myhre committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

// -- ListNodeAssembler.ValueAssembler -->

func (lva *plainList__ValueAssembler) BeginMap(sizeHint int) (ipld.MapNodeAssembler, error) {
	ma := plainList__ValueAssemblerMap{}
	ma.ca.w = &plainMap{}
	ma.p = lva.la
	_, err := ma.ca.BeginMap(sizeHint)
	return &ma, err
}
func (lva *plainList__ValueAssembler) BeginList(sizeHint int) (ipld.ListNodeAssembler, error) {
	la := plainList__ValueAssemblerList{}
	la.ca.w = &plainList{}
	la.p = lva.la
	_, err := la.ca.BeginList(sizeHint)
	return &la, err
}
240 241 242 243 244 245 246
func (lva *plainList__ValueAssembler) AssignNull() error {
	return lva.AssignNode(ipld.Null)
}
func (lva *plainList__ValueAssembler) AssignBool(v bool) error {
	vb := plainBool(v)
	return lva.AssignNode(&vb)
}
Eric Myhre's avatar
Eric Myhre committed
247 248 249 250
func (lva *plainList__ValueAssembler) AssignInt(v int) error {
	vb := plainInt(v)
	return lva.AssignNode(&vb)
}
251 252 253 254
func (lva *plainList__ValueAssembler) AssignFloat(v float64) error {
	vb := plainFloat(v)
	return lva.AssignNode(&vb)
}
Eric Myhre's avatar
Eric Myhre committed
255 256 257 258
func (lva *plainList__ValueAssembler) AssignString(v string) error {
	vb := plainString(v)
	return lva.AssignNode(&vb)
}
259 260 261 262 263 264 265 266
func (lva *plainList__ValueAssembler) AssignBytes(v []byte) error {
	vb := plainBytes(v)
	return lva.AssignNode(&vb)
}
func (lva *plainList__ValueAssembler) AssignLink(v ipld.Link) error {
	vb := plainLink{v}
	return lva.AssignNode(&vb)
}
Eric Myhre's avatar
Eric Myhre committed
267 268 269 270 271 272
func (lva *plainList__ValueAssembler) AssignNode(v ipld.Node) error {
	lva.la.w.x = append(lva.la.w.x, v)
	lva.la.state = laState_initial
	lva.la = nil // invalidate self to prevent further incorrect use.
	return nil
}
273 274 275
func (plainList__ValueAssembler) Style() ipld.NodeStyle {
	return Style__Any{}
}
Eric Myhre's avatar
Eric Myhre committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

type plainList__ValueAssemblerMap struct {
	ca plainMap__Assembler
	p  *plainList__Assembler // pointer back to parent, for final insert and state bump
}

// we briefly state only the methods we need to delegate here.
// just embedding plainMap__Assembler also behaves correctly,
//  but causes a lot of unnecessary autogenerated functions in the final binary.

func (ma *plainList__ValueAssemblerMap) AssembleDirectly(k string) (ipld.NodeAssembler, error) {
	return ma.ca.AssembleDirectly(k)
}
func (ma *plainList__ValueAssemblerMap) AssembleKey() ipld.NodeAssembler {
	return ma.ca.AssembleKey()
}
func (ma *plainList__ValueAssemblerMap) AssembleValue() ipld.NodeAssembler {
	return ma.ca.AssembleValue()
}
295 296 297
func (plainList__ValueAssemblerMap) KeyStyle() ipld.NodeStyle {
	return Style__String{}
}
298
func (plainList__ValueAssemblerMap) ValueStyle(_ string) ipld.NodeStyle {
299 300
	return Style__Any{}
}
Eric Myhre's avatar
Eric Myhre committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

func (ma *plainList__ValueAssemblerMap) Finish() error {
	if err := ma.ca.Finish(); err != nil {
		return err
	}
	w := ma.ca.w
	ma.ca.w = nil
	return ma.p.va.AssignNode(w)
}

type plainList__ValueAssemblerList struct {
	ca plainList__Assembler
	p  *plainList__Assembler // pointer back to parent, for final insert and state bump
}

// we briefly state only the methods we need to delegate here.
// just embedding plainList__Assembler also behaves correctly,
//  but causes a lot of unnecessary autogenerated functions in the final binary.

func (la *plainList__ValueAssemblerList) AssembleValue() ipld.NodeAssembler {
	return la.ca.AssembleValue()
}
323 324 325
func (plainList__ValueAssemblerList) ValueStyle() ipld.NodeStyle {
	return Style__Any{}
}
Eric Myhre's avatar
Eric Myhre committed
326 327 328 329 330 331 332 333 334

func (la *plainList__ValueAssemblerList) Finish() error {
	if err := la.ca.Finish(); err != nil {
		return err
	}
	w := la.ca.w
	la.ca.w = nil
	return la.p.va.AssignNode(w)
}