rot13substrate.go 6.09 KB
Newer Older
1 2 3
package rot13adl

import (
tavit ohanian's avatar
tavit ohanian committed
4 5 6
	"gitlab.dms3.io/ld/go-ld-prime"
	"gitlab.dms3.io/ld/go-ld-prime/node/mixins"
	"gitlab.dms3.io/ld/go-ld-prime/schema"
7 8 9
)

// Substrate returns the root node of the raw internal data form of the ADL's content.
tavit ohanian's avatar
tavit ohanian committed
10
func (n *_R13String) Substrate() ld.Node {
11 12 13 14 15 16 17 18
	// 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 -->

tavit ohanian's avatar
tavit ohanian committed
19
var _ ld.Node = (*_Substrate)(nil)
20 21 22

// 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.
23 24 25
//
// When implementing other more complex ADLs, it will probably be more common to have
// the synthesized high-level node type either embed or have a pointer to the substrate root node.
26 27 28 29 30 31 32
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?

tavit ohanian's avatar
tavit ohanian committed
33 34
func (*_Substrate) Kind() ld.Kind {
	return ld.Kind_String
35
}
tavit ohanian's avatar
tavit ohanian committed
36
func (*_Substrate) LookupByString(string) (ld.Node, error) {
37
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupByString("")
38
}
tavit ohanian's avatar
tavit ohanian committed
39
func (*_Substrate) LookupByNode(ld.Node) (ld.Node, error) {
40
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupByNode(nil)
41
}
tavit ohanian's avatar
tavit ohanian committed
42
func (*_Substrate) LookupByIndex(idx int64) (ld.Node, error) {
43
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupByIndex(0)
44
}
tavit ohanian's avatar
tavit ohanian committed
45
func (*_Substrate) LookupBySegment(seg ld.PathSegment) (ld.Node, error) {
46
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupBySegment(seg)
47
}
tavit ohanian's avatar
tavit ohanian committed
48
func (*_Substrate) MapIterator() ld.MapIterator {
49 50
	return nil
}
tavit ohanian's avatar
tavit ohanian committed
51
func (*_Substrate) ListIterator() ld.ListIterator {
52 53
	return nil
}
54
func (*_Substrate) Length() int64 {
55 56 57 58 59 60 61 62 63
	return -1
}
func (*_Substrate) IsAbsent() bool {
	return false
}
func (*_Substrate) IsNull() bool {
	return false
}
func (*_Substrate) AsBool() (bool, error) {
64
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsBool()
65
}
66
func (*_Substrate) AsInt() (int64, error) {
67
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsInt()
68 69
}
func (*_Substrate) AsFloat() (float64, error) {
70
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsFloat()
71 72
}
func (n *_Substrate) AsString() (string, error) {
73
	return n.raw, nil
74 75
}
func (*_Substrate) AsBytes() ([]byte, error) {
76
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsBytes()
77
}
tavit ohanian's avatar
tavit ohanian committed
78
func (*_Substrate) AsLink() (ld.Link, error) {
79
	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsLink()
80
}
tavit ohanian's avatar
tavit ohanian committed
81
func (*_Substrate) Prototype() ld.NodePrototype {
82 83 84 85 86
	return _Substrate__Prototype{}
}

// -- NodePrototype -->

tavit ohanian's avatar
tavit ohanian committed
87
var _ ld.NodePrototype = _Substrate__Prototype{}
88 89 90 91 92

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

tavit ohanian's avatar
tavit ohanian committed
93
func (np _Substrate__Prototype) NewBuilder() ld.NodeBuilder {
94 95 96 97 98
	return &_Substrate__Builder{}
}

// -- NodeBuilder -->

tavit ohanian's avatar
tavit ohanian committed
99
var _ ld.NodeBuilder = (*_Substrate__Builder)(nil)
100 101 102 103 104

type _Substrate__Builder struct {
	_Substrate__Assembler
}

tavit ohanian's avatar
tavit ohanian committed
105
func (nb *_Substrate__Builder) Build() ld.Node {
106 107 108 109 110 111 112 113 114 115 116
	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 -->

tavit ohanian's avatar
tavit ohanian committed
117
var _ ld.NodeAssembler = (*_Substrate__Assembler)(nil)
118 119 120 121 122 123

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.
}

tavit ohanian's avatar
tavit ohanian committed
124
func (_Substrate__Assembler) BeginMap(sizeHint int64) (ld.MapAssembler, error) {
125
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.BeginMap(0)
126
}
tavit ohanian's avatar
tavit ohanian committed
127
func (_Substrate__Assembler) BeginList(sizeHint int64) (ld.ListAssembler, error) {
128
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.BeginList(0)
129 130 131
}
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?
132
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignNull()
133 134
}
func (_Substrate__Assembler) AssignBool(bool) error {
135
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignBool(false)
136
}
137
func (_Substrate__Assembler) AssignInt(int64) error {
138
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignInt(0)
139 140
}
func (_Substrate__Assembler) AssignFloat(float64) error {
141
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignFloat(0)
142 143 144 145 146 147 148 149 150 151 152 153 154 155
}
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 {
156
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignBytes(nil)
157
}
tavit ohanian's avatar
tavit ohanian committed
158
func (_Substrate__Assembler) AssignLink(ld.Link) error {
159
	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignLink(nil)
160
}
tavit ohanian's avatar
tavit ohanian committed
161
func (na *_Substrate__Assembler) AssignNode(v ld.Node) error {
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	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)
	}
}
tavit ohanian's avatar
tavit ohanian committed
180
func (_Substrate__Assembler) Prototype() ld.NodePrototype {
181 182
	return _Substrate__Prototype{}
}