Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed severity config via protolint yaml config #417

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
2 changes: 2 additions & 0 deletions _testdata/validconfig/protolint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ lint:

rules_option:
max_line_length:
severity: note
max_chars: 80
tab_chars: 2

indent:
severity: warning
style: tab
newline: "\n"
12 changes: 9 additions & 3 deletions internal/linter/config/customizableSeverityOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import "github.com/yoheimuta/protolint/linter/rule"
// CustomizableSeverityOption represents an option where the
// severity of a rule can be configured via yaml.
type CustomizableSeverityOption struct {
severity *rule.Severity `yaml:"severity" toml:"severity"`
// @ugly
// In order to unmarshall from yaml the field in the struct must be public.
// But we want to hide direct access to the field in order to
// force the usage of function below which contains the fallback to the error severity.
// This gives us a name clash so we added the internal suffix.
// I am no go expert (yet) but i am sure that a more elegant solution exists
SeverityInternal *rule.Severity `yaml:"severity" toml:"severity"`
}

// Severity returns the configured severity. If no severity
// is set, the default severity will be ERROR
func (c CustomizableSeverityOption) Severity() rule.Severity {
if c.severity == nil {
if c.SeverityInternal == nil {
return rule.SeverityError
}

return *c.severity
return *c.SeverityInternal
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// EnumFieldNamesZeroValueEndWithOption represents the option for the ENUM_FIELD_NAMES_ZERO_VALUE_END_WITH rule.
type EnumFieldNamesZeroValueEndWithOption struct {
CustomizableSeverityOption
Suffix string `yaml:"suffix" json:"suffix" toml:"suffix"`
CustomizableSeverityOption `yaml:",inline"`
Suffix string `yaml:"suffix" json:"suffix" toml:"suffix"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/enumFieldsHaveCommentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// EnumFieldsHaveCommentOption represents the option for the ENUM_FIELDS_HAVE_COMMENT rule.
type EnumFieldsHaveCommentOption struct {
CustomizableSeverityOption
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
CustomizableSeverityOption `yaml:",inline"`
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/enumsHaveCommentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// EnumsHaveCommentOption represents the option for the ENUMS_HAVE_COMMENT rule.
type EnumsHaveCommentOption struct {
CustomizableSeverityOption
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
CustomizableSeverityOption `yaml:",inline"`
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
}
10 changes: 10 additions & 0 deletions internal/linter/config/externalConfigProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/yoheimuta/protolint/internal/setting_test"

"github.com/yoheimuta/protolint/internal/linter/config"
"github.com/yoheimuta/protolint/linter/rule"
)

func TestGetExternalConfig(t *testing.T) {
Expand Down Expand Up @@ -65,10 +66,19 @@ func TestGetExternalConfig(t *testing.T) {
},
RulesOption: config.RulesOption{
MaxLineLength: config.MaxLineLengthOption{
CustomizableSeverityOption: config.CustomizableSeverityOption{
SeverityInternal: func(s rule.Severity) *rule.Severity { return &s }(rule.SeverityNote),
},
MaxChars: 80,
TabChars: 2,
},
Indent: config.IndentOption{
CustomizableSeverityOption: config.CustomizableSeverityOption{
SeverityInternal: func() *rule.Severity {
severity := rule.SeverityWarning
return &severity
}(),
},
Style: "\t",
Newline: "\n",
},
Expand Down
6 changes: 3 additions & 3 deletions internal/linter/config/fieldNamesExcludePrepositionsOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

// FieldNamesExcludePrepositionsOption represents the option for the FIELD_NAMES_EXCLUDE_PREPOSITIONS rule.
type FieldNamesExcludePrepositionsOption struct {
CustomizableSeverityOption
Prepositions []string `yaml:"prepositions" json:"prepositions" toml:"prepositions"`
Excludes []string `yaml:"excludes" json:"excludes" toml:"excludes"`
CustomizableSeverityOption `yaml:",inline"`
Prepositions []string `yaml:"prepositions" json:"prepositions" toml:"prepositions"`
Excludes []string `yaml:"excludes" json:"excludes" toml:"excludes"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/fieldsHaveCommentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// FieldsHaveCommentOption represents the option for the FIELDS_HAVE_COMMENT rule.
type FieldsHaveCommentOption struct {
CustomizableSeverityOption
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
CustomizableSeverityOption `yaml:",inline"`
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/fileNamesLowerSnakeCaseOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// FileNamesLowerSnakeCaseOption represents the option for the FILE_NAMES_LOWER_SNAKE_CASE rule.
type FileNamesLowerSnakeCaseOption struct {
CustomizableSeverityOption
Excludes []string `yaml:"excludes" json:"excludes" toml:"excludes"`
CustomizableSeverityOption `yaml:",inline"`
Excludes []string `yaml:"excludes" json:"excludes" toml:"excludes"`
}
4 changes: 3 additions & 1 deletion internal/linter/config/importsSortedOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type ImportsSortedOption struct {
// UnmarshalYAML implements yaml.v2 Unmarshaler interface.
func (i *ImportsSortedOption) UnmarshalYAML(unmarshal func(interface{}) error) error {
var option struct {
Newline string `yaml:"newline"`
CustomizableSeverityOption `yaml:",inline"`
Newline string `yaml:"newline"`
}
if err := unmarshal(&option); err != nil {
return err
Expand All @@ -26,6 +27,7 @@ func (i *ImportsSortedOption) UnmarshalYAML(unmarshal func(interface{}) error) e
default:
return fmt.Errorf(`%s is an invalid newline option. valid option is \n, \r or \r\n`, option.Newline)
}
i.CustomizableSeverityOption = option.CustomizableSeverityOption
return nil
}

Expand Down
8 changes: 5 additions & 3 deletions internal/linter/config/indentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type IndentOption struct {
// UnmarshalYAML implements yaml.v2 Unmarshaler interface.
func (i *IndentOption) UnmarshalYAML(unmarshal func(interface{}) error) error {
var option struct {
Style string `yaml:"style"`
Newline string `yaml:"newline"`
NotInsertNewline bool `yaml:"not_insert_newline"`
CustomizableSeverityOption `yaml:",inline"`
Style string `yaml:"style"`
Newline string `yaml:"newline"`
NotInsertNewline bool `yaml:"not_insert_newline"`
}
if err := unmarshal(&option); err != nil {
return err
Expand Down Expand Up @@ -47,6 +48,7 @@ func (i *IndentOption) UnmarshalYAML(unmarshal func(interface{}) error) error {
return fmt.Errorf(`%s is an invalid newline option. valid option is \n, \r or \r\n`, option.Newline)
}
i.NotInsertNewline = option.NotInsertNewline
i.CustomizableSeverityOption = option.CustomizableSeverityOption
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/linter/config/maxLineLengthOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

// MaxLineLengthOption represents the option for the MAX_LINE_LENGTH rule.
type MaxLineLengthOption struct {
CustomizableSeverityOption
MaxChars int `yaml:"max_chars" json:"max_chars" toml:"max_chars"`
TabChars int `yaml:"tab_chars" json:"tab_chars" toml:"tab_chars"`
CustomizableSeverityOption `yaml:",inline"`
MaxChars int `yaml:"max_chars" json:"max_chars" toml:"max_chars"`
TabChars int `yaml:"tab_chars" json:"tab_chars" toml:"tab_chars"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

// MessageNamesExcludePrepositionsOption represents the option for the MESSAGE_NAMES_EXCLUDE_PREPOSITIONS rule.
type MessageNamesExcludePrepositionsOption struct {
CustomizableSeverityOption
Prepositions []string `yaml:"prepositions" json:"prepositions" toml:"prepositions"`
Excludes []string `yaml:"excludes" json:"excludes" toml:"excludes"`
CustomizableSeverityOption `yaml:",inline"`
Prepositions []string `yaml:"prepositions" json:"prepositions" toml:"prepositions"`
Excludes []string `yaml:"excludes" json:"excludes" toml:"excludes"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/messagesHaveCommentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// MessagesHaveCommentOption represents the option for the MESSAGES_HAVE_COMMENT rule.
type MessagesHaveCommentOption struct {
CustomizableSeverityOption
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
CustomizableSeverityOption `yaml:",inline"`
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
}
4 changes: 3 additions & 1 deletion internal/linter/config/quoteConsistentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type QuoteConsistentOption struct {
// UnmarshalYAML implements yaml.v2 Unmarshaler interface.
func (r *QuoteConsistentOption) UnmarshalYAML(unmarshal func(interface{}) error) error {
var option struct {
Quote string `yaml:"quote"`
CustomizableSeverityOption `yaml:",inline"`
Quote string `yaml:"quote"`
}
if err := unmarshal(&option); err != nil {
return err
Expand All @@ -45,6 +46,7 @@ func (r *QuoteConsistentOption) UnmarshalYAML(unmarshal func(interface{}) error)
}
r.Quote = quote
}
r.CustomizableSeverityOption = option.CustomizableSeverityOption
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions internal/linter/config/repeatedFieldNamesPluralizedOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package config

// RepeatedFieldNamesPluralizedOption represents the option for the REPEATED_FIELD_NAMES_PLURALIZED rule.
type RepeatedFieldNamesPluralizedOption struct {
CustomizableSeverityOption
PluralRules map[string]string `yaml:"plural_rules" json:"plural_rules" toml:"plural_rules"`
SingularRules map[string]string `yaml:"singular_rules" json:"singular_rules" toml:"singular_rules"`
UncountableRules []string `yaml:"uncountable_rules" json:"uncountable_rules" toml:"uncountable_rules"`
IrregularRules map[string]string `yaml:"irregular_rules" json:"irregular_rules" toml:"irregular_rules"`
CustomizableSeverityOption `yaml:",inline"`
PluralRules map[string]string `yaml:"plural_rules" json:"plural_rules" toml:"plural_rules"`
SingularRules map[string]string `yaml:"singular_rules" json:"singular_rules" toml:"singular_rules"`
UncountableRules []string `yaml:"uncountable_rules" json:"uncountable_rules" toml:"uncountable_rules"`
IrregularRules map[string]string `yaml:"irregular_rules" json:"irregular_rules" toml:"irregular_rules"`
}
4 changes: 3 additions & 1 deletion internal/linter/config/rpcNamesCaseOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type RPCNamesCaseOption struct {
// UnmarshalYAML implements yaml.v2 Unmarshaler interface.
func (r *RPCNamesCaseOption) UnmarshalYAML(unmarshal func(interface{}) error) error {
var option struct {
Convention string `yaml:"convention"`
CustomizableSeverityOption `yaml:",inline"`
Convention string `yaml:"convention"`
}
if err := unmarshal(&option); err != nil {
return err
Expand All @@ -47,6 +48,7 @@ func (r *RPCNamesCaseOption) UnmarshalYAML(unmarshal func(interface{}) error) er
}
r.Convention = convention
}
r.CustomizableSeverityOption = option.CustomizableSeverityOption
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/linter/config/rpcsHaveCommentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// RPCsHaveCommentOption represents the option for the RPCS_HAVE_COMMENT rule.
type RPCsHaveCommentOption struct {
CustomizableSeverityOption
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
CustomizableSeverityOption `yaml:",inline"`
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/serviceNamesEndWithOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// ServiceNamesEndWithOption represents the option for the SERVICE_NAMES_END_WITH rule.
type ServiceNamesEndWithOption struct {
CustomizableSeverityOption
Text string `yaml:"text" json:"text" toml:"text"`
CustomizableSeverityOption `yaml:",inline"`
Text string `yaml:"text" json:"text" toml:"text"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/servicesHaveCommentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// ServicesHaveCommentOption represents the option for the SERVICES_HAVE_COMMENT rule.
type ServicesHaveCommentOption struct {
CustomizableSeverityOption
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
CustomizableSeverityOption `yaml:",inline"`
ShouldFollowGolangStyle bool `yaml:"should_follow_golang_style" json:"should_follow_golang_style" toml:"should_follow_golang_style"`
}
4 changes: 2 additions & 2 deletions internal/linter/config/syntaxConsistentOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package config

// SyntaxConsistentOption represents the option for the SYNTAX_CONSISTENT rule.
type SyntaxConsistentOption struct {
CustomizableSeverityOption
Version string `yaml:"version" json:"version" toml:"version"`
CustomizableSeverityOption `yaml:",inline"`
Version string `yaml:"version" json:"version" toml:"version"`
}