Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
add inline comment for reserved and extensions
Browse files Browse the repository at this point in the history
Change-Id: I2b7797d38e90862927d7482815e589da49e5a19b
  • Loading branch information
emicklei committed Feb 7, 2017
1 parent 1ba421b commit 1a88e9f
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 19 deletions.
12 changes: 7 additions & 5 deletions cmd/protofmt/unformatted.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ message Message

string name =1;
// this is a thing
google.protobuf.Any anything = 2 [packed=true, danger=false];
google.protobuf.Any anything = 2 [packed=true, danger=false]; // anything

repeated
Message
Expand All @@ -39,14 +39,16 @@ message Message
BILL_BAILEY = 3;
}

reserved 2, 15, 9 to 11;
reserved "foo", "bar";
reserved 2, 15, 9 to 11;
reserved "foo", "bar"; // foo bar

extensions 100 to 199; // no 200

map<string, Nested> terrain = 4; // terrain

enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
option allow_alias = true; // allow_alias
UNKNOWN = 0; // UNKNOWN
STARTED = 1;
RUN = 2 [(custom_option) = "hello world"];
}
Expand Down
21 changes: 20 additions & 1 deletion extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package proto
// Extensions declare that a range of field numbers in a message are available for third-party extensions.
// proto2 only
type Extensions struct {
Ranges string
Ranges string
Comment *Comment
}

// inlineComment is part of commentInliner.
func (e *Extensions) inlineComment(c *Comment) {
e.Comment = c
}

// Accept dispatches the call to the visitor.
Expand All @@ -15,5 +21,18 @@ func (e *Extensions) Accept(v Visitor) {
func (e *Extensions) parse(p *Parser) error {
// TODO proper range parsing
e.Ranges = p.s.scanUntil(';')
p.s.unread(';') // for reading inline comment
return nil
}

// columns returns printable source tokens
func (e *Extensions) columns() (cols []aligned) {
cols = append(cols,
notAligned("extensions "),
leftAligned(e.Ranges),
alignedSemicolon)
if e.Comment != nil {
cols = append(cols, notAligned(" //"), notAligned(e.Comment.Message))
}
return
}
28 changes: 28 additions & 0 deletions extensions_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
package proto

import "testing"

func TestExtensions(t *testing.T) {
proto := `message M {
extensions 4, 20 to max; // max
}`
p := newParserOn(proto)
p.scanIgnoreWhitespace() // consume extensions
m := new(Message)
err := m.parse(p)
if err != nil {
t.Fatal(err)
}
if len(m.Elements) == 0 {
t.Fatal("extensions expected")
}
f := m.Elements[0].(*Extensions)
if got, want := f.Ranges, " 4, 20 to max"; got != want {
t.Errorf("got [%s] want [%s]", got, want)
}
if f.Comment == nil {
t.Fatal("comment expected")
}
if got, want := f.Comment.Message, " max"; got != want {
t.Errorf("got [%s] want [%s]", got, want)
}
}
28 changes: 28 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ func (f *MapField) Accept(v Visitor) {
v.VisitMapField(f)
}

// columns returns printable source tokens
func (f *MapField) columns() (cols []aligned) {
cols = append(cols,
notAligned("map <"),
rightAligned(f.KeyType),
notAligned(","),
leftAligned(f.Type),
notAligned("> "),
rightAligned(f.Name),
alignedEquals,
rightAligned(strconv.Itoa(f.Sequence)))
if len(f.Options) > 0 {
cols = append(cols, leftAligned(" ["))
for i, each := range f.Options {
if i > 0 {
cols = append(cols, alignedComma)
}
cols = append(cols, each.keyValuePair(true)...)
}
cols = append(cols, leftAligned("]"))
}
cols = append(cols, alignedSemicolon)
if f.Comment != nil {
cols = append(cols, notAligned(" //"), notAligned(f.Comment.Message))
}
return
}

// parse expects:
// mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
// keyType = "int32" | "int64" | "uint32" | "uint64" | "sint32" | "sint64" |
Expand Down
12 changes: 3 additions & 9 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,14 @@ func (f *Formatter) VisitReserved(r *Reserved) {
fmt.Fprintf(f.w, "%q", each)
}
}
io.WriteString(f.w, ";\n")
f.endWithComment(r.Comment)
}

// VisitRPC formats a RPC.
func (f *Formatter) VisitRPC(r *RPC) {}

// VisitMapField formats a MapField.
func (f *Formatter) VisitMapField(m *MapField) {
f.begin("map")
fmt.Fprintf(f.w, "map<%s,%s> %s = %d;\n", m.KeyType, m.Type, m.Name, m.Sequence)
}
func (f *Formatter) VisitMapField(m *MapField) {}

// VisitNormalField formats a NormalField.
func (f *Formatter) VisitNormalField(f1 *NormalField) {}
Expand All @@ -174,7 +171,4 @@ func (f *Formatter) VisitGroup(g *Group) {
}

// VisitExtensions formats a proto2 Extensions.
func (f *Formatter) VisitExtensions(e *Extensions) {
f.indent(0)
fmt.Fprintf(f.w, "extensions %s;\n", e.Ranges)
}
func (f *Formatter) VisitExtensions(e *Extensions) {}
10 changes: 10 additions & 0 deletions formatter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,13 @@ func (f *Formatter) printAsGroups(list []Visitee) {
// print last group
f.printListOfColumns(group, lastGroupName)
}

// endWithComment writes a statement end (;) followed by inline comment if present.
func (f *Formatter) endWithComment(commentOrNil *Comment) {
io.WriteString(f.w, ";")
if commentOrNil != nil {
io.WriteString(f.w, " //")
io.WriteString(f.w, commentOrNil.Message)
}
io.WriteString(f.w, "\n")
}
2 changes: 2 additions & 0 deletions proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ func (c Comment) IsMultiline() bool {
return strings.Contains(c.Message, "\n")
}

// commentInliner is for types that can have an inline comment.
type commentInliner interface {
inlineComment(c *Comment)
}

// elementContainer unifies types that have elements.
type elementContainer interface {
addElement(v Visitee)
elements() []Visitee
Expand Down
8 changes: 4 additions & 4 deletions proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func parseFormattedParsed(t *testing.T, filename string) {
// open it
f, err := os.Open(filename)
if err != nil {
t.Fatal(err)
t.Fatal(filename, err)
}
defer f.Close()
// parse it
p := NewParser(f)
def, err := p.Parse()
if err != nil {
t.Fatal(err)
t.Fatal(filename, err)
}
// count it
c := new(counter)
Expand All @@ -49,14 +49,14 @@ func parseFormattedParsed(t *testing.T, filename string) {
fp := NewParser(bytes.NewReader(out.Bytes()))
_, err = fp.Parse()
if err != nil {
t.Fatal(err)
t.Fatal(filename, err)
}
// count it again
c.count = 0
c.Count(def.Elements)
afterCount := c.count
if got, want := afterCount, beforeCount; got != want {
t.Errorf("got [%v] want [%v]", got, want)
t.Errorf("[%s] got [%v] want [%v]", filename, got, want)
}
t.Log("# proto elements", afterCount)
}
Expand Down
7 changes: 7 additions & 0 deletions reserved.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
type Reserved struct {
Ranges string
FieldNames []string
Comment *Comment
}

// inlineComment is part of commentInliner.
func (r *Reserved) inlineComment(c *Comment) {
r.Comment = c
}

// Range is to specify number intervals
Expand Down Expand Up @@ -41,5 +47,6 @@ func (r *Reserved) parse(p *Parser) error {
} else {
r.Ranges = content
}
p.s.unread(';') // put it back for reading inline comments TODO
return nil
}

0 comments on commit 1a88e9f

Please sign in to comment.