Skip to content

Commit db28780

Browse files
committed
cli/compose/convert: convertUlimits: modernize
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 44ca067 commit db28780

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

cli/compose/convert/service.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
package convert
25

36
import (
7+
"cmp"
48
"context"
59
"errors"
610
"fmt"
711
"net/netip"
812
"os"
13+
"slices"
914
"sort"
1015
"strings"
1116
"time"
@@ -702,28 +707,22 @@ func convertCredentialSpec(namespace Namespace, spec composetypes.CredentialSpec
702707
}
703708

704709
func convertUlimits(origUlimits map[string]*composetypes.UlimitsConfig) []*container.Ulimit {
705-
newUlimits := make(map[string]*container.Ulimit)
710+
ulimits := make([]*container.Ulimit, 0, len(origUlimits))
706711
for name, u := range origUlimits {
712+
soft, hard := int64(u.Soft), int64(u.Hard)
707713
if u.Single != 0 {
708-
newUlimits[name] = &container.Ulimit{
709-
Name: name,
710-
Soft: int64(u.Single),
711-
Hard: int64(u.Single),
712-
}
713-
} else {
714-
newUlimits[name] = &container.Ulimit{
715-
Name: name,
716-
Soft: int64(u.Soft),
717-
Hard: int64(u.Hard),
718-
}
714+
soft, hard = int64(u.Single), int64(u.Single)
719715
}
716+
717+
ulimits = append(ulimits, &container.Ulimit{
718+
Name: name,
719+
Soft: soft,
720+
Hard: hard,
721+
})
720722
}
721-
ulimits := make([]*container.Ulimit, 0, len(newUlimits))
722-
for _, ulimit := range newUlimits {
723-
ulimits = append(ulimits, ulimit)
724-
}
725-
sort.SliceStable(ulimits, func(i, j int) bool {
726-
return ulimits[i].Name < ulimits[j].Name
723+
724+
slices.SortFunc(ulimits, func(a, b *container.Ulimit) int {
725+
return cmp.Compare(a.Name, b.Name)
727726
})
728727
return ulimits
729728
}

0 commit comments

Comments
 (0)