Skip to content

Commit 1079498

Browse files
authored
refactor: use maps.Copy for cleaner map handling (#6283)
Signed-off-by: gopherorg <[email protected]>
1 parent a13ea39 commit 1079498

File tree

15 files changed

+40
-75
lines changed

15 files changed

+40
-75
lines changed

pkg/input/formats/openapi/examples.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openapi
22

33
import (
44
"fmt"
5+
"maps"
56
"slices"
67

78
"github.com/getkin/kin-openapi/openapi3"
@@ -162,9 +163,7 @@ func openAPIExample(schema *openapi3.Schema, cache map[*openapi3.Schema]*cachedS
162163
return nil, ErrNoExample
163164
}
164165

165-
for k, v := range value {
166-
example[k] = v
167-
}
166+
maps.Copy(example, value)
168167
}
169168
return example, nil
170169
}

pkg/js/gojs/gojs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gojs
22

33
import (
44
"context"
5+
"maps"
56
"reflect"
67
"sync"
78

@@ -103,9 +104,7 @@ func wrapModuleFunc(runtime *goja.Runtime, fn interface{}) interface{} {
103104
}
104105

105106
func (p *GojaModule) Set(objects Objects) Module {
106-
for k, v := range objects {
107-
p.sets[k] = v
108-
}
107+
maps.Copy(p.sets, objects)
109108
return p
110109
}
111110

pkg/operators/operators.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package operators
22

33
import (
44
"fmt"
5+
"maps"
56
"strconv"
67
"strings"
78

@@ -218,9 +219,7 @@ func (r *Result) Merge(result *Result) {
218219
r.DynamicValues[k] = sliceutil.Dedupe(append(r.DynamicValues[k], v...))
219220
}
220221
}
221-
for k, v := range result.PayloadValues {
222-
r.PayloadValues[k] = v
223-
}
222+
maps.Copy(r.PayloadValues, result.PayloadValues)
224223
}
225224

226225
// MatchFunc performs matching operation for a matcher on model and returns true or false.

pkg/projectfile/httputil.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/gob"
77
"encoding/hex"
88
"io"
9+
"maps"
910
"net/http"
1011
)
1112

@@ -85,9 +86,7 @@ func toInternalResponse(resp *http.Response, body []byte) *InternalResponse {
8586
intResp.HTTPMinor = resp.ProtoMinor
8687
intResp.StatusCode = resp.StatusCode
8788
intResp.StatusReason = resp.Status
88-
for k, v := range resp.Header {
89-
intResp.Headers[k] = v
90-
}
89+
maps.Copy(intResp.Headers, resp.Header)
9190
intResp.Body = body
9291
return intResp
9392
}

pkg/protocols/common/generators/generators.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package generators
44

55
import (
66
"github.com/pkg/errors"
7+
"maps"
78

89
"github.com/projectdiscovery/nuclei/v3/pkg/catalog"
910
"github.com/projectdiscovery/nuclei/v3/pkg/types"
@@ -32,9 +33,7 @@ func New(payloads map[string]interface{}, attackType AttackType, templatePath st
3233
if err != nil {
3334
return nil, errors.Wrap(err, "could not parse payloads with aggression")
3435
}
35-
for k, v := range values {
36-
payloadsFinal[k] = v
37-
}
36+
maps.Copy(payloadsFinal, values)
3837
default:
3938
payloadsFinal[payloadName] = v
4039
}

pkg/protocols/common/generators/maps.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generators
22

33
import (
4+
maps0 "maps"
45
"reflect"
56
)
67

@@ -47,9 +48,7 @@ func MergeMapsMany(maps ...interface{}) map[string][]string {
4748
func MergeMaps(maps ...map[string]interface{}) map[string]interface{} {
4849
merged := make(map[string]interface{})
4950
for _, m := range maps {
50-
for k, v := range m {
51-
merged[k] = v
52-
}
51+
maps0.Copy(merged, m)
5352
}
5453
return merged
5554
}

pkg/protocols/dns/request.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dns
33
import (
44
"encoding/hex"
55
"fmt"
6+
maps0 "maps"
67
"strings"
78
"sync"
89

@@ -181,12 +182,8 @@ func (request *Request) execute(input *contextargs.Context, domain string, metad
181182
// expose response variables in proto_var format
182183
// this is no-op if the template is not a multi protocol template
183184
request.options.AddTemplateVars(input.MetaInput, request.Type(), request.ID, outputEvent)
184-
for k, v := range previous {
185-
outputEvent[k] = v
186-
}
187-
for k, v := range vars {
188-
outputEvent[k] = v
189-
}
185+
maps0.Copy(outputEvent, previous)
186+
maps0.Copy(outputEvent, vars)
190187
// add variables from template context before matching/extraction
191188
if request.options.HasTemplateCtx(input.MetaInput) {
192189
outputEvent = generators.MergeMaps(outputEvent, request.options.GetTemplateCtx(input.MetaInput).GetAll())

pkg/protocols/file/request.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"fmt"
88
"io"
9+
"maps"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -276,9 +277,7 @@ func (request *Request) findMatchesWithReader(reader io.Reader, input *contextar
276277

277278
gologger.Verbose().Msgf("[%s] Processing file %s chunk %s/%s", request.options.TemplateID, filePath, processedBytes, totalBytesString)
278279
dslMap := request.responseToDSLMap(lineContent, input.MetaInput.Input, filePath)
279-
for k, v := range previous {
280-
dslMap[k] = v
281-
}
280+
maps.Copy(dslMap, previous)
282281
// add vars to template context
283282
request.options.AddTemplateVars(input.MetaInput, request.Type(), request.ID, dslMap)
284283
// add template context variables to DSL map
@@ -347,9 +346,7 @@ func (request *Request) buildEvent(input, filePath string, fileMatches []FileMat
347346
exprLines := make(map[string][]int)
348347
exprBytes := make(map[string][]int)
349348
internalEvent := request.responseToDSLMap("", input, filePath)
350-
for k, v := range previous {
351-
internalEvent[k] = v
352-
}
349+
maps.Copy(internalEvent, previous)
353350
for _, fileMatch := range fileMatches {
354351
exprLines[fileMatch.Expr] = append(exprLines[fileMatch.Expr], fileMatch.Line)
355352
exprBytes[fileMatch.Expr] = append(exprBytes[fileMatch.Expr], fileMatch.ByteIndex)

pkg/protocols/http/operators.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package http
22

33
import (
4+
"maps"
45
"net/http"
56
"strings"
67
"time"
@@ -108,9 +109,7 @@ func (request *Request) getMatchPart(part string, data output.InternalEvent) (st
108109
// responseToDSLMap converts an HTTP response to a map for use in DSL matching
109110
func (request *Request) responseToDSLMap(resp *http.Response, host, matched, rawReq, rawResp, body, headers string, duration time.Duration, extra map[string]interface{}) output.InternalEvent {
110111
data := make(output.InternalEvent, 12+len(extra)+len(resp.Header)+len(resp.Cookies()))
111-
for k, v := range extra {
112-
data[k] = v
113-
}
112+
maps.Copy(data, extra)
114113
for _, cookie := range resp.Cookies() {
115114
request.setHashOrDefault(data, strings.ToLower(cookie.Name), cookie.Value)
116115
}

pkg/protocols/http/request.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"fmt"
88
"io"
9+
"maps"
910
"net/http"
1011
"strconv"
1112
"strings"
@@ -974,12 +975,8 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ
974975
if request.options.Interactsh != nil {
975976
request.options.Interactsh.MakePlaceholders(generatedRequest.interactshURLs, outputEvent)
976977
}
977-
for k, v := range previousEvent {
978-
finalEvent[k] = v
979-
}
980-
for k, v := range outputEvent {
981-
finalEvent[k] = v
982-
}
978+
maps.Copy(finalEvent, previousEvent)
979+
maps.Copy(finalEvent, outputEvent)
983980

984981
// Add to history the current request number metadata if asked by the user.
985982
if request.NeedsRequestCondition() {

0 commit comments

Comments
 (0)