any.go 4.55 KB
Newer Older
1 2
package impls

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

var (
	//_ ipld.Node          = &anyNode{}
	_ ipld.NodeStyle   = Style__Any{}
	_ ipld.NodeBuilder = &anyBuilder{}
	//_ ipld.NodeAssembler = &anyAssembler{}
)

// anyNode is a union meant for alloc amortization; see anyAssembler.
// Note that anyBuilder doesn't use anyNode, because it's not aiming to amortize anything.
//
// REVIEW: if there's any point in keeping this around.  It's here for completeness,
// but not currently used anywhere in package, and also not currently exported.
type anyNode struct {
	plainMap
	plainString
	plainInt
	// TODO: more of these embeds for the remaining kinds.
}

// -- Node interface methods -->

// Unimplemented at present -- see "REVIEW" comment on anyNode.

// -- NodeStyle -->

type Style__Any struct{}

func (Style__Any) NewBuilder() ipld.NodeBuilder {
	return &anyBuilder{}
36 37
}

Eric Myhre's avatar
Eric Myhre committed
38 39 40 41 42 43 44 45 46 47 48 49 50
// -- NodeBuilder -->

// anyBuilder is a builder for any kind of node.
//
// anyBuilder is a little unusual in its internal workings:
// unlike most builders, it doesn't embed the corresponding assembler,
// nor will it end up using anyNode,
// but instead embeds a builder for each of the kinds it might contain.
// This is because we want a more granular return at the end:
// if we used anyNode, and returned a pointer to just the relevant part of it,
// we'd have all the extra bytes of anyNode still reachable in GC terms
// for as long as that handle to the interior of it remains live.
type anyBuilder struct {
51
	kind ipld.ReprKind // used to select which builder to delegate 'Build' to!  Set on first interaction.
Eric Myhre's avatar
Eric Myhre committed
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

	// Only one of the following ends up being used...
	//  but we don't know in advance which one, so all are embeded here.
	//   This uses excessive space, but amortizes allocations, and all will be
	//    freed as soon as the builder is done.

	mapBuilder    plainMap__Builder
	stringBuilder plainString__Builder
	intBuilder    plainInt__Builder
	// TODO: more of these embeds for the remaining kinds.
}

func (nb *anyBuilder) Reset() {
	*nb = anyBuilder{}
}

func (nb *anyBuilder) BeginMap(sizeHint int) (ipld.MapNodeAssembler, error) {
	if nb.kind != ipld.ReprKind_Invalid {
		panic("misuse")
	}
	nb.kind = ipld.ReprKind_Map
	return nb.mapBuilder.BeginMap(sizeHint)
}
func (nb *anyBuilder) BeginList(sizeHint int) (ipld.ListNodeAssembler, error) {
	panic("soon")
}
func (nb *anyBuilder) AssignNull() error {
	if nb.kind != ipld.ReprKind_Invalid {
		panic("misuse")
	}
	nb.kind = ipld.ReprKind_Null
	return nil
}
func (nb *anyBuilder) AssignBool(v bool) error {
	panic("soon")
}
func (nb *anyBuilder) AssignInt(v int) error {
	if nb.kind != ipld.ReprKind_Invalid {
		panic("misuse")
	}
	nb.kind = ipld.ReprKind_Int
	return nb.intBuilder.AssignInt(v)
}
func (nb *anyBuilder) AssignFloat(v float64) error {
	panic("soon")
}
func (nb *anyBuilder) AssignString(v string) error {
	if nb.kind != ipld.ReprKind_Invalid {
		panic("misuse")
	}
	nb.kind = ipld.ReprKind_String
	return nb.stringBuilder.AssignString(v)
}
func (nb *anyBuilder) AssignBytes(v []byte) error {
	panic("soon")
}
108 109 110
func (nb *anyBuilder) AssignLink(v ipld.Link) error {
	panic("soon")
}
Eric Myhre's avatar
Eric Myhre committed
111
func (nb *anyBuilder) AssignNode(v ipld.Node) error {
Eric Myhre's avatar
Eric Myhre committed
112 113 114 115 116 117 118
	// TODO what to do here?  should we just... keep it, in another `Node` field?
	panic("soon")
}
func (anyBuilder) Style() ipld.NodeStyle {
	return Style__Any{}
}

119
func (nb *anyBuilder) Build() ipld.Node {
Eric Myhre's avatar
Eric Myhre committed
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
	switch nb.kind {
	case ipld.ReprKind_Invalid:
		panic("misuse")
	case ipld.ReprKind_Map:
		return nb.mapBuilder.Build()
	case ipld.ReprKind_List:
		panic("soon")
	case ipld.ReprKind_Null:
		//return ipld.Null
		panic("soon")
	case ipld.ReprKind_Bool:
		panic("soon")
	case ipld.ReprKind_Int:
		return nb.intBuilder.Build()
	case ipld.ReprKind_Float:
		panic("soon")
	case ipld.ReprKind_String:
		return nb.stringBuilder.Build()
	case ipld.ReprKind_Bytes:
		panic("soon")
	case ipld.ReprKind_Link:
		panic("soon")
	default:
		panic("unreachable")
	}
}

// -- NodeAssembler -->

// ... oddly enough, we seem to be able to put off implementing this
//  until we also implement something that goes full-hog on amortization
//   and actually has a slab of `anyNode`.  Which so far, nothing does.
//    See "REVIEW" comment on anyNode.
153
type anyAssembler struct {
Eric Myhre's avatar
Eric Myhre committed
154 155 156 157 158 159 160 161 162
	w *anyNode
}

// -- Additional typedefs for maintaining 'any' style property -->

type anyInhabitedByString plainString

func (anyInhabitedByString) Style() ipld.NodeStyle {
	return Style__Any{}
163
}
Eric Myhre's avatar
Eric Myhre committed
164 165 166 167 168 169 170 171

type anyInhabitedByInt plainInt

func (anyInhabitedByInt) Style() ipld.NodeStyle {
	return Style__Any{}
}

// TODO: more of these typedefs for the remaining kinds.