github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/serviceNamesEndWithRule.go (about) 1 package rules 2 3 import ( 4 "strings" 5 6 "github.com/yoheimuta/go-protoparser/v4/parser" 7 8 "github.com/yoheimuta/protolint/linter/report" 9 "github.com/yoheimuta/protolint/linter/rule" 10 "github.com/yoheimuta/protolint/linter/visitor" 11 ) 12 13 // ServiceNamesEndWithRule verifies that all service names end with the specified value. 14 type ServiceNamesEndWithRule struct { 15 RuleWithSeverity 16 text string 17 } 18 19 // NewServiceNamesEndWithRule creates a new ServiceNamesEndWithRule. 20 func NewServiceNamesEndWithRule( 21 severity rule.Severity, 22 text string, 23 ) ServiceNamesEndWithRule { 24 return ServiceNamesEndWithRule{ 25 RuleWithSeverity: RuleWithSeverity{severity: severity}, 26 text: text, 27 } 28 } 29 30 // ID returns the ID of this rule. 31 func (r ServiceNamesEndWithRule) ID() string { 32 return "SERVICE_NAMES_END_WITH" 33 } 34 35 // Purpose returns the purpose of this rule. 36 func (r ServiceNamesEndWithRule) Purpose() string { 37 return "Verifies that all service names end with the specified value." 38 } 39 40 // IsOfficial decides whether or not this rule belongs to the official guide. 41 func (r ServiceNamesEndWithRule) IsOfficial() bool { 42 return false 43 } 44 45 // Apply applies the rule to the proto. 46 func (r ServiceNamesEndWithRule) Apply(proto *parser.Proto) ([]report.Failure, error) { 47 v := &serviceNamesEndWithVisitor{ 48 BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID(), string(r.Severity())), 49 text: r.text, 50 } 51 52 return visitor.RunVisitor(v, proto, r.ID()) 53 } 54 55 type serviceNamesEndWithVisitor struct { 56 *visitor.BaseAddVisitor 57 text string 58 } 59 60 // VisitService checks the service. 61 func (v *serviceNamesEndWithVisitor) VisitService(service *parser.Service) bool { 62 if !strings.HasSuffix(service.ServiceName, v.text) { 63 v.AddFailuref(service.Meta.Pos, "Service name %q must end with %s", service.ServiceName, v.text) 64 } 65 return false 66 }