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

Commit

Permalink
format option, remove line fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Micklei authored and Ernest Micklei committed Jan 29, 2017
1 parent 0ce3cac commit f60be1e
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 81 deletions.
7 changes: 7 additions & 0 deletions cmd/proto3fmt/example0.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ message Message
}

map<string, Nested> terrain = 4;

enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
STARTED = 1;
RUNNING = 2 [(custom_option) = "hello world"];
}
}
56 changes: 0 additions & 56 deletions cmd/proto3fmt/example1.proto

This file was deleted.

29 changes: 27 additions & 2 deletions cmd/proto3fmt/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ func (f *formatter) VisitEnum(e *proto3.Enum) {
func (f *formatter) VisitEnumField(e *proto3.EnumField) {
f.begin("field")
io.WriteString(f.w, paddedTo(e.Name, 10))
fmt.Fprintf(f.w, " = %d", e.Integer)
if e.ValueOption != nil {
io.WriteString(f.w, " ")
e.ValueOption.Accept(f)
} else {
io.WriteString(f.w, ";\n")
}
fmt.Fprintf(f.w, " = %d;\n", e.Integer)
}

func (f *formatter) VisitField(f1 *proto3.Field) {
Expand Down Expand Up @@ -76,7 +79,29 @@ func (f *formatter) VisitMessage(m *proto3.Message) {
}

func (f *formatter) VisitOption(o *proto3.Option) {
panic(errors.New("Not implemented"))
if o.PartOfFieldOrEnum {
io.WriteString(f.w, "[(")
} else {
f.begin("option")
io.WriteString(f.w, "option ")
}
if len(o.Name) > 0 {
io.WriteString(f.w, o.Name)
}
if o.PartOfFieldOrEnum {
io.WriteString(f.w, ")")
}
io.WriteString(f.w, " = ")
if len(o.String) > 0 {
fmt.Fprintf(f.w, "%q", o.String)
} else {
fmt.Fprintf(f.w, "%s", o.Identifier)
}
if o.PartOfFieldOrEnum {
io.WriteString(f.w, "];\n")
} else {
io.WriteString(f.w, ";\n")
}
}

func (f *formatter) VisitPackage(p *proto3.Package) {
Expand Down
1 change: 1 addition & 0 deletions enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (f *EnumField) parse(p *Parser) error {
tok, lit = p.scanIgnoreWhitespace()
if tok == tLEFTSQUARE {
o := new(Option)
o.PartOfFieldOrEnum = true
err := o.parse(p)
if err != nil {
return err
Expand Down
22 changes: 16 additions & 6 deletions enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package proto3
import "testing"

func TestEnum(t *testing.T) {
proto := `
// EnumAllowingAlias is part of TestEnumWithBody
proto := `
enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
Expand All @@ -16,14 +15,25 @@ enum EnumAllowingAlias {
if err != nil {
t.Fatal(err)
}
if got, want := len(collect(pr).Enums()), 1; got != want {
enums := collect(pr).Enums()
if got, want := len(enums), 1; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := len(collect(pr).Enums()[0].Elements), 4; got != want {
if got, want := len(enums[0].Elements), 4; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
e := collect(pr).Enums()[0].Elements[1].(*EnumField)
if got, want := e.Integer, 0; got != want {
ef1 := enums[0].Elements[1].(*EnumField)
if got, want := ef1.Integer, 0; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
ef3 := enums[0].Elements[3].(*EnumField)
if got, want := ef3.Integer, 2; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := ef3.ValueOption.Name, "custom_option"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := ef3.ValueOption.String, "hello world"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
6 changes: 1 addition & 5 deletions import.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package proto3

import "fmt"

// Import holds a filename to another .proto definition.
type Import struct {
Line int
Filename string
Kind string // weak, public, <empty>
}
Expand All @@ -16,7 +13,6 @@ func (i *Import) Accept(v Visitor) {

func (i *Import) parse(p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
i.Line = p.s.line
switch tok {
case tWEAK:
i.Kind = lit
Expand All @@ -27,7 +23,7 @@ func (i *Import) parse(p *Parser) error {
case tQUOTE:
i.Filename = p.s.scanUntil('"')
default:
return fmt.Errorf("found %q, expected weak|public|quoted identifier", lit)
return p.unexpected(lit, "weak|public|quoted identifier")
}
return nil
}
6 changes: 3 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import "fmt"
// Option is a protoc compiler option
type Option struct {
Name string
Identifier string
String string
Boolean bool
String string // will be quoted
Identifier string // will not be quoted
PartOfFieldOrEnum bool
IsCustom bool // TODO needed?
}

// Accept dispatches the call to the visitor.
Expand Down
5 changes: 1 addition & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ func (p *Parser) unscan() { p.buf.n = 1 }

// newComment returns a comment with line indication.
func (p *Parser) newComment(lit string) *Comment {
return &Comment{
Line: p.s.line - 1, // line number that started the comment
Message: lit,
}
return &Comment{Message: lit}
}

// Line returns the line number on which the last token was read.
Expand Down
1 change: 0 additions & 1 deletion proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type Proto struct {

// Comment holds a message and line number.
type Comment struct {
Line int
Message string
}

Expand Down
1 change: 0 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "fmt"

// Service defines a set of RPC calls.
type Service struct {
Line int
Name string
RPCalls []*RPcall
}
Expand Down
3 changes: 0 additions & 3 deletions syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,5 @@ func TestCommentAroundSyntax(t *testing.T) {
if got, want := comments[i-1].Message, " comment"+strconv.Itoa(i); got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := comments[i-1].Line, i+1; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
}

0 comments on commit f60be1e

Please sign in to comment.