forked from ipld/go-ipld-prime
/
genpartsMap.go
322 lines (313 loc) · 13.7 KB
/
genpartsMap.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
package gengo
import (
"io"
)
// FIXME docs: these methods all say "-oid" but I think that was overoptimistic and not actually that applicable, really.
// AssignNode? Okay, that one's fine.
// The rest? They're all *very* emphatic about knowing either:
// - that na.w.t and na.w.m are fields; or,
// - that there's only one 'ka' and 'va' (one type each; and that it's reused).
// The reuse level for those two traits is pretty minimal.
func emitNodeAssemblerMethodBeginMap_mapoid(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
// This method contains a branch to support MaybeUsesPtr because new memory may need to be allocated.
// This allocation only happens if the 'w' ptr is nil, which means we're being used on a Maybe;
// otherwise, the 'w' ptr should already be set, and we fill that memory location without allocating, as usual.
doTemplate(`
func (na *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) BeginMap(sizeHint int64) (ipld.MapAssembler, error) {
switch *na.m {
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
case midvalue:
panic("invalid state: it makes no sense to 'begin' twice on the same assembler!")
}
*na.m = midvalue
if sizeHint < 0 {
sizeHint = 0
}
{{- if .Type | MaybeUsesPtr }}
if na.w == nil {
na.w = &_{{ .Type | TypeSymbol }}{}
}
{{- end}}
na.w.m = make(map[_{{ .Type.KeyType | TypeSymbol }}]{{if .Type.ValueIsNullable }}Maybe{{else}}*_{{end}}{{ .Type.ValueType | TypeSymbol }}, sizeHint)
na.w.t = make([]_{{ .Type | TypeSymbol }}__entry, 0, sizeHint)
return na, nil
}
`, w, adjCfg, data)
}
func emitNodeAssemblerMethodAssignNode_mapoid(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
// AssignNode goes through three phases:
// 1. is it null? Jump over to AssignNull (which may or may not reject it).
// 2. is it our own type? Handle specially -- we might be able to do efficient things.
// 3. is it the right kind to morph into us? Do so.
//
// We do not set m=midvalue in phase 3 -- it shouldn't matter unless you're trying to pull off concurrent access, which is wrong and unsafe regardless.
//
// This works easily for both type-level and representational nodes because
// any divergences that have to do with the child value are nicely hidden behind `AssembleKey` and `AssembleValue`.
doTemplate(`
func (na *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssignNode(v ipld.Node) error {
if v.IsNull() {
return na.AssignNull()
}
if v2, ok := v.(*_{{ .Type | TypeSymbol }}); ok {
switch *na.m {
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
case midvalue:
panic("invalid state: cannot assign null into an assembler that's already begun working on recursive structures!")
}
{{- if .Type | MaybeUsesPtr }}
if na.w == nil {
na.w = v2
*na.m = schema.Maybe_Value
return nil
}
{{- end}}
*na.w = *v2
*na.m = schema.Maybe_Value
return nil
}
if v.Kind() != ipld.Kind_Map {
return ipld.ErrWrongKind{TypeName: "{{ .PkgName }}.{{ .Type.Name }}{{ if .IsRepr }}.Repr{{end}}", MethodName: "AssignNode", AppropriateKind: ipld.KindSet_JustMap, ActualKind: v.Kind()}
}
itr := v.MapIterator()
for !itr.Done() {
k, v, err := itr.Next()
if err != nil {
return err
}
if err := na.AssembleKey().AssignNode(k); err != nil {
return err
}
if err := na.AssembleValue().AssignNode(v); err != nil {
return err
}
}
return na.Finish()
}
`, w, adjCfg, data)
}
func emitNodeAssemblerHelper_mapoid_keyTidyHelper(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
// This function attempts to clean up the state machine to acknolwedge key assembly finish.
// If the child was finished and we just collected it, return true and update state to maState_expectValue.
// Collecting the child includes updating the 'ma.w.m' to point into the relevant row of 'ma.w.t', since that couldn't be done earlier,
// AND initializing the 'ma.va' (since we're already holding relevant offsets into 'ma.w.t').
// Otherwise, if it wasn't done, return false;
// and the caller is almost certain to emit an error momentarily.
// The function will only be called when the current state is maState_midKey.
// (In general, the idea is that if the user is doing things correctly,
// this function will only be called when the child is in fact finished.)
// Completion info always comes via 'cm', and we reset it to its initial condition of Maybe_Absent here.
// At the same time, we nil the 'w' pointer for the child assembler; otherwise its own state machine would probably let it modify 'w' again!
//
// DRY(nope): Can this be extracted to be a shared function between repr and type level nodes?
// It is textually identical, so... yeah, that'd be nice. But...
// Nope. It touches `ma.ka` and `ma.va` directly.
// Attempting to extract or hide those behind an interface would create virtual function calls in a very tight spot, and we don't want the execution time cost.
doTemplate(`
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) keyFinishTidy() bool {
switch ma.cm {
case schema.Maybe_Value:
ma.ka.w = nil
tz := &ma.w.t[len(ma.w.t)-1]
ma.cm = schema.Maybe_Absent
ma.state = maState_expectValue
ma.w.m[tz.k] = &tz.v
{{- if .Type.ValueIsNullable }}
{{- if not (MaybeUsesPtr .Type.ValueType) }}
ma.va.w = &tz.v.v
{{- end}}
ma.va.m = &tz.v.m
tz.v.m = allowNull
{{- else}}
ma.va.w = &tz.v
ma.va.m = &ma.cm
{{- end}}
ma.ka.reset()
return true
default:
return false
}
}
`, w, adjCfg, data)
}
func emitNodeAssemblerHelper_mapoid_valueTidyHelper(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
// This function attempts to clean up the state machine to acknolwedge child value assembly finish.
// If the child was finished and we just collected it, return true and update state to maState_initial.
// Otherwise, if it wasn't done, return false;
// and the caller is almost certain to emit an error momentarily.
// The function will only be called when the current state is maState_midValue.
// (In general, the idea is that if the user is doing things correctly,
// this function will only be called when the child is in fact finished.)
// If 'cm' is used, we reset it to its initial condition of Maybe_Absent here.
// At the same time, we nil the 'w' pointer for the child assembler; otherwise its own state machine would probably let it modify 'w' again!
//
// DRY(nope): Can this be extracted to be a shared function between repr and type level nodes?
// Exact same story as the key tidy helper -- touches child assemblers concretely, and that blocks extraction.
doTemplate(`
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) valueFinishTidy() bool {
{{- if .Type.ValueIsNullable }}
tz := &ma.w.t[len(ma.w.t)-1]
switch tz.v.m {
case schema.Maybe_Null:
ma.state = maState_initial
ma.va.reset()
return true
case schema.Maybe_Value:
{{- if (MaybeUsesPtr .Type.ValueType) }}
tz.v.v = ma.va.w
{{- end}}
ma.va.w = nil
ma.state = maState_initial
ma.va.reset()
return true
{{- else}}
switch ma.cm {
case schema.Maybe_Value:
ma.va.w = nil
ma.cm = schema.Maybe_Absent
ma.state = maState_initial
ma.va.reset()
return true
{{- end}}
default:
return false
}
}
`, w, adjCfg, data)
}
func emitNodeAssemblerHelper_mapoid_mapAssemblerMethods(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
// FUTURE: some of the setup of the child assemblers could probably be DRY'd up.
//
// REVIEW: there's a copy-by-value of k2 that's avoidable. But it simplifies the error path. Worth working on?
//
// REVIEW: processing the key via the reprPrototype of the key even when we're at the type level if it's type kind isn't string is currently supported, but should it be? or is that more confusing than valuable?
// Very possible that it shouldn't be supported: the full-on keyAssembler route won't accept this, so consistency with that might be best.
// On the other hand, lookups by string *do* support this kind of processing (and it must, or PathSegment utility becomes unacceptably damaged), so either way, something feels surprising.
//
// DRY(nope): Can this be extracted to a shared function in the output?
// Same story as the tidy helpers -- it touches `va` and `ka` concretely in several places, and that blocks extraction.
//
// DRY: a lot of the state transition fences again are common for all mapoids, and could probably even be a function over '*state'...
// except for the fact they need to call the valueFinishTidy function, which is another one of those points that blocks extraction because we strongly don't want virtual functions calls there.
// Maybe the templates can be textually dedup'd more, though, at least.
doTemplate(`
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssembleEntry(k string) (ipld.NodeAssembler, error) {
switch ma.state {
case maState_initial:
// carry on
case maState_midKey:
panic("invalid state: AssembleEntry cannot be called when in the middle of assembling another key")
case maState_expectValue:
panic("invalid state: AssembleEntry cannot be called when expecting start of value assembly")
case maState_midValue:
if !ma.valueFinishTidy() {
panic("invalid state: AssembleEntry cannot be called when in the middle of assembling a value")
} // if tidy success: carry on
case maState_finished:
panic("invalid state: AssembleEntry cannot be called on an assembler that's already finished")
}
var k2 _{{ .Type.KeyType | TypeSymbol }}
{{- if or (not (eq .Type.KeyType.TypeKind.String "String")) .IsRepr }}
if err := (_{{ .Type.KeyType | TypeSymbol }}__ReprPrototype{}).fromString(&k2, k); err != nil {
return nil, err // TODO wrap in some kind of ErrInvalidKey
}
{{- else}}
if err := (_{{ .Type.KeyType | TypeSymbol }}__Prototype{}).fromString(&k2, k); err != nil {
return nil, err // TODO wrap in some kind of ErrInvalidKey
}
{{- end}}
if _, exists := ma.w.m[k2]; exists {
return nil, ipld.ErrRepeatedMapKey{Key: &k2}
}
ma.w.t = append(ma.w.t, _{{ .Type | TypeSymbol }}__entry{k: k2})
tz := &ma.w.t[len(ma.w.t)-1]
ma.state = maState_midValue
ma.w.m[k2] = &tz.v
{{- if .Type.ValueIsNullable }}
{{- if not (MaybeUsesPtr .Type.ValueType) }}
ma.va.w = &tz.v.v
{{- end}}
ma.va.m = &tz.v.m
tz.v.m = allowNull
{{- else}}
ma.va.w = &tz.v
ma.va.m = &ma.cm
{{- end}}
return &ma.va, nil
}
`, w, adjCfg, data)
doTemplate(`
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssembleKey() ipld.NodeAssembler {
switch ma.state {
case maState_initial:
// carry on
case maState_midKey:
panic("invalid state: AssembleKey cannot be called when in the middle of assembling another key")
case maState_expectValue:
panic("invalid state: AssembleKey cannot be called when expecting start of value assembly")
case maState_midValue:
if !ma.valueFinishTidy() {
panic("invalid state: AssembleKey cannot be called when in the middle of assembling a value")
} // if tidy success: carry on
case maState_finished:
panic("invalid state: AssembleKey cannot be called on an assembler that's already finished")
}
ma.w.t = append(ma.w.t, _{{ .Type | TypeSymbol }}__entry{})
ma.state = maState_midKey
ma.ka.m = &ma.cm
ma.ka.w = &ma.w.t[len(ma.w.t)-1].k
return &ma.ka
}
`, w, adjCfg, data)
doTemplate(`
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssembleValue() ipld.NodeAssembler {
switch ma.state {
case maState_initial:
panic("invalid state: AssembleValue cannot be called when no key is primed")
case maState_midKey:
if !ma.keyFinishTidy() {
panic("invalid state: AssembleValue cannot be called when in the middle of assembling a key")
} // if tidy success: carry on
case maState_expectValue:
// carry on
case maState_midValue:
panic("invalid state: AssembleValue cannot be called when in the middle of assembling another value")
case maState_finished:
panic("invalid state: AssembleValue cannot be called on an assembler that's already finished")
}
ma.state = maState_midValue
return &ma.va
}
`, w, adjCfg, data)
doTemplate(`
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) Finish() error {
switch ma.state {
case maState_initial:
// carry on
case maState_midKey:
panic("invalid state: Finish cannot be called when in the middle of assembling a key")
case maState_expectValue:
panic("invalid state: Finish cannot be called when expecting start of value assembly")
case maState_midValue:
if !ma.valueFinishTidy() {
panic("invalid state: Finish cannot be called when in the middle of assembling a value")
} // if tidy success: carry on
case maState_finished:
panic("invalid state: Finish cannot be called on an assembler that's already finished")
}
ma.state = maState_finished
*ma.m = schema.Maybe_Value
return nil
}
`, w, adjCfg, data)
doTemplate(`
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) KeyPrototype() ipld.NodePrototype {
return _{{ .Type.KeyType | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Prototype{}
}
func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) ValuePrototype(_ string) ipld.NodePrototype {
return _{{ .Type.ValueType | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Prototype{}
}
`, w, adjCfg, data)
}