Skip to content

refactor: use maps.Copy for cleaner map handling#6283

Merged
ehsandeep merged 1 commit intoprojectdiscovery:devfrom
gopherorg:dev
Jul 11, 2025
Merged

refactor: use maps.Copy for cleaner map handling#6283
ehsandeep merged 1 commit intoprojectdiscovery:devfrom
gopherorg:dev

Conversation

@gopherorg
Copy link
Contributor

@gopherorg gopherorg commented Jun 27, 2025

Proposed changes

There is a new function added in the go1.21 standard library, which can make the code more concise and easy to read.

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • Refactor
    • Simplified internal data handling by optimizing map merging and copying processes across various features, enhancing performance and maintainability without impacting user experience or functionality.

@auto-assign auto-assign bot requested a review from dwisiswant0 June 27, 2025 03:48
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 27, 2025

Walkthrough

The changes replace manual map copying loops with the standard library's maps.Copy function across multiple packages. This refactor simplifies merging and copying of map entries in various functions handling schema examples, module settings, event building, request execution, and response processing. No public API signatures were altered.

Changes

File(s) Change Summary
pkg/input/formats/openapi/examples.go Replaced manual map copying in openAPIExample schema merging with maps.Copy.
pkg/js/gojs/gojs.go Updated GojaModule.Set to use maps.Copy for copying map entries.
pkg/operators/operators.go Changed Result.Merge to use maps.Copy for merging PayloadValues.
pkg/projectfile/httputil.go Simplified header copying in toInternalResponse with maps.Copy.
pkg/protocols/common/generators/generators.go Used maps.Copy to merge maps in New function.
pkg/protocols/common/generators/maps.go Replaced manual map merging with maps.Copy in MergeMaps function.
pkg/protocols/dns/request.go Replaced manual map copying with maps.Copy calls to merge maps into outputEvent.
pkg/protocols/file/request.go Used maps.Copy to replace manual copying of maps in findMatchesWithReader and buildEvent.
pkg/protocols/http/operators.go Simplified map copying in responseToDSLMap using maps.Copy.
pkg/protocols/http/request.go Replaced manual map merges with maps.Copy in executeRequest.
pkg/protocols/javascript/js.go Used maps.Copy to replace manual map copying in ExecuteWithResults and generateEventData.
pkg/protocols/network/request.go Replaced manual map merges with maps.Copy in executeRequestWithPayloads.
pkg/protocols/offlinehttp/operators.go Simplified map copying in responseToDSLMap with maps.Copy.
pkg/protocols/ssl/ssl.go Replaced manual map copying with maps.Copy in ExecuteWithResults.
pkg/protocols/websocket/websocket.go Used maps.Copy to merge maps in executeRequestWithPayloads and readWriteInputWebsocket.

Suggested reviewers

  • dwisiswant0
  • dogancanbakir
  • ehsandeep

Poem

In fields of code where maps once grew,
Rabbits hopped and copied too—
But now with maps.Copy, swift and neat,
Our keys and values quickly meet.
No more loops, just one quick hop,
Thanks to Go, the work won't stop!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 35311f7 and 001d5f5.

📒 Files selected for processing (15)
  • pkg/input/formats/openapi/examples.go (2 hunks)
  • pkg/js/gojs/gojs.go (2 hunks)
  • pkg/operators/operators.go (2 hunks)
  • pkg/projectfile/httputil.go (2 hunks)
  • pkg/protocols/common/generators/generators.go (2 hunks)
  • pkg/protocols/common/generators/maps.go (2 hunks)
  • pkg/protocols/dns/request.go (2 hunks)
  • pkg/protocols/file/request.go (3 hunks)
  • pkg/protocols/http/operators.go (2 hunks)
  • pkg/protocols/http/request.go (2 hunks)
  • pkg/protocols/javascript/js.go (3 hunks)
  • pkg/protocols/network/request.go (3 hunks)
  • pkg/protocols/offlinehttp/operators.go (2 hunks)
  • pkg/protocols/ssl/ssl.go (3 hunks)
  • pkg/protocols/websocket/websocket.go (3 hunks)
✅ Files skipped from review due to trivial changes (4)
  • pkg/protocols/offlinehttp/operators.go
  • pkg/protocols/http/operators.go
  • pkg/protocols/network/request.go
  • pkg/protocols/http/request.go
🚧 Files skipped from review as they are similar to previous changes (11)
  • pkg/input/formats/openapi/examples.go
  • pkg/js/gojs/gojs.go
  • pkg/projectfile/httputil.go
  • pkg/protocols/common/generators/maps.go
  • pkg/protocols/dns/request.go
  • pkg/protocols/common/generators/generators.go
  • pkg/protocols/websocket/websocket.go
  • pkg/protocols/file/request.go
  • pkg/protocols/ssl/ssl.go
  • pkg/operators/operators.go
  • pkg/protocols/javascript/js.go
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Member

@dwisiswant0 dwisiswant0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scanned it with Semgrep, found a few more! Could you also update the following occurrences?


pkg/protocols/common/generators/generators.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

 35┆ for k, v := range values {
 36┆   payloadsFinal[k] = v
 37┆ }

pkg/protocols/common/generators/maps.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

 50┆ for k, v := range m {
 51┆   merged[k] = v
 52┆ }

pkg/protocols/common/helpers/eventcreator/eventcreator.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

 36┆ for k, v := range result.DynamicValues {
 37┆   event.InternalEvent[k] = v
 38┆ }

pkg/protocols/dns/request.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

184┆ for k, v := range previous {
185┆   outputEvent[k] = v
186┆ }
  ⋮┆----------------------------------------
187┆ for k, v := range vars {
188┆   outputEvent[k] = v
189┆ }

pkg/protocols/file/request.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

265┆ for k, v := range previous {
266┆   dslMap[k] = v
267┆ }
  ⋮┆----------------------------------------
336┆ for k, v := range previous {
337┆   internalEvent[k] = v
338┆ }

pkg/protocols/http/operators.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

111┆ for k, v := range extra {
112┆   data[k] = v
113┆ }

pkg/protocols/http/request.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

972┆ for k, v := range previousEvent {
973┆   finalEvent[k] = v
974┆ }
  ⋮┆----------------------------------------
975┆ for k, v := range outputEvent {
976┆   finalEvent[k] = v
977┆ }

pkg/protocols/javascript/js.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

306┆ for k, v := range dynamicValues {
307┆   payloadValues[k] = v
308┆ }
  ⋮┆----------------------------------------
615┆ for k, v := range values {
616┆   data[k] = v
617┆ }

pkg/protocols/network/request.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

376┆ for k, v := range values {
377┆   payloads[k] = v
378┆ }
  ⋮┆----------------------------------------
428┆ for k, v := range previous {
429┆   outputEvent[k] = v
430┆ }
  ⋮┆----------------------------------------
431┆ for k, v := range interimValues {
432┆   outputEvent[k] = v
433┆ }
  ⋮┆----------------------------------------
434┆ for k, v := range inputEvents {
435┆   outputEvent[k] = v
436┆ }

pkg/protocols/offlinehttp/operators.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

106┆ for k, v := range extra {
107┆   data[k] = v
108┆ }

pkg/protocols/ssl/ssl.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

208┆ for k, v := range dynamicValues {
209┆   payloadValues[k] = v
210┆ }
  ⋮┆----------------------------------------
273┆ for k, v := range payloadValues {
274┆   data[k] = v
275┆ }

pkg/protocols/websocket/websocket.go
❯❱ tmp.use-maps-copy-instead
Use maps.Copy instead

275┆ for k, v := range previous {
276┆   data[k] = v
277┆ }
  ⋮┆----------------------------------------
278┆ for k, v := range events {
279┆   data[k] = v
280┆ }
  ⋮┆----------------------------------------
338┆ for k, v := range values {
339┆   inputEvents[k] = v
340┆ }

@gopherorg
Copy link
Contributor Author

gopherorg commented Jul 2, 2025

Scanned it with Semgrep, found a few more! Could you also update the following occurrences?

Thanks for your help!

Modified. Please review again.

@dwisiswant0 dwisiswant0 added the Type: Maintenance Updating phrasing or wording to make things clearer or removing ambiguity. label Jul 8, 2025
@dwisiswant0 dwisiswant0 removed the Type: Maintenance Updating phrasing or wording to make things clearer or removing ambiguity. label Jul 8, 2025
Signed-off-by: gopherorg <gopherworld@icloud.com>
@gopherorg
Copy link
Contributor Author

I have resolve the conflict.

Please review again.

@dwisiswant0 dwisiswant0 requested a review from ehsandeep July 10, 2025 16:32
@ehsandeep ehsandeep merged commit 1079498 into projectdiscovery:dev Jul 11, 2025
19 checks passed
Ice3man543 pushed a commit that referenced this pull request Aug 2, 2025
Signed-off-by: gopherorg <gopherworld@icloud.com>
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.

[MAINTENANCE] Simplifying map merging and copying using builtin maps.Copy

3 participants