returnplan

package
v1.0.0-alpha.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package returnplan is the schema-free core of response shaping (`Request.Return`): the path grammar, the compiled selection plan and its digest, and the per-node verdict a pruner or wire encoder asks while walking a response.

It imports the standard library only (TestReturnPlan_ImportBoundary), so the public `types` package can carry a *Plan in an unexported field without growing a dependency, and both the no-execute descriptor layer and the engine can share it. Schema knowledge — which paths exist, which an instance hides, which data columns a request produces — lives in internal/descriptor (ResolveReturn), never here.

Index

Constants

View Source
const DigestPrefix = "rp1:"

DigestPrefix versions the digest scheme ("rp1:" + sha256 hex).

View Source
const PresetCustom = "custom"

PresetCustom is the Plan.Preset of a block that names no preset but selects (include) or carves (exclude) explicitly.

Variables

This section is empty.

Functions

func CompanionStrings

func CompanionStrings(cs []Companion) []string

CompanionStrings renders the companions in canonical spelling.

func EncodeRow

func EncodeRow(owner any, row map[string]any) (b []byte, ok bool, err error)

EncodeRow runs the installed row encoder; ok is false when none is installed.

func Quote

func Quote(name string) string

Quote renders name as a quoted path segment: wrapped in `"`, with `"` and `\` inside escaped by a backslash (`\"`, `\\`). Any name — an expanded vector element column such as `emb_mean[q3]` included — round-trips through Parse.

func SetApplier

func SetApplier(a Applier)

SetApplier installs the applier. Called once, from package types' init.

func SetRowEncoder

func SetRowEncoder(e RowEncoder)

SetRowEncoder installs the row encoder. Called once, from package types' init.

func Strings

func Strings(ps []Path) []string

Strings renders a path list in canonical spelling (never nil).

Types

type Applier

type Applier func(target any, p *Plan) (unmatched []Path, ok bool)

Applier prunes target (a *types.Response) under p in place, attaches p to it so its wire encoder honours the plan, and returns the Open include paths that matched no concrete node. ok is false when target is not a type the applier knows.

type Companion

type Companion struct {
	Path Path
	With string
}

Companion is a conditional selection over a map: the map key Path names (its last segment) is selected whole wherever its parent map also holds the key With AND the plan keeps that sibling. It is how a preset keeps a figure that only means something beside another one (the pairwise `auxiliary.n` beside an inference `auxiliary.p`) without emitting it everywhere. An exclude covering Path (or the sibling) wins, as everywhere.

func (Companion) String

func (c Companion) String() string

String renders the companion as `<path> (with <sibling>)`.

type Path

type Path struct {
	Segments []Segment
	// Open is set by the schema resolver when the path steps through a
	// map key or an open (`any`) value, whose keys predict cannot know:
	// at runtime such an include may match nothing (the
	// PULSE_RETURN_PATH_UNMATCHED warning). Not part of the canonical
	// form.
	Open bool
}

Path is a parsed Return path.

func Apply

func Apply(target any, p *Plan) (unmatched []Path, ok bool)

Apply runs the installed applier; ok is false when none is installed or it does not know target's type.

func Parse

func Parse(s string) (Path, error)

Parse reads one path. The grammar:

path    = name *( "." name / "[*]" )
name    = bare / quoted
bare    = 1*char [ "*" ] / "*"        ; char: anything but . [ ] *,
                                      ; and the first is not "
quoted  = DQUOTE 1*qchar DQUOTE [ "*" ]
qchar   = any byte but " and \ / "\" DQUOTE / "\\"

A path starts with a name (the Response root is an object). `[*]` is the only array step — no numeric index. `*` may appear only as the last character of a name, where it makes that segment a prefix glob. Whitespace is part of a name; nothing is trimmed.

A quoted name spells a key the bare form cannot — one holding `.`, `[`, `]` or `*`, such as an expanded vector element column: `data[*]."emb_mean[q3]"`. Inside the quotes every byte is literal except `\"` (a quote) and `\\` (a backslash); any other escape, an empty `""` and an unterminated quote are refused. Only a name that STARTS with `"` is quoted — a `"` later in a bare name stays literal, so every path whose names do not open with `"` (every real response key) parses, and canonicalizes, exactly as before.

func (Path) Matches

func (p Path) Matches(c []Segment) bool

Matches reports whether the pattern selects exactly the concrete node at c: same depth, every segment covering c's.

func (Path) Selects

func (p Path) Selects(c []Segment) bool

Selects reports whether the pattern matches the concrete node at c or one of its ancestors: c lies in a subtree the pattern names.

func (Path) String

func (p Path) String() string

String is the canonical spelling: names joined by `.`, `[*]` attached to the preceding segment.

type Plan

type Plan struct {
	// Preset is the preset name the block resolved from, PresetCustom
	// for an explicit include / exclude without one.
	Preset string
	// Include, Exclude and Keep are canonical: deduplicated, sorted by
	// spelling, with every path another one already covers removed, an
	// include an exclude covers removed, an exclude that touches no
	// include or keep path removed, and a keep path an include covers
	// removed.
	Include []Path
	Exclude []Path
	Keep    []Path
	// Companions are the CONDITIONAL selections a preset carries: each
	// one keeps a map key beside a kept sibling (Companion). Canonical
	// like Keep: one an include or exclude covers is removed. Answered by
	// VisitIn, never by Visit (which cannot see the parent map).
	Companions []Companion
	// Precision is the wire float significant-digit count, 0 unlimited.
	Precision int
	// Exact lists the REQUEST-DERIVED precision exemptions: nodes (and
	// their subtrees) whose floats carry integer semantics for this
	// request — a count aggregation's data column, a count crosstab
	// cell — written at full precision even when Precision is set. The
	// resolver fills it (internal/descriptor/return_resolve.go); the
	// response-type exemptions that hold for every request live beside
	// the encoder (types/return_shape.go, precisionExempt). Derived from
	// the request, never part of the selection, so outside the digest.
	Exact []Path
	// SelectsAll reports that Include covers every top-level key of the
	// root, so the root itself is selected whole.
	SelectsAll bool
	// Digest identifies the resolved selection (Include, Exclude, Keep,
	// Companions, Precision — not the preset name), so equivalent
	// spellings share it.
	Digest string
}

Plan is a resolved Return selection. Build one with New; the zero value selects nothing.

A concrete response node is emitted iff no Exclude path matches it or an ancestor, and either an Include path matches it or an ancestor (the whole subtree), or it is an ancestor of something an Include path selects (emitted as a container holding only selected children), or a Keep path matches it exactly and its parent is emitted. Keep paths are the "retained unless excluded" nested `warnings` slots: kept wherever their parent object appears, never conjured on their own.

func New

func New(preset string, include, exclude, keep []Path, precision int, rootKeys []string) *Plan

New canonicalizes include / exclude / keep and computes the digest. rootKeys is every visible top-level key of the root object; an include set covering all of them selects the root whole.

func NewWithCompanions

func NewWithCompanions(preset string, include, exclude, keep []Path, companions []Companion, precision int, rootKeys []string) *Plan

NewWithCompanions is New plus the preset's conditional selections (Companion). A companion an exclude or an include covers is dropped (excluded, or already selected unconditionally).

func (*Plan) Identity

func (p *Plan) Identity() bool

Identity reports whether the plan changes nothing: everything selected, nothing excluded, no precision.

func (*Plan) Visit

func (p *Plan) Visit(c []Segment) Verdict

Visit answers for the concrete node at path c (Key / Elem segments; the empty path is the root). The caller only asks for a child of a node it emits.

func (*Plan) VisitIn

func (p *Plan) VisitIn(c []Segment, has func(key string) bool) Verdict

VisitIn is Visit for the map key at c (a Key segment last) whose parent map holds the keys has reports: on top of Visit's verdict it answers the plan's Companions — a companion key is kept whole beside a kept With sibling the parent holds. Exclude wins. Use it wherever a walker iterates a map's keys; Visit alone never keeps a companion.

type RowEncoder

type RowEncoder func(owner any, row map[string]any) ([]byte, error)

RowEncoder writes one result row (a streamed row, or one element of a Response's `data`) as its wire form under owner's plan — owner is the *types.Response the row belongs to (its plan is unexported). A nil owner, or one carrying no non-identity plan, writes the row exactly as the unshaped encoder does. Installed by package types at init, for the same reason as the Applier.

type Segment

type Segment struct {
	// Name is the JSON key, or the glob PREFIX when Glob is set (so
	// `effect_*` is Name "effect_" with Glob true; `*` alone is Name ""
	// with Glob true). Empty for an Index segment.
	Name string
	// Glob marks a suffix `*` map-key glob.
	Glob bool
	// Index marks the `[*]` array wildcard.
	Index bool
}

Segment is one step of a path: an object or map key (Name, optionally a suffix-glob prefix when Glob is set) or an array step (Index, the `[*]` wildcard over every element).

func Elem

func Elem() Segment

Elem is the concrete array-element segment.

func Key

func Key(name string) Segment

Key is a concrete object / map key segment.

func (Segment) String

func (s Segment) String() string

String renders the segment in path syntax. A name the bare form cannot spell — one holding `.`, `[`, `]` or `*`, or starting with `"` — renders quoted (Quote); every other name renders bare, exactly as before quoting existed, so the canonical form (and the plan digest) of a bare-spellable path never moves.

type SyntaxError

type SyntaxError struct {
	Path   string
	Reason string
}

SyntaxError reports a malformed path. Reason is short stable prose.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type Verdict

type Verdict struct {
	// Keep is false when the node is absent on the wire.
	Keep bool
	// Whole means the node's whole subtree is emitted untouched: the
	// caller need not descend. Keep && !Whole means descend and ask for
	// each child.
	Whole bool
}

Verdict is the plan's answer for one concrete node.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL