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 util.Keys and util.KeysSorted #7285

Merged
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
8 changes: 1 addition & 7 deletions cmd/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"math"
"net/http"
"os"
"sort"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -608,12 +607,7 @@ func renderBenchmarkResult(params benchmarkCommandParams, br testing.BenchmarkRe
})
}

var keys []string
for k := range br.Extra {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
for _, k := range util.KeysSorted(br.Extra) {
data = append(data, []string{k, prettyFormatFloat(br.Extra[k])})
}

Expand Down
10 changes: 2 additions & 8 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Example:
bundle.tar.gz
$ opa inspect bundle.tar.gz

You can provide exactly one OPA bundle, path to a bundle directory, or direct path to a Rego file to the 'inspect' command
You can provide exactly one OPA bundle, path to a bundle directory, or direct path to a Rego file to the 'inspect' command
on the command-line. If you provide a path referring to a directory, the 'inspect' command will load that path as a bundle
and summarize its structure and contents. If you provide a path referring to a Rego file, the 'inspect' command will load
that file and summarize its structure and contents.
Expand Down Expand Up @@ -210,13 +210,7 @@ func populateNamespaces(out io.Writer, n map[string][]string) error {
t.SetAutoMergeCellsByColumnIndex([]int{0})
var lines [][]string

keys := make([]string, 0, len(n))
for k := range n {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
for _, k := range util.KeysSorted(n) {
for _, file := range n[k] {
lines = append(lines, []string{k, truncateFileName(file)})
}
Expand Down
10 changes: 2 additions & 8 deletions internal/pathwatcher/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"context"
"os"
"path/filepath"
"sort"

"github.com/fsnotify/fsnotify"
initload "github.com/open-policy-agent/opa/internal/runtime/init"
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/loader"
"github.com/open-policy-agent/opa/v1/storage"
"github.com/open-policy-agent/opa/v1/util"
)

// CreatePathWatcher creates watchers to monitor for path changes
Expand Down Expand Up @@ -119,13 +119,7 @@ func getWatchPaths(rootPaths []string) ([]string, error) {
}
}

u := make([]string, 0, len(unique))
for k := range unique {
u = append(u, k)
}
sort.Strings(u)

paths = append(paths, u...)
paths = append(paths, util.KeysSorted(unique)...)
}

return paths, nil
Expand Down
17 changes: 2 additions & 15 deletions internal/providers/aws/signing_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"

v4 "github.com/open-policy-agent/opa/internal/providers/aws/v4"

"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/util"
)

func stringFromTerm(t *ast.Term) string {
Expand Down Expand Up @@ -67,19 +67,6 @@ func sha256MAC(message string, key []byte) []byte {
return mac.Sum(nil)
}

func sortKeys(strMap map[string][]string) []string {
keys := make([]string, len(strMap))

i := 0
for k := range strMap {
keys[i] = k
i++
}
sort.Strings(keys)

return keys
}

// SignRequest modifies an http.Request to include an AWS V4 signature based on the provided credentials.
func SignRequest(req *http.Request, service string, creds Credentials, theTime time.Time, sigVersion string) error {
// General ref. https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
Expand Down Expand Up @@ -168,7 +155,7 @@ func SignV4(headers map[string][]string, method string, theURL *url.URL, body []
canonicalReq += theURL.RawQuery + "\n" // RAW Query String

// include the values for the signed headers
orderedKeys := sortKeys(headersToSign)
orderedKeys := util.KeysSorted(headersToSign)
for _, k := range orderedKeys {
canonicalReq += k + ":" + strings.Join(headersToSign[k], ",") + "\n"
}
Expand Down
Loading