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

Commit

Permalink
aligning message fields
Browse files Browse the repository at this point in the history
Change-Id: I4c70397e4ac6e9ddfd3577ab59a919c0f7adbad5
  • Loading branch information
emicklei committed Feb 2, 2017
1 parent 34001c2 commit 886454e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
7 changes: 6 additions & 1 deletion aligned.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ type aligned struct {
left bool
}

var alignedEquals = leftAligned(" = ")
var (
alignedEquals = leftAligned(" = ")
alignedShortEquals = leftAligned("=")
alignedSpace = leftAligned(" ")
alignedComma = leftAligned(",")
)

func leftAligned(src string) aligned { return aligned{src, true} }
func rightAligned(src string) aligned { return aligned{src, false} }
Expand Down
2 changes: 1 addition & 1 deletion cmd/protofmt/unformatted.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ message Message
STARTED = 1;
RUN = 2 [(custom_option) = "hello world"];
}
}
} // end message


service SearchService { // comment
Expand Down
23 changes: 23 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package proto

import "strconv"

// Field is an abstract message field.
type Field struct {
Name string
Expand All @@ -22,6 +24,27 @@ func (f *NormalField) Accept(v Visitor) {
v.VisitNormalField(f)
}

// columns returns printable source tokens
func (f *NormalField) columns() (cols []aligned) {
if f.Repeated {
cols = append(cols, leftAligned("repeated "))
} else {
cols = append(cols, alignedSpace)
}
cols = append(cols, leftAligned(f.Name), rightAligned(f.Type), 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()...)
}
cols = append(cols, leftAligned("]"))
}
return
}

// parse expects:
// [ "repeated" | "optional" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
func (f *NormalField) parse(p *Parser) error {
Expand Down
4 changes: 2 additions & 2 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestPrintListOfColumns(t *testing.T) {
b := new(bytes.Buffer)
f := NewFormatter(b, " ")
f.printListOfColumns(list)
formatted := `A = 1 [a = 1234];
ABC = 12 [ab = 1234];
formatted := `A = 1 [a =1234];
ABC = 12 [ab=1234];
`
if got, want := b.String(), formatted; got != want {
t.Errorf("got [%v] want [%v]", got, want)
Expand Down
7 changes: 6 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ func (o *Option) columns() (cols []aligned) {
} else {
cols = append(cols, leftAligned(" ["))
}
cols = append(cols, leftAligned(o.Name), alignedEquals, rightAligned(o.Constant.String()))
cols = append(cols, o.keyValuePair()...)
if o.IsEmbedded {
cols = append(cols, leftAligned("]"))
}
return
}

// keyValuePair returns key = value or "value"
func (o *Option) keyValuePair() (cols []aligned) {
return append(cols, leftAligned(o.Name), alignedShortEquals, rightAligned(o.Constant.String()))
}

// parse reads an Option body
// ( ident | "(" fullIdent ")" ) { "." ident } "=" constant ";"
func (o *Option) parse(p *Parser) error {
Expand Down

0 comments on commit 886454e

Please sign in to comment.