This also gets confusing in terms of when there are options slices etc, but as an example, here's a visitor I wrote for making sure there are no c-style comments. Note the calls that have to be individually made. This was probably a design decision but food for thought.
type commentsNoCStyleVisitor struct {
baseAddVisitor
}
func (v commentsNoCStyleVisitor) VisitMessage(element *proto.Message) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitService(element *proto.Service) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitSyntax(element *proto.Syntax) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
}
func (v commentsNoCStyleVisitor) VisitPackage(element *proto.Package) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
}
func (v commentsNoCStyleVisitor) VisitOption(element *proto.Option) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
}
func (v commentsNoCStyleVisitor) VisitImport(element *proto.Import) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
}
func (v commentsNoCStyleVisitor) VisitNormalField(element *proto.NormalField) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
for _, child := range element.Options {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitEnumField(element *proto.EnumField) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
if element.ValueOption != nil {
element.ValueOption.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitEnum(element *proto.Enum) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitComment(element *proto.Comment) {
v.checkComments(element.Position, element)
}
func (v commentsNoCStyleVisitor) VisitOneof(element *proto.Oneof) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitOneofField(element *proto.OneOfField) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
for _, child := range element.Options {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitReserved(element *proto.Reserved) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
}
func (v commentsNoCStyleVisitor) VisitRPC(element *proto.RPC) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
for _, child := range element.Options {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitMapField(element *proto.MapField) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
for _, child := range element.Options {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitGroup(element *proto.Group) {
v.checkComments(element.Position, element.Comment)
for _, child := range element.Elements {
child.Accept(v)
}
}
func (v commentsNoCStyleVisitor) VisitExtensions(element *proto.Extensions) {
v.checkComments(element.Position, element.Comment, element.InlineComment)
}
func (v commentsNoCStyleVisitor) checkComments(position scanner.Position, comments ...*proto.Comment) {
for _, comment := range comments {
if comment != nil {
if comment.Cstyle {
v.AddFailuref(position, "c-style comments are not allowed")
}
}
}
}