mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
Enable modernize linter for golangci-lint (#8996)
Didn't know this was a thing now. That certainly helps! Also some follow-up fixes from the previous modernize PR. --------- Signed-off-by: Anders Eknert <anders.eknert@apple.com> Signed-off-by: Charlie Egan <charlie_egan@apple.com> Co-authored-by: Charlie Egan <charlie_egan@apple.com>
This commit is contained in:
@@ -12,6 +12,7 @@ linters:
|
|||||||
- intrange
|
- intrange
|
||||||
- mirror
|
- mirror
|
||||||
- misspell
|
- misspell
|
||||||
|
- modernize
|
||||||
- perfsprint
|
- perfsprint
|
||||||
- prealloc
|
- prealloc
|
||||||
- revive # replacement for golint
|
- revive # replacement for golint
|
||||||
@@ -60,6 +61,11 @@ linters:
|
|||||||
- nilness
|
- nilness
|
||||||
lll:
|
lll:
|
||||||
line-length: 200
|
line-length: 200
|
||||||
|
modernize:
|
||||||
|
disable:
|
||||||
|
# this should be enabled only outside of hot paths, as it's likely
|
||||||
|
# less performant (and there have been reports of that)
|
||||||
|
- slicesbackward
|
||||||
perfsprint:
|
perfsprint:
|
||||||
# only rule disabled by default, but it's a good one
|
# only rule disabled by default, but it's a good one
|
||||||
err-error: true
|
err-error: true
|
||||||
|
|||||||
@@ -151,23 +151,31 @@ func SignV4(headers map[string][]string, method string, theURL *url.URL, body []
|
|||||||
|
|
||||||
// the "canonical request" is the normalized version of the AWS service access
|
// the "canonical request" is the normalized version of the AWS service access
|
||||||
// that we're attempting to perform
|
// that we're attempting to perform
|
||||||
canonicalReq := method + "\n" // HTTP method
|
buf := bytes.NewBufferString(method)
|
||||||
canonicalReq += theURL.EscapedPath() + "\n" // URI-escaped path
|
buf.WriteByte('\n')
|
||||||
canonicalReq += theURL.RawQuery + "\n" // RAW Query String
|
buf.WriteString(theURL.EscapedPath())
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
buf.WriteString(theURL.RawQuery)
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
|
||||||
// include the values for the signed headers
|
// include the values for the signed headers
|
||||||
orderedKeys := util.KeysSorted(headersToSign)
|
orderedKeys := util.KeysSorted(headersToSign)
|
||||||
for _, k := range orderedKeys {
|
for _, k := range orderedKeys {
|
||||||
// TODO: fix later
|
buf.WriteString(k)
|
||||||
//nolint:perfsprint
|
buf.WriteByte(':')
|
||||||
canonicalReq += k + ":" + strings.Join(headersToSign[k], ",") + "\n"
|
buf.WriteString(strings.Join(headersToSign[k], ","))
|
||||||
|
buf.WriteByte('\n')
|
||||||
}
|
}
|
||||||
canonicalReq += "\n" // linefeed to terminate headers
|
|
||||||
|
buf.WriteByte('\n') // linefeed to terminate headers
|
||||||
|
|
||||||
// include the list of the signed headers
|
// include the list of the signed headers
|
||||||
headerList := strings.Join(orderedKeys, ";")
|
headerList := strings.Join(orderedKeys, ";")
|
||||||
canonicalReq += headerList + "\n"
|
buf.WriteString(headerList)
|
||||||
canonicalReq += contentSha256
|
buf.WriteByte('\n')
|
||||||
|
buf.WriteString(contentSha256)
|
||||||
|
|
||||||
|
canonicalReq := buf.String()
|
||||||
|
|
||||||
// the "string to sign" is a time-bounded, scoped request token which
|
// the "string to sign" is a time-bounded, scoped request token which
|
||||||
// is linked to the "canonical request" by inclusion of its SHA-256 hash
|
// is linked to the "canonical request" by inclusion of its SHA-256 hash
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !darwin
|
//go:build !darwin
|
||||||
// +build !darwin
|
|
||||||
|
|
||||||
package test
|
package test
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package ast
|
package ast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -218,12 +219,18 @@ func runParseStatementBenchmarkWithError(b *testing.B, stmt string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateModule(numRules int) string {
|
func generateModule(numRules int) string {
|
||||||
mod := "package bench\n"
|
mod := bytes.NewBufferString("package bench\n")
|
||||||
for i := range numRules {
|
for i := range numRules {
|
||||||
//nolint:perfsprint
|
is := strconv.Itoa(i)
|
||||||
mod += fmt.Sprintf("p%d if { input.x%d = %d }\n", i, i, i)
|
mod.WriteByte('p')
|
||||||
|
mod.WriteString(is)
|
||||||
|
mod.WriteString(" if { input.x")
|
||||||
|
mod.WriteString(is)
|
||||||
|
mod.WriteString(" = ")
|
||||||
|
mod.WriteString(is)
|
||||||
|
mod.WriteString(" }\n")
|
||||||
}
|
}
|
||||||
return mod
|
return mod.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateArrayStatement(size int) string {
|
func generateArrayStatement(size int) string {
|
||||||
|
|||||||
@@ -5626,12 +5626,12 @@ func TestRuleFromBody(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify the rule and rule and rule head col/loc values
|
// Verify the rule and rule and rule head col/loc values
|
||||||
testModule := "package a.b.c\n\n"
|
testModule := bytes.NewBufferString("package a.b.c\n\n")
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
//nolint:perfsprint
|
testModule.WriteString(tc.input)
|
||||||
testModule += tc.input + "\n"
|
testModule.WriteByte('\n')
|
||||||
}
|
}
|
||||||
module, err := ParseModuleWithOpts("test.rego", testModule, popts)
|
module, err := ParseModuleWithOpts("test.rego", testModule.String(), popts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,12 +136,12 @@ func TestConfigValidationUpdate(t *testing.T) {
|
|||||||
expParsedMax := time.Second * time.Duration(expMax)
|
expParsedMax := time.Second * time.Duration(expMax)
|
||||||
var config Config
|
var config Config
|
||||||
|
|
||||||
if err := json.Unmarshal([]byte(fmt.Sprintf(`{
|
if err := json.Unmarshal(fmt.Appendf(nil, `{
|
||||||
"polling": {
|
"polling": {
|
||||||
"min_delay_seconds": %d,
|
"min_delay_seconds": %d,
|
||||||
"max_delay_seconds": %d
|
"max_delay_seconds": %d
|
||||||
}
|
}
|
||||||
}`, expMin, expMax)), &config); err != nil {
|
}`, expMin, expMax), &config); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7371,7 +7371,7 @@ func TestPluginManualTriggerWithServerError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t.Fatalf("expected type of error to be %s but got %s", reflect.TypeOf(bundleErrors), reflect.TypeOf(err))
|
t.Fatalf("expected type of error to be %s but got %s", reflect.TypeFor[Errors](), reflect.TypeOf(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1849,15 +1849,14 @@ func TestS3SigningMultiCredentialProvider(t *testing.T) {
|
|||||||
t.Fatalf("Client config S3 signing credentials setup unexpected")
|
t.Fatalf("Client config S3 signing credentials setup unexpected")
|
||||||
}
|
}
|
||||||
|
|
||||||
awsCredentialServiceChain, ok := awsPlugin.awsCredentialService().(*awsCredentialServiceChain)
|
chain, ok := awsPlugin.awsCredentialService().(*awsCredentialServiceChain)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("Unexpected AWS credential service:%v is not a chain",
|
t.Fatalf("Unexpected AWS credential service: %T is not a chain", chain)
|
||||||
reflect.TypeOf(awsCredentialServiceChain))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(awsCredentialServiceChain.awsCredentialServices) != credentialProviderCount {
|
if len(chain.awsCredentialServices) != credentialProviderCount {
|
||||||
t.Fatalf("Credential provider count mismatch %d != %d", credentialProviderCount,
|
t.Fatalf("Credential provider count mismatch %d != %d", credentialProviderCount,
|
||||||
len(awsCredentialServiceChain.awsCredentialServices))
|
len(chain.awsCredentialServices))
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOrder := []awsCredentialService{
|
expectedOrder := []awsCredentialService{
|
||||||
@@ -1867,8 +1866,7 @@ func TestS3SigningMultiCredentialProvider(t *testing.T) {
|
|||||||
&awsMetadataCredentialService{},
|
&awsMetadataCredentialService{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(awsCredentialServiceChain.awsCredentialServices,
|
if !reflect.DeepEqual(chain.awsCredentialServices, expectedOrder) {
|
||||||
expectedOrder) {
|
|
||||||
t.Fatalf("Ordering is unexpected")
|
t.Fatalf("Ordering is unexpected")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,11 +245,11 @@ func (r PrettyReporter) fmtBenchmark(tr *Result) string {
|
|||||||
//
|
//
|
||||||
// This converts the test case name like data.foo.bar.test_auth to be more
|
// This converts the test case name like data.foo.bar.test_auth to be more
|
||||||
// like BenchmarkDataFooBarTestAuth.
|
// like BenchmarkDataFooBarTestAuth.
|
||||||
camelCaseName := ""
|
var camelCaseName strings.Builder
|
||||||
for part := range strings.SplitSeq(strings.ReplaceAll(name, "_", "."), ".") {
|
for part := range strings.SplitSeq(strings.ReplaceAll(name, "_", "."), ".") {
|
||||||
camelCaseName += strings.Title(part) //nolint:perfsprint,staticcheck
|
camelCaseName.WriteString(strings.Title(part)) //nolint:staticcheck
|
||||||
}
|
}
|
||||||
name = "Benchmark" + camelCaseName
|
name = "Benchmark" + camelCaseName.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
result := fmt.Sprintf("%s\t%s", name, tr.BenchmarkResult.String())
|
result := fmt.Sprintf("%s\t%s", name, tr.BenchmarkResult.String())
|
||||||
|
|||||||
@@ -339,13 +339,11 @@ func BenchmarkJSONPatchPathologicalNestedAddChainObject(b *testing.B) {
|
|||||||
for _, n := range []int{10, 100, 500, 1000, 5000, 10000} {
|
for _, n := range []int{10, 100, 500, 1000, 5000, 10000} {
|
||||||
b.Run(strconv.Itoa(n), func(b *testing.B) {
|
b.Run(strconv.Itoa(n), func(b *testing.B) {
|
||||||
patchList := make([]*ast.Term, n)
|
patchList := make([]*ast.Term, n)
|
||||||
path := ""
|
|
||||||
for i := range n {
|
for i := range n {
|
||||||
path += "/a"
|
|
||||||
patchList[i] = ast.NewTerm(ast.NewObject(
|
patchList[i] = ast.NewTerm(ast.NewObject(
|
||||||
[2]*ast.Term{ast.InternedTerm("op"), ast.InternedTerm("add")},
|
[2]*ast.Term{ast.InternedTerm("op"), ast.InternedTerm("add")},
|
||||||
[2]*ast.Term{ast.InternedTerm("value"), ast.ObjectTerm()},
|
[2]*ast.Term{ast.InternedTerm("value"), ast.ObjectTerm()},
|
||||||
[2]*ast.Term{ast.InternedTerm("path"), ast.InternedTerm(path)},
|
[2]*ast.Term{ast.InternedTerm("path"), ast.InternedTerm(strings.Repeat("/a", i+1))},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
runJSONPatchBenchmarkTest(b, ast.NewObject(), ast.NewArray(patchList...))
|
runJSONPatchBenchmarkTest(b, ast.NewObject(), ast.NewArray(patchList...))
|
||||||
@@ -357,13 +355,11 @@ func BenchmarkJSONPatchPathologicalNestedAddChainArray(b *testing.B) {
|
|||||||
for _, n := range []int{10, 100, 500, 1000, 5000, 10000} {
|
for _, n := range []int{10, 100, 500, 1000, 5000, 10000} {
|
||||||
b.Run(strconv.Itoa(n), func(b *testing.B) {
|
b.Run(strconv.Itoa(n), func(b *testing.B) {
|
||||||
patchList := make([]*ast.Term, n)
|
patchList := make([]*ast.Term, n)
|
||||||
path := ""
|
|
||||||
for i := range n {
|
for i := range n {
|
||||||
path += "/0"
|
|
||||||
patchList[i] = ast.NewTerm(ast.NewObject(
|
patchList[i] = ast.NewTerm(ast.NewObject(
|
||||||
[2]*ast.Term{ast.InternedTerm("op"), ast.InternedTerm("add")},
|
[2]*ast.Term{ast.InternedTerm("op"), ast.InternedTerm("add")},
|
||||||
[2]*ast.Term{ast.InternedTerm("value"), ast.ArrayTerm()},
|
[2]*ast.Term{ast.InternedTerm("value"), ast.ArrayTerm()},
|
||||||
[2]*ast.Term{ast.InternedTerm("path"), ast.StringTerm(path)},
|
[2]*ast.Term{ast.InternedTerm("path"), ast.StringTerm(strings.Repeat("/0", i+1))},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
runJSONPatchBenchmarkTest(b, ast.NewArray(), ast.NewArray(patchList...))
|
runJSONPatchBenchmarkTest(b, ast.NewArray(), ast.NewArray(patchList...))
|
||||||
|
|||||||
@@ -192,7 +192,8 @@ func benchmarkConcurrency(b *testing.B, params []storage.TransactionParams) {
|
|||||||
for range 1000 / len(params) {
|
for range 1000 / len(params) {
|
||||||
txn, err := store.NewTransaction(ctx, param)
|
txn, err := store.NewTransaction(ctx, param)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("Unexpected transaction error: %v", err)
|
b.Errorf("Unexpected transaction error: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
rs, err := NewQuery(body).
|
rs, err := NewQuery(body).
|
||||||
WithCompiler(compiler).
|
WithCompiler(compiler).
|
||||||
@@ -200,10 +201,9 @@ func benchmarkConcurrency(b *testing.B, params []storage.TransactionParams) {
|
|||||||
WithTransaction(txn).
|
WithTransaction(txn).
|
||||||
Run(ctx)
|
Run(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("Unexpected topdown query error: %v", err)
|
b.Errorf("Unexpected topdown query error: %v", err)
|
||||||
}
|
} else if len(rs) != 1 || !rs[0][ast.Var("x")].Equal(ast.BooleanTerm(true)) {
|
||||||
if len(rs) != 1 || !rs[0][ast.Var("x")].Equal(ast.BooleanTerm(true)) {
|
b.Errorf("Unexpected undefined/extra/bad result: %v", rs)
|
||||||
b.Fatalf("Unexpected undefined/extra/bad result: %v", rs)
|
|
||||||
}
|
}
|
||||||
store.Abort(ctx, txn)
|
store.Abort(ctx, txn)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ func PartialObjectBenchmarkCrossModule(n int) []string {
|
|||||||
bazMod += fmt.Sprintf(`rule_%d if {
|
bazMod += fmt.Sprintf(`rule_%d if {
|
||||||
%s
|
%s
|
||||||
}`, idx, ruleBuilder)
|
}`, idx, ruleBuilder)
|
||||||
|
//nolint:modernize
|
||||||
fooMod += fmt.Sprintf(`
|
fooMod += fmt.Sprintf(`
|
||||||
final_decision = "allow" if {
|
final_decision = "allow" if {
|
||||||
baz.rule_%d
|
baz.rule_%d
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !darwin
|
//go:build !darwin
|
||||||
// +build !darwin
|
|
||||||
|
|
||||||
package test
|
package test
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user