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

Commit

Permalink
allow options in oneoffield. remove unused code. fix readme link
Browse files Browse the repository at this point in the history
Change-Id: Ibd16945a637f61ebf71d6b34b74a6d5f8e0fff91
  • Loading branch information
emicklei committed Feb 6, 2017
1 parent c133878 commit e3f28dc
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 68 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/emicklei/proto)](https://goreportcard.com/report/github.com/emicklei/proto)
[![GoDoc](https://godoc.org/github.com/emicklei/proto?status.svg)](https://godoc.org/github.com/emicklei/proto)

Package in Go for parsing and formatting Google Protocol Buffers [.proto files version 2 + 3] (https://developers.google.com/protocol-buffers/docs/reference/proto-spec)
Package in Go for parsing and formatting Google Protocol Buffers [.proto files version 2 + 3] (https://developers.google.com/protocol-buffers/docs/reference/proto3-spec)

### usage as package

parser := proto.NewParser(anIOReader)
proto, err := parser.Parse()
parser := proto.NewParser(reader)
definition, err := parser.Parse()
if err != nil {
log.Fatalln("proto parsing failed", err)
}
formatter := proto.NewFormatter(anIOWriter," ")
formatter.Format(proto)

formatter := proto.NewFormatter(writer," ")
formatter.Format(definition)

### usage of protofmt command

Expand Down
8 changes: 7 additions & 1 deletion cmd/protofmt/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# protofmt

protofmt -help
formatting tool for Google ProtocolBuffers version 2 and 3

> protofmt -help
Usage of protofmt [flags] [path ...]
-w write result to (source) file instead of stdout

© 2017, [ernestmicklei.com](http://ernestmicklei.com). MIT License.
39 changes: 5 additions & 34 deletions oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ func (o *Oneof) parse(p *Parser) error {
tok, lit = p.scanIgnoreWhitespace()
switch tok {
case tIDENT:
f := new(OneOfField)
f := newOneOfField()
f.Type = lit
err := f.parse(p)
if err != nil {
if err := parseFieldAfterType(f.Field, p); err != nil {
return err
}
o.Elements = append(o.Elements, f)
Expand Down Expand Up @@ -59,12 +58,11 @@ func (o *Oneof) Accept(v Visitor) {

// OneOfField is part of Oneof.
type OneOfField struct {
Name string
Type string
Sequence int
Options []*Option
*Field
}

func newOneOfField() *OneOfField { return &OneOfField{Field: new(Field)} }

// Accept dispatches the call to the visitor.
func (o *OneOfField) Accept(v Visitor) {
v.VisitOneofField(o)
Expand All @@ -90,30 +88,3 @@ func (o *OneOfField) columns() (cols []aligned) {
}
return
}

func (o *OneOfField) parse(p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
if !isKeyword(tok) {
return p.unexpected(lit, "oneof field identifier", o)
}
}
o.Name = lit
tok, lit = p.scanIgnoreWhitespace()
if tok != tEQUALS {
return p.unexpected(lit, "oneof field =", o)
}
i, err := p.s.scanInteger()
if err != nil {
return p.unexpected(lit, "oneof field sequence number", o)
}
o.Sequence = i
tok, _ = p.scanIgnoreWhitespace()
if tLEFTSQUARE == tok {
// TODO options
p.s.scanUntil(']')
} else {
p.unscan()
}
return nil
}
2 changes: 1 addition & 1 deletion oneof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestOneof(t *testing.T) {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := len(o.Elements), 2; got != want {
t.Errorf("got [%v] want [%v]", got, want)
t.Fatalf("got [%v] want [%v]", got, want)
}
second := o.Elements[1].(*OneOfField)
if got, want := second.Name, "sub_message"; got != want {
Expand Down
1 change: 0 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ type Option struct {
Name string
Constant Literal
IsEmbedded bool
IsCustom bool // TODO needed?
}

// Accept dispatches the call to the visitor.
Expand Down
2 changes: 1 addition & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestOption(t *testing.T) {
t.Fatal(err)
}
if got, want := len(pr.Elements), 1; got != want {
t.Errorf("[%d] got [%v] want [%v]", i, got, want)
t.Fatalf("[%d] got [%v] want [%v]", i, got, want)
}
o := pr.Elements[0].(*Option)
if got, want := o.Name, each.name; got != want {
Expand Down
21 changes: 0 additions & 21 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,3 @@ func (p *Parser) unexpected(found, expected string, obj interface{}) error {
}
return fmt.Errorf("found %q on line %d, expected %s%s", found, p.s.line, expected, debug)
}

// read a single or double-quoted single-line string
// TODO used?
func (p *Parser) scanStringLiteral() (string, error) {
tok, lit := p.scanIgnoreWhitespace()
if tok == tQUOTE {
s := p.s.scanUntil('"')
if len(s) == 0 {
return "", p.unexpected(lit, "quoted string", nil)
}
return s, nil
}
if tok == tSINGLEQUOTE {
s := p.s.scanUntil('\'')
if len(s) == 0 {
return "", p.unexpected(lit, "single quoted string", nil)
}
return s, nil
}
return "", p.unexpected(lit, "single or double quoted string", nil)
}
2 changes: 1 addition & 1 deletion scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *scanner) scanLiteral() (string, bool) {
return buf.String(), false
}

// TODO use scanLiteral?
// scanInteger reads an integer representation.
func (s *scanner) scanInteger() (int, error) {
var i int
if _, err := fmt.Fscanf(s.r, "%d", &i); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func (s *Syntax) parse(p *Parser) error {
if tok, lit := p.scanIgnoreWhitespace(); tok != tEQUALS {
return p.unexpected(lit, "syntax =", s)
}
lit, err := p.scanStringLiteral()
if err != nil {
return err
lit, ok := p.s.scanLiteral()
if !ok {
return p.unexpected(lit, "syntax string constant", s)
}
s.Value = lit
return nil
Expand Down

0 comments on commit e3f28dc

Please sign in to comment.