rot13substrate.go 5.84 KB
Newer Older
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
package rot13adl

import (
	"github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/node/mixins"
	"github.com/ipld/go-ipld-prime/schema"
)

// Substrate returns the root node of the raw internal data form of the ADL's content.
func (n *_R13String) Substrate() ipld.Node {
	// This is a very minor twist in the case of the rot13 ADL.
	//  However, for larger ADLs (especially those relating to multi-block collections),
	//   this could be quite a bit more involved, and would almost certainly be the root node of a larger tree.
	return (*_Substrate)(n)
}

// -- Node -->

var _ ipld.Node = (*_Substrate)(nil)

// Somewhat unusually for an ADL, there's only one substrate node type,
// and we actually made it have the same in-memory structure as the synthesized view node.
type _Substrate _R13String

// REVIEW: what on earth we think the "TypeName" strings in error messages and other references to this node should be.
//  At the moment, it shares a prefix with the synthesized node, which is potentially confusing (?),
//  and I'm not sure what, if any, suffix actually makes meaningful sense to a user either.
//  I added the segment ".internal." to the middle of the name mangle; does this seem helpful?

func (*_Substrate) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_String
}
func (*_Substrate) LookupByString(string) (ipld.Node, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.LookupByString("")
}
func (*_Substrate) LookupByNode(ipld.Node) (ipld.Node, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.LookupByNode(nil)
}
func (*_Substrate) LookupByIndex(idx int) (ipld.Node, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.LookupByIndex(0)
}
func (*_Substrate) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.LookupBySegment(seg)
}
func (*_Substrate) MapIterator() ipld.MapIterator {
	return nil
}
func (*_Substrate) ListIterator() ipld.ListIterator {
	return nil
}
func (*_Substrate) Length() int {
	return -1
}
func (*_Substrate) IsAbsent() bool {
	return false
}
func (*_Substrate) IsNull() bool {
	return false
}
func (*_Substrate) AsBool() (bool, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.AsBool()
}
func (*_Substrate) AsInt() (int, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.AsInt()
}
func (*_Substrate) AsFloat() (float64, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.AsFloat()
}
func (n *_Substrate) AsString() (string, error) {
	return n.synthesized, nil
}
func (*_Substrate) AsBytes() ([]byte, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.AsBytes()
}
func (*_Substrate) AsLink() (ipld.Link, error) {
	return mixins.String{"rot13adl.internal.Substrate"}.AsLink()
}
func (*_Substrate) Prototype() ipld.NodePrototype {
	return _Substrate__Prototype{}
}

// -- NodePrototype -->

var _ ipld.NodePrototype = Prototype{}

type SubstrateRootPrototype = _Substrate__Prototype

type _Substrate__Prototype struct {
	// There's no configuration to this ADL.
}

func (np _Substrate__Prototype) NewBuilder() ipld.NodeBuilder {
	return &_Substrate__Builder{}
}

// -- NodeBuilder -->

var _ ipld.NodeBuilder = (*_Substrate__Builder)(nil)

type _Substrate__Builder struct {
	_Substrate__Assembler
}

func (nb *_Substrate__Builder) Build() ipld.Node {
	if nb.m != schema.Maybe_Value {
		panic("invalid state: cannot call Build on an assembler that's not finished")
	}
	return nb.w
}
func (nb *_Substrate__Builder) Reset() {
	*nb = _Substrate__Builder{}
}

// -- NodeAssembler -->

var _ ipld.NodeAssembler = (*_Substrate__Assembler)(nil)

type _Substrate__Assembler struct {
	w *_Substrate
	m schema.Maybe // REVIEW: if the package where this Maybe enum lives is maybe not the right home for it after all.  Or should this line use something different?  We're only using some of its values after all.
}

func (_Substrate__Assembler) BeginMap(sizeHint int) (ipld.MapAssembler, error) {
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.BeginMap(0)
}
func (_Substrate__Assembler) BeginList(sizeHint int) (ipld.ListAssembler, error) {
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.BeginList(0)
}
func (na *_Substrate__Assembler) AssignNull() error {
	// REVIEW: unclear how this might compose with some other context (like a schema) which does allow nulls.  Probably a wrapper type?
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.AssignNull()
}
func (_Substrate__Assembler) AssignBool(bool) error {
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.AssignBool(false)
}
func (_Substrate__Assembler) AssignInt(int) error {
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.AssignInt(0)
}
func (_Substrate__Assembler) AssignFloat(float64) error {
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.AssignFloat(0)
}
func (na *_Substrate__Assembler) AssignString(v string) error {
	switch na.m {
	case schema.Maybe_Value:
		panic("invalid state: cannot assign into assembler that's already finished")
	}
	na.w = &_Substrate{
		raw:         v,
		synthesized: unrotate(v),
	}
	na.m = schema.Maybe_Value
	return nil
}
func (_Substrate__Assembler) AssignBytes([]byte) error {
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.AssignBytes(nil)
}
func (_Substrate__Assembler) AssignLink(ipld.Link) error {
	return mixins.StringAssembler{"rot13adl.internal.Substrate"}.AssignLink(nil)
}
func (na *_Substrate__Assembler) AssignNode(v ipld.Node) error {
	if v.IsNull() {
		return na.AssignNull()
	}
	if v2, ok := v.(*_Substrate); ok {
		switch na.m {
		case schema.Maybe_Value:
			panic("invalid state: cannot assign into assembler that's already finished")
		}
		na.w = v2
		na.m = schema.Maybe_Value
		return nil
	}
	if v2, err := v.AsString(); err != nil {
		return err
	} else {
		return na.AssignString(v2)
	}
}
func (_Substrate__Assembler) Prototype() ipld.NodePrototype {
	return _Substrate__Prototype{}
}