Skip to content

Commit acfda87

Browse files
authored
refactor: use maps.Copy for cleaner map handling (#189)
There is a [new function](https://pkg.go.dev/maps@go1.21.1#Copy) added in the go1.21 standard library, which can make the code more concise and easy to read. Signed-off-by: jishudashu <979260390@qq.com>
1 parent ffb3e75 commit acfda87

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

playground/artifacts.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"log"
14+
"maps"
1415
"math/big"
1516
"os"
1617
"path/filepath"
@@ -201,9 +202,7 @@ func (b *ArtifactsBuilder) Build() (*Artifacts, error) {
201202
if err := json.Unmarshal(contents, &alloc); err != nil {
202203
return nil, fmt.Errorf("failed to unmarshal opState: %w", err)
203204
}
204-
for addr, account := range alloc {
205-
gen.Alloc[addr] = account
206-
}
205+
maps.Copy(gen.Alloc, alloc)
207206
}
208207

209208
block := gen.ToBlock()

playground/local_runner.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"maps"
78
"net"
89
"os"
910
"os/exec"
@@ -134,9 +135,7 @@ func NewLocalRunner(cfg *RunnerConfig) (*LocalRunner, error) {
134135
if cfg.Overrides == nil {
135136
cfg.Overrides = make(map[string]string)
136137
}
137-
for k, v := range cfg.Manifest.overrides {
138-
cfg.Overrides[k] = v
139-
}
138+
maps.Copy(cfg.Overrides, cfg.Manifest.overrides)
140139

141140
// Create the concrete instances to run
142141
instances := []*instance{}
@@ -627,9 +626,7 @@ func (d *LocalRunner) toDockerComposeService(s *Service) (map[string]interface{}
627626
}
628627

629628
// apply the user defined labels
630-
for k, v := range d.labels {
631-
labels[k] = v
632-
}
629+
maps.Copy(labels, d.labels)
633630

634631
// add the local ports exposed by the service as labels
635632
// we have to do this for now since we do not store the manifest in JSON yet.

0 commit comments

Comments
 (0)