Skip to content

Commit

Permalink
several: use stdlib functions
Browse files Browse the repository at this point in the history
Replace some functions with simpler code that uses
recent functions added to the standard library.

Change-Id: Ifcd570679a897fb165a4307c7ce9f0f53fccea05
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/609116
kokoro-CI: kokoro <noreply+kokoro@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
jba committed Sep 4, 2024
1 parent 6851d87 commit f5afe02
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
13 changes: 3 additions & 10 deletions internal/licenses/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"fmt"
"io"
"io/fs"
"maps"
"path"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -575,7 +577,7 @@ func DetectFile(contents []byte, filename string, logf func(string, ...any)) ([]
logf("%s failed to classify license (%+v), skipping", filename, cov)
return []string{unknownLicenseType}, cov
}
return setToSortedSlice(types), cov
return slices.Sorted(maps.Keys(types)), cov
}

// Redistributable reports whether the set of license types establishes that a
Expand Down Expand Up @@ -603,12 +605,3 @@ func types(lics []*License) []string {
}
return types
}

func setToSortedSlice(m map[string]bool) []string {
var s []string
for e := range m {
s = append(s, e)
}
sort.Strings(s)
return s
}
14 changes: 4 additions & 10 deletions internal/postgres/insert_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"fmt"
"hash/fnv"
"io"
"maps"
"slices"
"sort"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -442,15 +444,7 @@ func insertPaths(ctx context.Context, tx *database.DB, m *internal.Module) (path
curPathsSet[internal.V1Path(u.Path, m.ModulePath)] = true
curPathsSet[internal.SeriesPathForModule(m.ModulePath)] = true
}
return upsertPaths(ctx, tx, stringSetToSlice(curPathsSet))
}

func stringSetToSlice(m map[string]bool) []string {
var s []string
for e := range m {
s = append(s, e)
}
return s
return upsertPaths(ctx, tx, slices.Collect(maps.Keys(curPathsSet)))
}

func insertUnits(ctx context.Context, db *database.DB, unitValues []any) (pathIDToUnitID map[int]int, err error) {
Expand Down Expand Up @@ -581,7 +575,7 @@ func insertImports(ctx context.Context, tx *database.DB,
importPathSet[imp] = true
}
}
pathToID, err := upsertPaths(ctx, tx, stringSetToSlice(importPathSet))
pathToID, err := upsertPaths(ctx, tx, slices.Collect(maps.Keys(importPathSet)))
if err != nil {
return err
}
Expand Down

0 comments on commit f5afe02

Please sign in to comment.