dsl

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 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

Overview

Package dsl is a declarative, user-extensible text-filter engine, adapted from rtk's TOML filter DSL (design D11). It lets command/log/tool-output shrinking be authored in YAML — no recompile — with the same 8-stage pipeline and Lossiness typing rtk proved out.

Adaptation for the proxy world: rtk matches a shell command string; we match a per-message "selector" (the tool name, or the first line of content) since a proxy sees tool OUTPUTS, not the command that produced them. The content stages are unchanged.

Pipeline order (each stage optional, applied in this exact order):

  1. strip_ansi 2. replace[] 3. match_output[]+unless
  2. strip/keep lines 5. truncate_lines_at 6. head/tail
  3. max_lines 8. on_empty

Filters are lossy (they drop lines), so the cmdfilter component that wraps this engine is an Offload: it stashes the original before applying a filter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunTests

func RunTests(b []byte) (failures []string, err error)

RunTests compiles and runs the inline [tests] in a filter document, returning the names of failing cases (empty = all pass). Powers a `verify` command.

Types

type Compiled

type Compiled struct {
	Name string
	// contains filtered or unexported fields
}

Compiled is a filter with its regexes prebuilt.

func Compile

func Compile(name string, d Def) (*Compiled, error)

Compile validates and precompiles a filter definition.

type Def

type Def struct {
	Description        string        `yaml:"description"`
	Match              string        `yaml:"match"` // regex against the selector key
	StripANSI          bool          `yaml:"strip_ansi"`
	Replace            []ReplaceRule `yaml:"replace"`
	MatchOutput        []MatchRule   `yaml:"match_output"`
	StripLinesMatching []string      `yaml:"strip_lines_matching"`
	KeepLinesMatching  []string      `yaml:"keep_lines_matching"`
	TruncateLinesAt    *int          `yaml:"truncate_lines_at"`
	HeadLines          *int          `yaml:"head_lines"`
	TailLines          *int          `yaml:"tail_lines"`
	MaxLines           *int          `yaml:"max_lines"`
	OnEmpty            *string       `yaml:"on_empty"`
}

Def is a raw filter definition (from YAML). All fields except Match are optional.

type File

type File struct {
	SchemaVersion int                   `yaml:"schema_version"`
	Filters       map[string]Def        `yaml:"filters"`
	Tests         map[string][]TestCase `yaml:"tests"`
}

File is a filter document: a schema version + named filters + inline tests.

type Lossiness

type Lossiness int

Lossiness records what a filter did to its input, so the caller can emit an accurate recovery hint (after rtk's Lossiness enum).

const (
	LossNone  Lossiness = iota // nothing dropped, or only reversible reformatting
	LossTail                   // a clean contiguous tail was dropped (tail -n +N recovers)
	LossWhole                  // non-contiguous / whole-blob loss; only full retrieval recovers
)

func Apply

func Apply(c *Compiled, input string) (string, Lossiness)

Apply runs the 8-stage pipeline over input, returning the filtered output and what kind of loss occurred.

type MatchRule

type MatchRule struct {
	Pattern string `yaml:"pattern"`
	Message string `yaml:"message"`
	Unless  string `yaml:"unless"`
}

MatchRule short-circuits: if Pattern matches the whole blob (and Unless does not), return Message immediately.

type Registry

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

Registry holds compiled filters, matched first-by-sorted-name for determinism.

func (*Registry) Len

func (r *Registry) Len() int

Len reports how many filters are loaded.

func (*Registry) Load

func (r *Registry) Load(b []byte) error

Load parses a YAML filter document and appends its filters to the registry. schema_version must be 1. Filters are stored sorted by name.

func (*Registry) Match

func (r *Registry) Match(key string) *Compiled

Match returns the first filter whose match regex matches key, or nil.

type ReplaceRule

type ReplaceRule struct {
	Pattern     string `yaml:"pattern"`
	Replacement string `yaml:"replacement"`
}

ReplaceRule is a chained line-by-line regex substitution ($1 backrefs allowed).

type TestCase

type TestCase struct {
	Name     string `yaml:"name"`
	Input    string `yaml:"input"`
	Expected string `yaml:"expected"`
}

TestCase is an inline filter test (input -> expected), run by RunTests.

Jump to

Keyboard shortcuts

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