Skip to content

Commit 08e9a9f

Browse files
committed
use slices.DeleteFunc, and some minor fixes
1 parent bf03a38 commit 08e9a9f

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ require (
3939
github.com/stretchr/testify v1.8.2
4040
github.com/wk8/go-ordered-map/v2 v2.1.8
4141
github.com/zealic/go2node v0.1.0
42-
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
43-
golang.org/x/mod v0.9.0
42+
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de
43+
golang.org/x/mod v0.11.0
4444
gopkg.in/natefinch/lumberjack.v2 v2.2.1
4545
gopkg.in/yaml.v3 v3.0.1
4646
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036/go.mod h1:gqRgreBU
223223
github.com/zaffka/mongodb-boltdb-mock v0.0.0-20221014194232-b4bb03fbe3a0/go.mod h1:GsDD1qsG+86MeeCG7ndi6Ei3iGthKL3wQ7PTFigDfNY=
224224
github.com/zealic/go2node v0.1.0 h1:ofxpve08cmLJBwFdI0lPCk9jfwGWOSD+s6216x0oAaA=
225225
github.com/zealic/go2node v0.1.0/go.mod h1:GrkFr+HctXwP7vzcU9RsgtAeJjTQ6Ud0IPCQAqpTfBg=
226-
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
227-
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
228-
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
229-
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
226+
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de h1:l5Za6utMv/HsBWWqzt4S8X17j+kt1uVETUX5UFhn2rE=
227+
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
228+
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
229+
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
230230
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
231231
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
232232
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

internal/devconfig/packages.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/pkg/errors"
88
orderedmap "github.com/wk8/go-ordered-map/v2"
9+
"golang.org/x/exp/slices"
910
)
1011

1112
type jsonKind int
@@ -33,7 +34,7 @@ type Packages struct {
3334
// example:
3435
// ["package1", "package2@latest", "[email protected]"]
3536
func (pkgs *Packages) VersionedNames() []string {
36-
result := []string{}
37+
result := make([]string, 0, len(pkgs.Collection))
3738
for _, p := range pkgs.Collection {
3839
name := p.name
3940
if p.Version != "" {
@@ -51,15 +52,11 @@ func (pkgs *Packages) Add(versionedName string) {
5152
}
5253

5354
// Remove removes a package from the list of packages
54-
func (pkgs *Packages) Remove(versionedName string) error {
55+
func (pkgs *Packages) Remove(versionedName string) {
5556
name, version := parseVersionedName(versionedName)
56-
for idx, pkg := range pkgs.Collection {
57-
if pkg.name == name && pkg.Version == version {
58-
pkgs.Collection = append(pkgs.Collection[:idx], pkgs.Collection[idx+1:]...)
59-
return nil
60-
}
61-
}
62-
return errors.Errorf("package %s not found", versionedName)
57+
pkgs.Collection = slices.DeleteFunc(pkgs.Collection, func(pkg Package) bool {
58+
return pkg.name == name && pkg.Version == version
59+
})
6360
}
6461

6562
func (pkgs *Packages) UnmarshalJSON(data []byte) error {
@@ -97,7 +94,7 @@ func (pkgs *Packages) UnmarshalJSON(data []byte) error {
9794

9895
func (pkgs *Packages) MarshalJSON() ([]byte, error) {
9996
if pkgs.jsonKind == jsonList {
100-
packagesList := []string{}
97+
packagesList := make([]string, 0, len(pkgs.Collection))
10198
for _, p := range pkgs.Collection {
10299

103100
// Version may be empty for unversioned packages
@@ -167,15 +164,14 @@ func (p *Package) UnmarshalJSON(data []byte) error {
167164
}
168165

169166
// Second, attempt to unmarshal as a Package struct
170-
type Alias Package // Use an alias-type to avoid infinite recursion
171-
alias := &Alias{}
167+
type packageAlias Package // Use an alias-type to avoid infinite recursion
168+
alias := &packageAlias{}
172169
if err := json.Unmarshal(data, alias); err != nil {
173170
return errors.WithStack(err)
174171
}
175172

176-
// more robust way to copy all fields from alias?
173+
*p = Package(*alias)
177174
p.kind = regular
178-
p.Version = alias.Version
179175
return nil
180176
}
181177

@@ -185,8 +181,8 @@ func (p Package) MarshalJSON() ([]byte, error) {
185181
}
186182

187183
// If we have a regular package, we want to marshal the entire struct:
188-
type Alias Package // Use an alias-type to avoid infinite recursion
189-
return json.Marshal((Alias)(p))
184+
type packageAlias Package // Use an alias-type to avoid infinite recursion
185+
return json.Marshal((packageAlias)(p))
190186
}
191187

192188
// parseVersionedName parses the name and version from package@version representation

internal/impl/packages.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ func (d *Devbox) Remove(ctx context.Context, pkgs ...string) error {
136136
found, _ := d.findPackageByName(pkg)
137137
if found != "" {
138138
packagesToUninstall = append(packagesToUninstall, found)
139-
if err := d.cfg.Packages.Remove(found); err != nil {
140-
return errors.WithStack(err)
141-
}
139+
d.cfg.Packages.Remove(found)
142140
} else {
143141
missingPkgs = append(missingPkgs, pkg)
144142
}

0 commit comments

Comments
 (0)