Improve gojq usage: ValueError handling, WithVariables, filter cache, inferSchema decoupling, any alias#7680
Merged
Merged
Conversation
…ter cache, inferSchema decoupling, any type alias
Copilot
AI
changed the title
[WIP] Review gojq module for Go integration
Improve gojq usage: ValueError handling, WithVariables, filter cache, inferSchema decoupling, Jun 17, 2026
any alias
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refines the gateway’s jq (gojq) integration in internal/middleware by improving runtime error diagnostics, adding an injection-safe compilation path for variable-bound filters, caching compiled filter bytecode, and separating schema inference into a gojq-independent helper. It also updates middleware-facing APIs to use Go’s any alias consistently.
Changes:
- Added
gojq.ValueErrorhandling inrunJqCodeto distinguish intentionalerror(...)failures from runtime type errors and include a null-safety hint for the latter. - Introduced
CompileToolResponseFilterWithVars(usesgojq.WithVariables) and added async.Mapcache for compiled non-parameterized filters. - Moved
inferSchemainto a newinternal/middleware/schema.goto decouple schema inference from gojq imports, and updatedinterface{}→anyacross the middleware surface.
Show a summary per file
| File | Description |
|---|---|
| internal/middleware/schema.go | New gojq-independent inferSchema implementation for schema inference. |
| internal/middleware/jqschema.go | Adds filter compilation cache, ValueError vs runtime-type error handling, WithVariables compilation helper, and any type updates. |
| internal/middleware/jqschema_coverage_test.go | Adds coverage for ValueError/type-error paths, WithVars compilation, and cache hit behavior. |
| internal/config/validation.go | Adds validateToolResponseFiltersWithVars helper to mirror WithVariables compile options for startup validation. |
| internal/config/validation_test.go | Tests the new validateToolResponseFiltersWithVars validation helper. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 5/5 changed files
- Comments generated: 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses all five recommendations from the gojq module review (#7670): better runtime error diagnostics, injection-safe variable binding, compile-time filter caching, decoupled schema logic, and idiomatic Go 1.25 types.
Error handling (
runJqCode)gojq.ValueErrorcheck betweenHaltErrorand the generic fallback — intentionalerror()builtin calls get a clean message; all unexported type errors (null access, wrong-type builtins) get a "check filter handles null/missing fields" hintgojq.TypeErrorfrom the issue report does not exist in the public API;gojq.ValueErroris the correct interfaceVariable injection (
CompileToolResponseFilterWithVars)New function using
gojq.WithVariablesfor per-call context without string interpolation:validateToolResponseFiltersWithVarsadded inconfig/validation.goto mirror the same compile options for fail-fast startup validation.Filter compilation cache (
filterCodeCache sync.Map)CompileToolResponseFilterchecks the cache before parsing; stores on misscached.(*gojq.Code)) to avoid potential panics on unexpected cache contentsinferSchemadecoupled from gojqinternal/middleware/schema.go— imports onlyencoding/jsonandreflect, no gojq dependencyinterface{}→anyAll internal and public signatures in
jqschema.goupdated toany(idiomatic Go 1.18+, project targets Go 1.25).