Skip to content

Commit

Permalink
make the models private where possible
Browse files Browse the repository at this point in the history
Update tests where required
  • Loading branch information
owenrumney committed Oct 24, 2020
1 parent da5548f commit c494769
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 69 deletions.
4 changes: 2 additions & 2 deletions models/general.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package models

// TextBlock is a general block which includes a text attribute
type TextBlock struct {
// textBlock is a general block which includes a text attribute
type textBlock struct {
Text string `json:"text"`
}
30 changes: 15 additions & 15 deletions models/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ package models
// Result represents the results block in the sarif report
type Result struct {
Level string `json:"level"`
Message *TextBlock `json:"message"`
Message *textBlock `json:"message"`
RuleId string `json:"ruleId"`
RuleIndex int `json:"ruleIndex"`
Locations []*ResultLocation `json:"locations,omitempty"`
Locations []*resultLocation `json:"locations,omitempty"`
}

type ResultLocation struct {
PhysicalLocation *PhysicalLocation `json:"physicalLocation,omitempty"`
type resultLocation struct {
PhysicalLocation *physicalLocation `json:"physicalLocation,omitempty"`
}

type PhysicalLocation struct {
ArtifactLocation *ArtifactLocation `json:"artifactLocation"`
Region *Region `json:"region"`
type physicalLocation struct {
ArtifactLocation *artifactLocation `json:"artifactLocation"`
Region *region `json:"region"`
}

type Region struct {
type region struct {
StartLine int `json:"startLine"`
StartColumn int `json:"startColumn"`
}

type ArtifactLocation struct {
type artifactLocation struct {
Uri string `json:"uri"`
Index int `json:"index"`
}

type Location struct {
type l struct {
Uri string `json:"uri"`
}

Expand All @@ -44,23 +44,23 @@ func (result *Result) WithLevel(level string) *Result {
}

func (result *Result) WithMessage(message string) *Result {
result.Message = &TextBlock{
result.Message = &textBlock{
Text: message,
}
return result
}

func (result *Result) WithLocationDetails(path string, startLine, startColumn int) *Result {
location := &PhysicalLocation{
ArtifactLocation: &ArtifactLocation{
location := &physicalLocation{
ArtifactLocation: &artifactLocation{
Uri: path,
},
Region: &Region{
Region: &region{
StartLine: startLine,
StartColumn: startColumn,
},
}
result.Locations = append(result.Locations, &ResultLocation{
result.Locations = append(result.Locations, &resultLocation{
PhysicalLocation: location,
})
return result
Expand Down
33 changes: 23 additions & 10 deletions models/run.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
package models

type Run struct {
Tool *Tool `json:"tool"`
Tool *tool `json:"tool"`
Artifacts []*LocationWrapper `json:"artifacts,omitempty"`
Results []*Result `json:"results,omitempty"`
}

type LocationWrapper struct {
Location *Location `json:"location,omitentry"`
Location *l `json:"location,omitentry"`
}

// AddArtifact returns the index of the newly added ArtifactLocation
func (run *Run) AddArtifact(location *Location) int {
func NewRun(toolName, informationUri string) *Run {
tool := &tool{
Driver: &driver{
Name: toolName,
InformationUri: informationUri,
},
}
run := &Run{
Tool: tool,
}
return run
}

// AddArtifact returns the index of the newly added artifactLocation
func (run *Run) AddArtifact(location string) int {
for i, l := range run.Artifacts {
if l.Location.Uri == location.Uri {
if l.Location.Uri == location {
return i
}
}
run.Artifacts = append(run.Artifacts, &LocationWrapper{
Location: &Location{
Uri: location.Uri,
Location: &l{
Uri: location,
},
})
return len(run.Artifacts) - 1
}

func (run *Run) AddRule(ruleId string) *Rule {
func (run *Run) AddRule(ruleId string) *rule {
for _, rule := range run.Tool.Driver.Rules {
if rule.Id == ruleId {
return rule
Expand All @@ -48,10 +61,10 @@ func (run *Run) AddResult(ruleId string) *Result {
}

// AddResultDetails adds rules to the driver and artifact locations if they are missing. It adds the result to the result block as well
func (run *Run) AddResultDetails(rule *Rule, result *Result, location string) {
func (run *Run) AddResultDetails(rule *rule, result *Result, location string) {
ruleIndex := run.Tool.Driver.getOrCreateRule(rule)
result.RuleIndex = ruleIndex
locationIndex := run.AddArtifact(&Location{Uri: location})
locationIndex := run.AddArtifact(location)
updateResultLocationIndex(result, location, locationIndex)
}

Expand Down
26 changes: 13 additions & 13 deletions models/tool.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package models

type Tool struct {
Driver *Driver `json:"driver"`
type tool struct {
Driver *driver `json:"driver"`
}

type Driver struct {
type driver struct {
Name string `json:"name"`
InformationUri string `json:"informationUri"`
Rules []*Rule `json:"rules,omitempty"`
Rules []*rule `json:"rules,omitempty"`
}

type Rule struct {
type rule struct {
Id string `json:"id"`
ShortDescription *TextBlock `json:"shortDescription"`
ShortDescription *textBlock `json:"shortDescription"`
HelpUri string `json:"helpUri"`
Properties map[string]string `json:"properties,omitempty"`
}

func (driver *Driver) getOrCreateRule(rule *Rule) int {
func (driver *driver) getOrCreateRule(rule *rule) int {
for i, r := range driver.Rules {
if r.Id == rule.Id {
return i
Expand All @@ -27,25 +27,25 @@ func (driver *Driver) getOrCreateRule(rule *Rule) int {
return len(driver.Rules) - 1
}

func newRule(ruleId string) *Rule {
return &Rule{
func newRule(ruleId string) *rule {
return &rule{
Id: ruleId,
}
}

func (rule *Rule) WithDescription(description string) *Rule {
rule.ShortDescription = &TextBlock{
func (rule *rule) WithDescription(description string) *rule {
rule.ShortDescription = &textBlock{
Text: description,
}
return rule
}

func (rule *Rule) WithHelpUri(helpUrl string) *Rule {
func (rule *rule) WithHelpUri(helpUrl string) *rule {
rule.HelpUri = helpUrl
return rule
}

func (rule *Rule) WithProperties(properties map[string]string) *Rule {
func (rule *rule) WithProperties(properties map[string]string) *rule {
rule.Properties = properties
return rule
}
10 changes: 1 addition & 9 deletions sarif/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ func New(version Version) (*Report, error) {
}

func (sarif *Report) AddRun(toolName, informationUri string) *models.Run {
tool := &models.Tool{
Driver: &models.Driver{
Name: toolName,
InformationUri: informationUri,
},
}
run := &models.Run{
Tool: tool,
}
run := models.NewRun(toolName, informationUri)
sarif.Runs = append(sarif.Runs, run)
return run
}
Expand Down
23 changes: 3 additions & 20 deletions test/run_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ func createNewRunTest(t *testing.T) (*runTest, *runTest, *runTest) {
}

func (rt *runTest) a_new_run_is_created() {
rt.run = &models.Run{
Tool: &models.Tool{
Driver: &models.Driver{
Name: "tfsec",
InformationUri: "https://tfsec.dev",
},
},
}
rt.run = models.NewRun("tfsec", "https://tfsec.dev")
}

func (rt *runTest) the_run_is_converted_to_a_string() {
Expand All @@ -45,13 +38,7 @@ func (rt *runTest) the_json_string_representation_of_the_run_should_be(expected
}

func (rt *runTest) an_artifact_is_added_to_the_run(locationUri string) *runTest {

location := &models.Location{
Uri: locationUri,
}

rt.run.AddArtifact(location)

rt.run.AddArtifact(locationUri)
return rt
}

Expand All @@ -60,11 +47,7 @@ func (rt *runTest) and() *runTest {
}

func (rt *runTest) the_index_of_location_is(locationUri string, expectedIndex int) *runTest {
location := &models.Location{
Uri: locationUri,
}

locationIndex := rt.run.AddArtifact(location)
locationIndex := rt.run.AddArtifact(locationUri)
assert.Equal(rt.t, expectedIndex, locationIndex)
return rt
}
Expand Down

0 comments on commit c494769

Please sign in to comment.