internal

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultMaxColumns = 20

DefaultMaxColumns is the column cap applied to `select *` output when ExecOptions does not set an explicit value.

Variables

This section is empty.

Functions

func FieldName

func FieldName(e Expr, idx int) string

FieldName returns a sensible column header for an expression. Field references use the field name; anything else falls back to a positional placeholder.

func FormatValue

func FormatValue(v Value) string

FormatValue renders a Value as a plain string for table output.

func PrintTable

func PrintTable(w io.Writer, headers []string, rows []TableRow)

PrintTable writes a tab-separated table to w: filename column plus the supplied headers and the materialized values from each TableRow.

func Write

func Write(path FilePath, d *Document) error

Write serializes a Document back to disk, replacing the original file.

Types

type AlterOp

type AlterOp int

AlterOp distinguishes the two forms of alter query.

const (
	AlterDrop AlterOp = iota
	AlterRename
)

type AlterQuery

type AlterQuery struct {
	Op     AlterOp
	Drop   []Field      // populated when Op == AlterDrop
	Rename []RenamePair // populated when Op == AlterRename
	// contains filtered or unexported fields
}

AlterQuery represents: alter <globs> drop/rename ... [where <expr>]

func (AlterQuery) Eval

func (q AlterQuery) Eval(fm FrontMatter) (*TableRow, error)

Eval drops or renames fields, then projects the affected fields. For drop, projection happens before deletion so dropped values are visible. For rename, projection happens after rename so headers match new names.

func (AlterQuery) Globs

func (q AlterQuery) Globs() []string

func (AlterQuery) IsMutation

func (q AlterQuery) IsMutation() bool

func (*AlterQuery) Parse

func (q *AlterQuery) Parse(r io.Reader) error

type Assign

type Assign struct {
	Field Field
	Op    AssignOp
	Value Expr
}

Assign is one assignment in an update...set clause. Value is nil for the cast-only form (e.g. `set foo:int`).

func (Assign) Apply

func (a Assign) Apply(fm FrontMatter) error

Apply evaluates a.Value (if present), casts to a.Field.Type, and writes the result into fm under a.Field.Name. A runtime cast failure is returned as an error so the caller can halt the current file per Manual.md.

func (*Assign) Parse

func (a *Assign) Parse(r io.Reader) error

type AssignOp

type AssignOp int

AssignOp is the operator used in an update...set assignment.

const (
	OpSet AssignOp = iota // =
	OpAdd                 // +=
	OpSub                 // -=
)

type BinExpr

type BinExpr struct {
	Op    BinOp
	Left  Expr
	Right Expr
}

BinExpr is a binary operation: Left Op Right.

func (BinExpr) Eval

func (e BinExpr) Eval(fm FrontMatter) Value

func (*BinExpr) Parse

func (e *BinExpr) Parse(r io.Reader) error

type BinOp

type BinOp int

BinOp is a binary operator, ordered by precedence group (lowest first).

const (
	// Boolean connectives (lowest precedence).
	BinOr  BinOp = iota // or
	BinAnd              // and

	// Comparison operators.
	BinEq      // =
	BinNe      // !=
	BinLt      // <
	BinLe      // <=
	BinGt      // >
	BinGe      // >=
	BinOverlap // <=>  set overlap (lists share ≥1 element)

	// Arithmetic operators.
	BinAdd // +
	BinSub // -
	BinMul // *
	BinDiv // /
)

type Document

type Document struct {
	FrontMatter FrontMatter
	Body        string
}

func ReadDocument

func ReadDocument(path string) (*Document, error)

ReadDocument loads a Markdown file and splits its YAML frontmatter from the body. A file without a leading `---\n` block is treated as body-only.

type ExecOptions

type ExecOptions struct {
	// DryRun runs all statements in memory but does not write mutated documents back to disk.
	DryRun bool
	// MaxColumns caps the number of frontmatter columns rendered for `select *`
	// output. Zero means use DefaultMaxColumns. Excess columns are dropped from
	// the printed table; row count is unaffected.
	MaxColumns int
	// IncludeHidden includes files whose basename begins with '.' in glob
	// expansion. Default skips them.
	IncludeHidden bool
}

ExecOptions controls how a Program is executed.

type Expr

type Expr interface {
	Eval(fm FrontMatter) Value
	// contains filtered or unexported methods
}

Expr is the expression tree node interface.

func ParseExpr

func ParseExpr(r io.Reader) (Expr, error)

ParseExpr reads an expression from r and returns the appropriate Expr node.

type Field

type Field struct {
	Name string
	Type FieldType
}

Field is a field reference with an optional type annotation: identifier[:type].

func (*Field) Parse

func (f *Field) Parse(r io.Reader) error

type FieldExpr

type FieldExpr struct {
	Field Field
}

FieldExpr is a field reference atom. Evaluates to the field's value in the current file's frontmatter, or void if absent.

func (FieldExpr) Eval

func (e FieldExpr) Eval(fm FrontMatter) Value

func (*FieldExpr) Parse

func (e *FieldExpr) Parse(r io.Reader) error

type FieldType

type FieldType int

FieldType is the declared type of a frontmatter field.

const (
	TypeAny      FieldType = iota // default when no type annotation is given
	TypeString                    // string
	TypeBool                      // bool
	TypeInt                       // int
	TypeNumber                    // numeric (float)
	TypeDate                      // date (YYYY-MM-DD)
	TypeDatetime                  // datetime (ISO 8601 local time)
	TypeLink                      // [[ref]] or [[ref|title]]
	TypeMdLink                    // [title](ref)
	TypeList                      // list[:elemtype]
)

func (FieldType) String

func (f FieldType) String() string

type FilePath

type FilePath = string

func ExpandGlobs

func ExpandGlobs(globs []string) ([]FilePath, error)

ExpandGlobs expands the patterns to file paths. Bare paths that exist are passed through; tokens containing glob metacharacters are expanded via filepath.Glob. A bare-path token that does not exist is an error.

type FrontMatter

type FrontMatter map[string]any

type ListExpr added in v0.2.0

type ListExpr struct {
	Elems []Expr
}

ListExpr is a bracketed list literal: [e1, e2, ...]. Evaluates element-wise.

func (ListExpr) Eval added in v0.2.0

func (e ListExpr) Eval(fm FrontMatter) Value

type LitExpr

type LitExpr struct {
	Kind  LiteralKind
	Value string
}

LitExpr is a literal constant atom. Value holds the raw source text: unquoted string content, digit string, "true", "false", or "null".

func (LitExpr) Eval

func (e LitExpr) Eval(_ FrontMatter) Value

func (*LitExpr) Parse

func (e *LitExpr) Parse(r io.Reader) error

type LiteralKind

type LiteralKind int

LiteralKind identifies the kind of a LitExpr.

const (
	LitString  LiteralKind = iota // quoted or raw string
	LitInt                        // decimal or 0x… integer
	LitNumeric                    // floating-point
	LitBool                       // true | false
	LitNull                       // null
)

type Output

type Output struct {
	// contains filtered or unexported fields
}

Output accumulates per-statement result tables across a multi-file evaluation.

func NewOutput

func NewOutput(p *Program, err io.Writer, maxColumns int) *Output

NewOutput allocates one Table per statement, in source order. maxColumns caps the column count rendered for `select *` tables; non-star tables are unaffected.

func (*Output) AllDone

func (o *Output) AllDone() bool

AllDone reports whether every statement has reached its short-circuit.

func (*Output) Append

func (o *Output) Append(path FilePath, index int, row *TableRow) (done bool)

Append stamps row with path, stores it in the i'th table, and returns true when no further input is needed for that statement.

func (*Output) Done

func (o *Output) Done(index int) bool

Done reports whether statement i has collected enough rows to short-circuit remaining files (only true for select with limit and no sort).

func (*Output) Error

func (o *Output) Error(path FilePath, err error)

Error reports a per-file evaluation error to the configured error sink.

func (*Output) Finalize

func (o *Output) Finalize()

Finalize sorts and truncates each table according to its source statement.

func (*Output) Print

func (o *Output) Print(w io.Writer)

Print writes every table to w in source order. Multi-statement programs separate consecutive tables with a blank line.

type Program

type Program struct {
	Stmts []Query
}

func ParseProgram

func ParseProgram(r io.Reader) (Program, error)

ParseProgram reads a sequence of statements from r and returns a Program. Statements are separated by ';'. '--' starts a line comment that runs to the next newline and is treated as whitespace. Consecutive ';' separators (including leading and trailing ones) are tolerated. An input that contains only whitespace and comments yields an empty Program.

func (Program) Run

func (p Program) Run(opts ExecOptions, okOut, errOut io.Writer) (ok bool)

Run executes the program. For each file in the union of all statement globs, the document is read once, every applicable statement runs against the in-memory frontmatter, and the file is written back if any mutation succeeded. A failing statement halts further work on that file and cancels its write; the loop proceeds to the next file. Output for completed statements is preserved. Tables are written to okOut in source order; per-file errors are reported to errOut. The returned bool is false if any file failed to read, write, or evaluate.

type Query

type Query interface {
	Eval(fm FrontMatter) (*TableRow, error)
	IsMutation() bool
	Globs() []string
	// contains filtered or unexported methods
}

func ParseQuery

func ParseQuery(r io.Reader) (Query, error)

ParseQuery reads a single query from r and returns a SelectQuery, UpdateQuery, or AlterQuery. Trailing input is not validated.

type RenamePair

type RenamePair struct {
	From string
	To   string
}

RenamePair is one rename in an alter...rename clause.

func (*RenamePair) Parse

func (p *RenamePair) Parse(r io.Reader) error

type SelectQuery

type SelectQuery = query

SelectQuery represents: select <exprs> from <globs> [where <expr>] [sort by <terms>] [limit <n>]

func (*SelectQuery) Parse

func (q *SelectQuery) Parse(r io.Reader) error

type SortTerm

type SortTerm struct {
	Expr Expr
	Desc bool
}

SortTerm is one key in a sort-by clause.

func (SortTerm) Eval

func (s SortTerm) Eval(fm FrontMatter) Value

Eval is a thin wrapper around the underlying expression.

func (*SortTerm) Parse

func (s *SortTerm) Parse(r io.Reader) error

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table holds the accumulated rows for one statement.

type TableRow

type TableRow struct {
	// contains filtered or unexported fields
}

TableRow is one projected row: the file it came from and the materialized values for the statement's Select and SortBy expressions. star is populated instead of print when the source statement was `select *`.

func (TableRow) IsZero

func (r TableRow) IsZero() bool

type UnaryExpr

type UnaryExpr struct {
	Op      UnaryOp
	Operand Expr
}

UnaryExpr is a unary operation: Op Operand.

func (UnaryExpr) Eval

func (e UnaryExpr) Eval(fm FrontMatter) Value

func (*UnaryExpr) Parse

func (e *UnaryExpr) Parse(r io.Reader) error

type UnaryOp

type UnaryOp int

UnaryOp is a unary operator.

const (
	UnaryNot UnaryOp = iota // not  (boolean negation)
	UnaryNeg                // -    (arithmetic negation)
)

type UpdateQuery

type UpdateQuery struct {
	Set []Assign
	// contains filtered or unexported fields
}

UpdateQuery represents: update <globs> set <assigns> [where <expr>]

func (UpdateQuery) Eval

func (q UpdateQuery) Eval(fm FrontMatter) (*TableRow, error)

Eval applies all assignments in order, then projects the affected fields. A failed assignment returns (nil, err); fm may be partially mutated. Caller is responsible for discarding the file on error.

func (UpdateQuery) Globs

func (q UpdateQuery) Globs() []string

func (UpdateQuery) IsMutation

func (q UpdateQuery) IsMutation() bool

func (*UpdateQuery) Parse

func (q *UpdateQuery) Parse(r io.Reader) error

type Value

type Value struct {
	Kind FieldType
	Data any
	Null bool
}

Value is the runtime value produced by evaluating an expression. Null marks absence: a missing field or a cast that could not produce a value. Per Manual.md: null propagates through arithmetic; in boolean context it is falsey.

func Cast

func Cast(v Value, target FieldType) (Value, error)

Cast converts v to target. Returns error if conversion is not possible.

func (Value) String

func (v Value) String() string

String returns a formatted representation of v like bool(true) or string("haha").

Jump to

Keyboard shortcuts

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