Skip to content

Commit f283feb

Browse files
emickleiErnest Micklei
and
Ernest Micklei
authored
walk options of enumfield #140 (#141)
Co-authored-by: Ernest Micklei <ernest@ag5.com>
1 parent 4ced960 commit f283feb

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

enum.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ type EnumField struct {
140140
Parent Visitee
141141
}
142142

143+
// elements is part of elementContainer
144+
func (f *EnumField) elements() []Visitee {
145+
return f.Elements
146+
}
147+
148+
// takeLastComment is part of elementContainer
149+
// removes and returns the last element of the list if it is a Comment.
150+
func (f *EnumField) takeLastComment(expectedOnLine int) (last *Comment) {
151+
last, f.Elements = takeLastCommentIfEndsOnLine(f.Elements, expectedOnLine)
152+
return
153+
}
154+
143155
// Accept dispatches the call to the visitor.
144156
func (f *EnumField) Accept(v Visitor) {
145157
v.VisitEnumField(f)

enum_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
package proto
2525

26-
import "testing"
26+
import (
27+
"testing"
28+
)
2729

2830
func TestEnum(t *testing.T) {
2931
proto := `
@@ -191,3 +193,32 @@ func TestEnumInlineCommentBeforeBody(t *testing.T) {
191193
t.Errorf("got %d want %d lines", got, want)
192194
}
193195
}
196+
197+
func TestEnumFieldWalkWithComment(t *testing.T) {
198+
src := `enum HideIt
199+
{
200+
PRIVATE = 1 [
201+
// hidden
202+
// field
203+
(google.api.value_visibility).restriction = "HIDDEN"
204+
];
205+
}
206+
`
207+
p := newParserOn(src)
208+
e := new(Enum)
209+
p.next()
210+
if err := e.parse(p); err != nil {
211+
t.Fatal(err)
212+
}
213+
var name, msg string
214+
walk(e, WithOption(func(o *Option) {
215+
name = o.Name
216+
msg = o.Comment.Message()
217+
}))
218+
if got, want := name, "(google.api.value_visibility).restriction"; got != want {
219+
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
220+
}
221+
if got, want := msg, " hidden"; got != want {
222+
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
223+
}
224+
}

0 commit comments

Comments
 (0)