github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/rpcNamesCaseRule.go (about) 1 package rules 2 3 import ( 4 "github.com/yoheimuta/go-protoparser/v4/parser" 5 "github.com/yoheimuta/protolint/internal/linter/config" 6 7 "github.com/yoheimuta/protolint/linter/report" 8 "github.com/yoheimuta/protolint/linter/rule" 9 "github.com/yoheimuta/protolint/linter/strs" 10 "github.com/yoheimuta/protolint/linter/visitor" 11 ) 12 13 // RPCNamesCaseRule verifies that all rpc names conform to the specified convention. 14 type RPCNamesCaseRule struct { 15 RuleWithSeverity 16 convention config.ConventionType 17 } 18 19 // NewRPCNamesCaseRule creates a new RPCNamesCaseRule. 20 func NewRPCNamesCaseRule( 21 severity rule.Severity, 22 convention config.ConventionType, 23 ) RPCNamesCaseRule { 24 return RPCNamesCaseRule{ 25 RuleWithSeverity: RuleWithSeverity{severity: severity}, 26 convention: convention, 27 } 28 } 29 30 // ID returns the ID of this rule. 31 func (r RPCNamesCaseRule) ID() string { 32 return "RPC_NAMES_CASE" 33 } 34 35 // Purpose returns the purpose of this rule. 36 func (r RPCNamesCaseRule) Purpose() string { 37 return "Verifies that all rpc names conform to the specified convention." 38 } 39 40 // IsOfficial decides whether or not this rule belongs to the official guide. 41 func (r RPCNamesCaseRule) IsOfficial() bool { 42 return false 43 } 44 45 // Apply applies the rule to the proto. 46 func (r RPCNamesCaseRule) Apply(proto *parser.Proto) ([]report.Failure, error) { 47 v := &rpcNamesCaseVisitor{ 48 BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID(), string(r.Severity())), 49 convention: r.convention, 50 } 51 return visitor.RunVisitor(v, proto, r.ID()) 52 } 53 54 type rpcNamesCaseVisitor struct { 55 *visitor.BaseAddVisitor 56 convention config.ConventionType 57 } 58 59 // VisitRPC checks the rpc. 60 func (v *rpcNamesCaseVisitor) VisitRPC(rpc *parser.RPC) bool { 61 if v.convention == config.ConventionLowerCamel && !strs.IsLowerCamelCase(rpc.RPCName) { 62 v.AddFailuref(rpc.Meta.Pos, "RPC name %q must be LowerCamelCase", rpc.RPCName) 63 } else if v.convention == config.ConventionUpperSnake && !strs.IsUpperSnakeCase(rpc.RPCName) { 64 v.AddFailuref(rpc.Meta.Pos, "RPC name %q must be UpperSnakeCase", rpc.RPCName) 65 } else if v.convention == config.ConventionLowerSnake && !strs.IsLowerSnakeCase(rpc.RPCName) { 66 v.AddFailuref(rpc.Meta.Pos, "RPC name %q must be LowerSnakeCase", rpc.RPCName) 67 } 68 return false 69 }