-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresult.go
77 lines (63 loc) · 1.76 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package codacytool
import (
"encoding/json"
"github.com/CycloneDX/cyclonedx-go"
"github.com/sirupsen/logrus"
)
// Result encompasses all possible results: Issues and File Errors.
type Result interface {
// ToJSON returns a JSON representation of the result.
ToJSON() ([]byte, error)
// GetFile returns the file for this result.
GetFile() string
}
// Issue is the output for each issue found by the tool.
type Issue struct {
PatternID string `json:"patternId"`
File string `json:"filename"`
Line int `json:"line"`
Message string `json:"message"`
Suggestion string `json:"suggestion,omitempty"`
}
func (i Issue) ToJSON() ([]byte, error) {
return json.Marshal(i)
}
func (i Issue) GetFile() string {
return i.File
}
// FileError represents an error analysing a file.
// If this result is returned from an analysis, the referenced file is not considered to have been analysed.
type FileError struct {
File string `json:"filename"`
Message string `json:"message"`
}
func (i FileError) ToJSON() ([]byte, error) {
return json.Marshal(i)
}
func (i FileError) GetFile() string {
return i.File
}
// SBOM represents a Software Bill of Materials in the CycloneDX format.
type SBOM struct {
cyclonedx.BOM
}
func (s SBOM) ToJSON() ([]byte, error) {
return json.Marshal(s)
}
// GetFile always returns an empty value since SBOM is for the whole project, not a single file.
func (s SBOM) GetFile() string {
return ""
}
type Results []Result
func (r Results) ToJSON() []string {
var jsonResults []string
for _, result := range r {
jsonResult, err := result.ToJSON()
if err != nil {
logrus.Errorf("Failed to convert Result to JSON: %+v\n%s", result, err.Error())
} else {
jsonResults = append(jsonResults, string(jsonResult))
}
}
return jsonResults
}