Make great documentation goal 1.2, and plan the README's reorganisation and the nested-template gaps #33

Open
lilleman wants to merge 966 commits from worktree-todo-relative-dates-exact-counts into main
7 changed files with 63 additions and 47 deletions
Showing only changes of commit 194f194f81 - Show all commits
+1 -1
View File
@@ -24,7 +24,7 @@ package fejkdata
// start one each, `FakeStruct` one per record, and `expandAnew` one per // start one each, `FakeStruct` one per record, and `expandAnew` one per
// iteration of a `template.repeat`. // iteration of a `template.repeat`.
// - hold — keeping one draw of a name, so every route to it reads that value: // - 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: // - draw group — reference draws held apart inside one render:
// `template.drawGroup` as the data spells it, `template.drawGroupKey` as a // `template.drawGroup` as the data spells it, `template.drawGroupKey` as a
// render reads it, `holdSet`, `renderScope`. // render reads it, `holdSet`, `renderScope`.
+1 -1
View File
@@ -134,7 +134,7 @@ func renderRoot(s *session, t *template) {
var set holdSet var set holdSet
sc := renderScope{set: &set} sc := renderScope{set: &set}
sc.hold().pins.pin(t.table, r) 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: case t.repeat > 1:
expand(s, t, sc) expand(s, t, sc)
+27 -25
View File
@@ -5,14 +5,19 @@ import (
"strings" "strings"
) )
// hold is what has already been drawn for held names: the variant each was drawn as, so every // hold is what one expansion has drawn for the names it holds: the variant each was drawn as, so
// path under it reads one row; the value each read produced, by its path, so the same read // every path under it reads one variant; and the value each read produced, by its path, so the
// written twice reads one value; and the table rows those reads pinned. An expansion keeps one for // same read written twice reads one value.
// its sibling names, and a render one per group for its reference paths.
type hold struct { type hold struct {
variant map[string]node variant map[string]node
value map[string]readValue 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. // 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 // 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 // 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 // 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 { func readField(s *session, t *template, held *hold, sc renderScope, a arm) readValue {
if s.trace != nil { if s.trace != nil {
traceRead(s.trace, t, sc, a) 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)} return readValue{text: render(s, t.head(a.key), sc)}
} }
d := readHold(held, sc, a) w := pathDraw{s: s, held: held, a: &a}
if r, done := d.value[a.path]; done { 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 return r
} }
if len(a.tail) > 0 { leaf := drawPath(t.head(a.key), a.tail, a.key, &w)
sc.pins = &d.pins 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) r := renderLeaf(s, leaf, sc)
if d.value == nil { if w.held.value == nil {
d.value = map[string]readValue{} w.held.value = map[string]readValue{}
} }
d.value[a.path] = r w.held.value[a.path] = r
return r return r
} }
@@ -64,21 +76,11 @@ func traceRead(trace *renderTrace, t *template, sc renderScope, a arm) {
case t.cellOf != nil: case t.cellOf != nil:
table, row = t.cellOf.path, t.cellRow table, row = t.cellOf.path, t.cellRow
case t.table != nil: 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) 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 // 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. // reference alone whose read drew null.
func renderLeaf(s *session, n node, sc renderScope) readValue { func renderLeaf(s *session, n node, sc renderScope) readValue {
+25 -12
View File
@@ -1,23 +1,23 @@
package fejkdata package fejkdata
import ( import (
"fmt"
"strings" "strings"
) )
// holdSet is one render's reference draws: the unnamed draw group's, and each named one's. // holdSet is one render's reference draws: the unnamed draw group's, and each named one's.
type holdSet struct { type holdSet struct {
unnamed hold unnamed groupHold
named map[string]*hold named map[string]*groupHold
} }
// renderScope is where a render reads its reference paths: a hold set, in the draw group of the // 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, // 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
// else the hold's. 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. // a func value only as a copy; else sc, and with it every render's hold set, moves to the heap.
type renderScope struct { type renderScope struct {
set *holdSet set *holdSet
group string group string
pins *pinSet row tablePin
} }
// renderOnce renders n as one render, over a hold set of its own. // 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 return sc
} }
func (sc renderScope) rows() *pinSet { // at is the scope n, the leaf of a path that pinned its rows in pins, renders in: where n is a
if sc.pins != nil { // table's row or column, at the row pins holds for that table.
return sc.pins 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 == "" { if sc.group == "" {
return &sc.set.unnamed return &sc.set.unnamed
} }
d, drew := sc.set.named[sc.group] d, drew := sc.set.named[sc.group]
if !drew { if !drew {
if sc.set.named == nil { 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 sc.set.named[strings.Clone(sc.group)] = d
} }
return d return d
+3 -3
View File
@@ -332,9 +332,9 @@ func (w *pathProbe) readRow(t *table, r tableRoute) error {
return nil return nil
} }
// pathDraw draws the rows and variants a proved path reads, pinning them in pins. // pathDraw draws the rows and variants a proved path reads, pinning the rows in pins, which
// For a held read, held keeps the variant drawn at each level of a, so paths // only a path from a reference needs: a table is a category. For a held read, held keeps the
// sharing a prefix share it. // variant drawn at each level of a, so paths sharing a prefix share it.
type pathDraw struct { type pathDraw struct {
s *session s *session
pins *pinSet 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. // over sc's hold; a table's columns read the row pinned there.
func renderRecord(s *session, t *template, columns []Column, sc renderScope) *Record { func renderRecord(s *session, t *template, columns []Column, sc renderScope) *Record {
sc = sc.in(t) sc = sc.in(t)
if t.table != nil {
sc = sc.at(t.table.pinnedRow, &sc.hold().pins)
}
r := &Record{columns: append([]Column(nil), columns...)} r := &Record{columns: append([]Column(nil), columns...)}
for i := range r.columns { for i := range r.columns {
column := renderLeaf(s, t.fields[r.columns[i].Name], sc) 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 { if _, ok := n.(*folder); ok {
return "", fmt.Errorf("fejkdata: %s names a folder, not a value", path) 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 // 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: case *nullItem:
return "" return ""
case *table: case *table:
var own pinSet sc.row = tablePin{n, n.drawRow(s)}
own.add(n, n.drawRow(s))
sc.pins = &own
return expand(s, n.formatTemplate, sc) return expand(s, n.formatTemplate, sc)
case *tableRow: case *tableRow:
return expand(s, n.t.formatTemplate, sc) return expand(s, n.t.formatTemplate, sc)
case *tableColumn: case *tableColumn:
row := sc.rows().mustRow(n.t) row := sc.rowOf(n.t)
if cell := n.t.cellTemplate(row, n.i); cell != nil { if cell := n.t.cellTemplate(row, n.i); cell != nil {
return render(s, cell, sc) return render(s, cell, sc)
} }