[Repo Assist] refactor(guard): extract decodeWasmCallResult and unmarshalWasmResponse helpers#6397
Merged
lpcox merged 3 commits intoMay 24, 2026
Conversation
…se helpers Eliminates ~30 lines of duplicated WASM call+decode logic in tryCallWasmFunction (wasm_parse.go) and ~6 lines of duplicated JSON unmarshal boilerplate in LabelResource/LabelResponse (wasm.go). - Extract decodeWasmCallResult: shared fn.Call + buffer-protocol decode logic used by both the allocator path and the direct-memory fallback. Any future change to buffer sizing, error codes, or copy strategy now has a single edit point. - Extract unmarshalWasmResponse: adds the function name to the error message so callers can be distinguished in logs. Addresses duplicate-code analysis reports #6384 and #6385. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors the WASM guard integration to remove duplicated “call + decode buffer-protocol” logic and duplicated JSON-unmarshal boilerplate, centralizing both behaviors into shared helpers.
Changes:
- Extracted
decodeWasmCallResulthelper to consolidate WASM function invocation + buffer-protocol result decoding intryCallWasmFunction. - Extracted
unmarshalWasmResponsehelper to centralize JSON decoding and include the WASM function name in unmarshal error messages. - Updated
LabelResource/LabelResponseto use the new unmarshal helper.
Show a summary per file
| File | Description |
|---|---|
internal/guard/wasm_parse.go |
Removes duplicated WASM call/result decoding by delegating both memory paths to decodeWasmCallResult. |
internal/guard/wasm.go |
Uses unmarshalWasmResponse to share JSON decoding and improve error context for labeling calls. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 3
| @@ -296,21 +276,17 @@ func (g *WasmGuard) tryCallWasmFunction(ctx context.Context, fn api.Function, me | |||
| return nil, 0, fmt.Errorf("WASM function call failed: %w", err) | |||
| } | |||
Comment on lines
+522
to
+530
| return nil, difc.OperationWrite, fmt.Errorf("failed to unmarshal WASM response: %w", err) | ||
| response, err := unmarshalWasmResponse("label_resource", resultJSON) | ||
| if err != nil { | ||
| return nil, difc.OperationWrite, err |
Comment on lines
+556
to
+569
| return nil, fmt.Errorf("failed to unmarshal WASM response: %w", err) | ||
| responseMap, err := unmarshalWasmResponse("label_response", resultJSON) | ||
| if err != nil { | ||
| return nil, err |
Collaborator
|
@copilot address review feedback |
Contributor
Addressed in commits c1b1985 and 3d91a22.
|
This was referenced May 24, 2026
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Eliminates duplicated WASM call-and-decode logic identified in issues #6384 and #6385.
Changes
internal/guard/wasm_parse.go— closes #6384tryCallWasmFunctioncontained two execution paths (allocator path and direct-memory fallback) with ~30 character-for-character identical lines: thefn.Call(...)invocation and the entire buffer-protocol result decode. Both paths now delegate to a new package-level helper:Any future change to the buffer-too-small protocol (
-2return code), error code interpretation, or output-copy strategy now has a single edit point.internal/guard/wasm.go— closes #6385LabelResourceandLabelResponseboth contained the samejson.Unmarshal+ identical error message"failed to unmarshal WASM response". Both now call a new helper:The helper includes the function name in the error message, making it possible to distinguish which call failed in log output.
Test Status
go.modcannot be downloaded in this sandbox environment (network access toproxy.golang.orgis blocked). Build and tests could not be run locally.The changes are purely structural refactors with no behavioral changes:
internal/guard/wasm_test.gocovertryCallWasmFunctionandcallWasmFunctionwith both memory paths; they will exercisedecodeWasmCallResulton CI.Net diff: −49 lines, +35 lines (net −14 lines of duplication removed)
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgSee Network Configuration for more information.