Skip to content
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
chore: Change empty interface{} to any throughout the codebase
The any type is a clearer intent than an empty interface.

Signed-off-by: James Alseth <[email protected]>
  • Loading branch information
jalseth committed Feb 19, 2025
commit ec78094098498bb9a9f95cb21e335582cf8c12e7
6 changes: 3 additions & 3 deletions internal/commands/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewParseCommand() *cobra.Command {
return nil
},
RunE: func(_ *cobra.Command, files []string) error {
var configurations map[string]interface{}
var configurations map[string]any
var err error
if viper.GetString("parser") != "" {
configurations, err = parser.ParseConfigurationsAs(files, viper.GetString("parser"))
Expand Down Expand Up @@ -77,11 +77,11 @@ func NewParseCommand() *cobra.Command {
return &cmd
}

func formatSingleJSON(configurations map[string]interface{}) (string, error) {
func formatSingleJSON(configurations map[string]any) (string, error) {
if len(configurations) != 1 {
return "", fmt.Errorf("formatSingleJSON: only supports one configuration")
}
var config interface{}
var config any
for _, cfg := range configurations {
config = cfg
}
Expand Down
10 changes: 5 additions & 5 deletions output/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import "fmt"

// Result describes the result of a single rule evaluation.
type Result struct {
Message string `json:"msg"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Outputs []string `json:"outputs,omitempty"`
Message string `json:"msg"`
Metadata map[string]any `json:"metadata,omitempty"`
Outputs []string `json:"outputs,omitempty"`
}

// NewResult creates a new result. An error is returned if the
// metadata could not be successfully parsed.
func NewResult(metadata map[string]interface{}) (Result, error) {
func NewResult(metadata map[string]any) (Result, error) {
if _, ok := metadata["msg"]; !ok {
return Result{}, fmt.Errorf("rule missing msg field: %v", metadata)
}
Expand All @@ -21,7 +21,7 @@ func NewResult(metadata map[string]interface{}) (Result, error) {

result := Result{
Message: metadata["msg"].(string),
Metadata: make(map[string]interface{}),
Metadata: make(map[string]any),
}

for k, v := range metadata {
Expand Down
4 changes: 2 additions & 2 deletions output/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ func (s *SARIF) Output(results []CheckResult) error {
if hasSuccesses {
statusResult := Result{
Message: successDesc,
Metadata: map[string]interface{}{
Metadata: map[string]any{
"description": successDesc,
},
}
addResult(run, statusResult, result.Namespace, "success", "none", result.FileName, indices)
} else {
statusResult := Result{
Message: skippedDesc,
Metadata: map[string]interface{}{
Metadata: map[string]any{
"description": skippedDesc,
},
}
Expand Down
12 changes: 6 additions & 6 deletions output/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestSARIF_Output(t *testing.T) {
Failures: []Result{
{
Message: "test failure",
Metadata: map[string]interface{}{
Metadata: map[string]any{
"package": "test",
"rule": "rule1",
},
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestSARIF_Output(t *testing.T) {
Warnings: []Result{
{
Message: "test warning",
Metadata: map[string]interface{}{
Metadata: map[string]any{
"foo": "bar",
},
},
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestSARIF_Output(t *testing.T) {
Exceptions: []Result{
{
Message: "test exception",
Metadata: map[string]interface{}{
Metadata: map[string]any{
"description": "test exception description",
},
},
Expand Down Expand Up @@ -344,14 +344,14 @@ func TestSARIF_Output(t *testing.T) {
Failures: []Result{
{
Message: "test failure 1",
Metadata: map[string]interface{}{
Metadata: map[string]any{
"package": "test",
"rule": "rule1",
},
},
{
Message: "test failure 2",
Metadata: map[string]interface{}{
Metadata: map[string]any{
"package": "test",
"rule": "rule1",
},
Expand Down Expand Up @@ -575,7 +575,7 @@ func TestSARIF_Report(t *testing.T) {
// JSON strings are normalised to their canonical form without whitespace.
func compareJSON(t *testing.T, got, want string) {
t.Helper()
var gotJSON, wantJSON interface{}
var gotJSON, wantJSON any
if err := json.Unmarshal([]byte(got), &gotJSON); err != nil {
t.Fatalf("failed to unmarshal actual JSON: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion parser/cue/cue.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type Parser struct{}

// Unmarshal unmarshals CUE files.
func (*Parser) Unmarshal(p []byte, v interface{}) error {
func (*Parser) Unmarshal(p []byte, v any) error {
out, err := cformat.Source(p)
if err != nil {
return fmt.Errorf("format cue: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions parser/cue/cue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCueParser(t *testing.T) {

parser := &Parser{}

var input interface{}
var input any
if err := parser.Unmarshal([]byte(p), &input); err != nil {
t.Fatalf("parser should not have thrown an error: %v", err)
}
Expand All @@ -34,7 +34,7 @@ func TestCueParser(t *testing.T) {
t.Error("There should be information parsed but its nil")
}

inputMap := input.(map[string]interface{})
inputMap := input.(map[string]any)
kind := inputMap["kind"]
if kind != "Deployment" {
t.Error("Parsed cuelang file should be a deployment, but was not")
Expand Down
2 changes: 1 addition & 1 deletion parser/cyclonedx/cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Parser struct{}

// Unmarshal unmarshals CycloneDX files.
func (*Parser) Unmarshal(p []byte, v interface{}) error {
func (*Parser) Unmarshal(p []byte, v any) error {
bomFileFormat := cyclonedx.BOMFileFormatJSON
if !json.Valid(p) {
bomFileFormat = cyclonedx.BOMFileFormatXML
Expand Down
16 changes: 8 additions & 8 deletions parser/cyclonedx/cyclonedx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCycloneDXParserValid(t *testing.T) {

parser := &Parser{}

var input interface{}
var input any
if err := parser.Unmarshal([]byte(sbom), &input); err != nil {
t.Fatalf("parser should not have thrown an error: %v", err)
}
Expand All @@ -52,9 +52,9 @@ func TestCycloneDXParserValid(t *testing.T) {
//#nosec until https://github.com/securego/gosec/issues/1001 is fixed
expectedSHA256 := "sha256:d7ec60cf8390612b360c857688b383068b580d9a6ab78417c9493170ad3f1616"

metadata := input.(map[string]interface{})["metadata"]
component := metadata.(map[string]interface{})["component"]
currentSHA256 := component.(map[string]interface{})["version"]
metadata := input.(map[string]any)["metadata"]
component := metadata.(map[string]any)["component"]
currentSHA256 := component.(map[string]any)["version"]

if expectedSHA256 != currentSHA256 {
t.Fatalf("current SHA256 %s is different from the expected SHA256 %s", currentSHA256, expectedSHA256)
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestCycloneDXParserInValid(t *testing.T) {

parser := &Parser{}

var input interface{}
var input any
if err := parser.Unmarshal([]byte(sbom), &input); err != nil {
t.Fatalf("parser should not have thrown an error: %v", err)
}
Expand All @@ -107,9 +107,9 @@ func TestCycloneDXParserInValid(t *testing.T) {
//#nosec until https://github.com/securego/gosec/issues/1001 is fixed
expectedSHA256 := "sha256:d7ec60cf8390612b360c857688b383068b580d9a6ab78417c9493170ad3f1616"

metadata := input.(map[string]interface{})["metadata"]
component := metadata.(map[string]interface{})["component"]
currentSHA256 := component.(map[string]interface{})["version"]
metadata := input.(map[string]any)["metadata"]
component := metadata.(map[string]any)["component"]
currentSHA256 := component.(map[string]any)["version"]

var err error
if expectedSHA256 != currentSHA256 {
Expand Down
2 changes: 1 addition & 1 deletion parser/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Command struct {
}

// Unmarshal unmarshals Dockerfiles
func (dp *Parser) Unmarshal(p []byte, v interface{}) error {
func (dp *Parser) Unmarshal(p []byte, v any) error {
r := bytes.NewReader(p)
res, err := parser.Parse(r)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions parser/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestParser_Unmarshal(t *testing.T) {
COPY . /
RUN echo hello`

var input interface{}
var input any
if err := parser.Unmarshal([]byte(sample), &input); err != nil {
t.Fatalf("parser should not have thrown an error: %v", err)
}
Expand All @@ -20,11 +20,11 @@ RUN echo hello`
t.Error("there should be information parsed but its nil")
}

dockerFile := input.([]interface{})[0]
commands := dockerFile.([]interface{})[0]
dockerFile := input.([]any)[0]
commands := dockerFile.([]any)[0]

expected := "from"
actual := commands.(map[string]interface{})["Cmd"]
actual := commands.(map[string]any)["Cmd"]

if actual != expected {
t.Errorf("first Docker command should be '%v', was '%v'", expected, actual)
Expand All @@ -44,7 +44,7 @@ COPY . .
FROM base as builder
RUN go build -o conftest`

var input interface{}
var input any
if err := parser.Unmarshal([]byte(sample), &input); err != nil {
t.Fatalf("parser should not have thrown an error: %v", err)
}
Expand All @@ -53,17 +53,17 @@ RUN go build -o conftest`
t.Error("there should be information parsed but its nil")
}

dockerFile := input.([]interface{})[0]
commands := dockerFile.([]interface{})
dockerFile := input.([]any)[0]
commands := dockerFile.([]any)

cmd := commands[1]
stage := cmd.(map[string]interface{})["Stage"].(float64)
stage := cmd.(map[string]any)["Stage"].(float64)
if stage != 0 {
t.Errorf("expected command to be in stage 0, not stage: %v", stage)
}

cmd = commands[6]
stage = cmd.(map[string]interface{})["Stage"].(float64)
stage = cmd.(map[string]any)["Stage"].(float64)
if stage != 1 {
t.Errorf("expected command to be in stage 1, not stage: %v", stage)
}
Expand Down
2 changes: 1 addition & 1 deletion parser/dotenv/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type Parser struct{}

// Unmarshal unmarshals dotenv files.
func (i *Parser) Unmarshal(p []byte, v interface{}) error {
func (i *Parser) Unmarshal(p []byte, v any) error {
r := bytes.NewReader(p)
cfg, err := gotenv.StrictParse(r)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions parser/dotenv/dotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestDotenvParser(t *testing.T) {
MYSQL_USER=root
MYSQL_PASSWORD=root`

var input interface{}
var input any
if err := parser.Unmarshal([]byte(sample), &input); err != nil {
t.Fatalf("parser should not have thrown an error: %v", err)
}
Expand All @@ -22,7 +22,7 @@ func TestDotenvParser(t *testing.T) {
t.Error("there should be information parsed but its nil")
}

inputMap := input.(map[string]interface{})
inputMap := input.(map[string]any)
if len(inputMap) == 0 {
t.Error("there should be at least one item defined in the parsed file, but none found")
}
Expand Down
20 changes: 10 additions & 10 deletions parser/edn/edn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ import (
type Parser struct{}

// Unmarshal unmarshals EDN encoded files.
func (tp *Parser) Unmarshal(p []byte, v interface{}) error {
var res interface{}
func (tp *Parser) Unmarshal(p []byte, v any) error {
var res any

if err := edn.Unmarshal(p, &res); err != nil {
return fmt.Errorf("unmarshal EDN: %w", err)
}

*v.(*interface{}) = cleanupMapValue(res)
*v.(*any) = cleanupMapValue(res)

return nil
}

func cleanupInterfaceArray(in []interface{}) []interface{} {
res := make([]interface{}, len(in))
func cleanupInterfaceArray(in []any) []any {
res := make([]any, len(in))
for i, v := range in {
res[i] = cleanupMapValue(v)
}
return res
}

func cleanupInterfaceMap(in map[interface{}]interface{}) map[string]interface{} {
res := make(map[string]interface{})
func cleanupInterfaceMap(in map[any]any) map[string]any {
res := make(map[string]any)
for k, v := range in {
res[fmt.Sprintf("%v", k)] = cleanupMapValue(v)
}
return res
}

func cleanupMapValue(v interface{}) interface{} {
func cleanupMapValue(v any) any {
switch v := v.(type) {
case []interface{}:
case []any:
return cleanupInterfaceArray(v)
case map[interface{}]interface{}:
case map[any]any:
return cleanupInterfaceMap(v)
case string:
return v
Expand Down
8 changes: 4 additions & 4 deletions parser/edn/edn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ func TestEDNParser(t *testing.T) {
testTable := []struct {
name string
controlConfigs []byte
expectedResult interface{}
expectedResult any
}{
{
name: "a single config",
controlConfigs: []byte(`{:sample true}`),
expectedResult: map[string]interface{}{
expectedResult: map[string]any{
":sample": "true",
},
},
Expand All @@ -26,7 +26,7 @@ func TestEDNParser(t *testing.T) {
:sample1 "my-username",
:sample2 false,
:sample3 5432}`),
expectedResult: map[string]interface{}{
expectedResult: map[string]any{
":sample1": "my-username",
":sample2": "false",
":sample3": "5432",
Expand All @@ -36,7 +36,7 @@ func TestEDNParser(t *testing.T) {

for _, test := range testTable {
t.Run(test.name, func(t *testing.T) {
var unmarshalledConfigs interface{}
var unmarshalledConfigs any
ednParser := new(edn.Parser)

if err := ednParser.Unmarshal(test.controlConfigs, &unmarshalledConfigs); err != nil {
Expand Down
Loading