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

Add slicesextended and syncextended #2568

Merged
merged 4 commits into from
Nov 10, 2023
Merged
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
4 changes: 2 additions & 2 deletions private/buf/bufsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/storage/storagegit"
"github.com/bufbuild/buf/private/pkg/stringutil"
"go.uber.org/multierr"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -181,7 +181,7 @@ func (s *syncer) prepareSync(ctx context.Context) error {
var branchesToSync []string
if s.syncAllBranches {
// sync all branches
branchesToSync = stringutil.MapToSlice(allBranches)
branchesToSync = slicesextended.MapToSlice(allBranches)
} else {
// sync current branch, make sure it's present
currentBranch, err := s.repo.CurrentBranch(ctx)
Expand Down
6 changes: 3 additions & 3 deletions private/buf/bufwire/module_config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/storage/storageos"
"github.com/bufbuild/buf/private/pkg/stringutil"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -268,8 +268,8 @@ func (m *moduleConfigReader) getProtoFileModuleSourceConfigSet(
if err != nil {
return nil, err
}
workspaceConfigs := stringutil.SliceToMap(bufwork.AllConfigFilePaths)
moduleConfigs := stringutil.SliceToMap(bufconfig.AllConfigFilePaths)
workspaceConfigs := slicesextended.ToMap(bufwork.AllConfigFilePaths)
moduleConfigs := slicesextended.ToMap(bufconfig.AllConfigFilePaths)
terminateFileProvider := readBucketCloser.TerminateFileProvider()
var workspaceConfigDirectory string
var moduleConfigDirectory string
Expand Down
4 changes: 2 additions & 2 deletions private/buf/bufwork/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"sort"

"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/slicesextended"
)

func newConfigV1(externalConfig ExternalConfigV1, workspaceID string) (*Config, error) {
Expand Down Expand Up @@ -49,7 +49,7 @@ func newConfigV1(externalConfig ExternalConfigV1, workspaceID string) (*Config,
}
// It's very important that we sort the directories here so that the
// constructed modules and/or images are in a deterministic order.
directories := stringutil.MapToSlice(directorySet)
directories := slicesextended.MapToSlice(directorySet)
sort.Slice(directories, func(i int, j int) bool {
return directories[i] < directories[j]
})
Expand Down
4 changes: 2 additions & 2 deletions private/buf/cmd/buf/command/beta/studioagent/studioagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appflag"
"github.com/bufbuild/buf/private/pkg/cert/certclient"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/transport/http/httpserver"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -186,7 +186,7 @@ func run(
container.Logger(),
flags.Origin,
clientTLSConfig,
stringutil.SliceToMap(flags.DisallowedHeaders),
slicesextended.ToMap(flags.DisallowedHeaders),
flags.ForwardHeaders,
flags.PrivateNetwork,
)
Expand Down
3 changes: 2 additions & 1 deletion private/buf/cmd/buf/command/breaking/breaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appflag"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -304,5 +305,5 @@ func getExternalPathsForImages(imageConfigs []bufwire.ImageConfig, excludeImport
externalPaths[imageFile.ExternalPath()] = struct{}{}
}
}
return stringutil.MapToSlice(externalPaths), nil
return slicesextended.MapToSlice(externalPaths), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/bufbuild/buf/private/pkg/protodescriptor"
"github.com/bufbuild/buf/private/pkg/protosource"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/stringutil"
"google.golang.org/protobuf/types/descriptorpb"
)
Expand Down Expand Up @@ -134,7 +135,7 @@ func checkEnumValueSameName(add addFunc, corpus *corpus, previousNameToEnumValue
// ie if you now have FOO=2, BAR=2, you need to have had FOO=2, BAR=2 previously
// FOO=2, BAR=2, BAZ=2 now would pass
// FOO=2, BAR=2, BAZ=2 previously would fail
if !stringutil.SliceElementsContained(names, previousNames) {
if !slicesextended.ElementsContained(names, previousNames) {
previousNamesString := stringutil.JoinSliceQuoted(previousNames, ", ")
namesString := stringutil.JoinSliceQuoted(names, ", ")
nameSuffix := ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/protosource"
"github.com/bufbuild/buf/private/pkg/protoversion"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/stringutil"
)

Expand Down Expand Up @@ -116,13 +117,13 @@ func checkDirectorySamePackage(add addFunc, dirPath string, files []protosource.
if _, ok := pkgMap[""]; ok {
delete(pkgMap, "")
if len(pkgMap) > 1 {
messagePrefix = fmt.Sprintf("Multiple packages %q and file with no package", strings.Join(stringutil.MapToSortedSlice(pkgMap), ","))
messagePrefix = fmt.Sprintf("Multiple packages %q and file with no package", strings.Join(slicesextended.MapToSortedSlice(pkgMap), ","))
} else {
// Join works with only one element as well by adding no comma
messagePrefix = fmt.Sprintf("Package %q and file with no package", strings.Join(stringutil.MapToSortedSlice(pkgMap), ","))
messagePrefix = fmt.Sprintf("Package %q and file with no package", strings.Join(slicesextended.MapToSortedSlice(pkgMap), ","))
}
} else {
messagePrefix = fmt.Sprintf("Multiple packages %q", strings.Join(stringutil.MapToSortedSlice(pkgMap), ","))
messagePrefix = fmt.Sprintf("Multiple packages %q", strings.Join(slicesextended.MapToSortedSlice(pkgMap), ","))
}
for _, file := range files {
add(file, file.PackageLocation(), nil, "%s detected within directory %q.", messagePrefix, dirPath)
Expand Down Expand Up @@ -525,7 +526,7 @@ func checkPackageSameDirectory(add addFunc, pkg string, files []protosource.File
dirMap[normalpath.Dir(file.Path())] = struct{}{}
}
if len(dirMap) > 1 {
dirs := stringutil.MapToSortedSlice(dirMap)
dirs := slicesextended.MapToSortedSlice(dirMap)
for _, file := range files {
add(file, file.PackageLocation(), nil, "Multiple directories %q contain files with package %q.", strings.Join(dirs, ","), pkg)
}
Expand Down Expand Up @@ -602,7 +603,7 @@ func checkPackageSameOptionValue(
if len(optionValueMap) > 1 {
_, noOptionValue := optionValueMap[""]
delete(optionValueMap, "")
optionValues := stringutil.MapToSortedSlice(optionValueMap)
optionValues := slicesextended.MapToSortedSlice(optionValueMap)
for _, file := range files {
if noOptionValue {
add(file, getOptionLocation(file), nil, "Files in package %q have both values %q and no value for option %q and all values must be equal.", pkg, strings.Join(optionValues, ","), name)
Expand Down
8 changes: 4 additions & 4 deletions private/bufpkg/bufcheck/internal/version_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package internal
import (
"sort"

"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/slicesextended"
)

// VersionSpec specifies the rules, ids, and categories for a given version.
Expand All @@ -39,7 +39,7 @@ func AllCategoriesForVersionSpec(versionSpec *VersionSpec) []string {
categoriesMap[category] = struct{}{}
}
}
categories := stringutil.MapToSlice(categoriesMap)
categories := slicesextended.MapToSlice(categoriesMap)
sort.Slice(
categories,
func(i int, j int) bool {
Expand All @@ -57,7 +57,7 @@ func AllIDsForVersionSpec(versionSpec *VersionSpec) []string {
for id := range versionSpec.IDToCategories {
m[id] = struct{}{}
}
return stringutil.MapToSortedSlice(m)
return slicesextended.MapToSortedSlice(m)
}

// AllCategoriesAndIDsForVersionSpec returns all categories and rules for the VersionSpec.
Expand All @@ -71,5 +71,5 @@ func AllCategoriesAndIDsForVersionSpec(versionSpec *VersionSpec) []string {
m[category] = struct{}{}
}
}
return stringutil.MapToSortedSlice(m)
return slicesextended.MapToSortedSlice(m)
}
6 changes: 3 additions & 3 deletions private/bufpkg/bufimage/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
imagev1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/image/v1"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/protodescriptor"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -45,7 +45,7 @@ func imageWithOnlyPaths(image Image, fileOrDirPaths []string, excludeFileOrDirPa
if err := normalpath.ValidatePathsNormalizedValidatedUnique(excludeFileOrDirPaths); err != nil {
return nil, err
}
excludeFileOrDirPathMap := stringutil.SliceToMap(excludeFileOrDirPaths)
excludeFileOrDirPathMap := slicesextended.ToMap(excludeFileOrDirPaths)
// These are the files that fileOrDirPaths actually reference and will
// result in the non-imports in our resulting Image. The Image will also include
// the ImageFiles that the nonImportImageFiles import
Expand Down Expand Up @@ -133,7 +133,7 @@ func imageWithOnlyPaths(image Image, fileOrDirPaths []string, excludeFileOrDirPa
// make a map of the directory paths
// note that we do not make this a map to begin with as maps are unordered,
// and we want to make sure we iterate over the paths in a deterministic order
potentialDirPathMap := stringutil.SliceToMap(potentialDirPaths)
potentialDirPathMap := slicesextended.ToMap(potentialDirPaths)

// map of all paths based on the imageFiles
// the map of paths within potentialDirPath that matches a file in image.Files()
Expand Down
3 changes: 2 additions & 1 deletion private/bufpkg/bufmodule/bufmoduleconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/internal"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/stringutil"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ func newConfigV1Beta1(externalConfig ExternalConfigV1Beta1, deps ...string) (*Co
}

// verify that all excludes are within a root
rootMap := stringutil.SliceToMap(roots)
rootMap := slicesextended.ToMap(roots)
for _, fullExclude := range fullExcludes {
switch matchingRoots := normalpath.MapAllEqualOrContainingPaths(rootMap, fullExclude, normalpath.Relative); len(matchingRoots) {
case 0:
Expand Down
6 changes: 3 additions & 3 deletions private/bufpkg/bufmodule/targeting_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/stringutil"
)

type targetingModule struct {
Expand Down Expand Up @@ -56,7 +56,7 @@ func (m *targetingModule) TargetFileInfos(ctx context.Context) (fileInfos []bufm
bufmoduleref.SortFileInfos(fileInfos)
}
}()
excludePathMap := stringutil.SliceToMap(m.excludePaths)
excludePathMap := slicesextended.ToMap(m.excludePaths)
// We start by ensuring that no paths have been duplicated between target and exclude pathes.
for _, targetPath := range m.targetPaths {
if _, ok := excludePathMap[targetPath]; ok {
Expand Down Expand Up @@ -150,7 +150,7 @@ func (m *targetingModule) TargetFileInfos(ctx context.Context) (fileInfos []bufm
}
// We have potential directory paths, do the expensive operation to
// make a map of the directory paths.
potentialDirPathMap := stringutil.SliceToMap(potentialDirPaths)
potentialDirPathMap := slicesextended.ToMap(potentialDirPaths)
// The map of paths within potentialDirPath that matches a file.
// This needs to contain all paths in potentialDirPathMap at the end for us to
// have had matches for every targetPath input.
Expand Down
6 changes: 3 additions & 3 deletions private/pkg/bandeps/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -172,7 +172,7 @@ func (s *state) packagesForPackageExpressionUncached(
span.SetStatus(codes.Error, err.Error())
return nil, err
}
return stringutil.SliceToMap(getNonEmptyLines(string(data))), nil
return slicesextended.ToMap(getNonEmptyLines(string(data))), nil
}

func (s *state) depsForPackage(
Expand Down Expand Up @@ -234,7 +234,7 @@ func (s *state) depsForPackageUncached(
span.SetStatus(codes.Error, err.Error())
return nil, err
}
return stringutil.SliceToMap(getNonEmptyLines(string(data))), nil
return slicesextended.ToMap(getNonEmptyLines(string(data))), nil
}

type packagesResult struct {
Expand Down
5 changes: 3 additions & 2 deletions private/pkg/git/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/bufbuild/buf/private/pkg/stringutil"
)

Expand Down Expand Up @@ -64,7 +65,7 @@ func (l *lister) ListFilesAndUnstagedFiles(
if err != nil {
return nil, err
}
return stringutil.SliceToUniqueSortedSlice(
return slicesextended.ToUniqueSorted(
filterNonRegularFiles(
stringSliceExceptMatches(
stringSliceExcept(
Expand All @@ -81,7 +82,7 @@ func (l *lister) ListFilesAndUnstagedFiles(

// stringSliceExcept returns all elements in source that are not in except.
func stringSliceExcept(source []string, except []string) []string {
exceptMap := stringutil.SliceToMap(except)
exceptMap := slicesextended.ToMap(except)
result := make([]string, 0, len(source))
for _, s := range source {
if _, ok := exceptMap[s]; !ok {
Expand Down
4 changes: 2 additions & 2 deletions private/pkg/normalpath/normalpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"sort"
"strings"

"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/slicesextended"
)

const (
Expand Down Expand Up @@ -297,7 +297,7 @@ func MapAllEqualOrContainingPaths(m map[string]struct{}, path string, pathType P
if len(m) == 0 {
return nil
}
return stringutil.MapToSortedSlice(MapAllEqualOrContainingPathMap(m, path, pathType))
return slicesextended.MapToSortedSlice(MapAllEqualOrContainingPathMap(m, path, pathType))
}

// StripComponents strips the specified number of components.
Expand Down
10 changes: 5 additions & 5 deletions private/pkg/normalpath/normalpath_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sort"
"testing"

"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/slicesextended"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -342,7 +342,7 @@ func TestMapHasEqualOrContainingPath(t *testing.T) {
}

func testMapHasEqualOrContainingPath(t *testing.T, expected bool, path string, keys ...string) {
keyMap := stringutil.SliceToMap(keys)
keyMap := slicesextended.ToMap(keys)
assert.Equal(t, expected, MapHasEqualOrContainingPath(keyMap, path, Relative), fmt.Sprintf("%s %v", path, keys))
}

Expand All @@ -367,7 +367,7 @@ func testMapAllEqualOrContainingPaths(t *testing.T, expected []string, path stri
expected = make([]string, 0)
}
sort.Strings(expected)
keyMap := stringutil.SliceToMap(keys)
keyMap := slicesextended.ToMap(keys)
assert.Equal(t, expected, MapAllEqualOrContainingPaths(keyMap, path, Relative), fmt.Sprintf("%s %v", path, keys))
}

Expand Down Expand Up @@ -424,7 +424,7 @@ func TestMapHasEqualOrContainingPathAbs(t *testing.T) {
}

func testMapHasEqualOrContainingPathAbs(t *testing.T, expected bool, path string, keys ...string) {
keyMap := stringutil.SliceToMap(keys)
keyMap := slicesextended.ToMap(keys)
assert.Equal(t, expected, MapHasEqualOrContainingPath(keyMap, path, Absolute), fmt.Sprintf("%s %v", path, keys))
}

Expand All @@ -449,6 +449,6 @@ func testMapAllEqualOrContainingPathsAbs(t *testing.T, expected []string, path s
expected = make([]string, 0)
}
sort.Strings(expected)
keyMap := stringutil.SliceToMap(keys)
keyMap := slicesextended.ToMap(keys)
assert.Equal(t, expected, MapAllEqualOrContainingPaths(keyMap, path, Absolute), fmt.Sprintf("%s %v", path, keys))
}
Loading