Commit 2152fae
authored
feat(middleware): replace recursive jq walk_schema with native Go implementation (#4750)
The `itchyny/gojq` module review identified three actionable
improvements to `internal/middleware/jqschema.go`: replace the pure-jq
recursive schema walk with a native Go function, harden the type switch
against `json.Number`/`nil` inputs, and clarify benchmark intent.
## Native Go `walk_schema` via `gojq.WithFunction`
Adds `inferSchema(v interface{}) interface{}` — a pure-Go recursive
schema walker — and registers it as a native gojq function. The jq
filter collapses from 12 lines of recursive jq bytecode to a single
call:
```go
// Before: 12-line recursive jq def
const jqSchemaFilter = `
def walk_schema:
. as $in |
if type == "object" then
reduce keys[] as $k ({}; . + {($k): ($in[$k] | walk_schema)})
...
walk_schema
`
// After: native Go function, trivial jq entry point
const jqSchemaFilter = `walk_schema`
// Registered at compile time:
gojq.Compile(query,
gojq.WithFunction("walk_schema", 0, 0, func(v interface{}, _ []interface{}) interface{} {
return inferSchema(v)
}),
)
```
`inferSchema` handles all jq type names (`"null"`, `"boolean"`,
`"number"`, `"string"`) and preserves the array-collapsing behaviour
(first element only, `[]` for empty).
## Defensive type switch in `WrapToolHandler`
Adds `case nil` (early return, nil schema) and `case json.Number`
(convert via `.Float64()`) to the type switch that decides whether to
skip the `json.Unmarshal` round-trip. Guards against decoders using
`UseNumber()`.
## Tests
- `TestInferSchema` — exhaustive unit tests for every type case of
`inferSchema`
- `TestInferSchema_MatchesJqOutput` — cross-checks native Go output
against `applyJqSchema` to prevent drift
- `TestWrapToolHandler_JsonNumberData` — end-to-end coverage of the new
`json.Number` path
## Benchmark clarification
Adds an explicit comment to `BenchmarkApplyJqSchema_ParseEveryTime`
noting it intentionally simulates the degraded parse-every-call path for
comparison purposes only.
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `example.com`
> - Triggering command: `/tmp/go-build2697671537/b513/launcher.test
/tmp/go-build2697671537/b513/launcher.test
-test.testlogfile=/tmp/go-build2697671537/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2697671537/b410/vet.cfg se 690953/b038/vet.cfg x_amd64/vet
. pb` (dns block)
> - Triggering command: `/tmp/go-build2068934155/b509/launcher.test
/tmp/go-build2068934155/b509/launcher.test
-test.testlogfile=/tmp/go-build2068934155/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 83096d3e13560ff0
64/pkg/tool/linu/opt/hostedtoolcjson 7671537/b486/vet.cfg OuzHqgGgH cfg
64/pkg/tool/linu--log
/opt/hostedtoolc/var/lib/docker/buildkit/executor/runc-log.json -ato��
-bool ettings.Ports}} 3874a3be3dd1a781b6hh82lckewk8vbuoyc6zqssf
-errorsas -ifaceassert -nilfunc ache/go/1.25.9/x-buildtags` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build2697671537/b495/config.test
/tmp/go-build2697671537/b495/config.test
-test.testlogfile=/tmp/go-build2697671537/b495/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2697671537/b395/vet.cfg se t.go x_amd64/compile . --gdwarf2
--64 x_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build2068934155/b491/config.test
/tmp/go-build2068934155/b491/config.test
-test.testlogfile=/tmp/go-build2068934155/b491/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
5272951ab22a7972c140e0b7a44abee6634cf399ad4a01e54bb x_amd64/compile
ateway_with_pipe.sh` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build2697671537/b513/launcher.test
/tmp/go-build2697671537/b513/launcher.test
-test.testlogfile=/tmp/go-build2697671537/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2697671537/b410/vet.cfg se 690953/b038/vet.cfg x_amd64/vet
. pb` (dns block)
> - Triggering command: `/tmp/go-build2068934155/b509/launcher.test
/tmp/go-build2068934155/b509/launcher.test
-test.testlogfile=/tmp/go-build2068934155/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 83096d3e13560ff0
64/pkg/tool/linu/opt/hostedtoolcjson 7671537/b486/vet.cfg OuzHqgGgH cfg
64/pkg/tool/linu--log
/opt/hostedtoolc/var/lib/docker/buildkit/executor/runc-log.json -ato��
-bool ettings.Ports}} 3874a3be3dd1a781b6hh82lckewk8vbuoyc6zqssf
-errorsas -ifaceassert -nilfunc ache/go/1.25.9/x-buildtags` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build2697671537/b513/launcher.test
/tmp/go-build2697671537/b513/launcher.test
-test.testlogfile=/tmp/go-build2697671537/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2697671537/b410/vet.cfg se 690953/b038/vet.cfg x_amd64/vet
. pb` (dns block)
> - Triggering command: `/tmp/go-build2068934155/b509/launcher.test
/tmp/go-build2068934155/b509/launcher.test
-test.testlogfile=/tmp/go-build2068934155/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 83096d3e13560ff0
64/pkg/tool/linu/opt/hostedtoolcjson 7671537/b486/vet.cfg OuzHqgGgH cfg
64/pkg/tool/linu--log
/opt/hostedtoolc/var/lib/docker/buildkit/executor/runc-log.json -ato��
-bool ettings.Ports}} 3874a3be3dd1a781b6hh82lckewk8vbuoyc6zqssf
-errorsas -ifaceassert -nilfunc ache/go/1.25.9/x-buildtags` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build2697671537/b522/mcp.test
/tmp/go-build2697671537/b522/mcp.test
-test.testlogfile=/tmp/go-build2697671537/b522/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -o g_.a
elemetry.io/otel-ifaceassert x_amd64/vet -p
net/http/interna/usr/bin/runc -lang=go1.25 x_amd64/vet -uns�� .cfg
elemetry.io/otel@v1.43.0/propagation/doc.go x_amd64/vet go1.25.9
.io/otel/baggage/usr/bin/runc -nolocalimports x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build2068934155/b518/mcp.test
/tmp/go-build2068934155/b518/mcp.test
-test.testlogfile=/tmp/go-build2068934155/b518/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 93bff293c3594aa8
-buildtags .d/chrony-onoffline by/edae42a48ad93bash -ifaceassert
-nilfunc .d/chrony-onoffline -tes�� cf399ad4a01e54bb -test.timeout=10m0s
"CURL_CA_BUNDLE=/-id by/17dafc0aef64ebash t.go x_amd64/compile
152/log.json` (dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>4 files changed
Lines changed: 266 additions & 135 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
97 | 112 | | |
98 | 113 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | 114 | | |
109 | 115 | | |
110 | 116 | | |
111 | | - | |
| 117 | + | |
| 118 | + | |
112 | 119 | | |
113 | 120 | | |
114 | 121 | | |
115 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
116 | 126 | | |
117 | 127 | | |
118 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
49 | 48 | | |
50 | | - | |
| 49 | + | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
55 | 54 | | |
56 | 55 | | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
82 | 60 | | |
83 | 61 | | |
84 | 62 | | |
85 | 63 | | |
86 | 64 | | |
87 | | - | |
88 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
89 | 123 | | |
90 | 124 | | |
91 | 125 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | 126 | | |
96 | 127 | | |
97 | 128 | | |
| |||
101 | 132 | | |
102 | 133 | | |
103 | 134 | | |
104 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
105 | 140 | | |
106 | 141 | | |
107 | 142 | | |
108 | 143 | | |
109 | 144 | | |
110 | 145 | | |
111 | 146 | | |
112 | | - | |
| 147 | + | |
113 | 148 | | |
114 | 149 | | |
115 | 150 | | |
| |||
350 | 385 | | |
351 | 386 | | |
352 | 387 | | |
353 | | - | |
| 388 | + | |
354 | 389 | | |
355 | 390 | | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
356 | 402 | | |
357 | 403 | | |
358 | 404 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
86 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
87 | 92 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
| 93 | + | |
169 | 94 | | |
170 | 95 | | |
171 | 96 | | |
| |||
0 commit comments