Skip to content

Commit

Permalink
Rename OutputMapping to ResultMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvelabs committed Jan 13, 2023
1 parent 543417f commit 6dd77f3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 92 deletions.
4 changes: 2 additions & 2 deletions internal/stylist/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Command struct {
InputType InputType `yaml:"input" default:"variadic"`
OutputType OutputType `yaml:"output" default:"stdout"`
OutputFormat OutputFormat `yaml:"format" default:"none"`
OutputMapping OutputMapping `yaml:"mapping"`
ResultMapping ResultMapping `yaml:"mapping"`
Parallelism int `yaml:"parallelism"`
BatchSize int `yaml:"batch_size"`
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (c *Command) executeBatch(ctx context.Context, paths []string) ([]*Result,
logger.Debugln("Output:", output.String())

// Parse the output using the appropriate parser.
parsed, err := NewOutputParser(c.OutputFormat).Parse(output, c.OutputMapping)
parsed, err := NewOutputParser(c.OutputFormat).Parse(output, c.ResultMapping)
if err != nil {
return nil, err
}
Expand Down
18 changes: 9 additions & 9 deletions internal/stylist/output_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
//
// Parse parses command output into a slice of results.
type OutputParser interface {
Parse(output CommandOutput, mapping OutputMapping) ([]*Result, error)
Parse(output CommandOutput, mapping ResultMapping) ([]*Result, error)
}

// NewOutputParser returns the appropriate parser for the given output type.
Expand Down Expand Up @@ -39,7 +39,7 @@ type JSONOutputParser struct {
}

// Parse parses command output into a slice of results.
func (p *JSONOutputParser) Parse(output CommandOutput, mapping OutputMapping) ([]*Result, error) {
func (p *JSONOutputParser) Parse(output CommandOutput, mapping ResultMapping) ([]*Result, error) {
buf := &bytes.Buffer{}
_, err := buf.ReadFrom(output.Content)
if err != nil {
Expand All @@ -65,8 +65,8 @@ func (p *JSONOutputParser) Parse(output CommandOutput, mapping OutputMapping) ([
}
result := gjson.Get(json, pattern)

// Transform the GJSON results into outputData
items := []outputData{}
// Transform the GJSON results into resultData
items := []resultData{}
if !result.IsArray() {
return nil, fmt.Errorf(
"invalid output: pattern=%v is not an array, json=%v",
Expand All @@ -81,10 +81,10 @@ func (p *JSONOutputParser) Parse(output CommandOutput, mapping OutputMapping) ([
)
}
item := r.Value().(map[string]any)
items = append(items, outputData(item))
items = append(items, resultData(item))
}

// Transform the outputData into `Result` structs.
// Transform the resultData into `Result` structs.
return mapping.ToResultSlice(items)
}

Expand All @@ -97,7 +97,7 @@ type NoneOutputParser struct {
}

// Parse parses command output into a slice of results.
func (p *NoneOutputParser) Parse(output CommandOutput, mapping OutputMapping) ([]*Result, error) {
func (p *NoneOutputParser) Parse(output CommandOutput, mapping ResultMapping) ([]*Result, error) {
return nil, nil
}

Expand All @@ -110,7 +110,7 @@ type RegexpOutputParser struct {
}

// Parse parses command output into a slice of results.
func (p *RegexpOutputParser) Parse(output CommandOutput, mapping OutputMapping) ([]*Result, error) {
func (p *RegexpOutputParser) Parse(output CommandOutput, mapping ResultMapping) ([]*Result, error) {
return nil, nil
}

Expand All @@ -123,6 +123,6 @@ type SarifOutputParser struct {
}

// Parse parses command output into a slice of results.
func (p *SarifOutputParser) Parse(output CommandOutput, mapping OutputMapping) ([]*Result, error) {
func (p *SarifOutputParser) Parse(output CommandOutput, mapping ResultMapping) ([]*Result, error) {
return nil, nil
}
2 changes: 1 addition & 1 deletion internal/stylist/output_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestJSONOutputParser_Parse(t *testing.T) {
CommandOutput{
Content: file,
},
OutputMapping{
ResultMapping{
Level: render.MustCompile(`{{ .level }}`),
Path: render.MustCompile(`{{ .file }}`),
StartLine: render.MustCompile(`{{ .line }}`),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const (
strNoValue string = "<no value>"
)

// outputData is a map of key/value pairs parsed from CommandOutput
// and passed to an OutputMapping to be converted into a Result.
type outputData map[string]any
// resultData is a map of key/value pairs parsed from CommandOutput
// and passed to a ResultMapping to be converted into a Result.
type resultData map[string]any

// OutputMapping is a set of rules for how to map command output to a result.
// ResultMapping is a set of rules for how to map command output to a result.
//
// Mappings are typically defined in stylist.yml when the output type
// has been set to "json" or "regexp".
type OutputMapping struct {
type ResultMapping struct {
Pattern string `yaml:"pattern"`
Level *render.Template `yaml:"level"`
Path *render.Template `yaml:"path"`
Expand All @@ -34,7 +34,7 @@ type OutputMapping struct {
}

// ToResult converts a map of output data to a Result struct.
func (m OutputMapping) ToResult(item outputData) (*Result, error) {
func (m ResultMapping) ToResult(item resultData) (*Result, error) {
var err error

result := &Result{
Expand Down Expand Up @@ -89,7 +89,7 @@ func (m OutputMapping) ToResult(item outputData) (*Result, error) {
}

// ToResultSlice converts a slice of output data to a slice of results.
func (m OutputMapping) ToResultSlice(items []outputData) ([]*Result, error) {
func (m ResultMapping) ToResultSlice(items []resultData) ([]*Result, error) {
results := []*Result{}

for _, item := range items {
Expand All @@ -106,7 +106,7 @@ func (m OutputMapping) ToResultSlice(items []outputData) ([]*Result, error) {
// RenderLevel renders the Level template using item.
// The rendered value will be normalized to one of the valid ResultLevel
// enum values.
func (m OutputMapping) RenderLevel(item outputData) (ResultLevel, error) {
func (m ResultMapping) RenderLevel(item resultData) (ResultLevel, error) {
rendered, err := m.RenderString(m.Level, item)
if err != nil {
return ResultLevel(rendered), err
Expand All @@ -128,7 +128,7 @@ func (m OutputMapping) RenderLevel(item outputData) (ResultLevel, error) {

// RenderInt renders a template with the given output data and returns
// the rendered value as an int.
func (m OutputMapping) RenderInt(t *render.Template, item outputData) (int, error) {
func (m ResultMapping) RenderInt(t *render.Template, item resultData) (int, error) {
if t == nil {
return 0, nil
}
Expand All @@ -148,7 +148,7 @@ func (m OutputMapping) RenderInt(t *render.Template, item outputData) (int, erro

// RenderString renders a template with the given output data and returns
// the rendered value as a string.
func (m OutputMapping) RenderString(t *render.Template, item outputData) (string, error) {
func (m ResultMapping) RenderString(t *render.Template, item resultData) (string, error) {
if t == nil {
return strEmpty, nil
}
Expand Down
Loading

0 comments on commit 6dd77f3

Please sign in to comment.