github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/proto3GroupsAvoidRule.go (about) 1 package rules 2 3 import ( 4 "github.com/yoheimuta/go-protoparser/v4/parser" 5 "github.com/yoheimuta/protolint/linter/autodisable" 6 "github.com/yoheimuta/protolint/linter/report" 7 "github.com/yoheimuta/protolint/linter/rule" 8 "github.com/yoheimuta/protolint/linter/visitor" 9 ) 10 11 // Proto3GroupsAvoidRule verifies that all groups should be avoided for proto3. 12 // See https://developers.google.com/protocol-buffers/docs/style#things-to-avoid 13 type Proto3GroupsAvoidRule struct { 14 RuleWithSeverity 15 autoDisableType autodisable.PlacementType 16 } 17 18 // NewProto3GroupsAvoidRule creates a new Proto3GroupsAvoidRule. 19 func NewProto3GroupsAvoidRule( 20 severity rule.Severity, 21 autoDisableType autodisable.PlacementType, 22 ) Proto3GroupsAvoidRule { 23 return Proto3GroupsAvoidRule{ 24 RuleWithSeverity: RuleWithSeverity{severity: severity}, 25 autoDisableType: autoDisableType, 26 } 27 } 28 29 // ID returns the ID of this rule. 30 func (r Proto3GroupsAvoidRule) ID() string { 31 return "PROTO3_GROUPS_AVOID" 32 } 33 34 // Purpose returns the purpose of this rule. 35 func (r Proto3GroupsAvoidRule) Purpose() string { 36 return "Verifies that all groups should be avoided for proto3." 37 } 38 39 // IsOfficial decides whether or not this rule belongs to the official guide. 40 func (r Proto3GroupsAvoidRule) IsOfficial() bool { 41 return true 42 } 43 44 // Apply applies the rule to the proto. 45 func (r Proto3GroupsAvoidRule) Apply(proto *parser.Proto) ([]report.Failure, error) { 46 v := &proto3GroupsAvoidVisitor{ 47 BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID(), string(r.Severity())), 48 } 49 return visitor.RunVisitorAutoDisable(v, proto, r.ID(), r.autoDisableType) 50 } 51 52 type proto3GroupsAvoidVisitor struct { 53 *visitor.BaseAddVisitor 54 isProto3 bool 55 } 56 57 // VisitSyntax checks the syntax. 58 func (v *proto3GroupsAvoidVisitor) VisitSyntax(s *parser.Syntax) bool { 59 v.isProto3 = s.ProtobufVersion == "proto3" 60 return false 61 } 62 63 // VisitGroupField checks the group field. 64 func (v *proto3GroupsAvoidVisitor) VisitGroupField(field *parser.GroupField) bool { 65 if v.isProto3 { 66 v.AddFailuref(field.Meta.Pos, `Group %q should be avoided for proto3`, field.GroupName) 67 } 68 return false 69 }