Skip to content

Commit

Permalink
chore: replace interface{} with any (#6751)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin authored May 27, 2024
1 parent 9c3e895 commit ebb123f
Show file tree
Hide file tree
Showing 246 changed files with 6,191 additions and 6,188 deletions.
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ linters-settings:
check-shadowing: false
gofmt:
simplify: false
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
revive:
ignore-generated-header: true
gocyclo:
Expand Down
4 changes: 2 additions & 2 deletions integration/client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func TestClientServerWithFormat(t *testing.T) {
}

fakeTime := time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC)
report.CustomTemplateFuncMap = map[string]interface{}{
report.CustomTemplateFuncMap = map[string]any{
"now": func() time.Time {
return fakeTime
},
Expand All @@ -388,7 +388,7 @@ func TestClientServerWithFormat(t *testing.T) {
t.Setenv("GITHUB_WORKFLOW", "workflow-name")

t.Cleanup(func() {
report.CustomTemplateFuncMap = map[string]interface{}{}
report.CustomTemplateFuncMap = map[string]any{}
})

addr, cacheDir := setup(t, setupOptions{})
Expand Down
32 changes: 16 additions & 16 deletions internal/testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/aquasecurity/trivy/pkg/iac/scan"
)

func AssertRuleFound(t *testing.T, ruleID string, results scan.Results, message string, args ...interface{}) {
func AssertRuleFound(t *testing.T, ruleID string, results scan.Results, message string, args ...any) {
found := ruleIDInResults(ruleID, results.GetFailed())
assert.True(t, found, append([]interface{}{message}, args...)...)
assert.True(t, found, append([]any{message}, args...)...)
for _, result := range results.GetFailed() {
if result.Rule().LongID() == ruleID {
m := result.Metadata()
Expand All @@ -31,9 +31,9 @@ func AssertRuleFound(t *testing.T, ruleID string, results scan.Results, message
}
}

func AssertRuleNotFound(t *testing.T, ruleID string, results scan.Results, message string, args ...interface{}) {
func AssertRuleNotFound(t *testing.T, ruleID string, results scan.Results, message string, args ...any) {
found := ruleIDInResults(ruleID, results.GetFailed())
assert.False(t, found, append([]interface{}{message}, args...)...)
assert.False(t, found, append([]any{message}, args...)...)
}

func ruleIDInResults(ruleID string, results scan.Results) bool {
Expand All @@ -57,46 +57,46 @@ func CreateFS(t *testing.T, files map[string]string) fs.FS {
return memfs
}

func AssertDefsecEqual(t *testing.T, expected, actual interface{}) {
func AssertDefsecEqual(t *testing.T, expected, actual any) {
expectedJson, err := json.MarshalIndent(expected, "", "\t")
require.NoError(t, err)
actualJson, err := json.MarshalIndent(actual, "", "\t")
require.NoError(t, err)

if expectedJson[0] == '[' {
var expectedSlice []map[string]interface{}
var expectedSlice []map[string]any
require.NoError(t, json.Unmarshal(expectedJson, &expectedSlice))
var actualSlice []map[string]interface{}
var actualSlice []map[string]any
require.NoError(t, json.Unmarshal(actualJson, &actualSlice))
expectedSlice = purgeMetadataSlice(expectedSlice)
actualSlice = purgeMetadataSlice(actualSlice)
assert.Equal(t, expectedSlice, actualSlice, "defsec adapted and expected values do not match")
} else {
var expectedMap map[string]interface{}
var expectedMap map[string]any
require.NoError(t, json.Unmarshal(expectedJson, &expectedMap))
var actualMap map[string]interface{}
var actualMap map[string]any
require.NoError(t, json.Unmarshal(actualJson, &actualMap))
expectedMap = purgeMetadata(expectedMap)
actualMap = purgeMetadata(actualMap)
assert.Equal(t, expectedMap, actualMap, "defsec adapted and expected values do not match")
}
}

func purgeMetadata(input map[string]interface{}) map[string]interface{} {
func purgeMetadata(input map[string]any) map[string]any {
for k, v := range input {
if k == "metadata" || k == "Metadata" {
delete(input, k)
continue
}
if v, ok := v.(map[string]interface{}); ok {
if v, ok := v.(map[string]any); ok {
input[k] = purgeMetadata(v)
}
if v, ok := v.([]interface{}); ok {
if v, ok := v.([]any); ok {
if len(v) > 0 {
if _, ok := v[0].(map[string]interface{}); ok {
maps := make([]map[string]interface{}, len(v))
if _, ok := v[0].(map[string]any); ok {
maps := make([]map[string]any, len(v))
for i := range v {
maps[i] = v[i].(map[string]interface{})
maps[i] = v[i].(map[string]any)
}
input[k] = purgeMetadataSlice(maps)
}
Expand All @@ -106,7 +106,7 @@ func purgeMetadata(input map[string]interface{}) map[string]interface{} {
return input
}

func purgeMetadataSlice(input []map[string]interface{}) []map[string]interface{} {
func purgeMetadataSlice(input []map[string]any) []map[string]any {
for i := range input {
input[i] = purgeMetadata(input[i])
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/attestation/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// Cosign uses this structure when creating an SBOM attestation.
// cf. https://github.com/sigstore/cosign/blob/e0547cff64f98585a837a524ff77ff6b47ff5609/pkg/cosign/attestation/attestation.go#L39-L43
type CosignPredicate struct {
Data interface{}
Data any
}

// Statement holds in-toto statement headers and the predicate.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/aws/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func EndpointResolver(endpoint string) aws.EndpointResolverWithOptionsFunc {
return aws.EndpointResolverWithOptionsFunc(func(_, reg string, options ...interface{}) (aws.Endpoint, error) {
return aws.EndpointResolverWithOptionsFunc(func(_, reg string, options ...any) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: endpoint,
Expand Down
2 changes: 1 addition & 1 deletion pkg/compliance/report/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (jw JSONWriter) Write(report *ComplianceReport) error {
var output []byte
var err error

var v interface{}
var v any
switch jw.Report {
case allReport:
v = report
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package xerrors

func Errorf(format string, a ...interface{}) error {
func Errorf(format string, a ...any) error {
return nil
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package xerrors

func Errorf(format string, a ...interface{}) error {
func Errorf(format string, a ...any) error {
return nil
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package xerrors

func Errorf(format string, a ...interface{}) error {
func Errorf(format string, a ...any) error {
return nil
}
14 changes: 7 additions & 7 deletions pkg/dependency/parser/nodejs/packagejson/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var nameRegexp = regexp.MustCompile(`^(@[A-Za-z0-9-._]+/)?[A-Za-z0-9-._]+$`)
type packageJSON struct {
Name string `json:"name"`
Version string `json:"version"`
License interface{} `json:"license"`
License any `json:"license"`
Dependencies map[string]string `json:"dependencies"`
OptionalDependencies map[string]string `json:"optionalDependencies"`
DevDependencies map[string]string `json:"devDependencies"`
Expand Down Expand Up @@ -69,14 +69,14 @@ func (p *Parser) Parse(r io.Reader) (Package, error) {
}, nil
}

func parseLicense(val interface{}) []string {
func parseLicense(val any) []string {
// the license isn't always a string, check for legacy struct if not string
switch v := val.(type) {
case string:
if v != "" {
return []string{v}
}
case map[string]interface{}:
case map[string]any:
if license, ok := v["type"]; ok {
if s, ok := license.(string); ok && s != "" {
return []string{s}
Expand All @@ -92,17 +92,17 @@ func parseWorkspaces(val any) []string {
switch ws := val.(type) {
// Workspace as object (map[string][]string)
// e.g. "workspaces": {"packages": ["packages/*", "plugins/*"]},
case map[string]interface{}:
case map[string]any:
// Take only workspaces for `packages` - https://classic.yarnpkg.com/blog/2018/02/15/nohoist/
if pkgsWorkspaces, ok := ws["packages"]; ok {
return lo.Map(pkgsWorkspaces.([]interface{}), func(workspace interface{}, _ int) string {
return lo.Map(pkgsWorkspaces.([]any), func(workspace any, _ int) string {
return workspace.(string)
})
}
// Workspace as string array
// e.g. "workspaces": ["packages/*", "backend"],
case []interface{}:
return lo.Map(ws, func(workspace interface{}, _ int) string {
case []any:
return lo.Map(ws, func(workspace any, _ int) string {
return workspace.(string)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/nodejs/pnpm/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (p *Parser) parseVersion(depPath, ver string, lockVer float64) string {
return ver
}

func isDirectPkg(name string, directDeps map[string]interface{}) bool {
func isDirectPkg(name string, directDeps map[string]any) bool {
_, ok := directDeps[name]
return ok
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/dependency/parser/python/poetry/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (

type Lockfile struct {
Packages []struct {
Category string `toml:"category"`
Description string `toml:"description"`
Marker string `toml:"marker,omitempty"`
Name string `toml:"name"`
Optional bool `toml:"optional"`
PythonVersions string `toml:"python-versions"`
Version string `toml:"version"`
Dependencies map[string]interface{} `toml:"dependencies"`
Metadata interface{}
Category string `toml:"category"`
Description string `toml:"description"`
Marker string `toml:"marker,omitempty"`
Name string `toml:"name"`
Optional bool `toml:"optional"`
PythonVersions string `toml:"python-versions"`
Version string `toml:"version"`
Dependencies map[string]any `toml:"dependencies"`
Metadata any
} `toml:"package"`
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (p *Parser) parseDependency(name string, versRange any, pkgVersions map[str
switch r := versRange.(type) {
case string:
vRange = r
case map[string]interface{}:
case map[string]any:
for k, v := range r {
if k == "version" {
vRange = v.(string)
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/python/poetry/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestParseDependency(t *testing.T) {
tests := []struct {
name string
packageName string
versionRange interface{}
versionRange any
pkgsVersions map[string][]string
want string
wantErr string
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestParseDependency(t *testing.T) {
{
name: "version range as json",
packageName: "test",
versionRange: map[string]interface{}{
versionRange: map[string]any{
"version": ">=4.8.3",
"markers": "python_version < \"3.8\"",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/python/pyproject/pyproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Tool struct {
}

type Poetry struct {
Dependencies map[string]interface{} `toml:"dependencies"`
Dependencies map[string]any `toml:"dependencies"`
}

// Parser parses pyproject.toml defined in PEP518.
Expand All @@ -28,7 +28,7 @@ func NewParser() *Parser {
return &Parser{}
}

func (p *Parser) Parse(r io.Reader) (map[string]interface{}, error) {
func (p *Parser) Parse(r io.Reader) (map[string]any, error) {
var conf PyProject
if _, err := toml.NewDecoder(r).Decode(&conf); err != nil {
return nil, xerrors.Errorf("toml decode error: %w", err)
Expand Down
12 changes: 6 additions & 6 deletions pkg/dependency/parser/python/pyproject/pyproject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ func TestParser_Parse(t *testing.T) {
tests := []struct {
name string
file string
want map[string]interface{}
want map[string]any
wantErr assert.ErrorAssertionFunc
}{
{
name: "happy path",
file: "testdata/happy.toml",
want: map[string]interface{}{
want: map[string]any{
"flask": "^1.0",
"python": "^3.9",
"requests": map[string]interface{}{
"requests": map[string]any{
"version": "2.28.1",
"optional": true,
},
"virtualenv": []interface{}{
map[string]interface{}{
"virtualenv": []any{
map[string]any{
"version": "^20.4.3,!=20.4.5,!=20.4.6",
},
map[string]interface{}{
map[string]any{
"version": "<20.16.6",
"markers": "sys_platform == 'win32' and python_version == '3.9'",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/swift/cocoapods/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
continue
}
parsedDeps[pkg.Name] = pkg
case map[string]interface{}: // dependency with its child dependencies
case map[string]any:
for dep, childDeps := range dep {
pkg, err := parseDep(dep)
if err != nil {
Expand All @@ -56,7 +56,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
}
parsedDeps[pkg.Name] = pkg

children, ok := childDeps.([]interface{})
children, ok := childDeps.([]any)
if !ok {
return nil, nil, xerrors.Errorf("invalid value of cocoapods direct dependency: %q", childDeps)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/fanal/analyzer/language/dart/pub/pubspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ func cacheDir() string {
}

type pubSpecYaml struct {
Name string `yaml:"name"`
Version string `yaml:"version,omitempty"`
Dependencies map[string]interface{} `yaml:"dependencies,omitempty"`
Name string `yaml:"name"`
Version string `yaml:"version,omitempty"`
Dependencies map[string]any `yaml:"dependencies,omitempty"`
}

func parsePubSpecYaml(r io.Reader) (string, []string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/python/poetry/poetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (a poetryAnalyzer) mergePyProject(fsys fs.FS, dir string, app *types.Applic
return nil
}

func (a poetryAnalyzer) parsePyProject(fsys fs.FS, path string) (map[string]interface{}, error) {
func (a poetryAnalyzer) parsePyProject(fsys fs.FS, path string) (map[string]any, error) {
// Parse pyproject.toml
f, err := fsys.Open(path)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/language/rust/cargo/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type cargoTomlWorkspace struct {
Members []string `toml:"members"`
}

type Dependencies map[string]interface{}
type Dependencies map[string]any

// parseRootCargoTOML parses top-level Cargo.toml and returns dependencies.
// It also parses workspace members and their dependencies.
Expand Down Expand Up @@ -196,7 +196,7 @@ func (a cargoAnalyzer) parseRootCargoTOML(fsys fs.FS, filePath string) (map[stri
case string:
// e.g. regex = "1.5"
deps[name] = ver
case map[string]interface{}:
case map[string]any:
// e.g. serde = { version = "1.0", features = ["derive"] }
for k, v := range ver {
if k == "version" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/applier/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func ApplyLayers(layers []ftypes.BlobInfo) ftypes.ArtifactDetail {
}

// nolint
_ = nestedMap.Walk(func(keys []string, value interface{}) error {
_ = nestedMap.Walk(func(keys []string, value any) error {
switch v := value.(type) {
case ftypes.PackageInfo:
mergedLayer.Packages = append(mergedLayer.Packages, v.Packages...)
Expand Down
Loading

0 comments on commit ebb123f

Please sign in to comment.