summaryrefslogtreecommitdiff
path: root/protoReformat.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-03-26 09:48:03 -0500
committerJeff Carr <[email protected]>2025-03-26 20:44:06 -0500
commitf3b9d40dfdbadb3255b7e06ab63ab268d09ae316 (patch)
treee1d4423be82e9b75d99b7cf7d92954fb9a0a4c9d /protoReformat.go
parentcb8d3f624cf8dd958c75e5f83128e38ed8547b00 (diff)
works again on my simple proto buf examples
Diffstat (limited to 'protoReformat.go')
-rw-r--r--protoReformat.go182
1 files changed, 127 insertions, 55 deletions
diff --git a/protoReformat.go b/protoReformat.go
index 46f93f4..ee3d2fa 100644
--- a/protoReformat.go
+++ b/protoReformat.go
@@ -67,76 +67,107 @@ func protoReformat(filename string) error {
fmtmsg.MaxVarname = bigName
fmtmsg.MaxVartype = bigType
+ // getMessage(fmtmsg)
+
// write out the messages
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 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)
+ getInceptionEnum(newmsg)
+ fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg)
+ continue
+ }
+ */
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = bigName
+ newmsg.MaxVartype = bigType
+ newmsg.Lines = append(newmsg.Lines, line)
+ getInceptionEnum(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 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)
+ getInceptionEnum(newmsg)
+ fmtmsg.Enums = append(fmtmsg.Enums, newmsg)
+ continue
+ }
+ */
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = bigName
+ newmsg.MaxVartype = bigType
+ newmsg.Header = line
+ getInceptionEnum(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
-
+ newmsg := FormatMsg{
+ MaxVarname: bigName,
+ MaxVartype: bigType,
+ Header: line,
}
- inMessage = true
- parts := strings.Fields(line)
- if len(parts) > 3 {
- // hack to actually indent comments on the message line itself. you're welcome
- start := parts[0] + " " + parts[1] + " " + parts[2]
- end := strings.Join(parts[3:], " ")
- offset := int(bigName) + int(bigType) + 16 - len(start)
- pad := fmt.Sprintf("%d", offset)
- hmm := "%s %" + pad + "s %s"
- line = fmt.Sprintf(hmm, start, " ", end)
+ getInceptionMsg(&newmsg)
+ for _, newline := range formatMessage2(&newmsg) {
+ newfile += fmt.Sprintln(newline)
}
- newfile += fmt.Sprintln(line)
+ /*
+ 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)
+ fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
+ continue
+
+ }
+ inMessage = true
+ parts := strings.Fields(line)
+ if len(parts) > 3 {
+ // hack to actually indent comments on the message line itself. you're welcome
+ start := parts[0] + " " + parts[1] + " " + parts[2]
+ end := strings.Join(parts[3:], " ")
+ offset := int(bigName) + int(bigType) + 16 - len(start)
+ pad := fmt.Sprintf("%d", offset)
+ hmm := "%s %" + pad + "s %s"
+ line = fmt.Sprintf(hmm, start, " ", end)
+ }
+ newfile += fmt.Sprintln(line)
+ */
continue
}
- // find the end of the message
- if strings.HasPrefix(line, "}") {
- inMessage = false
- // format and write the last message to the file
- for _, newline := range formatMessage2(fmtmsg) {
- newfile += fmt.Sprintln(newline)
+ /*
+ // find the end of the message
+ if strings.HasPrefix(line, "}") {
+ inMessage = false
+ // format and write the last message to the file
+ for _, newline := range formatMessage2(fmtmsg) {
+ newfile += fmt.Sprintln(newline)
+ }
+ newfile += fmt.Sprintln(line)
+ fmtmsg = new(FormatMsg)
+ fmtmsg.MaxVarname = bigName
+ fmtmsg.MaxVartype = bigType
+ continue
}
- newfile += fmt.Sprintln(line)
- fmtmsg = new(FormatMsg)
- fmtmsg.MaxVarname = bigName
- fmtmsg.MaxVartype = bigType
- continue
- }
+ */
// don't format or change anything when not in a "message {" section
if !inMessage {
@@ -144,7 +175,7 @@ func protoReformat(filename string) error {
continue
}
- fmtmsg.Lines = append(fmtmsg.Lines, line)
+ // fmtmsg.Lines = append(fmtmsg.Lines, line)
}
pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
@@ -160,6 +191,45 @@ func protoReformat(filename string) error {
return nil
}
+func getInceptionMsg(fmtmsg *FormatMsg) {
+ for allTheLines.Scan() {
+ line := allTheLines.Next()
+ if strings.HasPrefix(line, "oneof ") {
+ // message inception. search for the architect. don't forget your totem
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = fmtmsg.MaxVarname
+ newmsg.MaxVartype = fmtmsg.MaxVartype
+ newmsg.Header = line
+ getInceptionEnum(newmsg)
+ fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg)
+ continue
+ }
+ if strings.HasPrefix(line, "enum ") {
+ // message inception. search for the architect. don't forget your totem
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = fmtmsg.MaxVarname
+ newmsg.MaxVartype = fmtmsg.MaxVartype
+ newmsg.Header = line
+ fmtmsg.Enums = append(fmtmsg.Enums, newmsg)
+ continue
+ }
+ if strings.HasPrefix(line, "message ") {
+ // message inception. search for the architect. don't forget your totem
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = fmtmsg.MaxVarname
+ newmsg.MaxVartype = fmtmsg.MaxVartype
+ newmsg.Header = line
+ fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
+ continue
+ }
+ if strings.HasPrefix(line, "}") {
+ fmtmsg.Footer = line
+ return
+ }
+ fmtmsg.Lines = append(fmtmsg.Lines, line)
+ }
+}
+
// returns vartype, varname, id, end
func tokenMsgVar(line string) (string, string, string, string) {
parts := strings.Split(line, ";")
@@ -204,7 +274,7 @@ func makeLineIter(data []byte) iter.Seq[string] {
}
}
-func getInceptionMsg(curmsg *FormatMsg) {
+func getInceptionEnum(curmsg *FormatMsg) {
for allTheLines.Scan() {
line := allTheLines.Next()
if strings.HasPrefix(line, "}") {
@@ -218,6 +288,7 @@ func getInceptionMsg(curmsg *FormatMsg) {
func formatMessage2(curmsg *FormatMsg) []string {
var newmsg []string
+ newmsg = append(newmsg, curmsg.Header) // +" //header")
// find the max length of varname and vartype
for _, line := range curmsg.Lines {
parts := strings.Split(line, ";")
@@ -261,6 +332,7 @@ func formatMessage2(curmsg *FormatMsg) []string {
newline = strings.TrimRight(newline, " ")
newmsg = append(newmsg, newline)
}
+ newmsg = append(newmsg, curmsg.Footer) // +" //footer")
return newmsg
}