Skip to content

Commit bd70e01

Browse files
committed
Modernize the code
1. Replaces interface{} with any. 2. Use slices.Contains where appropriate. Generated by modernize -fix ./... Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent b01335e commit bd70e01

File tree

6 files changed

+28
-33
lines changed

6 files changed

+28
-33
lines changed

cgroups/cgroups.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"slices"
89
"strings"
910

1011
rspec "github.com/opencontainers/runtime-spec/specs-go"
@@ -89,10 +90,8 @@ func GetSubsystemPath(pid int, subsystem string) (string, error) {
8990
continue
9091
}
9192
subelems := strings.Split(elem[1], ",")
92-
for _, subelem := range subelems {
93-
if subelem == subsystem {
94-
return elem[2], nil
95-
}
93+
if slices.Contains(subelems, subsystem) {
94+
return elem[2], nil
9695
}
9796
}
9897

cmd/runtimetest/main.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ func (c *complianceTester) validateLinuxProcess(spec *rspec.Spec) error {
236236

237237
args := bytes.Split(bytes.Trim(cmdlineBytes, "\x00"), []byte("\x00"))
238238
c.harness.Ok(len(args) == len(spec.Process.Args), "has expected number of process arguments")
239-
_ = c.harness.YAML(map[string]interface{}{
239+
_ = c.harness.YAML(map[string]any{
240240
"expected": spec.Process.Args,
241241
"actual": args,
242242
})
243243
for i, a := range args {
244244
c.harness.Ok(string(a) == spec.Process.Args[i], fmt.Sprintf("has expected process argument %d", i))
245-
_ = c.harness.YAML(map[string]interface{}{
245+
_ = c.harness.YAML(map[string]any{
246246
"index": i,
247247
"expected": spec.Process.Args[i],
248248
"actual": string(a),
@@ -362,7 +362,7 @@ func (c *complianceTester) validateRlimits(spec *rspec.Spec) error {
362362
if err != nil {
363363
return err
364364
}
365-
_ = c.harness.YAML(map[string]interface{}{
365+
_ = c.harness.YAML(map[string]any{
366366
"level": rfcError.Level.String(),
367367
"reference": rfcError.Reference,
368368
"type": r.Type,
@@ -374,7 +374,7 @@ func (c *complianceTester) validateRlimits(spec *rspec.Spec) error {
374374
if err != nil {
375375
return err
376376
}
377-
_ = c.harness.YAML(map[string]interface{}{
377+
_ = c.harness.YAML(map[string]any{
378378
"level": rfcError.Level.String(),
379379
"reference": rfcError.Reference,
380380
"type": r.Type,
@@ -727,7 +727,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
727727
if err != nil {
728728
return err
729729
}
730-
_ = c.harness.YAML(map[string]interface{}{
730+
_ = c.harness.YAML(map[string]any{
731731
"level": rfcError.Level.String(),
732732
"reference": rfcError.Reference,
733733
"path": device.Path,
@@ -738,7 +738,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
738738
if err != nil {
739739
return err
740740
}
741-
_ = c.harness.YAML(map[string]interface{}{
741+
_ = c.harness.YAML(map[string]any{
742742
"level": rfcError.Level.String(),
743743
"reference": rfcError.Reference,
744744
"path": device.Path,
@@ -756,7 +756,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
756756
if err != nil {
757757
return err
758758
}
759-
_ = c.harness.YAML(map[string]interface{}{
759+
_ = c.harness.YAML(map[string]any{
760760
"level": rfcError.Level.String(),
761761
"reference": rfcError.Reference,
762762
"path": device.Path,
@@ -777,7 +777,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
777777
if err != nil {
778778
return err
779779
}
780-
_ = c.harness.YAML(map[string]interface{}{
780+
_ = c.harness.YAML(map[string]any{
781781
"level": rfcError.Level.String(),
782782
"reference": rfcError.Reference,
783783
"path": device.Path,
@@ -793,7 +793,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
793793
if err != nil {
794794
return err
795795
}
796-
_ = c.harness.YAML(map[string]interface{}{
796+
_ = c.harness.YAML(map[string]any{
797797
"level": rfcError.Level.String(),
798798
"reference": rfcError.Reference,
799799
"path": device.Path,
@@ -838,7 +838,7 @@ func (c *complianceTester) validateDefaultSymlinks(spec *rspec.Spec) error {
838838
if err != nil {
839839
return err
840840
}
841-
_ = c.harness.YAML(map[string]interface{}{
841+
_ = c.harness.YAML(map[string]any{
842842
"level": rfcError.Level.String(),
843843
"reference": rfcError.Reference,
844844
"path": symlink,
@@ -991,7 +991,7 @@ func (c *complianceTester) validateOOMScoreAdj(spec *rspec.Spec) error {
991991
if err != nil {
992992
return err
993993
}
994-
_ = c.harness.YAML(map[string]interface{}{
994+
_ = c.harness.YAML(map[string]any{
995995
"level": rfcError.Level.String(),
996996
"reference": rfcError.Reference,
997997
"expected": expected,
@@ -1052,7 +1052,7 @@ func (c *complianceTester) validateIDMappings(mappings []rspec.LinuxIDMapping, p
10521052
return err
10531053
}
10541054
c.harness.Ok(len(idMaps) == len(mappings), fmt.Sprintf("%s has expected number of mappings", path))
1055-
_ = c.harness.YAML(map[string]interface{}{
1055+
_ = c.harness.YAML(map[string]any{
10561056
"expected": mappings,
10571057
"actual": idMaps,
10581058
})
@@ -1185,13 +1185,13 @@ func (c *complianceTester) validatePosixMounts(spec *rspec.Spec) error {
11851185
} else {
11861186
rfcError, err = c.Ok(foundInOrder, specerror.MountsInOrder, spec.Version, fmt.Sprintf("mounts[%d] (%s) found in order", i, configMount.Destination))
11871187
}
1188-
_ = c.harness.YAML(map[string]interface{}{
1188+
_ = c.harness.YAML(map[string]any{
11891189
"level": rfcError.Level.String(),
11901190
"reference": rfcError.Reference,
11911191
"config": configMount,
11921192
"indexConfig": i,
11931193
"indexSystem": configSys[i],
1194-
"earlier": map[string]interface{}{
1194+
"earlier": map[string]any{
11951195
"config": spec.Mounts[highestMatchedConfig],
11961196
"indexConfig": highestMatchedConfig,
11971197
"indexSystem": configSys[highestMatchedConfig],

generate/generate.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"slices"
910
"strings"
1011

1112
"github.com/moby/sys/capability"
@@ -595,10 +596,8 @@ func (g *Generator) ClearProcessAdditionalGids() {
595596
// AddProcessAdditionalGid adds an additional gid into g.Config.Process.AdditionalGids.
596597
func (g *Generator) AddProcessAdditionalGid(gid uint32) {
597598
g.initConfigProcess()
598-
for _, group := range g.Config.Process.User.AdditionalGids {
599-
if group == gid {
600-
return
601-
}
599+
if slices.Contains(g.Config.Process.User.AdditionalGids, gid) {
600+
return
602601
}
603602
g.Config.Process.User.AdditionalGids = append(g.Config.Process.User.AdditionalGids, gid)
604603
}

validate/validate.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"regexp"
1313
"runtime"
14+
"slices"
1415
"strings"
1516
"unicode"
1617
"unicode/utf8"
@@ -719,17 +720,13 @@ func (v *Validator) rlimitValid(rlimit rspec.POSIXRlimit) (errs error) {
719720
}
720721

721722
if v.platform == "linux" {
722-
for _, val := range linuxRlimits {
723-
if val == rlimit.Type {
724-
return
725-
}
723+
if slices.Contains(linuxRlimits, rlimit.Type) {
724+
return
726725
}
727726
errs = multierror.Append(errs, specerror.NewError(specerror.PosixProcRlimitsTypeValueError, fmt.Errorf("rlimit type %q may not be valid", rlimit.Type), v.spec.Version))
728727
} else if v.platform == "solaris" {
729-
for _, val := range posixRlimits {
730-
if val == rlimit.Type {
731-
return
732-
}
728+
if slices.Contains(posixRlimits, rlimit.Type) {
729+
return
733730
}
734731
errs = multierror.Append(errs, specerror.NewError(specerror.PosixProcRlimitsTypeValueError, fmt.Errorf("rlimit type %q may not be valid", rlimit.Type), v.spec.Version))
735732
} else {
@@ -787,7 +784,7 @@ func checkMandatoryUnit(field reflect.Value, tagField reflect.StructField, paren
787784
return
788785
}
789786

790-
func checkMandatory(obj interface{}) (errs error) {
787+
func checkMandatory(obj any) (errs error) {
791788
objT := reflect.TypeOf(obj)
792789
objV := reflect.ValueOf(obj)
793790
if isStructPtr(objT) {

validation/misc_props/misc_props.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/opencontainers/runtime-tools/validation/util"
1515
)
1616

17-
func saveConfig(path string, v interface{}) error {
17+
func saveConfig(path string, v any) error {
1818
data, err := json.Marshal(v)
1919
if err != nil {
2020
return err

validation/util/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func Fatal(err error) {
9292
}
9393

9494
// Skip skips a full TAP suite.
95-
func Skip(message string, diagnostic interface{}) {
95+
func Skip(message string, diagnostic any) {
9696
t := tap.New()
9797
t.Header(1)
9898
t.Skip(1, message)

0 commit comments

Comments
 (0)