Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate golang.org/x/exp to stds #736

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonschema v1.2.0
github.com/xhit/go-str2duration/v2 v2.1.0
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
golang.org/x/sync v0.3.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.4.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu+kQvCqcsoVaQgSV60o=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
2 changes: 1 addition & 1 deletion graph/cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package graph

import (
"fmt"
"slices"
"strings"

"github.com/compose-spec/compose-go/v2/types"
"github.com/compose-spec/compose-go/v2/utils"
"golang.org/x/exp/slices"
)

// CheckCycle analyze project's depends_on relation and report an error on cycle detection
Expand Down
2 changes: 1 addition & 1 deletion graph/traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package graph

import (
"context"
"slices"
"sync"

"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
)

Expand Down
2 changes: 1 addition & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"slices"
"strconv"
"strings"

Expand All @@ -42,7 +43,6 @@ import (
"github.com/compose-spec/compose-go/v2/validation"
"github.com/go-viper/mapstructure/v2"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)

Expand Down
2 changes: 1 addition & 1 deletion override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package override
import (
"cmp"
"fmt"
"slices"
"strings"

"github.com/compose-spec/compose-go/v2/tree"
"golang.org/x/exp/slices"
)

// Merge applies overrides to a config model
Expand Down
9 changes: 5 additions & 4 deletions types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"sort"

"github.com/compose-spec/compose-go/v2/dotenv"
"github.com/compose-spec/compose-go/v2/errdefs"
"github.com/compose-spec/compose-go/v2/utils"
"github.com/distribution/reference"
godigest "github.com/opencontainers/go-digest"
"golang.org/x/exp/maps"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -120,21 +121,21 @@ func (p *Project) ServicesWithBuild() []string {
servicesBuild := p.Services.Filter(func(s ServiceConfig) bool {
return s.Build != nil && s.Build.Context != ""
})
return maps.Keys(servicesBuild)
return slices.AppendSeq(make([]string, 0), maps.Keys(servicesBuild))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slices.Collect will return nil which break testing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is AppendSeq required here, as maps.Keys already return a []string?

Copy link
Author

@morlay morlay Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it returns iter.Seq[string]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok got it. Then I don't understand you don't use slices.Collect which does the exact same afaict (https://github.com/golang/go/blob/go1.23.0/src/slices/iter.go#L56-L59)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/golang/go/blob/go1.23.0/src/slices/iter.go#L58
cause it nil slice as defaults, which will break the testings,
and i have no idea to update testings.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing uses assert.DeepEqual(t, []string{}, p.ServicesWithBuild()), you can just switch to assert.Equal(t, 0, len(p.ServicesWithBuild()) as nil and empty slice should be considered equivalent

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in #744

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

func (p *Project) ServicesWithExtends() []string {
servicesExtends := p.Services.Filter(func(s ServiceConfig) bool {
return s.Extends != nil && *s.Extends != (ExtendsConfig{})
})
return maps.Keys(servicesExtends)
return slices.AppendSeq(make([]string, 0), maps.Keys(servicesExtends))
}

func (p *Project) ServicesWithDependsOn() []string {
servicesDependsOn := p.Services.Filter(func(s ServiceConfig) bool {
return len(s.DependsOn) > 0
})
return maps.Keys(servicesDependsOn)
return slices.AppendSeq(make([]string, 0), maps.Keys(servicesDependsOn))
}

func (p *Project) ServicesWithCapabilities() ([]string, []string, []string) {
Expand Down
2 changes: 1 addition & 1 deletion types/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
_ "crypto/sha256"
"errors"
"fmt"
"slices"
"strings"
"testing"

"github.com/compose-spec/compose-go/v2/utils"
"github.com/distribution/reference"
"github.com/opencontainers/go-digest"
"golang.org/x/exp/slices"
"gotest.tools/v3/assert"
)

Expand Down
12 changes: 5 additions & 7 deletions utils/collectionutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package utils

import (
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"cmp"
"maps"
"slices"
)

func MapKeys[T constraints.Ordered, U any](theMap map[T]U) []T {
result := maps.Keys(theMap)
slices.Sort(result)
return result
func MapKeys[T cmp.Ordered, U any](theMap map[T]U) []T {
return slices.Sorted(maps.Keys(theMap))
}

func MapsAppend[T comparable, U any](target map[T]U, source map[T]U) map[T]U {
Expand Down