| 1 | // Copyright (c) 2018 Ernest Micklei |
| 2 | // |
| 3 | // MIT License |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining |
| 6 | // a copy of this software and associated documentation files (the |
| 7 | // "Software"), to deal in the Software without restriction, including |
| 8 | // without limitation the rights to use, copy, modify, merge, publish, |
| 9 | // distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | // permit persons to whom the Software is furnished to do so, subject to |
| 11 | // the following conditions: |
| 12 | // |
| 13 | // The above copyright notice and this permission notice shall be |
| 14 | // included in all copies or substantial portions of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | |
| 24 | package proto |
| 25 | |
| 26 | func getParent(child Visitee) Visitee { |
| 27 | if child == nil { |
| 28 | return nil |
| 29 | } |
| 30 | pa := new(parentAccessor) |
| 31 | child.Accept(pa) |
| 32 | return pa.parent |
| 33 | } |
| 34 | |
| 35 | type parentAccessor struct { |
| 36 | parent Visitee |
| 37 | } |
| 38 | |
| 39 | func (p *parentAccessor) VisitMessage(m *Message) { |
| 40 | p.parent = m.Parent |
| 41 | } |
| 42 | func (p *parentAccessor) VisitService(v *Service) { |
| 43 | p.parent = v.Parent |
| 44 | } |
| 45 | func (p *parentAccessor) VisitSyntax(s *Syntax) { |
| 46 | p.parent = s.Parent |
| 47 | } |
| 48 | func (p *parentAccessor) VisitPackage(pkg *Package) { |
| 49 | p.parent = pkg.Parent |
| 50 | } |
| 51 | func (p *parentAccessor) VisitOption(o *Option) { |
| 52 | p.parent = o.Parent |
| 53 | } |
| 54 | func (p *parentAccessor) VisitImport(i *Import) { |
| 55 | p.parent = i.Parent |
| 56 | } |
| 57 | func (p *parentAccessor) VisitNormalField(i *NormalField) { |
| 58 | p.parent = i.Parent |
| 59 | } |
| 60 | func (p *parentAccessor) VisitEnumField(i *EnumField) { |
| 61 | p.parent = i.Parent |
| 62 | } |
| 63 | func (p *parentAccessor) VisitEnum(e *Enum) { |
| 64 | p.parent = e.Parent |
| 65 | } |
| 66 | func (p *parentAccessor) VisitComment(e *Comment) {} |
| 67 | func (p *parentAccessor) VisitOneof(o *Oneof) { |
| 68 | p.parent = o.Parent |
| 69 | } |
| 70 | func (p *parentAccessor) VisitOneofField(o *OneOfField) { |
| 71 | p.parent = o.Parent |
| 72 | } |
| 73 | func (p *parentAccessor) VisitReserved(rs *Reserved) { |
| 74 | p.parent = rs.Parent |
| 75 | } |
| 76 | func (p *parentAccessor) VisitRPC(rpc *RPC) { |
| 77 | p.parent = rpc.Parent |
| 78 | } |
| 79 | func (p *parentAccessor) VisitMapField(f *MapField) { |
| 80 | p.parent = f.Parent |
| 81 | } |
| 82 | func (p *parentAccessor) VisitGroup(g *Group) { |
| 83 | p.parent = g.Parent |
| 84 | } |
| 85 | func (p *parentAccessor) VisitExtensions(e *Extensions) { |
| 86 | p.parent = e.Parent |
| 87 | } |
| 88 | func (p *parentAccessor) VisitEdition(e *Edition) { |
| 89 | p.parent = e.Parent |
| 90 | } |
| 91 | func (p *parentAccessor) VisitProto(*Proto) {} |