github.com/psrajat/prototool@v1.3.0/internal/lint/base_visitor.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package lint 22 23 import ( 24 "text/scanner" 25 26 "github.com/emicklei/proto" 27 "github.com/uber/prototool/internal/text" 28 ) 29 30 var _ proto.Visitor = baseVisitor{} 31 32 type baseVisitor struct{} 33 34 func (baseVisitor) OnStart(*proto.Proto) error { return nil } 35 func (baseVisitor) Finally() error { return nil } 36 37 func (baseVisitor) VisitMessage(m *proto.Message) {} 38 func (baseVisitor) VisitService(s *proto.Service) {} 39 func (baseVisitor) VisitSyntax(s *proto.Syntax) {} 40 func (baseVisitor) VisitPackage(p *proto.Package) {} 41 func (baseVisitor) VisitOption(o *proto.Option) {} 42 func (baseVisitor) VisitImport(i *proto.Import) {} 43 func (baseVisitor) VisitNormalField(i *proto.NormalField) {} 44 func (baseVisitor) VisitEnumField(i *proto.EnumField) {} 45 func (baseVisitor) VisitEnum(e *proto.Enum) {} 46 func (baseVisitor) VisitComment(e *proto.Comment) {} 47 func (baseVisitor) VisitOneof(o *proto.Oneof) {} 48 func (baseVisitor) VisitOneofField(o *proto.OneOfField) {} 49 func (baseVisitor) VisitReserved(r *proto.Reserved) {} 50 func (baseVisitor) VisitRPC(r *proto.RPC) {} 51 func (baseVisitor) VisitMapField(f *proto.MapField) {} 52 func (baseVisitor) VisitGroup(g *proto.Group) {} 53 func (baseVisitor) VisitExtensions(e *proto.Extensions) {} 54 55 type baseAddVisitor struct { 56 baseVisitor 57 add func(*text.Failure) 58 } 59 60 func newBaseAddVisitor(add func(*text.Failure)) baseAddVisitor { 61 return baseAddVisitor{add: add} 62 } 63 64 func (v baseAddVisitor) AddFailuref(position scanner.Position, format string, args ...interface{}) { 65 v.add(text.NewFailuref(position, "", format, args...)) 66 } 67 68 // extendedVisitor extends the proto.Visitor interface. 69 // extendedVisitors are expected to be called with one file at a time, 70 // and are not thread-safe. 71 type extendedVisitor interface { 72 proto.Visitor 73 74 // OnStart is called when visiting is started. 75 OnStart(*proto.Proto) error 76 // Finally is called when visiting is done. 77 Finally() error 78 } 79 80 func runVisitor(visitor extendedVisitor, descriptors []*proto.Proto) error { 81 for _, descriptor := range descriptors { 82 if err := visitor.OnStart(descriptor); err != nil { 83 return err 84 } 85 for _, element := range descriptor.Elements { 86 element.Accept(visitor) 87 } 88 if err := visitor.Finally(); err != nil { 89 return err 90 } 91 } 92 return nil 93 }