Files
fejkdata/hold.go
T

106 lines
3.3 KiB
Go

package fejkdata
import (
"fmt"
"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.
type hold struct {
variant map[string]node
value map[string]readValue
pins pinSet
}
// readValue is what one read drew: its text, and whether it landed on a null.
type readValue struct {
text string
null bool
}
// readField renders one arm of a token. A name the expansion holds — a level some
// 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.
func readField(s *session, t *template, held *hold, sc renderScope, a arm) readValue {
if s.trace != nil {
traceRead(s.trace, t, sc, a)
}
if !t.held[a.key] {
if len(a.tail) > 0 {
panic(fmt.Sprintf("fejkdata: %q reads a path into %q, which the expansion does not hold", a.spelling, a.key))
}
return readValue{text: render(s, t.head(a.key), sc)}
}
d := readHold(held, sc, a)
if r, done := d.value[a.path]; done {
return r
}
if len(a.tail) > 0 {
sc.pins = &d.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{}
}
d.value[a.path] = r
return r
}
// renderTrace is a test's view of the fields a render reads; nil outside a test.
type renderTrace struct {
read func(group, table string, row int, a arm)
repeatDepth int
}
func traceRead(trace *renderTrace, t *template, sc renderScope, a arm) {
var table string
var row int
switch {
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)
}
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 {
n = resolveChoice(s, n)
switch leaf := n.(type) {
case *nullItem:
return readValue{null: true}
case *template:
if leaf.readsColumn != nil {
return readField(s, leaf, nil, sc.in(leaf), leaf.readsColumn.a)
}
}
return readValue{text: render(s, n, sc)}
}
// resolveChoice resolves a choice to one variant, so a held head is a concrete node the
// rest of the expansion shares. Nested choices unwrap too: a draw is one value, not
// another set to pick from.
func resolveChoice(s *session, n node) node {
for c, ok := n.(*choice); ok; c, ok = n.(*choice) {
n = pick(s, c)
}
return n
}