298 lines
8.9 KiB
Go
298 lines
8.9 KiB
Go
package fejkdata
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// checkNestedDrawGroup refuses a template beneath one drawing in group that names group again,
|
|
// short of a repeat or another draw group.
|
|
func checkNestedDrawGroup(fields map[string]node, group string) error {
|
|
if group == "" {
|
|
return nil
|
|
}
|
|
var walk func(path string, n node) error
|
|
walk = func(path string, n node) error {
|
|
t, isTemplate := n.(*template)
|
|
switch {
|
|
case isTemplate && t.drawGroup == group:
|
|
return fmt.Errorf("%q names drawGroup %q, the draw group this template draws in already; drop it", path, group)
|
|
case isTemplate && (t.drawGroup != "" || t.repeat > 1):
|
|
return nil
|
|
}
|
|
for _, c := range contained(n) {
|
|
if err := walk(join(path, c.name), c.node); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
for _, name := range sortedNames(fields) {
|
|
if err := walk(name, fields[name]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// drawCheck fences each template of a scope as a render of its own, remembering which nodes read a
|
|
// reference path.
|
|
// docs/decisions.md#a-render-shares-one-reference-draw-per-category-per-group
|
|
// docs/decisions.md#the-expansion-hold-and-the-renders-draws-are-two-fences
|
|
type drawCheck struct {
|
|
memo map[readsMemo]bool
|
|
}
|
|
|
|
type readsMemo struct {
|
|
n node
|
|
stopAtGroup bool
|
|
}
|
|
|
|
// checkDrawGroup refuses a draw group that splits nothing: one whose render reads every reference
|
|
// path inside a repeat or a nested draw group, which draw apart from it whatever it names.
|
|
func (c *drawCheck) checkDrawGroup(path string, n node) error {
|
|
if t, ok := n.(*template); ok && t.drawGroup != "" && !c.splitsDraws(t) {
|
|
return fmt.Errorf("%s: drawGroup %q splits nothing, since nothing it renders reads a reference path outside a repeat or a nested drawGroup; drop it", path, t.drawGroup)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *drawCheck) checkDraws(path string, n node) error {
|
|
t, ok := n.(*template)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if err := checkOwnFamily(t); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
if !c.readsPath(t) {
|
|
return nil
|
|
}
|
|
w := newDrawWalk()
|
|
for _, e := range renderEdges(t) {
|
|
w.edge(t, e, drawAt{group: t.drawGroupKey, route: drawRoute{e.reached(), e.label}})
|
|
}
|
|
if err := w.check(); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkRecordDraws fences the columns of a record — a template compiled at the top without a
|
|
// repeat — so a load proves the record view of it as well as the string view.
|
|
// docs/decisions.md#a-category-never-references-itself-and-a-records-fences-run-at-load
|
|
func (c *drawCheck) checkRecordDraws(path string, n node) error {
|
|
t, ok := n.(*template)
|
|
if !ok || !t.record {
|
|
return nil
|
|
}
|
|
// A record-only template's format renders nothing, so weigh the columns, not the format.
|
|
columns := sortedNames(t.fields)
|
|
reads := false
|
|
for _, name := range columns {
|
|
reads = reads || c.readsPath(t.fields[name])
|
|
}
|
|
if !reads {
|
|
return nil
|
|
}
|
|
if err := checkColumnDraws(t, columns); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// readsPath reports whether rendering n reads a reference path, short of a repeat, which renders
|
|
// over draws of its own.
|
|
func (c *drawCheck) readsPath(n node) bool { return c.reads(n, false) }
|
|
|
|
// splitsDraws reports whether rendering n reads a reference path that n's own draw group answers
|
|
// for: one outside a repeat and outside a nested draw group, which hold their own draws.
|
|
func (c *drawCheck) splitsDraws(n node) bool { return c.reads(n, true) }
|
|
|
|
// reads walks what rendering n renders for a reference path, stopping at a repeat — and at a nested
|
|
// draw group when stopAtGroup — since each holds draws of its own.
|
|
func (c *drawCheck) reads(n node, stopAtGroup bool) bool {
|
|
k := readsMemo{n, stopAtGroup}
|
|
if r, done := c.memo[k]; done {
|
|
return r
|
|
}
|
|
r := false
|
|
for _, e := range renderEdges(n) {
|
|
if a, isRef := refRead(n, e.label); (isRef && (len(a.tail) > 0 || readsTable(n, a))) || (!repeats(e.to) && !(stopAtGroup && grouped(e.to)) && c.reads(e.to, stopAtGroup)) {
|
|
r = true
|
|
break
|
|
}
|
|
}
|
|
if c.memo == nil {
|
|
c.memo = map[readsMemo]bool{}
|
|
}
|
|
c.memo[k] = r
|
|
return r
|
|
}
|
|
|
|
func repeats(n node) bool {
|
|
t, isTemplate := n.(*template)
|
|
return isTemplate && t.repeat > 1
|
|
}
|
|
|
|
func grouped(n node) bool {
|
|
t, isTemplate := n.(*template)
|
|
return isTemplate && t.drawGroupKey != ""
|
|
}
|
|
|
|
// readsTable reports whether a reference of n names a table, whose draw the render's
|
|
// group answers for even when read whole.
|
|
func readsTable(n node, a arm) bool {
|
|
t, isTemplate := n.(*template)
|
|
if !isTemplate {
|
|
return false
|
|
}
|
|
_, isTable := t.head(a.key).(*table)
|
|
return isTable
|
|
}
|
|
|
|
// refRead is the reference an edge of n reads; false when the edge reads none.
|
|
func refRead(n node, label string) (arm, bool) {
|
|
t, isTemplate := n.(*template)
|
|
if !isTemplate {
|
|
return arm{}, false
|
|
}
|
|
a := splitArm(label, t.refs)
|
|
return a, isRef(a.key)
|
|
}
|
|
|
|
// checkColumnDraws fences a record's columns as one render.
|
|
func checkColumnDraws(t *template, columns []string) error {
|
|
w := newDrawWalk()
|
|
for _, name := range columns {
|
|
w.walk(t.fields[name], drawAt{group: t.drawGroupKey, route: drawRoute{spelling: fmt.Sprintf("column %q", name)}})
|
|
}
|
|
return w.check()
|
|
}
|
|
|
|
// drawWalk gathers the references one render reads, by draw group, for check to compare.
|
|
type drawWalk struct {
|
|
reads []pathRead
|
|
read map[readKey]bool
|
|
seen map[nodeVisit]bool
|
|
}
|
|
|
|
// drawAt is where a walk stands: the draw group it draws in, how the render's root reached it, the
|
|
// table rows it entered, and the table a whole read draws a row of.
|
|
type drawAt struct {
|
|
group string
|
|
route drawRoute
|
|
alt pinSet
|
|
drawn *table // left out of the visit keys: only this table's own cells compare against it, which the own-family fence keeps true
|
|
}
|
|
|
|
// drawRoute is how a render reaches a draw: as its author spells it, and the root edge's label.
|
|
type drawRoute struct{ spelling, label string }
|
|
|
|
// pathRead is one reference a render reads: a path, or a bare reference with no tail.
|
|
type pathRead struct {
|
|
at drawAt
|
|
a arm
|
|
tr *tableRead // set where the reference names a table
|
|
}
|
|
|
|
type nodeVisit struct {
|
|
n node
|
|
group string
|
|
alt string
|
|
}
|
|
|
|
type readKey struct {
|
|
group string
|
|
path string
|
|
alt string
|
|
}
|
|
|
|
func newDrawWalk() *drawWalk {
|
|
return &drawWalk{read: map[readKey]bool{}, seen: map[nodeVisit]bool{}}
|
|
}
|
|
|
|
// walk follows what rendering n renders. A repeat renders over draws of its own, so the walk stops
|
|
// there.
|
|
func (w *drawWalk) walk(n node, at drawAt) {
|
|
v := nodeVisit{n, at.group, at.alt.key()}
|
|
if w.seen[v] {
|
|
return
|
|
}
|
|
w.seen[v] = true
|
|
if repeats(n) {
|
|
return
|
|
}
|
|
if t, isTemplate := n.(*template); isTemplate && t.drawGroupKey != "" {
|
|
at.group = t.drawGroupKey
|
|
}
|
|
if t, isTable := n.(*table); isTable {
|
|
at.drawn = t
|
|
}
|
|
for _, e := range renderEdges(n) {
|
|
if cell, isCell := e.to.(*template); isCell && cell.cellOf != nil {
|
|
if at.alt.clash(cell.cellOf, cell.cellRow) != nil {
|
|
continue
|
|
}
|
|
in := at
|
|
in.alt = at.alt.entered(cell.cellOf, cell.cellRow, cell.cellOf != at.drawn)
|
|
w.edge(n, e, in)
|
|
continue
|
|
}
|
|
w.edge(n, e, at)
|
|
}
|
|
}
|
|
|
|
// edge records the reference an edge reads, then walks on with every row the read
|
|
// pins entered, so a selected row renders only its own cells.
|
|
func (w *drawWalk) edge(from node, e renderEdge, at drawAt) {
|
|
if a, reads := refRead(from, e.label); reads {
|
|
tr := tableReadOf(from.(*template).head(a.key), a, e.to)
|
|
if k := (readKey{at.group, a.path, at.alt.key()}); !w.read[k] {
|
|
w.read[k] = true
|
|
w.reads = append(w.reads, pathRead{at, a, tr})
|
|
}
|
|
if tr != nil {
|
|
tr.pins.each(func(t *table, r int) { at.alt = at.alt.entered(t, r, true) })
|
|
}
|
|
}
|
|
w.walk(e.to, at)
|
|
}
|
|
|
|
// check refuses what one draw per reference path cannot answer for: a read of a level beside a path
|
|
// another read takes into it, and two reads of one table family that select different rows. Reads
|
|
// are compared in path order, so which pair is reported does not vary.
|
|
func (w *drawWalk) check() error {
|
|
sort.SliceStable(w.reads, func(i, j int) bool {
|
|
if w.reads[i].at.group != w.reads[j].at.group {
|
|
return w.reads[i].at.group < w.reads[j].at.group
|
|
}
|
|
return w.reads[i].a.path < w.reads[j].a.path
|
|
})
|
|
for i, level := range w.reads {
|
|
for _, into := range w.reads[i+1:] {
|
|
if into.at.group != level.at.group || alternatives(level.at, into.at) {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(into.a.path, level.a.path+".") && !(level.tr != nil && level.tr.whole) {
|
|
return overlapError(level.at.route, level.a.name, into)
|
|
}
|
|
}
|
|
}
|
|
return checkFamilies(w.reads)
|
|
}
|
|
|
|
func overlapError(route drawRoute, ref string, into pathRead) error {
|
|
return fmt.Errorf("%s renders a level that %s reads a path into; name the fields you want instead, or draw them apart with a drawGroup", route.spelled(ref), into.at.route.spelled(into.a.name))
|
|
}
|
|
|
|
// spelled names the route, and the reference it reaches a draw by where its root edge is not that
|
|
// reference.
|
|
func (r drawRoute) spelled(ref string) string {
|
|
if ref == "" || ref == r.label {
|
|
return r.spelling
|
|
}
|
|
return fmt.Sprintf("%s with {%s}", r.spelling, ref)
|
|
}
|