rot13node.go 6.87 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
	rot13adl is a demo ADL -- its purpose is to show what an ADL and its public interface can look like.
	It implements a "rot13" string: when creating data through the ADL, the user gives it a regular string;
	the ADL will create aninternal representation of it which has the characters altered in a reversable way.

	It provides reference and example materal, but it's very unlikely you want to use it in real situations ;)

	There are several ways to move data in and out of the ADL:

10
		- treat it like a regular LD map:
11 12 13 14 15 16 17
			- using the exported NodePrototype can be used to get a NodeBuilder which can accept keys and values;
			- using the resulting Node and doing lookup operations on it like a regular map;
		- load up raw substrate data and `Reify()` it into the synthesized form, and *then* treat it like a regular map:
			- this is handy if the raw data already parsed into Nodes.
			- optionally, use `SubstrateRootPrototype` as the prototype for loading the raw substrate data;
			  any kind of Node is a valid input to Reify, but this one will generally have optimal performance.
		- take the synthesized form and inspect its substrate data:
tavit ohanian's avatar
tavit ohanian committed
18 19
			- the `Substrate()` method will return another ld.Node which is the root of the raw substrate data,
			  and can be walked normally like any other ld.Node.
20 21 22 23
*/
package rot13adl

import (
tavit ohanian's avatar
tavit ohanian committed
24
	"gitlab.dms3.io/ld/go-ld-prime"
tavit ohanian's avatar
tavit ohanian committed
25 26
	"gitlab.dms3.io/ld/go-ld-prime/node/mixins"
	"gitlab.dms3.io/ld/go-ld-prime/schema"
27 28 29 30
)

// -- Node -->

tavit ohanian's avatar
tavit ohanian committed
31
var _ ld.Node = (R13String)(nil)
32 33

type R13String = *_R13String
34 35 36 37 38 39

type _R13String struct {
	raw         string // the raw content, before our ADL lens is applied to it.
	synthesized string // the content that the ADL presents.  calculated proactively from the original, in this implementation (though you could imagine implementing it lazily, in either direction, too).
}

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

// -- NodePrototype -->

tavit ohanian's avatar
tavit ohanian committed
94
var _ ld.NodePrototype = _R13String__Prototype{}
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

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

	// A more complex ADL might have some kind of parameters here.
	//
	// The general contract of a NodePrototype is supposed to be that:
	// when you get one from an existing Node,
	//  it should have enough information to create a new Node that
	//   could "replace" the previous one in whatever context it's in.
	// For ADLs, that means it should carry most of the configuration.
	//
	// An ADL that does multi-block stuff might also need functions like a LinkLoader passed in through here.
}

tavit ohanian's avatar
tavit ohanian committed
110
func (np _R13String__Prototype) NewBuilder() ld.NodeBuilder {
111 112 113 114 115
	return &_R13String__Builder{}
}

// -- NodeBuilder -->

tavit ohanian's avatar
tavit ohanian committed
116
var _ ld.NodeBuilder = (*_R13String__Builder)(nil)
117 118 119 120 121

type _R13String__Builder struct {
	_R13String__Assembler
}

tavit ohanian's avatar
tavit ohanian committed
122
func (nb *_R13String__Builder) Build() ld.Node {
123 124 125 126 127 128 129 130 131 132 133
	if nb.m != schema.Maybe_Value {
		panic("invalid state: cannot call Build on an assembler that's not finished")
	}
	return nb.w
}
func (nb *_R13String__Builder) Reset() {
	*nb = _R13String__Builder{}
}

// -- NodeAssembler -->

tavit ohanian's avatar
tavit ohanian committed
134
var _ ld.NodeAssembler = (*_R13String__Assembler)(nil)
135 136 137 138 139 140

type _R13String__Assembler struct {
	w *_R13String
	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
141
func (_R13String__Assembler) BeginMap(sizeHint int64) (ld.MapAssembler, error) {
142
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.BeginMap(0)
143
}
tavit ohanian's avatar
tavit ohanian committed
144
func (_R13String__Assembler) BeginList(sizeHint int64) (ld.ListAssembler, error) {
145
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.BeginList(0)
146 147 148
}
func (na *_R13String__Assembler) AssignNull() error {
	// REVIEW: unclear how this might compose with some other context (like a schema) which does allow nulls.  Probably a wrapper type?
149
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignNull()
150 151
}
func (_R13String__Assembler) AssignBool(bool) error {
152
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignBool(false)
153
}
154
func (_R13String__Assembler) AssignInt(int64) error {
155
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignInt(0)
156 157
}
func (_R13String__Assembler) AssignFloat(float64) error {
158
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignFloat(0)
159 160 161 162 163 164 165 166 167 168 169 170 171 172
}
func (na *_R13String__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 = &_R13String{
		raw:         rotate(v),
		synthesized: v,
	}
	na.m = schema.Maybe_Value
	return nil
}
func (_R13String__Assembler) AssignBytes([]byte) error {
173
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignBytes(nil)
174
}
tavit ohanian's avatar
tavit ohanian committed
175
func (_R13String__Assembler) AssignLink(ld.Link) error {
176
	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignLink(nil)
177
}
tavit ohanian's avatar
tavit ohanian committed
178
func (na *_R13String__Assembler) AssignNode(v ld.Node) error {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	if v.IsNull() {
		return na.AssignNull()
	}
	if v2, ok := v.(*_R13String); 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
197
func (_R13String__Assembler) Prototype() ld.NodePrototype {
198 199
	return _R13String__Prototype{}
}