Move the render's read path into render.go and merge holdset.go into hold.go
This commit is contained in:
@@ -878,14 +878,13 @@ fejkdata.go Generator, New, options, the embedded data set, List
|
||||
node.go the node model and JSON -> node compilation
|
||||
table.go tables: the rows TSV, its options and links, row selection and draws
|
||||
path.go the dotted-path walks with their selectors, and proving a path resolves
|
||||
render.go Fake and the recursive renderer (choices, format strings, expansions)
|
||||
render.go Fake, the recursive renderer (choices, format strings, expansions) and the reads its tokens make
|
||||
record.go records: Record, the JSON/CSV/SQL serializers, and their entry points
|
||||
struct.go structs: FakeStruct, fake tags, and a field's Go type as its column's datatype
|
||||
inline.go inline templates: Template, NewTemplate, FakeTemplate, IsTemplate, and their compile and link
|
||||
format.go the {token} grammar: scanning, tokens, operands, validation, compiling a format
|
||||
hold.go the hold: one draw per held name, and reading a field through it
|
||||
hold.go the hold, one draw per held name, and the hold set and scope a render reads its reference paths through
|
||||
holdfence.go the load fences over the hold: the routes and spellings that read one held name two ways
|
||||
holdset.go one reference draw per render and group: the hold set a render reads its paths through
|
||||
drawfence.go the load fences over reference draws: a drawGroup's, a table's own family, and a render's and a record's columns through the survey
|
||||
readsurvey.go the load-time survey of what a render reads, and what a path reads of a table family, and its verdict on them
|
||||
pins.go the pin set: the table rows a render fixes, which the draw fences replay
|
||||
|
||||
@@ -23,92 +23,66 @@ type readValue struct {
|
||||
null bool
|
||||
}
|
||||
|
||||
// readField renders one arm of a token. A name the expansion holds — a level some
|
||||
// token addresses by a dotted path that is not a reference, or a field an operand
|
||||
// reads — is drawn once and kept in held, 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 isRef(a.key) && len(a.tail) > 0 {
|
||||
return readReference(s, t, sc, a)
|
||||
}
|
||||
if sc.set.trace != nil {
|
||||
traceRead(sc.set.trace, t, sc, a)
|
||||
}
|
||||
if !t.compiled.held[a.key] {
|
||||
if len(a.tail) > 0 {
|
||||
panic(internalError("%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)}
|
||||
}
|
||||
if held == nil {
|
||||
panic(internalError("%q reads a name the expansion holds, with no hold to keep it in", a.spelling))
|
||||
}
|
||||
return readHeld(s, t, held, nil, sc, a)
|
||||
}
|
||||
|
||||
// readReference reads a reference path, kept in the render's hold for its group, so its draw
|
||||
// spans the render.
|
||||
func readReference(s *session, t *template, sc renderScope, a arm) readValue {
|
||||
if sc.set.trace != nil {
|
||||
traceRead(sc.set.trace, t, sc, a)
|
||||
}
|
||||
group := sc.groupHold()
|
||||
return readHeld(s, t, &group.hold, &group.pins, sc, a)
|
||||
}
|
||||
|
||||
func readHeld(s *session, t *template, held *hold, pins *pinSet, sc renderScope, a arm) readValue {
|
||||
if r, done := held.value[a.path]; done {
|
||||
return r
|
||||
}
|
||||
leaf := drawPath(t.head(a.key), a.tail, a.key, &pathDraw{s: s, held: held, pins: pins, a: &a})
|
||||
if pins != nil {
|
||||
sc = sc.at(leaf, pins)
|
||||
}
|
||||
r := renderLeaf(s, leaf, sc)
|
||||
if held.value == nil {
|
||||
held.value = map[string]readValue{}
|
||||
}
|
||||
held.value[a.path] = r
|
||||
return r
|
||||
}
|
||||
|
||||
// renderTrace is a test's view of the fields a render reads; nil outside a test.
|
||||
type renderTrace func(group, table string, row int, a arm)
|
||||
|
||||
func traceRead(trace renderTrace, t *template, sc renderScope, a arm) {
|
||||
var table string
|
||||
row := t.site.row
|
||||
if t.site.table != nil {
|
||||
table = t.site.table.path
|
||||
}
|
||||
if row == formatRow {
|
||||
row = sc.rowOf(t.site.table)
|
||||
}
|
||||
trace(strings.Clone(sc.group), strings.Clone(table), row, a)
|
||||
// holdSet is one render's reference draws: the unnamed draw group's, and each named one's; and a
|
||||
// test's trace of its reads.
|
||||
type holdSet struct {
|
||||
unnamed groupHold
|
||||
named map[string]*groupHold
|
||||
trace renderTrace
|
||||
}
|
||||
|
||||
// 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.link.readsColumn != nil {
|
||||
return readReference(s, leaf, sc.in(leaf), leaf.link.readsColumn.a)
|
||||
// renderScope is where a render reads its reference paths: a hold set, in the draw group of the
|
||||
// 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
|
||||
row tablePin
|
||||
}
|
||||
|
||||
// in is the scope t renders in: its draw group where it names one, else its caller's.
|
||||
func (sc renderScope) in(t *template) renderScope {
|
||||
if t.link.drawGroupKey != "" {
|
||||
sc.group = t.link.drawGroupKey
|
||||
}
|
||||
return sc
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// rowOf is the row of t its columns render from.
|
||||
func (sc renderScope) rowOf(t *table) int {
|
||||
if sc.row.t != t {
|
||||
panic(internalError("a column of %s renders in a scope holding no row of it", t.segment))
|
||||
}
|
||||
return sc.row.row
|
||||
}
|
||||
|
||||
func (sc renderScope) groupHold() *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]*groupHold{}
|
||||
}
|
||||
d = &groupHold{}
|
||||
sc.set.named[strings.Clone(sc.group)] = d
|
||||
}
|
||||
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
|
||||
return d
|
||||
}
|
||||
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
package fejkdata
|
||||
|
||||
import "strings"
|
||||
|
||||
// holdSet is one render's reference draws: the unnamed draw group's, and each named one's; and a
|
||||
// test's trace of its reads.
|
||||
type holdSet struct {
|
||||
unnamed groupHold
|
||||
named map[string]*groupHold
|
||||
trace renderTrace
|
||||
}
|
||||
|
||||
// renderScope is where a render reads its reference paths: a hold set, in the draw group of the
|
||||
// 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
|
||||
row tablePin
|
||||
}
|
||||
|
||||
// renderOnce renders n as one render, over a hold set of its own.
|
||||
func renderOnce(s *session, n node) string {
|
||||
var set holdSet
|
||||
return render(s, n, renderScope{set: &set})
|
||||
}
|
||||
|
||||
// expandAnew expands one repeat iteration of t as a render of its own, in no group. Inlined into
|
||||
// render's loop, its hold set would move to the heap.
|
||||
//
|
||||
//go:noinline
|
||||
func expandAnew(s *session, t *template) string {
|
||||
var set holdSet
|
||||
return expand(s, t, renderScope{set: &set})
|
||||
}
|
||||
|
||||
// in is the scope t renders in: its draw group where it names one, else its caller's.
|
||||
func (sc renderScope) in(t *template) renderScope {
|
||||
if t.link.drawGroupKey != "" {
|
||||
sc.group = t.link.drawGroupKey
|
||||
}
|
||||
return sc
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// rowOf is the row of t its columns render from.
|
||||
func (sc renderScope) rowOf(t *table) int {
|
||||
if sc.row.t != t {
|
||||
panic(internalError("a column of %s renders in a scope holding no row of it", t.segment))
|
||||
}
|
||||
return sc.row.row
|
||||
}
|
||||
|
||||
func (sc renderScope) groupHold() *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]*groupHold{}
|
||||
}
|
||||
d = &groupHold{}
|
||||
sc.set.named[strings.Clone(sc.group)] = d
|
||||
}
|
||||
return d
|
||||
}
|
||||
@@ -45,6 +45,12 @@ func descend(s *session, root node, segments []string, sc renderScope) (node, er
|
||||
return drawPath(root, segments, "", &pathDraw{s: s, pins: &sc.groupHold().pins}), nil
|
||||
}
|
||||
|
||||
// renderOnce renders n as one render, over a hold set of its own.
|
||||
func renderOnce(s *session, n node) string {
|
||||
var set holdSet
|
||||
return render(s, n, renderScope{set: &set})
|
||||
}
|
||||
|
||||
// render evaluates a compiled node to a string. compile validates every node up
|
||||
// front, so rendering a compiled tree cannot fail. sc holds the reference draws the
|
||||
// render shares; each repeat iteration renders over a hold set of its own.
|
||||
@@ -88,6 +94,15 @@ func render(s *session, n node, sc renderScope) string {
|
||||
}
|
||||
}
|
||||
|
||||
// expandAnew expands one repeat iteration of t as a render of its own, in no group. Inlined into
|
||||
// render's loop, its hold set would move to the heap.
|
||||
//
|
||||
//go:noinline
|
||||
func expandAnew(s *session, t *template) string {
|
||||
var set holdSet
|
||||
return expand(s, t, renderScope{set: &set})
|
||||
}
|
||||
|
||||
// pick selects one item. Uniform choices are O(1); weighted choices are an
|
||||
// O(log n) search over precomputed cumulative weights. compile guarantees a
|
||||
// non-empty choice and a finite positive total, so the index is always in range.
|
||||
@@ -137,3 +152,90 @@ func expand(s *session, t *template, sc renderScope) string {
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// readField renders one arm of a token. A name the expansion holds — a level some
|
||||
// token addresses by a dotted path that is not a reference, or a field an operand
|
||||
// reads — is drawn once and kept in held, 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 isRef(a.key) && len(a.tail) > 0 {
|
||||
return readReference(s, t, sc, a)
|
||||
}
|
||||
if sc.set.trace != nil {
|
||||
traceRead(sc.set.trace, t, sc, a)
|
||||
}
|
||||
if !t.compiled.held[a.key] {
|
||||
if len(a.tail) > 0 {
|
||||
panic(internalError("%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)}
|
||||
}
|
||||
if held == nil {
|
||||
panic(internalError("%q reads a name the expansion holds, with no hold to keep it in", a.spelling))
|
||||
}
|
||||
return readHeld(s, t, held, nil, sc, a)
|
||||
}
|
||||
|
||||
// readReference reads a reference path, kept in the render's hold for its group, so its draw
|
||||
// spans the render.
|
||||
func readReference(s *session, t *template, sc renderScope, a arm) readValue {
|
||||
if sc.set.trace != nil {
|
||||
traceRead(sc.set.trace, t, sc, a)
|
||||
}
|
||||
group := sc.groupHold()
|
||||
return readHeld(s, t, &group.hold, &group.pins, sc, a)
|
||||
}
|
||||
|
||||
func readHeld(s *session, t *template, held *hold, pins *pinSet, sc renderScope, a arm) readValue {
|
||||
if r, done := held.value[a.path]; done {
|
||||
return r
|
||||
}
|
||||
leaf := drawPath(t.head(a.key), a.tail, a.key, &pathDraw{s: s, held: held, pins: pins, a: &a})
|
||||
if pins != nil {
|
||||
sc = sc.at(leaf, pins)
|
||||
}
|
||||
r := renderLeaf(s, leaf, sc)
|
||||
if held.value == nil {
|
||||
held.value = map[string]readValue{}
|
||||
}
|
||||
held.value[a.path] = r
|
||||
return r
|
||||
}
|
||||
|
||||
func traceRead(trace renderTrace, t *template, sc renderScope, a arm) {
|
||||
var table string
|
||||
row := t.site.row
|
||||
if t.site.table != nil {
|
||||
table = t.site.table.path
|
||||
}
|
||||
if row == formatRow {
|
||||
row = sc.rowOf(t.site.table)
|
||||
}
|
||||
trace(strings.Clone(sc.group), strings.Clone(table), row, a)
|
||||
}
|
||||
|
||||
// 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.link.readsColumn != nil {
|
||||
return readReference(s, leaf, sc.in(leaf), leaf.link.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
|
||||
}
|
||||
|
||||
@@ -28,10 +28,6 @@ ranking `readSurvey.walk` hardest and all seven the unit they would least modify
|
||||
|
||||
This round, in order:
|
||||
|
||||
- Move the render's read path — `readField`, `readReference`, `readHeld`, `traceRead`,
|
||||
`renderLeaf`, `resolveChoice`, `renderOnce` and `expandAnew` — into `render.go`, and
|
||||
merge what `hold.go` and `holdset.go` keep into one file: both architects followed one read
|
||||
across three files.
|
||||
- Give `prove`, `stepInto` and `columnItems` a case per node kind and panic through
|
||||
`internalError` on the rest, and list every such switch in `node`'s doc: `prove`'s
|
||||
default tells a typed column reading a bare table that it reads a null, and six seats
|
||||
|
||||
Reference in New Issue
Block a user