Skip to content

Commit ec2f4e2

Browse files
authored
Merge pull request moby#5030 from tonistiigi/slices-maps
refactor with slices and maps pkg
2 parents f529bae + dfc3527 commit ec2f4e2

File tree

26 files changed

+81
-147
lines changed

26 files changed

+81
-147
lines changed

cache/blobs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cache
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"os"
78
"strconv"
89

@@ -232,9 +233,7 @@ func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool
232233
if err != nil {
233234
return nil, errors.Wrapf(err, "failed to finalize compression")
234235
}
235-
for k, v := range a {
236-
desc.Annotations[k] = v
237-
}
236+
maps.Copy(desc.Annotations, a)
238237
}
239238
info, err := sr.cm.ContentStore.Info(ctx, desc.Digest)
240239
if err != nil {

cache/refs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cache
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"os"
78
"path/filepath"
89
"strings"
@@ -727,9 +728,7 @@ func (sr *immutableRef) ociDesc(ctx context.Context, dhs DescHandlers, preferNon
727728
}
728729
} else if dh, ok := dhs[desc.Digest]; ok {
729730
// No blob metadtata is stored in the content store. Try to get annotations from desc handlers.
730-
for k, v := range filterAnnotationsForSave(dh.Annotations) {
731-
desc.Annotations[k] = v
732-
}
731+
maps.Copy(desc.Annotations, filterAnnotationsForSave(dh.Annotations))
733732
}
734733

735734
diffID := sr.getDiffID()

cache/remote.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cache
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"net/url"
78
"strings"
89

@@ -235,9 +236,7 @@ func (sr *immutableRef) getRemote(ctx context.Context, createIfNeeded bool, refC
235236
for _, k := range addAnnotations {
236237
newDesc.Annotations[k] = desc.Annotations[k]
237238
}
238-
for k, v := range blobDesc.Annotations {
239-
newDesc.Annotations[k] = v
240-
}
239+
maps.Copy(newDesc.Annotations, blobDesc.Annotations)
241240
desc = newDesc
242241
}
243242
}

cache/remotecache/inline/inline.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package inline
33
import (
44
"context"
55
"encoding/json"
6+
"slices"
67

78
"github.com/containerd/containerd/labels"
89
"github.com/moby/buildkit/cache/remotecache"
@@ -167,8 +168,6 @@ func layerToBlobs(idx int, layers []v1.CacheLayer) []digest.Digest {
167168
idx = layer.ParentIndex
168169
}
169170
// reverse so they go lowest to highest
170-
for i, j := 0, len(ds)-1; i < j; i, j = i+1, j-1 {
171-
ds[i], ds[j] = ds[j], ds[i]
172-
}
171+
slices.Reverse(ds)
173172
return ds
174173
}

cache/remotecache/registry/registry.go

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

33
import (
44
"context"
5+
"maps"
56
"strconv"
67

78
"github.com/containerd/containerd/content"
@@ -163,9 +164,7 @@ func (dsl *withDistributionSourceLabel) SnapshotLabels(descs []ocispecs.Descript
163164
if labels == nil {
164165
labels = make(map[string]string)
165166
}
166-
for k, v := range estargz.SnapshotLabels(dsl.ref, descs, index) {
167-
labels[k] = v
168-
}
167+
maps.Copy(labels, estargz.SnapshotLabels(dsl.ref, descs, index))
169168
return labels
170169
}
171170

client/build.go

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

33
import (
44
"context"
5+
"maps"
56

67
"github.com/moby/buildkit/client/buildid"
78
gateway "github.com/moby/buildkit/frontend/gateway/client"
@@ -42,12 +43,10 @@ func (c *Client) Build(ctx context.Context, opt SolveOpt, product string, buildF
4243
}
4344

4445
cb := func(ref string, s *session.Session, opts map[string]string) error {
45-
for k, v := range opts {
46-
if feOpts == nil {
47-
feOpts = map[string]string{}
48-
}
49-
feOpts[k] = v
46+
if feOpts == nil {
47+
feOpts = map[string]string{}
5048
}
49+
maps.Copy(feOpts, opts)
5150
gwClient := c.gatewayClientForBuild(ref)
5251
g, err := grpcclient.New(ctx, feOpts, s.ID(), product, gwClient, gworkers)
5352
if err != nil {

client/llb/marshal.go

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

33
import (
44
"io"
5+
"maps"
56

67
"github.com/containerd/containerd/platforms"
78
"github.com/moby/buildkit/solver/pb"
@@ -18,24 +19,17 @@ type Definition struct {
1819
}
1920

2021
func (def *Definition) ToPB() *pb.Definition {
21-
md := make(map[digest.Digest]pb.OpMetadata, len(def.Metadata))
22-
for k, v := range def.Metadata {
23-
md[k] = v
24-
}
2522
return &pb.Definition{
2623
Def: def.Def,
2724
Source: def.Source,
28-
Metadata: md,
25+
Metadata: maps.Clone(def.Metadata),
2926
}
3027
}
3128

3229
func (def *Definition) FromPB(x *pb.Definition) {
3330
def.Def = x.Def
3431
def.Source = x.Source
35-
def.Metadata = make(map[digest.Digest]pb.OpMetadata)
36-
for k, v := range x.Metadata {
37-
def.Metadata[k] = v
38-
}
32+
def.Metadata = maps.Clone(x.Metadata)
3933
}
4034

4135
func (def *Definition) Head() (digest.Digest, error) {

client/llb/state.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"maps"
78
"net"
89
"strings"
910

@@ -566,9 +567,7 @@ func mergeMetadata(m1, m2 pb.OpMetadata) pb.OpMetadata {
566567
if m1.Description == nil {
567568
m1.Description = make(map[string]string)
568569
}
569-
for k, v := range m2.Description {
570-
m1.Description[k] = v
571-
}
570+
maps.Copy(m1.Description, m2.Description)
572571
}
573572
if m2.ExportCache != nil {
574573
m1.ExportCache = m2.ExportCache
@@ -597,9 +596,7 @@ func WithDescription(m map[string]string) ConstraintsOpt {
597596
if c.Metadata.Description == nil {
598597
c.Metadata.Description = map[string]string{}
599598
}
600-
for k, v := range m {
601-
c.Metadata.Description[k] = v
602-
}
599+
maps.Copy(c.Metadata.Description, m)
603600
})
604601
}
605602

client/solve.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"io"
8+
"maps"
89
"os"
910
"path/filepath"
1011
"strings"
@@ -219,13 +220,8 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
219220
})
220221
}
221222

222-
frontendAttrs := map[string]string{}
223-
for k, v := range opt.FrontendAttrs {
224-
frontendAttrs[k] = v
225-
}
226-
for k, v := range cacheOpt.frontendAttrs {
227-
frontendAttrs[k] = v
228-
}
223+
frontendAttrs := maps.Clone(opt.FrontendAttrs)
224+
maps.Copy(frontendAttrs, cacheOpt.frontendAttrs)
229225

230226
solveCtx, cancelSolve := context.WithCancelCause(ctx)
231227
var res *SolveResponse

cmd/buildctl/build/opt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package build
22

3+
import "maps"
4+
35
func ParseOpt(opts []string) (map[string]string, error) {
46
m := loadOptEnv()
57
m2, err := attrMap(opts)
68
if err != nil {
79
return nil, err
810
}
9-
for k, v := range m2 {
10-
m[k] = v
11-
}
11+
maps.Copy(m, m2)
1212
return m, nil
1313
}

0 commit comments

Comments
 (0)