summaryrefslogtreecommitdiff
path: root/protoReformat.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-03-26 05:28:26 -0500
committerJeff Carr <[email protected]>2025-03-26 20:44:06 -0500
commitcb8d3f624cf8dd958c75e5f83128e38ed8547b00 (patch)
tree470998d0819ee38da002aef40d8f2bd21ca1aa43 /protoReformat.go
parentae29d5cc674e0465c5861c1d261421bbe2725043 (diff)
add 'enum' and 'oneof' detection
Diffstat (limited to 'protoReformat.go')
-rw-r--r--protoReformat.go109
1 files changed, 51 insertions, 58 deletions
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