Skip to content

Commit

Permalink
fixes: rename Key to Name
Browse files Browse the repository at this point in the history
This is to make it more consistent with rule names

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Apr 18, 2024
1 parent eda82ba commit 71c4f52
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
10 changes: 5 additions & 5 deletions pkg/fixer/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (f *Fixer) RegisterFixes(fixes ...fixes.Fix) {
}

for _, fix := range fixes {
f.registeredFixes[fix.Key()] = fix
f.registeredFixes[fix.Name()] = fix
}
}

func (f *Fixer) GetFixForKey(key string) (fixes.Fix, bool) {
fix, ok := f.registeredFixes[key]
func (f *Fixer) GetFixForName(name string) (fixes.Fix, bool) {
fix, ok := f.registeredFixes[name]
if !ok {
return nil, false
}
Expand All @@ -52,7 +52,7 @@ func (f *Fixer) Fix(ctx context.Context, l *linter.Linter, fp fileprovider.FileP
var fixableEnabledRules []string

for _, rule := range enabledRules {
if _, ok := f.GetFixForKey(rule); ok {
if _, ok := f.GetFixForName(rule); ok {
fixableEnabledRules = append(fixableEnabledRules, rule)
}
}
Expand All @@ -77,7 +77,7 @@ func (f *Fixer) Fix(ctx context.Context, l *linter.Linter, fp fileprovider.FileP
}

for _, violation := range rep.Violations {
fixInstance, ok := f.GetFixForKey(violation.Title)
fixInstance, ok := f.GetFixForName(violation.Title)
if !ok {
return nil, fmt.Errorf("no fix for violation %s", violation.Title)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/fixer/fixes/fixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func NewDefaultFixes() []Fix {
return []Fix{
&Fmt{},
&Fmt{
KeyOverride: "use-rego-v1",
NameOverride: "use-rego-v1",
OPAFmtOpts: format.Opts{
RegoVersion: ast.RegoV0CompatV1,
},
Expand All @@ -23,9 +23,9 @@ func NewDefaultFixes() []Fix {

// Fix is the interface that must be implemented by all fixes.
type Fix interface {
// Key returns the unique key for the fix, this should correlate with the
// violation that the fix is meant to address.
Key() string
// Name returns the unique name for the fix, this should correlate with the
// violation title & rule name that the fix is meant to address.
Name() string
Fix(fc *FixCandidate, opts *RuntimeOptions) ([]FixResult, error)
}

Expand Down
18 changes: 8 additions & 10 deletions pkg/fixer/fixes/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
)

type Fmt struct {
// KeyOverride allows this fix config to also be registered under another key, see note
// in Key().
KeyOverride string
// NameOverride allows this fix config to also be registered under another name, see note
// in Name().
NameOverride string
// OPAFmtOpts are the options to pass to OPA's format.SourceWithOpts
// function.
OPAFmtOpts format.Opts
}

func (f *Fmt) Key() string {
// this allows this fix config to also be registered under another key so that different
// configurations can be registered under other linter rule keys.
if f.KeyOverride != "" {
return f.KeyOverride
func (f *Fmt) Name() string {
// this allows this fix config to also be registered under another name so that different
// configurations can be registered under other linter rule names.
if f.NameOverride != "" {
return f.NameOverride
}

return "opa-fmt"
Expand All @@ -41,8 +41,6 @@ func (f *Fmt) Fix(fc *FixCandidate, _ *RuntimeOptions) ([]FixResult, error) {
return nil, nil
}

// we always return true because the fix still completed successfully, and
// then we can say that the violation with this instance's key was fixed too.
return []FixResult{
{
Contents: formatted,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fixer/fixes/nowhitespacecomment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type NoWhitespaceComment struct{}

func (*NoWhitespaceComment) Key() string {
func (*NoWhitespaceComment) Name() string {
return "no-whitespace-comment"
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/fixer/fixes/useassignmentoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type UseAssignmentOperator struct{}

func (*UseAssignmentOperator) Key() string {
func (*UseAssignmentOperator) Name() string {
return "use-assignment-operator"
}

Expand Down

0 comments on commit 71c4f52

Please sign in to comment.