Skip to content

feat(scope): lookup + macro support — PR C - #22

Merged
rylero merged 1 commit into
mainfrom
feat/scope-query-external-files
Jul 6, 2026
Merged

rylero merged 1 commit into
mainfrom
feat/scope-query-external-files

Conversation

@rylero

@rylero rylero commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Third of 4 themed PRs building Tier-2 query features on the merged MVP (#19) + prior PRs (#20, #21). Theme: pulling in data/config from outside the query string.

Closes #15 (lookup), closes #17 (macros).

lookup — static CSV joins (#15)

Simplified from real SPL's lookup (no named lookup-table registration, no separate rename-on-input clause):

lookup "canids.csv" CANID output SubsystemName as Subsystem
  • CSV needs a header row; the join key column in the CSV must share the query field's name.
  • Multiple outputs, each optionally aliased: output A as x, B as y.
  • Unmatched keys get nil for every output column.
  • Loaded eagerly at parse time (query/lookup.go) — a missing file or bad column name is an immediate parse error, same posture as rex compiling its regex eagerly.

macros — reusable named queries (#17)

Backtick `name` references expand to a snippet from ~/.claudescope/macros.json:

{"brownout": "where BatteryVoltage < 7 | ranges"}
ClaudeScope query '`brownout`'

Deviation from the ticket: uses JSON, not YAML — the module had zero third-party dependencies before this PR (go.mod only pulls in go-nt4 for live NT), and a flat name→string map doesn't need YAML's extra expressiveness. Happy to swap to gopkg.in/yaml.v3 if you'd rather standardize on YAML across the project.

Expansion happens as a string pre-pass in Parse(), before tokenizing (backticks aren't otherwise meaningful in the grammar). Macros can reference other macros; capped at 10 levels to catch cycles. query.SetMacrosPath is exported so tests (and this PR's tests) can redirect the lookup path without touching $HOME.

Docs

Both added to ClaudeScope help and skills/scope/SKILL.md; lookup noted as simplified-from-SPL, same posture as ranges/transaction already being flagged as ClaudeScope extensions.

Test plan

  • go build ./..., go vet ./..., gofmt clean, go test ./... -race all pass
  • New tests: lookup join + unmatched-key nil + aliased/multi-output + missing-file error; macro expansion equivalence, unknown-macro error, cycle detection; new parse-error cases for malformed lookup/undefined macro
  • CI green on this PR

🤖 Generated with Claude Code

Closes #15, closes #17.

lookup (#15): left-joins a static CSV onto EventTable rows by field
equality. Simplified from real SPL's lookup (no named lookup-table
registration, no separate rename-on-input): `lookup "<path>.csv" <field>
output <col> [as <alias>][, ...]`. The CSV needs a header row with a
column matching <field>'s name; unmatched keys get nil. CSV is loaded
eagerly at parse time (query/lookup.go) so a missing file or bad column
name is an immediate parse error, matching how rex already compiles its
regex eagerly.

macros (#17): backtick `name` references in a query string expand to a
named snippet loaded from ~/.claudescope/macros.json (query/macros.go).
Uses JSON, not YAML as the ticket originally sketched, to avoid adding a
dependency (only stdlib deps existed before this PR) — a flat JSON
object of name -> pipe-query text covers the same use case. Macros can
reference other macros; expansion is capped at 10 levels to catch
cycles. Expansion happens as a string pre-pass in Parse(), before
tokenizing, since backticks aren't otherwise meaningful in the grammar.
SetMacrosPath is exported for tests to redirect the lookup path without
touching the real home directory.

Docs: lookup/macros added to `help` and skills/scope/SKILL.md; lookup
noted as simplified-from-SPL, matching how ranges/transaction are
already flagged as ClaudeScope extensions.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@rylero
rylero merged commit 574098b into main Jul 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

query: named macros (saved/reusable queries) query: lookup stage (join static reference tables, e.g. CAN ID -> subsystem name)

1 participant