github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/_example/plugin/customrules/enumNamesLowerSnakeCaseRule.go (about)

     1  package customrules
     2  
     3  import (
     4  	"github.com/yoheimuta/go-protoparser/v4/parser"
     5  
     6  	"github.com/yoheimuta/protolint/linter/report"
     7  	"github.com/yoheimuta/protolint/linter/rule"
     8  	"github.com/yoheimuta/protolint/linter/strs"
     9  	"github.com/yoheimuta/protolint/linter/visitor"
    10  )
    11  
    12  // EnumNamesLowerSnakeCaseRule verifies that all enum names are LowerSnakeCase.
    13  type EnumNamesLowerSnakeCaseRule struct {
    14  }
    15  
    16  // NewEnumNamesLowerSnakeCaseRule creates a new EnumNamesLowerSnakeCaseRule.
    17  func NewEnumNamesLowerSnakeCaseRule() EnumNamesLowerSnakeCaseRule {
    18  	return EnumNamesLowerSnakeCaseRule{}
    19  }
    20  
    21  // ID returns the ID of this rule.
    22  func (r EnumNamesLowerSnakeCaseRule) ID() string {
    23  	return "ENUM_NAMES_LOWER_SNAKE_CASE"
    24  }
    25  
    26  // Purpose returns the purpose of this rule.
    27  func (r EnumNamesLowerSnakeCaseRule) Purpose() string {
    28  	return "Verifies that all enum names are LowerSnakeCase."
    29  }
    30  
    31  // IsOfficial decides whether or not this rule belongs to the official guide.
    32  func (r EnumNamesLowerSnakeCaseRule) IsOfficial() bool {
    33  	return true
    34  }
    35  
    36  // Severity gets the severity of the rule
    37  func (r EnumNamesLowerSnakeCaseRule) Severity() rule.Severity {
    38  	return rule.SeverityWarning
    39  }
    40  
    41  // Apply applies the rule to the proto.
    42  func (r EnumNamesLowerSnakeCaseRule) Apply(proto *parser.Proto) ([]report.Failure, error) {
    43  	v := &enumNamesLowerSnakeCaseVisitor{
    44  		BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID(), string(r.Severity())),
    45  	}
    46  	return visitor.RunVisitor(v, proto, r.ID())
    47  }
    48  
    49  type enumNamesLowerSnakeCaseVisitor struct {
    50  	*visitor.BaseAddVisitor
    51  }
    52  
    53  // VisitEnum checks the enum field.
    54  func (v *enumNamesLowerSnakeCaseVisitor) VisitEnum(e *parser.Enum) bool {
    55  	if !strs.IsLowerSnakeCase(e.EnumName) {
    56  		v.AddFailuref(e.Meta.Pos, "Enum name %q must be underscore_separated_names", e.EnumName)
    57  	}
    58  	return false
    59  }