Deduplicate jq filter compile/cache pipeline in jqschema without changing public behavior - #8565
Merged
Merged
Conversation
Closed
6 tasks
Copilot
AI
changed the title
[WIP] Refactor duplicate code in jqschema.go
Deduplicate jq filter compile/cache pipeline in Jul 3, 2026
jqschema without changing public behavior
lpcox
marked this pull request as ready for review
July 3, 2026 12:43
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the jq tool-response filter compilation logic in internal/middleware/jqschema.go by consolidating the shared cache lookup → parse → compile → cache store pipeline into a single internal helper, while keeping the two public entry points’ intentional differences (cache-key shape and compile options) intact.
Changes:
- Introduced a shared internal compile function (
compileToolResponseFilterInternal) to deduplicate cache/compile flow and logging. - Kept
CompileToolResponseFilterandCompileToolResponseFilterWithVarsas thin wrappers to preserve API-specific behavior. - Added a coverage test ensuring the plain and var-enabled APIs do not share cached compiled code.
Show a summary per file
| File | Description |
|---|---|
| internal/middleware/jqschema.go | Deduplicates the filter compile/cache pipeline into a shared internal helper while preserving wrapper-level behavior differences. |
| internal/middleware/jqschema_coverage_test.go | Adds a focused test ensuring cache-key separation between plain vs var-enabled compilation paths. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Low
Comment on lines
+286
to
+290
| filter, | ||
| compileOptsWithVariables(varNames), | ||
| "CompileToolResponseFilterWithVars", | ||
| fmt.Sprintf(", vars=%v", varNames), | ||
| ) |
| code, ok := cached.(*gojq.Code) | ||
| if !ok { | ||
| // Should never happen; the cache only stores *gojq.Code values. | ||
| return nil, fmt.Errorf("internal error: unexpected cached value type for filter (len=%d)", len(filter)) |
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.
CompileToolResponseFilterandCompileToolResponseFilterWithVarsduplicated the same cache-check → parse → compile → cache-store flow, forcing parallel maintenance of error/log/cache behavior. This refactor consolidates that pipeline into one internal path while preserving the two intentional behavioral differences (cache key shape and compile options).Refactor: shared internal compile path
compileToolResponseFilterInternal[K comparable]to centralize:Preserved API-specific behavior
CompileToolResponseFilterstill uses:filterstringsecureCompileOptsCompileToolResponseFilterWithVarsstill uses:toolResponseFilterVarsCacheKey{filter, varNamesKey}compileOptsWithVariables(varNames)Coverage update for cache-key separation
*gojq.Code.