chore: Use maps.Copy instead of manual copy loops - #19727
SungJin1212 wants to merge 2 commits into
Conversation
05abc80 to
f51e864
Compare
skartikey
left a comment
There was a problem hiding this comment.
@SungJin1212 Thanks for the contribution!
f51e864 to
8dad7aa
Compare
srebhan
left a comment
There was a problem hiding this comment.
Thanks for your contribution @SungJin1212! Some comments, mostly about using maps.Clone instead of allocating then copying.
| j.Tags = make(map[string]string, len(o.Tags)) | ||
| for k, v := range o.Tags { | ||
| j.Tags[k] = v | ||
| } | ||
| maps.Copy(j.Tags, o.Tags) |
There was a problem hiding this comment.
Wouldn't
| j.Tags = make(map[string]string, len(o.Tags)) | |
| for k, v := range o.Tags { | |
| j.Tags[k] = v | |
| } | |
| maps.Copy(j.Tags, o.Tags) | |
| j.Tags = maps.Clone(o.Tags) |
be even better? More occurrences below...
| j.TagPassFilters = make(map[string][]string, len(o.TagPassFilters)) | ||
| for k, v := range o.TagPassFilters { | ||
| j.TagPassFilters[k] = v | ||
| } | ||
| maps.Copy(j.TagPassFilters, o.TagPassFilters) |
There was a problem hiding this comment.
Clone, same for the other places below.
| tags[key] = val | ||
| } | ||
| maps.Copy(tags, headerTags) | ||
| maps.Copy(tags, fullPath.tags(h.TagPathPrefix)) |
There was a problem hiding this comment.
Pre-allocation doesn't take this into account. Get the result of the function first and pre-allocate more precisely.
| lastData = make(map[string]interface{}, len(data)) | ||
| for k, v := range data { | ||
| lastData[k] = v | ||
| } | ||
| maps.Copy(lastData, data) |
| j.TagPassFilters = make(map[string][]string, len(o.TagPassFilters)) | ||
| for k, v := range o.TagPassFilters { | ||
| j.TagPassFilters[k] = v | ||
| } | ||
| maps.Copy(j.TagPassFilters, o.TagPassFilters) |
| t := make(map[string]string, len(tags)) | ||
| for k, v := range tags { | ||
| t[k] = v | ||
| } | ||
| maps.Copy(t, tags) |
| m := make(map[string]string, len(s.tags)) | ||
| for k, v := range s.tags { | ||
| m[k] = v | ||
| } | ||
| maps.Copy(m, s.tags) | ||
| return m |
There was a problem hiding this comment.
Clone.
| m := make(map[string]string, len(s.tags)) | |
| for k, v := range s.tags { | |
| m[k] = v | |
| } | |
| maps.Copy(m, s.tags) | |
| return m | |
| return maps.Clone(s.tags) |
| m := make(map[string]string, len(s.tags)) | ||
| for k, v := range s.tags { | ||
| m[k] = v | ||
| } | ||
| maps.Copy(m, s.tags) | ||
| return m |
| tagsCopy := make(map[string]string, len(tags)) | ||
| for k, v := range tags { | ||
| tagsCopy[k] = v | ||
| } | ||
| maps.Copy(tagsCopy, tags) |
| fieldsCopy := make(map[string]any, len(fields)) | ||
| for k, v := range fields { | ||
| fieldsCopy[k] = v | ||
| } | ||
| maps.Copy(fieldsCopy, fields) |
|
@srebhan But |
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
f0eff5f to
4ae4fce
Compare
|
Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip. 📦 Click here to get additional PR build artifactsArtifact URLs |
Summary
Part of the modernize linter series (follow-up to #19675).
This PR replaces
for k, v := range src { dst[k] = v }loops withmaps.Copy(analyzer:mapsloop).The rewrite is the output of the modernize linter with only the listed analyzer(s) enabled, applied via
golangci-lint run --fixfor GOOS=darwin, linux and windows, followed bygoimports/golangci-lint fmtto drop imports left unused and regroup new ones.Checklist
Related issues
resolves #