Split the expansion's hold from the group's, and read a table's row from where the table is entered

This commit is contained in:
2026-09-28 23:09:13 +02:00
committed by Lillem4n
parent 88f762077f
commit 194f194f81
7 changed files with 63 additions and 47 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ package fejkdata
// start one each, `FakeStruct` one per record, and `expandAnew` one per
// iteration of a `template.repeat`.
// - hold — keeping one draw of a name, so every route to it reads that value:
// `template.held`, `hold`, `readField`.
// `template.held`, `hold`, `groupHold`, `readField`.
// - draw group — reference draws held apart inside one render:
// `template.drawGroup` as the data spells it, `template.drawGroupKey` as a
// render reads it, `holdSet`, `renderScope`.
+1 -1
View File
@@ -134,7 +134,7 @@ func renderRoot(s *session, t *template) {
var set holdSet
sc := renderScope{set: &set}
sc.hold().pins.pin(t.table, r)
render(s, t.table.pinnedRow, sc)
render(s, t.table.pinnedRow, sc.at(t.table.pinnedRow, &sc.hold().pins))
}
case t.repeat > 1:
expand(s, t, sc)
+27 -25
View File
@@ -5,14 +5,19 @@ import (
"strings"
)
// hold is what has already been drawn for held names: the variant each was drawn as, so every
// path under it reads one row; the value each read produced, by its path, so the same read
// written twice reads one value; and the table rows those reads pinned. An expansion keeps one for
// its sibling names, and a render one per group for its reference paths.
// hold is what one expansion has drawn for the names it holds: the variant each was drawn as, so
// every path under it reads one variant; and the value each read produced, by its path, so the
// same read written twice reads one value.
type hold struct {
variant map[string]node
value map[string]readValue
pins pinSet
}
// groupHold is what one render's draw group has drawn for its reference paths: a hold, and the
// table rows those paths pinned.
type groupHold struct {
hold
pins pinSet
}
// readValue is what one read drew: its text, and whether it landed on a null.
@@ -25,7 +30,9 @@ type readValue struct {
// token addresses by dotted path, or a field an operand reads — is drawn once and
// kept, so {place.postal-code} and {place.locality} read one row, either read twice
// gives one value, and a shown operand is the operand computed. Every other name is
// drawn afresh, so {word} {word} still draws twice.
// drawn afresh, so {word} {word} still draws twice. A reference path is kept in the
// render's hold for its group, so its draw spans the render; every other held name in
// held, the expansion's.
func readField(s *session, t *template, held *hold, sc renderScope, a arm) readValue {
if s.trace != nil {
traceRead(s.trace, t, sc, a)
@@ -36,18 +43,23 @@ func readField(s *session, t *template, held *hold, sc renderScope, a arm) readV
}
return readValue{text: render(s, t.head(a.key), sc)}
}
d := readHold(held, sc, a)
if r, done := d.value[a.path]; done {
w := pathDraw{s: s, held: held, a: &a}
if isRef(a.key) && len(a.tail) > 0 {
group := sc.hold()
w.held, w.pins = &group.hold, &group.pins
}
if r, done := w.held.value[a.path]; done {
return r
}
if len(a.tail) > 0 {
sc.pins = &d.pins
leaf := drawPath(t.head(a.key), a.tail, a.key, &w)
if w.pins != nil {
sc = sc.at(leaf, w.pins)
}
r := renderLeaf(s, drawPath(t.head(a.key), a.tail, a.key, &pathDraw{s: s, pins: &d.pins, held: d, a: &a}), sc)
if d.value == nil {
d.value = map[string]readValue{}
r := renderLeaf(s, leaf, sc)
if w.held.value == nil {
w.held.value = map[string]readValue{}
}
d.value[a.path] = r
w.held.value[a.path] = r
return r
}
@@ -64,21 +76,11 @@ func traceRead(trace *renderTrace, t *template, sc renderScope, a arm) {
case t.cellOf != nil:
table, row = t.cellOf.path, t.cellRow
case t.table != nil:
table, row = t.table.path, sc.rows().mustRow(t.table)
table, row = t.table.path, sc.rowOf(t.table)
}
trace.read(strings.Clone(sc.group), strings.Clone(table), row, a)
}
// readHold is the hold a held read keeps its draw in: for a reference that reads a path,
// the render's hold for its group, so its draw spans the render; for a sibling, or a
// bare reference, held.
func readHold(held *hold, sc renderScope, a arm) *hold {
if isRef(a.key) && len(a.tail) > 0 {
return sc.hold()
}
return held
}
// renderLeaf draws and renders what a read lands on: null on a null item, or on a column of one
// reference alone whose read drew null.
func renderLeaf(s *session, n node, sc renderScope) readValue {
+25 -12
View File
@@ -1,23 +1,23 @@
package fejkdata
import (
"fmt"
"strings"
)
// holdSet is one render's reference draws: the unnamed draw group's, and each named one's.
type holdSet struct {
unnamed hold
named map[string]*hold
unnamed groupHold
named map[string]*groupHold
}
// renderScope is where a render reads its reference paths: a hold set, in the draw group of the
// template rendering, and the pin set a table's columns read their row from: a whole read's own,
// else the hold's. It passes by value, and what is read from it reaches a map key, an interface or
// template rendering; and row, the row of the table rendering, which its columns read. It passes by value, and what is read from it reaches a map key, an interface or
// a func value only as a copy; else sc, and with it every render's hold set, moves to the heap.
type renderScope struct {
set *holdSet
group string
pins *pinSet
row tablePin
}
// renderOnce renders n as one render, over a hold set of its own.
@@ -47,23 +47,36 @@ func (sc renderScope) in(t *template) renderScope {
return sc
}
func (sc renderScope) rows() *pinSet {
if sc.pins != nil {
return sc.pins
// at is the scope n, the leaf of a path that pinned its rows in pins, renders in: where n is a
// table's row or column, at the row pins holds for that table.
func (sc renderScope) at(n node, pins *pinSet) renderScope {
switch n := n.(type) {
case *tableRow:
sc.row = tablePin{n.t, pins.mustRow(n.t)}
case *tableColumn:
sc.row = tablePin{n.t, pins.mustRow(n.t)}
}
return &sc.hold().pins
return sc
}
func (sc renderScope) hold() *hold {
// rowOf is the row of t its columns render from.
func (sc renderScope) rowOf(t *table) int {
if sc.row.t != t {
panic(fmt.Sprintf("fejkdata: a column of %s is rendered with no row pinned", t.category))
}
return sc.row.row
}
func (sc renderScope) hold() *groupHold {
if sc.group == "" {
return &sc.set.unnamed
}
d, drew := sc.set.named[sc.group]
if !drew {
if sc.set.named == nil {
sc.set.named = map[string]*hold{}
sc.set.named = map[string]*groupHold{}
}
d = &hold{}
d = &groupHold{}
sc.set.named[strings.Clone(sc.group)] = d
}
return d
+3 -3
View File
@@ -332,9 +332,9 @@ func (w *pathProbe) readRow(t *table, r tableRoute) error {
return nil
}
// pathDraw draws the rows and variants a proved path reads, pinning them in pins.
// For a held read, held keeps the variant drawn at each level of a, so paths
// sharing a prefix share it.
// pathDraw draws the rows and variants a proved path reads, pinning the rows in pins, which
// only a path from a reference needs: a table is a category. For a held read, held keeps the
// variant drawn at each level of a, so paths sharing a prefix share it.
type pathDraw struct {
s *session
pins *pinSet
+3
View File
@@ -263,6 +263,9 @@ func recordOf(n node) (*template, []Column, error) {
// over sc's hold; a table's columns read the row pinned there.
func renderRecord(s *session, t *template, columns []Column, sc renderScope) *Record {
sc = sc.in(t)
if t.table != nil {
sc = sc.at(t.table.pinnedRow, &sc.hold().pins)
}
r := &Record{columns: append([]Column(nil), columns...)}
for i := range r.columns {
column := renderLeaf(s, t.fields[r.columns[i].Name], sc)
+3 -5
View File
@@ -33,7 +33,7 @@ func (f *Generator) Fake(path string) (string, error) {
if _, ok := n.(*folder); ok {
return "", fmt.Errorf("fejkdata: %s names a folder, not a value", path)
}
return render(f.rand, n, sc), nil
return render(f.rand, n, sc.at(n, &sc.hold().pins)), nil
}
// descend walks a caller's path to the node it names, pinning in sc the rows it
@@ -56,14 +56,12 @@ func render(s *session, n node, sc renderScope) string {
case *nullItem:
return ""
case *table:
var own pinSet
own.add(n, n.drawRow(s))
sc.pins = &own
sc.row = tablePin{n, n.drawRow(s)}
return expand(s, n.formatTemplate, sc)
case *tableRow:
return expand(s, n.t.formatTemplate, sc)
case *tableColumn:
row := sc.rows().mustRow(n.t)
row := sc.rowOf(n.t)
if cell := n.t.cellTemplate(row, n.i); cell != nil {
return render(s, cell, sc)
}