Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Parent field to all Visitee #68

Merged
merged 5 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,6 @@ func mergeOrReturnComment(elements []Visitee, lit string, pos scanner.Position)
last.Merge(com)
return nil
}

// parent is part of elementContainer
func (c *Comment) parent(Visitee) {}
22 changes: 18 additions & 4 deletions enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Enum struct {
Comment *Comment
Name string
Elements []Visitee
Parent Visitee
}

// Accept dispatches the call to the visitor.
Expand All @@ -47,6 +48,7 @@ func (e *Enum) Doc() *Comment {

// addElement is part of elementContainer
func (e *Enum) addElement(v Visitee) {
v.parent(e)
e.Elements = append(e.Elements, v)
}

Expand Down Expand Up @@ -79,7 +81,7 @@ func (e *Enum) parse(p *Parser) error {
switch tok {
case tCOMMENT:
if com := mergeOrReturnComment(e.elements(), lit, pos); com != nil { // not merged?
e.Elements = append(e.Elements, com)
e.addElement(com)
}
case tOPTION:
v := new(Option)
Expand All @@ -89,7 +91,7 @@ func (e *Enum) parse(p *Parser) error {
if err != nil {
return err
}
e.Elements = append(e.Elements, v)
e.addElement(v)
case tRIGHTCURLY, tEOF:
goto done
case tSEMICOLON:
Expand All @@ -103,7 +105,7 @@ func (e *Enum) parse(p *Parser) error {
if err != nil {
return err
}
e.Elements = append(e.Elements, f)
e.addElement(f)
}
}
done:
Expand All @@ -113,6 +115,9 @@ done:
return nil
}

// parent is part of elementContainer
func (e *Enum) parent(p Visitee) { e.Parent = p }

// EnumField is part of the body of an Enum.
type EnumField struct {
Position scanner.Position
Expand All @@ -123,6 +128,7 @@ type EnumField struct {
ValueOption *Option
Elements []Visitee // such as Option and Comment
InlineComment *Comment
Parent Visitee
}

// Accept dispatches the call to the visitor.
Expand Down Expand Up @@ -169,7 +175,7 @@ func (f *EnumField) parse(p *Parser) error {
}
// update deprecated field with the last option found
f.ValueOption = o
f.Elements = append(f.Elements, o)
f.addElement(o)
pos, tok, lit = p.next()
if tok == tCOMMA {
continue
Expand All @@ -184,3 +190,11 @@ func (f *EnumField) parse(p *Parser) error {
}
return nil
}

// addElement is part of elementContainer
func (f *EnumField) addElement(v Visitee) {
v.parent(f)
f.Elements = append(f.Elements, v)
}

func (f *EnumField) parent(v Visitee) { f.Parent = v }
4 changes: 4 additions & 0 deletions extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Extensions struct {
Comment *Comment
Ranges []Range
InlineComment *Comment
Parent Visitee
}

// inlineComment is part of commentInliner.
Expand All @@ -55,3 +56,6 @@ func (e *Extensions) parse(p *Parser) error {
e.Ranges = list
return nil
}

// parent is part of elementContainer
func (e *Extensions) parent(p Visitee) { e.Parent = p }
3 changes: 3 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Field struct {
Sequence int
Options []*Option
InlineComment *Comment
Parent Visitee
}

// inlineComment is part of commentInliner.
Expand Down Expand Up @@ -175,3 +176,5 @@ func (f *MapField) parse(p *Parser) error {
}
return parseFieldAfterType(f.Field, p)
}

func (f *Field) parent(v Visitee) { f.Parent = v }
4 changes: 4 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Group struct {
Required bool
Sequence int
Elements []Visitee
Parent Visitee
}

// Accept dispatches the call to the visitor.
Expand All @@ -47,6 +48,7 @@ func (g *Group) Accept(v Visitor) {

// addElement is part of elementContainer
func (g *Group) addElement(v Visitee) {
v.parent(g)
g.Elements = append(g.Elements, v)
}

Expand Down Expand Up @@ -92,3 +94,5 @@ func (g *Group) parse(p *Parser) error {
}
return parseMessageBody(p, g)
}

func (g *Group) parent(v Visitee) { g.Parent = v }
3 changes: 3 additions & 0 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Import struct {
Filename string
Kind string // weak, public, <empty>
InlineComment *Comment
Parent Visitee
}

func (i *Import) parse(p *Parser) error {
Expand Down Expand Up @@ -67,3 +68,5 @@ func (i *Import) inlineComment(c *Comment) {
func (i *Import) Doc() *Comment {
return i.Comment
}

func (i *Import) parent(v Visitee) { i.Parent = v }
4 changes: 4 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Message struct {
Name string
IsExtend bool
Elements []Visitee
Parent Visitee
}

func (m *Message) groupName() string {
Expand Down Expand Up @@ -209,6 +210,7 @@ func (m *Message) Accept(v Visitor) {

// addElement is part of elementContainer
func (m *Message) addElement(v Visitee) {
v.parent(m)
m.Elements = append(m.Elements, v)
}

Expand All @@ -226,3 +228,5 @@ func (m *Message) takeLastComment(expectedOnLine int) (last *Comment) {
func (m *Message) Doc() *Comment {
return m.Comment
}

func (m *Message) parent(v Visitee) { m.Parent = v }
12 changes: 8 additions & 4 deletions oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ type Oneof struct {
Comment *Comment
Name string
Elements []Visitee
Parent Visitee
}

// addElement is part of elementContainer
func (o *Oneof) addElement(v Visitee) {
v.parent(o)
o.Elements = append(o.Elements, v)
}

Expand Down Expand Up @@ -71,7 +73,7 @@ func (o *Oneof) parse(p *Parser) error {
switch tok {
case tCOMMENT:
if com := mergeOrReturnComment(o.elements(), lit, pos); com != nil { // not merged?
o.Elements = append(o.Elements, com)
o.addElement(com)
}
case tIDENT:
f := newOneOfField()
Expand All @@ -81,23 +83,23 @@ func (o *Oneof) parse(p *Parser) error {
if err := parseFieldAfterType(f.Field, p); err != nil {
return err
}
o.Elements = append(o.Elements, f)
o.addElement(f)
case tGROUP:
g := new(Group)
g.Position = pos
g.Comment, o.Elements = takeLastCommentIfEndsOnLine(o.elements(), pos.Line-1)
if err := g.parse(p); err != nil {
return err
}
o.Elements = append(o.Elements, g)
o.addElement(g)
case tOPTION:
opt := new(Option)
opt.Position = pos
opt.Comment, o.Elements = takeLastCommentIfEndsOnLine(o.elements(), pos.Line-1)
if err := opt.parse(p); err != nil {
return err
}
o.Elements = append(o.Elements, opt)
o.addElement(opt)
case tSEMICOLON:
maybeScanInlineComment(p, o)
// continue
Expand Down Expand Up @@ -134,3 +136,5 @@ func (o *OneOfField) Accept(v Visitor) {
func (o *OneOfField) Doc() *Comment {
return o.Comment
}

func (o *Oneof) parent(v Visitee) { o.Parent = v }
3 changes: 3 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Option struct {
IsEmbedded bool
AggregatedConstants []*NamedLiteral
InlineComment *Comment
Parent Visitee
}

// parse reads an Option body
Expand Down Expand Up @@ -233,3 +234,5 @@ func parseAggregateConstants(p *Parser, container interface{}) (list []*NamedLit
list = append(list, &NamedLiteral{Name: key, Literal: l, PrintsColon: printsColon})
}
}

func (o *Option) parent(v Visitee) { o.Parent = v }
3 changes: 3 additions & 0 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Package struct {
Comment *Comment
Name string
InlineComment *Comment
Parent Visitee
}

// Doc is part of Documented
Expand Down Expand Up @@ -58,3 +59,5 @@ func (p *Package) Accept(v Visitor) {
func (p *Package) inlineComment(c *Comment) {
p.InlineComment = c
}

func (p *Package) parent(v Visitee) { p.Parent = v }
88 changes: 88 additions & 0 deletions parent_accessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2018 Ernest Micklei
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package proto

func getParent(child Visitee) Visitee {
if child == nil {
return nil
}
pa := new(parentAccessor)
child.Accept(pa)
return pa.parent
}

type parentAccessor struct {
parent Visitee
}

func (p *parentAccessor) VisitMessage(m *Message) {
p.parent = m.Parent
}
func (p *parentAccessor) VisitService(v *Service) {
p.parent = v.Parent
}
func (p *parentAccessor) VisitSyntax(s *Syntax) {
p.parent = s.Parent
}
func (p *parentAccessor) VisitPackage(pkg *Package) {
p.parent = pkg.Parent
}
func (p *parentAccessor) VisitOption(o *Option) {
p.parent = o.Parent
}
func (p *parentAccessor) VisitImport(i *Import) {
p.parent = i.Parent
}
func (p *parentAccessor) VisitNormalField(i *NormalField) {
p.parent = i.Parent
}
func (p *parentAccessor) VisitEnumField(i *EnumField) {
p.parent = i.Parent
}
func (p *parentAccessor) VisitEnum(e *Enum) {
p.parent = e.Parent
}
func (p *parentAccessor) VisitComment(e *Comment) {}
func (p *parentAccessor) VisitOneof(o *Oneof) {
p.parent = o.Parent
}
func (p *parentAccessor) VisitOneofField(o *OneOfField) {
p.parent = o.Parent
}
func (p *parentAccessor) VisitReserved(rs *Reserved) {
p.parent = rs.Parent
}
func (p *parentAccessor) VisitRPC(rpc *RPC) {
p.parent = rpc.Parent
}
func (p *parentAccessor) VisitMapField(f *MapField) {
p.parent = f.Parent
}
func (p *parentAccessor) VisitGroup(g *Group) {
p.parent = g.Parent
}
func (p *parentAccessor) VisitExtensions(e *Extensions) {
p.parent = e.Parent
}
func (p *parentAccessor) VisitProto(*Proto) {}
Loading