/
genMap.go
349 lines (311 loc) · 12.7 KB
/
genMap.go
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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package gengo
import (
"io"
"github.com/ipld/go-ipld-prime/schema"
"github.com/ipld/go-ipld-prime/schema/gen/go/mixins"
)
type mapGenerator struct {
AdjCfg *AdjunctCfg
mixins.MapTraits
PkgName string
Type *schema.TypeMap
}
func (mapGenerator) IsRepr() bool { return false } // hint used in some generalized templates.
// --- native content and specializations --->
func (g mapGenerator) EmitNativeType(w io.Writer) {
// Maps do double bookkeeping.
// - 'm' is used for quick lookup.
// - 't' is used for both for order maintainence, and for allocation amortization for both keys and values.
// Note that the key in 'm' is *not* a pointer.
// The value in 'm' is a pointer into 't' (except when it's a maybe; maybes are already pointers).
doTemplate(`
{{- if Comments -}}
// {{ .Type | TypeSymbol }} matches the IPLD Schema type "{{ .Type.Name }}". It has {{ .Kind }} kind.
{{- end}}
type {{ .Type | TypeSymbol }} = *_{{ .Type | TypeSymbol }}
type _{{ .Type | TypeSymbol }} struct {
m map[_{{ .Type.KeyType | TypeSymbol }}]{{if .Type.ValueIsNullable }}Maybe{{else}}*_{{end}}{{ .Type.ValueType | TypeSymbol }}
t []_{{ .Type | TypeSymbol }}__entry
}
`, w, g.AdjCfg, g)
// - address of 'k' is used when we return keys as nodes, such as in iterators.
// Having these in the 't' slice above amortizes moving all of them to heap at once,
// which makes iterators that have to return them as an interface much (much) lower cost -- no 'runtime.conv*' pain.
// - address of 'v' is used in map values, to return, and of course also in iterators.
doTemplate(`
type _{{ .Type | TypeSymbol }}__entry struct {
k _{{ .Type.KeyType | TypeSymbol }}
v _{{ .Type.ValueType | TypeSymbol }}{{if .Type.ValueIsNullable }}__Maybe{{end}}
}
`, w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNativeAccessors(w io.Writer) {
// Generate a speciated Lookup as well as LookupMaybe method.
// The Lookup method returns nil in case of *either* an absent value or a null value,
// and so should only be used if the map type doesn't allow nullable keys or if the caller doesn't care about the difference.
// The LookupMaybe method returns a MaybeT type for the map value,
// and is needed if the map allows nullable values and the caller wishes to distinguish between null and absent.
// (The Lookup method should be preferred for maps that have non-nullable keys, because LookupMaybe may incur additional costs;
// boxing something into a maybe when it wasn't already stored that way costs an alloc(!),
// and may additionally incur a memcpy if the maybe for the value type doesn't use pointers internally).
doTemplate(`
func (n *_{{ .Type | TypeSymbol }}) Lookup(k {{ .Type.KeyType | TypeSymbol }}) {{ .Type.ValueType | TypeSymbol }} {
v, exists := n.m[*k]
if !exists {
return nil
}
{{- if .Type.ValueIsNullable }}
if v.m == schema.Maybe_Null {
return nil
}
return {{ if not (MaybeUsesPtr .Type.ValueType) }}&{{end}}v.v
{{- else}}
return v
{{- end}}
}
func (n *_{{ .Type | TypeSymbol }}) LookupMaybe(k {{ .Type.KeyType | TypeSymbol }}) Maybe{{ .Type.ValueType | TypeSymbol }} {
v, exists := n.m[*k]
if !exists {
return &_{{ .Type | TypeSymbol }}__valueAbsent
}
{{- if .Type.ValueIsNullable }}
return v
{{- else}}
return &_{{ .Type.ValueType | TypeSymbol }}__Maybe{
m: schema.Maybe_Value,
v: {{ if not (MaybeUsesPtr .Type.ValueType) }}*{{end}}v,
}
{{- end}}
}
var _{{ .Type | TypeSymbol }}__valueAbsent = _{{ .Type.ValueType | TypeSymbol }}__Maybe{m:schema.Maybe_Absent}
`, w, g.AdjCfg, g)
// Generate a speciated iterator.
// The main advantage of this over the general datamodel.MapIterator is of course keeping types visible (and concrete, to the compiler's eyes in optimizations, too).
// It also elides the error return from the iterator's Next method. (Overreads will result in nil keys; this is both easily avoidable, and unambiguous if you do goof and hit it.)
doTemplate(`
func (n {{ .Type | TypeSymbol }}) Iterator() *{{ .Type | TypeSymbol }}__Itr {
return &{{ .Type | TypeSymbol }}__Itr{n, 0}
}
type {{ .Type | TypeSymbol }}__Itr struct {
n {{ .Type | TypeSymbol }}
idx int
}
func (itr *{{ .Type | TypeSymbol }}__Itr) Next() (k {{ .Type.KeyType | TypeSymbol }}, v {{if .Type.ValueIsNullable }}Maybe{{end}}{{ .Type.ValueType | TypeSymbol }}) {
if itr.idx >= len(itr.n.t) {
return nil, nil
}
x := &itr.n.t[itr.idx]
k = &x.k
v = &x.v
itr.idx++
return
}
func (itr *{{ .Type | TypeSymbol }}__Itr) Done() bool {
return itr.idx >= len(itr.n.t)
}
`, w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNativeBuilder(w io.Writer) {
// Not yet clear what exactly might be most worth emitting here.
}
func (g mapGenerator) EmitNativeMaybe(w io.Writer) {
emitNativeMaybe(w, g.AdjCfg, g)
}
// --- type info --->
func (g mapGenerator) EmitTypeConst(w io.Writer) {
doTemplate(`
// TODO EmitTypeConst
`, w, g.AdjCfg, g)
}
// --- TypedNode interface satisfaction --->
func (g mapGenerator) EmitTypedNodeMethodType(w io.Writer) {
doTemplate(`
func ({{ .Type | TypeSymbol }}) Type() schema.Type {
return nil /*TODO:typelit*/
}
`, w, g.AdjCfg, g)
}
func (g mapGenerator) EmitTypedNodeMethodRepresentation(w io.Writer) {
emitTypicalTypedNodeMethodRepresentation(w, g.AdjCfg, g)
}
// --- Node interface satisfaction --->
func (g mapGenerator) EmitNodeType(w io.Writer) {
// No additional types needed. Methods all attach to the native type.
}
func (g mapGenerator) EmitNodeTypeAssertions(w io.Writer) {
emitNodeTypeAssertions_typical(w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNodeMethodLookupByString(w io.Writer) {
// What should be coercible in which directions (and how surprising that is) is an interesting question.
// Most of the answer comes from considering what needs to be possible when working with PathSegment:
// we *must* be able to accept a string in a PathSegment and be able to use it to navigate a map -- even if the map has complex keys.
// For that to work out, it means if the key type doesn't have a string type kind, we must be willing to reach into its representation and use the fromString there.
// If the key type *does* have a string kind at the type level, we'll use that; no need to consider going through the representation.
doTemplate(`
func (n {{ .Type | TypeSymbol }}) LookupByString(k string) (datamodel.Node, error) {
var k2 _{{ .Type.KeyType | TypeSymbol }}
{{- if eq .Type.KeyType.TypeKind.String "String" }}
if err := (_{{ .Type.KeyType | TypeSymbol }}__Prototype{}).fromString(&k2, k); err != nil {
return nil, err // TODO wrap in some kind of ErrInvalidKey
}
{{- else}}
if err := (_{{ .Type.KeyType | TypeSymbol }}__ReprPrototype{}).fromString(&k2, k); err != nil {
return nil, err // TODO wrap in some kind of ErrInvalidKey
}
{{- end}}
v, exists := n.m[k2]
if !exists {
return nil, datamodel.ErrNotExists{Segment: datamodel.PathSegmentOfString(k)}
}
{{- if .Type.ValueIsNullable }}
if v.m == schema.Maybe_Null {
return datamodel.Null, nil
}
return {{ if not (MaybeUsesPtr .Type.ValueType) }}&{{end}}v.v, nil
{{- else}}
return v, nil
{{- end}}
}
`, w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNodeMethodLookupByNode(w io.Writer) {
// LookupByNode will procede by cast if it can; or simply error if that doesn't work.
// There's no attempt to turn the node (or its repr) into a string and then reify that into a key;
// if you used a Node here, you should've meant it.
// REVIEW: by comparison structs will coerce anything stringish silently...! so we should figure out if that inconsistency is acceptable, and at least document it if so.
doTemplate(`
func (n {{ .Type | TypeSymbol }}) LookupByNode(k datamodel.Node) (datamodel.Node, error) {
k2, ok := k.({{ .Type.KeyType | TypeSymbol }})
if !ok {
panic("todo invalid key type error")
// 'schema.ErrInvalidKey{TypeName:"{{ .PkgName }}.{{ .Type.Name }}", Key:&_String{k}}' doesn't quite cut it: need room to explain the type, and it's not guaranteed k can be turned into a string at all
}
v, exists := n.m[*k2]
if !exists {
return nil, datamodel.ErrNotExists{Segment: datamodel.PathSegmentOfString(k2.String())}
}
{{- if .Type.ValueIsNullable }}
if v.m == schema.Maybe_Null {
return datamodel.Null, nil
}
return {{ if not (MaybeUsesPtr .Type.ValueType) }}&{{end}}v.v, nil
{{- else}}
return v, nil
{{- end}}
}
`, w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNodeMethodMapIterator(w io.Writer) {
doTemplate(`
func (n {{ .Type | TypeSymbol }}) MapIterator() datamodel.MapIterator {
return &_{{ .Type | TypeSymbol }}__MapItr{n, 0}
}
type _{{ .Type | TypeSymbol }}__MapItr struct {
n {{ .Type | TypeSymbol }}
idx int
}
func (itr *_{{ .Type | TypeSymbol }}__MapItr) Next() (k datamodel.Node, v datamodel.Node, _ error) {
if itr.idx >= len(itr.n.t) {
return nil, nil, datamodel.ErrIteratorOverread{}
}
x := &itr.n.t[itr.idx]
k = &x.k
{{- if .Type.ValueIsNullable }}
switch x.v.m {
case schema.Maybe_Null:
v = datamodel.Null
case schema.Maybe_Value:
v = {{ if not (MaybeUsesPtr .Type.ValueType) }}&{{end}}x.v.v
}
{{- else}}
v = &x.v
{{- end}}
itr.idx++
return
}
func (itr *_{{ .Type | TypeSymbol }}__MapItr) Done() bool {
return itr.idx >= len(itr.n.t)
}
`, w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNodeMethodLength(w io.Writer) {
doTemplate(`
func (n {{ .Type | TypeSymbol }}) Length() int64 {
return int64(len(n.t))
}
`, w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNodeMethodPrototype(w io.Writer) {
emitNodeMethodPrototype_typical(w, g.AdjCfg, g)
}
func (g mapGenerator) EmitNodePrototypeType(w io.Writer) {
emitNodePrototypeType_typical(w, g.AdjCfg, g)
}
// --- NodeBuilder and NodeAssembler --->
func (g mapGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return mapBuilderGenerator{
g.AdjCfg,
mixins.MapAssemblerTraits{
PkgName: g.PkgName,
TypeName: g.TypeName,
AppliedPrefix: "_" + g.AdjCfg.TypeSymbol(g.Type) + "__",
},
g.PkgName,
g.Type,
}
}
type mapBuilderGenerator struct {
AdjCfg *AdjunctCfg
mixins.MapAssemblerTraits
PkgName string
Type *schema.TypeMap
}
func (mapBuilderGenerator) IsRepr() bool { return false } // hint used in some generalized templates.
func (g mapBuilderGenerator) EmitNodeBuilderType(w io.Writer) {
emitEmitNodeBuilderType_typical(w, g.AdjCfg, g)
}
func (g mapBuilderGenerator) EmitNodeBuilderMethods(w io.Writer) {
emitNodeBuilderMethods_typical(w, g.AdjCfg, g)
}
func (g mapBuilderGenerator) EmitNodeAssemblerType(w io.Writer) {
// - 'w' is the "**w**ip" pointer.
// - 'm' is the **m**aybe which communicates our completeness to the parent if we're a child assembler.
// - 'state' is what it says on the tin. this is used for the map state (the broad transitions between null, start-map, and finish are handled by 'm' for consistency.)
// - there's no equivalent of the 'f' (**f**ocused next) field in struct assemblers -- that's implicitly the last row of the 'w.t'.
//
// - 'cm' is **c**hild **m**aybe and is used for the completion message from children.
// It's used for values if values aren't allowed to be nullable and thus don't have their own per-value maybe slot we can use.
// It's always used for key assembly, since keys are never allowed to be nullable and thus etc.
// - 'ka' and 'va' are the key assembler and value assembler respectively.
// Perhaps surprisingly, we can get away with using the assemblers for each type just straight up, no wrappers necessary;
// All of the required magic is handled through maybe pointers and some tidy methods used during state transitions.
doTemplate(`
type _{{ .Type | TypeSymbol }}__Assembler struct {
w *_{{ .Type | TypeSymbol }}
m *schema.Maybe
state maState
cm schema.Maybe
ka _{{ .Type.KeyType | TypeSymbol }}__Assembler
va _{{ .Type.ValueType | TypeSymbol }}__Assembler
}
func (na *_{{ .Type | TypeSymbol }}__Assembler) reset() {
na.state = maState_initial
na.ka.reset()
na.va.reset()
}
`, w, g.AdjCfg, g)
}
func (g mapBuilderGenerator) EmitNodeAssemblerMethodBeginMap(w io.Writer) {
emitNodeAssemblerMethodBeginMap_mapoid(w, g.AdjCfg, g)
}
func (g mapBuilderGenerator) EmitNodeAssemblerMethodAssignNull(w io.Writer) {
emitNodeAssemblerMethodAssignNull_recursive(w, g.AdjCfg, g)
}
func (g mapBuilderGenerator) EmitNodeAssemblerMethodAssignNode(w io.Writer) {
emitNodeAssemblerMethodAssignNode_mapoid(w, g.AdjCfg, g)
}
func (g mapBuilderGenerator) EmitNodeAssemblerOtherBits(w io.Writer) {
emitNodeAssemblerHelper_mapoid_keyTidyHelper(w, g.AdjCfg, g)
emitNodeAssemblerHelper_mapoid_valueTidyHelper(w, g.AdjCfg, g)
emitNodeAssemblerHelper_mapoid_mapAssemblerMethods(w, g.AdjCfg, g)
}