Skip to content

Commit 0beb0ad

Browse files
authored
Merge pull request #3287 from alexandear/refactor/slices-concat
refactor: Simplify slice concatenation with slices.Concat
2 parents b85a356 + 3ce95d1 commit 0beb0ad

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

pkg/limayaml/defaults.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
284284
}
285285
y.Arch = ptr.Of(ResolveArch(y.Arch))
286286

287-
y.Images = append(append(o.Images, y.Images...), d.Images...)
287+
y.Images = slices.Concat(o.Images, y.Images, d.Images)
288288
for i := range y.Images {
289289
img := &y.Images[i]
290290
if img.Arch == "" {
@@ -352,7 +352,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
352352
y.Disk = ptr.Of(defaultDiskSizeAsString())
353353
}
354354

355-
y.AdditionalDisks = append(append(o.AdditionalDisks, y.AdditionalDisks...), d.AdditionalDisks...)
355+
y.AdditionalDisks = slices.Concat(o.AdditionalDisks, y.AdditionalDisks, d.AdditionalDisks)
356356

357357
if y.Audio.Device == nil {
358358
y.Audio.Device = d.Audio.Device
@@ -394,7 +394,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
394394
y.Firmware.LegacyBIOS = ptr.Of(false)
395395
}
396396

397-
y.Firmware.Images = append(append(o.Firmware.Images, y.Firmware.Images...), d.Firmware.Images...)
397+
y.Firmware.Images = slices.Concat(o.Firmware.Images, y.Firmware.Images, d.Firmware.Images)
398398
for i := range y.Firmware.Images {
399399
f := &y.Firmware.Images[i]
400400
if f.Arch == "" {
@@ -475,7 +475,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
475475
}
476476
y.HostResolver.Hosts = hosts
477477

478-
y.Provision = append(append(o.Provision, y.Provision...), d.Provision...)
478+
y.Provision = slices.Concat(o.Provision, y.Provision, d.Provision)
479479
for i := range y.Provision {
480480
provision := &y.Provision[i]
481481
if provision.Mode == "" {
@@ -535,7 +535,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
535535
}
536536
}
537537

538-
y.Containerd.Archives = append(append(o.Containerd.Archives, y.Containerd.Archives...), d.Containerd.Archives...)
538+
y.Containerd.Archives = slices.Concat(o.Containerd.Archives, y.Containerd.Archives, d.Containerd.Archives)
539539
if len(y.Containerd.Archives) == 0 {
540540
y.Containerd.Archives = defaultContainerdArchives()
541541
}
@@ -546,7 +546,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
546546
}
547547
}
548548

549-
y.Probes = append(append(o.Probes, y.Probes...), d.Probes...)
549+
y.Probes = slices.Concat(o.Probes, y.Probes, d.Probes)
550550
for i := range y.Probes {
551551
probe := &y.Probes[i]
552552
if probe.Mode == "" {
@@ -562,13 +562,13 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
562562
}
563563
}
564564

565-
y.PortForwards = append(append(o.PortForwards, y.PortForwards...), d.PortForwards...)
565+
y.PortForwards = slices.Concat(o.PortForwards, y.PortForwards, d.PortForwards)
566566
for i := range y.PortForwards {
567567
FillPortForwardDefaults(&y.PortForwards[i], instDir, y.User, y.Param)
568568
// After defaults processing the singular HostPort and GuestPort values should not be used again.
569569
}
570570

571-
y.CopyToHost = append(append(o.CopyToHost, y.CopyToHost...), d.CopyToHost...)
571+
y.CopyToHost = slices.Concat(o.CopyToHost, y.CopyToHost, d.CopyToHost)
572572
for i := range y.CopyToHost {
573573
FillCopyToHostDefaults(&y.CopyToHost[i], instDir, y.User, y.Param)
574574
}
@@ -605,7 +605,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
605605

606606
networks := make([]Network, 0, len(d.Networks)+len(y.Networks)+len(o.Networks))
607607
iface := make(map[string]int)
608-
for _, nw := range append(append(d.Networks, y.Networks...), o.Networks...) {
608+
for _, nw := range slices.Concat(d.Networks, y.Networks, o.Networks) {
609609
if i, ok := iface[nw.Interface]; ok {
610610
if nw.Socket != "" {
611611
networks[i].Socket = nw.Socket
@@ -649,7 +649,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
649649
}
650650
}
651651

652-
y.MountTypesUnsupported = append(append(o.MountTypesUnsupported, y.MountTypesUnsupported...), d.MountTypesUnsupported...)
652+
y.MountTypesUnsupported = slices.Concat(o.MountTypesUnsupported, y.MountTypesUnsupported, d.MountTypesUnsupported)
653653
mountTypesUnsupported := make(map[string]struct{})
654654
for _, f := range y.MountTypesUnsupported {
655655
mountTypesUnsupported[f] = struct{}{}
@@ -698,7 +698,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
698698
// Only works for exact matches; does not normalize case or resolve symlinks.
699699
mounts := make([]Mount, 0, len(d.Mounts)+len(y.Mounts)+len(o.Mounts))
700700
location := make(map[string]int)
701-
for _, mount := range append(append(d.Mounts, y.Mounts...), o.Mounts...) {
701+
for _, mount := range slices.Concat(d.Mounts, y.Mounts, o.Mounts) {
702702
if out, err := executeHostTemplate(mount.Location, instDir, y.Param); err == nil {
703703
mount.Location = out.String()
704704
} else {
@@ -829,11 +829,8 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
829829
y.CACertificates.RemoveDefaults = ptr.Of(false)
830830
}
831831

832-
caFiles := unique(append(append(d.CACertificates.Files, y.CACertificates.Files...), o.CACertificates.Files...))
833-
y.CACertificates.Files = caFiles
834-
835-
caCerts := unique(append(append(d.CACertificates.Certs, y.CACertificates.Certs...), o.CACertificates.Certs...))
836-
y.CACertificates.Certs = caCerts
832+
y.CACertificates.Files = unique(slices.Concat(d.CACertificates.Files, y.CACertificates.Files, o.CACertificates.Files))
833+
y.CACertificates.Certs = unique(slices.Concat(d.CACertificates.Certs, y.CACertificates.Certs, o.CACertificates.Certs))
837834

838835
if runtime.GOOS == "darwin" && IsNativeArch(AARCH64) {
839836
if y.Rosetta.Enabled == nil {

pkg/limayaml/defaults_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -500,18 +500,18 @@ func TestFillDefault(t *testing.T) {
500500

501501
expect = y
502502

503-
expect.Provision = append(append([]Provision{}, y.Provision...), dExpect.Provision...)
504-
expect.Probes = append(append([]Probe{}, y.Probes...), dExpect.Probes...)
505-
expect.PortForwards = append(append([]PortForward{}, y.PortForwards...), dExpect.PortForwards...)
506-
expect.CopyToHost = append(append([]CopyToHost{}, y.CopyToHost...), dExpect.CopyToHost...)
507-
expect.Containerd.Archives = append(append([]File{}, y.Containerd.Archives...), dExpect.Containerd.Archives...)
503+
expect.Provision = slices.Concat(y.Provision, dExpect.Provision)
504+
expect.Probes = slices.Concat(y.Probes, dExpect.Probes)
505+
expect.PortForwards = slices.Concat(y.PortForwards, dExpect.PortForwards)
506+
expect.CopyToHost = slices.Concat(y.CopyToHost, dExpect.CopyToHost)
507+
expect.Containerd.Archives = slices.Concat(y.Containerd.Archives, dExpect.Containerd.Archives)
508508
expect.Containerd.Archives[2].Arch = *expect.Arch
509-
expect.AdditionalDisks = append(append([]Disk{}, y.AdditionalDisks...), dExpect.AdditionalDisks...)
510-
expect.Firmware.Images = append(append([]FileWithVMType{}, y.Firmware.Images...), dExpect.Firmware.Images...)
509+
expect.AdditionalDisks = slices.Concat(y.AdditionalDisks, dExpect.AdditionalDisks)
510+
expect.Firmware.Images = slices.Concat(y.Firmware.Images, dExpect.Firmware.Images)
511511

512512
// Mounts and Networks start with lowest priority first, so higher priority entries can overwrite
513-
expect.Mounts = append(append([]Mount{}, dExpect.Mounts...), y.Mounts...)
514-
expect.Networks = append(append([]Network{}, dExpect.Networks...), y.Networks...)
513+
expect.Mounts = slices.Concat(dExpect.Mounts, y.Mounts)
514+
expect.Networks = slices.Concat(dExpect.Networks, y.Networks)
515515

516516
expect.HostResolver.Hosts["default"] = dExpect.HostResolver.Hosts["default"]
517517

@@ -675,20 +675,20 @@ func TestFillDefault(t *testing.T) {
675675

676676
expect = o
677677

678-
expect.Provision = append(append(o.Provision, y.Provision...), dExpect.Provision...)
679-
expect.Probes = append(append(o.Probes, y.Probes...), dExpect.Probes...)
680-
expect.PortForwards = append(append(o.PortForwards, y.PortForwards...), dExpect.PortForwards...)
681-
expect.CopyToHost = append(append(o.CopyToHost, y.CopyToHost...), dExpect.CopyToHost...)
682-
expect.Containerd.Archives = append(append(o.Containerd.Archives, y.Containerd.Archives...), dExpect.Containerd.Archives...)
678+
expect.Provision = slices.Concat(o.Provision, y.Provision, dExpect.Provision)
679+
expect.Probes = slices.Concat(o.Probes, y.Probes, dExpect.Probes)
680+
expect.PortForwards = slices.Concat(o.PortForwards, y.PortForwards, dExpect.PortForwards)
681+
expect.CopyToHost = slices.Concat(o.CopyToHost, y.CopyToHost, dExpect.CopyToHost)
682+
expect.Containerd.Archives = slices.Concat(o.Containerd.Archives, y.Containerd.Archives, dExpect.Containerd.Archives)
683683
expect.Containerd.Archives[3].Arch = *expect.Arch
684-
expect.AdditionalDisks = append(append(o.AdditionalDisks, y.AdditionalDisks...), dExpect.AdditionalDisks...)
685-
expect.Firmware.Images = append(append(o.Firmware.Images, y.Firmware.Images...), dExpect.Firmware.Images...)
684+
expect.AdditionalDisks = slices.Concat(o.AdditionalDisks, y.AdditionalDisks, dExpect.AdditionalDisks)
685+
expect.Firmware.Images = slices.Concat(o.Firmware.Images, y.Firmware.Images, dExpect.Firmware.Images)
686686

687687
expect.HostResolver.Hosts["default"] = dExpect.HostResolver.Hosts["default"]
688688
expect.HostResolver.Hosts["MY.Host"] = dExpect.HostResolver.Hosts["host.lima.internal"]
689689

690690
// o.Mounts just makes dExpect.Mounts[0] writable because the Location matches
691-
expect.Mounts = append(append([]Mount{}, dExpect.Mounts...), y.Mounts...)
691+
expect.Mounts = slices.Concat(dExpect.Mounts, y.Mounts)
692692
expect.Mounts[0].Writable = ptr.Of(true)
693693
expect.Mounts[0].SSHFS.Cache = ptr.Of(false)
694694
expect.Mounts[0].SSHFS.FollowSymlinks = ptr.Of(true)
@@ -702,7 +702,7 @@ func TestFillDefault(t *testing.T) {
702702
expect.MountInotify = ptr.Of(true)
703703

704704
// o.Networks[1] is overriding the dExpect.Networks[0].Lima entry for the "def0" interface
705-
expect.Networks = append(append(dExpect.Networks, y.Networks...), o.Networks[0])
705+
expect.Networks = slices.Concat(dExpect.Networks, y.Networks, []Network{o.Networks[0]})
706706
expect.Networks[0].Lima = o.Networks[1].Lima
707707

708708
// Only highest prio DNS are retained

0 commit comments

Comments
 (0)