From 45cc8688a1c6a85665efb70ebf63ef7a3eb53213 Mon Sep 17 00:00:00 2001 From: Dmitriy Matrenichev Date: Mon, 5 Aug 2024 14:34:21 +0300 Subject: [PATCH] chore: replace `if` blocks with `min`/`max` functions Simplify code where possible. Signed-off-by: Dmitriy Matrenichev --- hack/gotagsrewrite/main.go | 4 +--- .../internal/server/v1alpha1/v1alpha1_monitoring.go | 4 +--- .../machined/internal/server/v1alpha1/v1alpha1_server.go | 5 +---- internal/app/machined/pkg/adapters/perf/cpu.go | 4 +--- .../machined/pkg/controllers/network/operator/dhcp4.go | 4 +--- .../machined/pkg/controllers/network/operator/dhcp6.go | 4 +--- .../app/machined/pkg/runtime/v1alpha1/v1alpha1_events.go | 9 ++------- internal/pkg/containers/cri/cri.go | 6 +----- internal/pkg/dashboard/components/components.go | 8 +++----- internal/pkg/dashboard/components/graphs.go | 4 +--- 10 files changed, 13 insertions(+), 39 deletions(-) diff --git a/hack/gotagsrewrite/main.go b/hack/gotagsrewrite/main.go index 503aa68fcf..e98c13b388 100644 --- a/hack/gotagsrewrite/main.go +++ b/hack/gotagsrewrite/main.go @@ -194,9 +194,7 @@ func findHighestProtoNum(structNode *ast.StructType) (int, error) { return nil, err } - if num > highestNum { - highestNum = num - } + highestNum = max(highestNum, num) return nil, nil }) diff --git a/internal/app/machined/internal/server/v1alpha1/v1alpha1_monitoring.go b/internal/app/machined/internal/server/v1alpha1/v1alpha1_monitoring.go index 11650e4510..9adec22860 100644 --- a/internal/app/machined/internal/server/v1alpha1/v1alpha1_monitoring.go +++ b/internal/app/machined/internal/server/v1alpha1/v1alpha1_monitoring.go @@ -93,9 +93,7 @@ func (s *Server) SystemStat(ctx context.Context, in *emptypb.Empty) (*machine.Sy maxCore := int64(-1) for core := range in { - if core > maxCore { - maxCore = core - } + maxCore = max(maxCore, core) } slc := make([]*machine.CPUStat, maxCore+1) diff --git a/internal/app/machined/internal/server/v1alpha1/v1alpha1_server.go b/internal/app/machined/internal/server/v1alpha1/v1alpha1_server.go index 0cd85df15c..81933aa3ef 100644 --- a/internal/app/machined/internal/server/v1alpha1/v1alpha1_server.go +++ b/internal/app/machined/internal/server/v1alpha1/v1alpha1_server.go @@ -973,10 +973,7 @@ func (s *Server) DiskUsage(req *machine.DiskUsageRequest, obj machine.MachineSer } else { currentDepth := int32(strings.Count(fi.FullPath, archiver.OSPathSeparator)) - rootDepth - size := fi.FileInfo.Size() - if size < 0 { - size = 0 - } + size := max(fi.FileInfo.Size(), 0) // kcore file size gives wrong value, this code should be smarter when it reads it // TODO: figure out better way to skip such file diff --git a/internal/app/machined/pkg/adapters/perf/cpu.go b/internal/app/machined/pkg/adapters/perf/cpu.go index 886d329f2a..7388779b72 100644 --- a/internal/app/machined/pkg/adapters/perf/cpu.go +++ b/internal/app/machined/pkg/adapters/perf/cpu.go @@ -44,9 +44,7 @@ func (a cpu) Update(stat *procfs.Stat) { maxCore := int64(-1) for core := range in { - if core > maxCore { - maxCore = core - } + maxCore = max(maxCore, core) } slc := make([]perf.CPUStat, maxCore+1) diff --git a/internal/app/machined/pkg/controllers/network/operator/dhcp4.go b/internal/app/machined/pkg/controllers/network/operator/dhcp4.go index 4b885d6d56..ba26fc536b 100644 --- a/internal/app/machined/pkg/controllers/network/operator/dhcp4.go +++ b/internal/app/machined/pkg/controllers/network/operator/dhcp4.go @@ -182,9 +182,7 @@ func (d *DHCP4) Run(ctx context.Context, notifyCh chan<- struct{}) { renewInterval /= 2 } - if renewInterval < minRenewDuration { - renewInterval = minRenewDuration - } + renewInterval = max(renewInterval, minRenewDuration) for { select { diff --git a/internal/app/machined/pkg/controllers/network/operator/dhcp6.go b/internal/app/machined/pkg/controllers/network/operator/dhcp6.go index e1d8d68a12..e59cb49674 100644 --- a/internal/app/machined/pkg/controllers/network/operator/dhcp6.go +++ b/internal/app/machined/pkg/controllers/network/operator/dhcp6.go @@ -95,9 +95,7 @@ func (d *DHCP6) Run(ctx context.Context, notifyCh chan<- struct{}) { renewInterval /= 2 } - if renewInterval < minRenewDuration { - renewInterval = minRenewDuration - } + renewInterval = max(renewInterval, minRenewDuration) select { case <-ctx.Done(): diff --git a/internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_events.go b/internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_events.go index 2ad6795a98..00f4b3e18d 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_events.go +++ b/internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_events.go @@ -127,10 +127,7 @@ func (e *Events) Watch(f runtime.WatchFunc, opt ...runtime.WatchOptionFunc) erro // event to be published pos := e.writePos minPos := e.writePos - int64(e.cap-e.gap) - - if minPos < 0 { - minPos = 0 - } + minPos = max(minPos, 0) // calculate initial position based on options switch { @@ -140,9 +137,7 @@ func (e *Events) Watch(f runtime.WatchFunc, opt ...runtime.WatchOptionFunc) erro } else { pos -= int64(opts.TailEvents) - if pos < minPos { - pos = minPos - } + pos = max(pos, minPos) } case !opts.TailID.IsNil(): pos = minPos + int64(sort.Search(int(pos-minPos), func(i int) bool { diff --git a/internal/pkg/containers/cri/cri.go b/internal/pkg/containers/cri/cri.go index c07e1b8507..d0972dacb3 100644 --- a/internal/pkg/containers/cri/cri.go +++ b/internal/pkg/containers/cri/cri.go @@ -286,11 +286,7 @@ func (i *inspector) buildContainer(container *runtimeapi.Container) (*ctrs.Conta } func safeCut(id string, i int) string { - if len(id) > i { - return id[:i] - } - - return id + return id[:min(i, len(id))] } // Pods collects information about running pods & containers. diff --git a/internal/pkg/dashboard/components/components.go b/internal/pkg/dashboard/components/components.go index 018209cd27..bf4b3b3903 100644 --- a/internal/pkg/dashboard/components/components.go +++ b/internal/pkg/dashboard/components/components.go @@ -46,15 +46,13 @@ func (fg *fieldGroup) String() string { } func (fg *fieldGroup) maxFieldNameLength() int { - max := 0 + result := 0 for _, f := range fg.fields { - if len(f.Name) > max { - max = len(f.Name) - } + result = max(result, len(f.Name)) } - return max + return result } // padRight pads a string to the specified width by appending spaces to the end. diff --git a/internal/pkg/dashboard/components/graphs.go b/internal/pkg/dashboard/components/graphs.go index c006894a1f..301b393622 100644 --- a/internal/pkg/dashboard/components/graphs.go +++ b/internal/pkg/dashboard/components/graphs.go @@ -49,9 +49,7 @@ func (widget *BaseGraph) OnAPIDataChange(node string, data *apidata.Data) { for i, name := range widget.DataLabels { series := nodeData.Series[name] - if len(series) < width { - width = len(series) - } + width = min(width, len(series)) widget.Data[i] = widget.leftPadSeries(series[len(series)-width:], 2) }