From f0ccb937d3fdf8d76f420357fba21e571124d819 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 16 Aug 2026 16:29:08 +0200 Subject: [PATCH] Use lo.Map instead of manual append loops Not only is this nicer code (and more idiomatic at least in this code base), but it also avoids linter warnings about missing preallocations (lo.Map does preallocate the result array). --- pkg/gocui/view_test.go | 13 ++++--------- pkg/integration/components/env.go | 10 +++++----- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/pkg/gocui/view_test.go b/pkg/gocui/view_test.go index 294f02b32..2ee5eb4b8 100644 --- a/pkg/gocui/view_test.go +++ b/pkg/gocui/view_test.go @@ -11,6 +11,7 @@ import ( "github.com/gdamore/tcell/v3" "github.com/gdamore/tcell/v3/color" "github.com/rivo/uniseg" + "github.com/samber/lo" "github.com/stretchr/testify/assert" ) @@ -106,10 +107,8 @@ func TestWriteString(t *testing.T) { for _, s := range test.stringsToWrite { v.writeString(s) } - var resultingLines [][]string - for _, l := range v.buf.lines { - resultingLines = append(resultingLines, cellsToStrings(l.cells)) - } + resultingLines := lo.Map(v.buf.lines, + func(l lineType, _ int) []string { return cellsToStrings(l.cells) }) assert.Equal(t, test.expectedLines, resultingLines) } } @@ -465,11 +464,7 @@ func cellsToString(cells []cell) string { } func cellsToStrings(cells []cell) []string { - s := []string{} - for _, c := range cells { - s = append(s, c.chr) - } - return s + return lo.Map(cells, func(c cell, _ int) string { return c.chr }) } func TestLineWrap(t *testing.T) { diff --git a/pkg/integration/components/env.go b/pkg/integration/components/env.go index 6306a88ba..39152092e 100644 --- a/pkg/integration/components/env.go +++ b/pkg/integration/components/env.go @@ -3,6 +3,8 @@ package components import ( "fmt" "os" + + "github.com/samber/lo" ) const ( @@ -43,11 +45,9 @@ var hostEnvironmentAllowlist = [...]string{ // Returns a copy of the environment filtered by // hostEnvironmentAllowlist func allowedHostEnvironment() []string { - env := []string{} - for _, envVar := range hostEnvironmentAllowlist { - env = append(env, fmt.Sprintf("%s=%s", envVar, os.Getenv(envVar))) - } - return env + return lo.Map(hostEnvironmentAllowlist[:], func(envVar string, _ int) string { + return fmt.Sprintf("%s=%s", envVar, os.Getenv(envVar)) + }) } func NewTestEnvironment(rootDir string) []string {