summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--file.proto3
-rw-r--r--protoReformat.go109
2 files changed, 53 insertions, 59 deletions
diff --git a/file.proto b/file.proto
index 94e05dd..62033f3 100644
--- a/file.proto
+++ b/file.proto
@@ -14,7 +14,7 @@ syntax = "proto3";
// You can generate Marshal & Unmarshal for any struct (message) you want
// You can generate SortBy and Append functions ONLY FOR 'repeated <message>'
// Also, those structs must be defined in the same file
-// Additionally, you must use `autogenpb:mutex` on the parent struct.
+// Additionally, you must use `autogenpb:mutex` on the parent struct.
// The autogenerated code requires a RW mutex and autogenpb will insert it into the struct
package main;
@@ -66,6 +66,7 @@ message FormatMsg {
int64 maxVartype = 3; // max string length of var types
repeated FormatMsg inceptionMsgs = 4; // messages inside messages
repeated FormatMsg enums = 5; // locally defined enums
+ repeated FormatMsg oneofs = 6; // locally defined oneofs
}
message Find {
diff --git a/protoReformat.go b/protoReformat.go
index 6b90347..46f93f4 100644
--- a/protoReformat.go
+++ b/protoReformat.go
@@ -13,10 +13,9 @@ import (
"go.wit.com/log"
)
-// like 'goimport' but for .proto files
+// like 'goimport', but for .proto files
-// var maxVarname int
-// var maxVartype int
+var allTheLines *LinesScanner
func protoReformat(filename string) error {
// read in the .proto file
@@ -69,12 +68,46 @@ func protoReformat(filename string) error {
fmtmsg.MaxVartype = bigType
// write out the messages
- all := newLinesScanner(strings.Split(string(data), "\n"))
- for all.Scan() {
- line := all.Next()
+ allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
+ for allTheLines.Scan() {
+ line := allTheLines.Next()
+ if strings.HasPrefix(line, "oneof ") {
+ if inMessage {
+ // message inception. search for the architect. don't forget your totem
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = bigName
+ newmsg.MaxVartype = bigType
+ newmsg.Lines = append(newmsg.Lines, line)
+ getInceptionMsg(newmsg)
+ newmsg.Enums = append(newmsg.Oneofs, newmsg)
+ continue
+ }
+ }
+
+ if strings.HasPrefix(line, "enum ") {
+ if inMessage {
+ // message inception. search for the architect. don't forget your totem
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = bigName
+ newmsg.MaxVartype = bigType
+ newmsg.Lines = append(newmsg.Lines, line)
+ getInceptionMsg(newmsg)
+ newmsg.Enums = append(newmsg.Enums, newmsg)
+ continue
+ }
+ }
+
if strings.HasPrefix(line, "message ") {
if inMessage {
// message inception. search for the architect. don't forget your totem
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = bigName
+ newmsg.MaxVartype = bigType
+ newmsg.Lines = append(newmsg.Lines, line)
+ getInceptionMsg(newmsg)
+ newmsg.InceptionMsgs = append(newmsg.InceptionMsgs, newmsg)
+ continue
+
}
inMessage = true
parts := strings.Fields(line)
@@ -127,57 +160,6 @@ func protoReformat(filename string) error {
return nil
}
-/*
-func formatMessage(curmsg []string) []string {
- var newmsg []string
-
- // find the max length of varname and vartype
- for _, line := range curmsg {
- parts := strings.Split(line, ";")
- if len(parts) < 2 {
- // line is blank or just a comment
- continue
- }
-
- vartype, varname, _, _ := tokenMsgVar(line)
- if len(vartype) > maxVartype {
- maxVartype = len(vartype)
- }
- if len(varname) > maxVarname {
- maxVarname = len(varname)
- }
- }
-
- for _, line := range curmsg {
- line = strings.TrimSpace(line)
- if line == "" {
- newmsg = append(newmsg, line)
- continue
- }
- if strings.HasPrefix(line, "//") {
- pad := fmt.Sprintf("%d", maxVartype+maxVarname+21)
- hmm := "%" + pad + "s %s"
- line = fmt.Sprintf(hmm, " ", line) // todo: compute 50
- newmsg = append(newmsg, line)
- continue
- }
- mt := fmt.Sprintf("%d", maxVartype)
- mv := fmt.Sprintf("%d", maxVarname)
-
- hmm := " %-" + mt + "s %-" + mv + "s = %-3s %s"
-
- vartype, varname, id, end := tokenMsgVar(line)
- end = strings.TrimSpace(end)
- id = id + ";"
-
- newline := fmt.Sprintf(hmm, vartype, varname, id, end)
- newline = strings.TrimRight(newline, " ")
- newmsg = append(newmsg, newline)
- }
- return newmsg
-}
-*/
-
// returns vartype, varname, id, end
func tokenMsgVar(line string) (string, string, string, string) {
parts := strings.Split(line, ";")
@@ -222,6 +204,17 @@ func makeLineIter(data []byte) iter.Seq[string] {
}
}
+func getInceptionMsg(curmsg *FormatMsg) {
+ for allTheLines.Scan() {
+ line := allTheLines.Next()
+ if strings.HasPrefix(line, "}") {
+ curmsg.Lines = append(curmsg.Lines, line)
+ return
+ }
+ curmsg.Lines = append(curmsg.Lines, line)
+ }
+}
+
func formatMessage2(curmsg *FormatMsg) []string {
var newmsg []string
@@ -299,7 +292,7 @@ func (it *LinesScanner) Next() string {
if it.index-1 == len(it.things) {
fmt.Println("Next() error in LinesScanner", it.index)
}
- return it.things[it.index-1]
+ return strings.TrimSpace(it.things[it.index-1])
}
// END DEFINE THE ITERATOR