This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
forked from emicklei/proto
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on formatter, TODO one of,reserved
- Loading branch information
Ernest Micklei
authored and
Ernest Micklei
committed
Jan 28, 2017
1 parent
1ab5546
commit 6607212
Showing
23 changed files
with
481 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// example0 | ||
syntax = "proto3"; | ||
|
||
|
||
|
||
|
||
// using Any | ||
import "google/protobuf/any.proto"; | ||
|
||
import public "testdata/test.proto"; | ||
|
||
|
||
|
||
/* This pkg | ||
*/ | ||
package here.proto3_proto ; | ||
|
||
|
||
// from a bottle | ||
message Message | ||
{ | ||
|
||
string name =1; | ||
// this is thing | ||
google.protobuf.Any anything = 2 [packed=true, danger=false]; | ||
|
||
repeated | ||
Message | ||
children | ||
= 3; | ||
|
||
|
||
enum Humour { | ||
// something we dont know | ||
UNKNOWN = 0; | ||
PUNS = 1; | ||
SLAPSTICK = 2; | ||
/* who is this? */ | ||
BILL_BAILEY = 3; | ||
} | ||
|
||
map<string, Nested> terrain = 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"strings" | ||
|
||
"github.com/emicklei/proto3" | ||
) | ||
|
||
type formatter struct { | ||
w io.Writer | ||
indentLevel int | ||
lastStmt string | ||
indentSeparator string | ||
} | ||
|
||
func (f *formatter) VisitComment(c *proto3.Comment) { | ||
f.begin("comment") | ||
if c.IsMultiline() { | ||
fmt.Fprintln(f.w, "/*") | ||
fmt.Fprint(f.w, strings.TrimSpace(c.Message)) | ||
fmt.Fprintf(f.w, "\n*/\n") | ||
} else { | ||
fmt.Fprintf(f.w, "//%s\n", c.Message) | ||
} | ||
} | ||
|
||
func (f *formatter) VisitEnum(e *proto3.Enum) { | ||
f.begin("enum") | ||
fmt.Fprintf(f.w, "enum %s {\n", e.Name) | ||
f.indentLevel++ | ||
for _, each := range e.Elements { | ||
each.Accept(f) | ||
} | ||
f.indent(-1) | ||
io.WriteString(f.w, "}\n") | ||
} | ||
|
||
func (f *formatter) VisitEnumField(e *proto3.EnumField) { | ||
f.begin("field") | ||
io.WriteString(f.w, paddedTo(e.Name, 10)) | ||
if e.ValueOption != nil { | ||
e.ValueOption.Accept(f) | ||
} | ||
fmt.Fprintf(f.w, " = %d;\n", e.Integer) | ||
} | ||
|
||
func (f *formatter) VisitField(f1 *proto3.Field) { | ||
f.begin("field") | ||
if f1.Repeated { | ||
io.WriteString(f.w, "repeated ") | ||
} | ||
fmt.Fprintf(f.w, "%s %s = %d;\n", f1.Type, f1.Name, f1.Sequence) | ||
} | ||
|
||
func (f *formatter) VisitImport(i *proto3.Import) { | ||
f.begin("import") | ||
if len(i.Kind) > 0 { | ||
fmt.Fprintf(f.w, "%s ", i.Kind) | ||
} | ||
fmt.Fprintf(f.w, "%q;\n", i.Filename) | ||
} | ||
|
||
func (f *formatter) VisitMessage(m *proto3.Message) { | ||
f.begin("message") | ||
fmt.Fprintf(f.w, "message %s {\n", m.Name) | ||
f.indentLevel++ | ||
for _, each := range m.Elements { | ||
each.Accept(f) | ||
} | ||
f.indentLevel++ | ||
io.WriteString(f.w, "}\n") | ||
} | ||
|
||
func (f *formatter) VisitOption(o *proto3.Option) { | ||
panic(errors.New("Not implemented")) | ||
} | ||
|
||
func (f *formatter) VisitPackage(p *proto3.Package) { | ||
f.begin("package") | ||
fmt.Fprintf(f.w, "package %s;\n", p.Name) | ||
} | ||
|
||
func (f *formatter) VisitService(s *proto3.Service) { | ||
panic(errors.New("Not implemented")) | ||
} | ||
|
||
func (f *formatter) VisitSyntax(s *proto3.Syntax) { | ||
fmt.Fprintf(f.w, "syntax = %q;\n\n", s.Value) | ||
} | ||
|
||
func (f *formatter) VisitOneof(o *proto3.Oneof) { | ||
panic(errors.New("Not implemented")) | ||
} | ||
|
||
func (f *formatter) VisitOneofField(o *proto3.OneOfField) { | ||
panic(errors.New("Not implemented")) | ||
} | ||
|
||
// Utils | ||
|
||
func (f *formatter) begin(stmt string) { | ||
if f.lastStmt != stmt && len(f.lastStmt) > 0 { // not the first line | ||
// add separator because stmt is changed, unless it was comment or a nested thingy | ||
if !strings.Contains("comment message enum", f.lastStmt) { | ||
io.WriteString(f.w, "\n") | ||
} | ||
} | ||
f.indent(0) | ||
f.lastStmt = stmt | ||
} | ||
|
||
func (f *formatter) indent(diff int) { | ||
f.indentLevel += diff | ||
for i := 0; i < f.indentLevel; i++ { | ||
io.WriteString(f.w, f.indentSeparator) | ||
} | ||
} | ||
|
||
func paddedTo(word string, length int) string { | ||
if len(word) >= length { | ||
return word | ||
} | ||
return word + strings.Repeat(" ", length-len(word)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.