Skip to content

Commit

Permalink
rename to MapKeys*
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversun9 committed Nov 20, 2023
1 parent e156109 commit 90a47cd
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion private/buf/bufsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (s *syncer) prepareSync(ctx context.Context) error {
var branchesToSync []string
if s.syncAllBranches {
// sync all branches
branchesToSync = slicesextended.MapToSlice(allBranches)
branchesToSync = slicesextended.MapKeysToSlice(allBranches)
} else {
// sync current branch, make sure it's present
currentBranch, err := s.repo.CheckedOutBranch()
Expand Down
2 changes: 1 addition & 1 deletion private/buf/bufwork/config.go
Original file line number Diff line number Diff line change
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 := slicesextended.MapToSlice(directorySet)
directories := slicesextended.MapKeysToSlice(directorySet)
sort.Slice(directories, func(i int, j int) bool {
return directories[i] < directories[j]
})
Expand Down
2 changes: 1 addition & 1 deletion private/buf/cmd/buf/command/breaking/breaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,5 @@ func getExternalPathsForImages(imageConfigs []bufwire.ImageConfig, excludeImport
externalPaths[imageFile.ExternalPath()] = struct{}{}
}
}
return slicesextended.MapToSlice(externalPaths), nil
return slicesextended.MapKeysToSlice(externalPaths), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,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(slicesextended.MapToSortedSlice(pkgMap), ","))
messagePrefix = fmt.Sprintf("Multiple packages %q and file with no package", strings.Join(slicesextended.MapKeysToSortedSlice(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(slicesextended.MapToSortedSlice(pkgMap), ","))
messagePrefix = fmt.Sprintf("Package %q and file with no package", strings.Join(slicesextended.MapKeysToSortedSlice(pkgMap), ","))
}
} else {
messagePrefix = fmt.Sprintf("Multiple packages %q", strings.Join(slicesextended.MapToSortedSlice(pkgMap), ","))
messagePrefix = fmt.Sprintf("Multiple packages %q", strings.Join(slicesextended.MapKeysToSortedSlice(pkgMap), ","))
}
for _, file := range files {
add(file, file.PackageLocation(), nil, "%s detected within directory %q.", messagePrefix, dirPath)
Expand Down Expand Up @@ -526,7 +526,7 @@ func checkPackageSameDirectory(add addFunc, pkg string, files []protosource.File
dirMap[normalpath.Dir(file.Path())] = struct{}{}
}
if len(dirMap) > 1 {
dirs := slicesextended.MapToSortedSlice(dirMap)
dirs := slicesextended.MapKeysToSortedSlice(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 @@ -603,7 +603,7 @@ func checkPackageSameOptionValue(
if len(optionValueMap) > 1 {
_, noOptionValue := optionValueMap[""]
delete(optionValueMap, "")
optionValues := slicesextended.MapToSortedSlice(optionValueMap)
optionValues := slicesextended.MapKeysToSortedSlice(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
6 changes: 3 additions & 3 deletions private/bufpkg/bufcheck/internal/version_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func AllCategoriesForVersionSpec(versionSpec *VersionSpec) []string {
categoriesMap[category] = struct{}{}
}
}
categories := slicesextended.MapToSlice(categoriesMap)
categories := slicesextended.MapKeysToSlice(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 slicesextended.MapToSortedSlice(m)
return slicesextended.MapKeysToSortedSlice(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 slicesextended.MapToSortedSlice(m)
return slicesextended.MapKeysToSortedSlice(m)
}
2 changes: 1 addition & 1 deletion private/pkg/normalpath/normalpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func MapAllEqualOrContainingPaths(m map[string]struct{}, path string, pathType P
if len(m) == 0 {
return nil
}
return slicesextended.MapToSortedSlice(MapAllEqualOrContainingPathMap(m, path, pathType))
return slicesextended.MapKeysToSortedSlice(MapAllEqualOrContainingPathMap(m, path, pathType))
}

// StripComponents strips the specified number of components.
Expand Down
12 changes: 6 additions & 6 deletions private/pkg/slicesextended/slicesextended.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ func ToMap[T comparable](s []T) map[T]struct{} {
return m
}

// MapToSortedSlice converts the map's keys to a sorted slice.
func MapToSortedSlice[M ~map[K]V, K Ordered, V any](m M) []K {
s := MapToSlice(m)
// MapKeysToSortedSlice converts the map's keys to a sorted slice.
func MapKeysToSortedSlice[M ~map[K]V, K Ordered, V any](m M) []K {
s := MapKeysToSlice(m)
// TODO: Replace with slices.Sort when we only support Go versions >= 1.21.
sort.Slice(
s,
Expand All @@ -160,8 +160,8 @@ func MapToSortedSlice[M ~map[K]V, K Ordered, V any](m M) []K {
return s
}

// MapToSlice converts the map's keys to a slice.
func MapToSlice[K comparable, V any](m map[K]V) []K {
// MapKeysToSlice converts the map's keys to a slice.
func MapKeysToSlice[K comparable, V any](m map[K]V) []K {
s := make([]K, 0, len(m))
for e := range m {
s = append(s, e)
Expand All @@ -171,7 +171,7 @@ func MapToSlice[K comparable, V any](m map[K]V) []K {

// ToUniqueSorted returns a sorted copy of s with no duplicates.
func ToUniqueSorted[S ~[]T, T Ordered](s S) S {
return MapToSortedSlice(ToMap(s))
return MapKeysToSortedSlice(ToMap(s))
}

// ToChunks splits s into chunks of the given chunk size.
Expand Down
6 changes: 3 additions & 3 deletions private/pkg/stringutil/stringutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func SplitTrimLinesNoEmpty(output string) []string {
//
// Deprecated: Use slicesextended.MapToSortedSlice instead.
func MapToSortedSlice(m map[string]struct{}) []string {
return slicesextended.MapToSortedSlice(m)
return slicesextended.MapKeysToSortedSlice(m)
}

// MapToSlice transforms m to a slice.
//
// Deprecated: Use slicesextended.MapToSlice instead.
func MapToSlice(m map[string]struct{}) []string {
return slicesextended.MapToSlice(m)
return slicesextended.MapKeysToSlice(m)
}

// SliceToMap transforms s to a map.
Expand All @@ -94,7 +94,7 @@ func SliceToUniqueSortedSliceFilterEmptyStrings(s []string) []string {
delete(m, key)
}
}
return slicesextended.MapToSortedSlice(m)
return slicesextended.MapKeysToSortedSlice(m)
}

// SliceToChunks splits s into chunks of the given chunk size.
Expand Down

0 comments on commit 90a47cd

Please sign in to comment.