works again on my simple proto buf examples

This commit is contained in:
Jeff Carr 2025-03-26 09:48:03 -05:00
parent cb8d3f624c
commit f3b9d40dfd
2 changed files with 132 additions and 57 deletions

View File

@ -59,7 +59,7 @@ message Sort {
bool needAll = 5; // bool needAll = 5; //
} }
// used to format protobuf files // used to auto-format protobuf files
message FormatMsg { message FormatMsg {
repeated string lines = 1; // keys to sort on repeated string lines = 1; // keys to sort on
int64 maxVarname = 2; // max string length of var names int64 maxVarname = 2; // max string length of var names
@ -67,6 +67,9 @@ message FormatMsg {
repeated FormatMsg inceptionMsgs = 4; // messages inside messages repeated FormatMsg inceptionMsgs = 4; // messages inside messages
repeated FormatMsg enums = 5; // locally defined enums repeated FormatMsg enums = 5; // locally defined enums
repeated FormatMsg oneofs = 6; // locally defined oneofs repeated FormatMsg oneofs = 6; // locally defined oneofs
string header = 7; // the 'message {','enum {', etc line
string footer = 8; // the '}' line
repeated string notes = 9; // unknown lines or comments
} }
message Find { message Find {

View File

@ -67,37 +67,65 @@ func protoReformat(filename string) error {
fmtmsg.MaxVarname = bigName fmtmsg.MaxVarname = bigName
fmtmsg.MaxVartype = bigType fmtmsg.MaxVartype = bigType
// getMessage(fmtmsg)
// write out the messages // write out the messages
allTheLines = newLinesScanner(strings.Split(string(data), "\n")) allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
for allTheLines.Scan() { for allTheLines.Scan() {
line := allTheLines.Next() line := allTheLines.Next()
if strings.HasPrefix(line, "oneof ") { if strings.HasPrefix(line, "oneof ") {
/*
if inMessage { if inMessage {
// message inception. search for the architect. don't forget your totem // message inception. search for the architect. don't forget your totem
newmsg := new(FormatMsg) newmsg := new(FormatMsg)
newmsg.MaxVarname = bigName newmsg.MaxVarname = bigName
newmsg.MaxVartype = bigType newmsg.MaxVartype = bigType
newmsg.Lines = append(newmsg.Lines, line) newmsg.Lines = append(newmsg.Lines, line)
getInceptionMsg(newmsg) getInceptionEnum(newmsg)
newmsg.Enums = append(newmsg.Oneofs, newmsg) fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg)
continue 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 strings.HasPrefix(line, "enum ") {
/*
if inMessage { if inMessage {
// message inception. search for the architect. don't forget your totem // message inception. search for the architect. don't forget your totem
newmsg := new(FormatMsg) newmsg := new(FormatMsg)
newmsg.MaxVarname = bigName newmsg.MaxVarname = bigName
newmsg.MaxVartype = bigType newmsg.MaxVartype = bigType
newmsg.Lines = append(newmsg.Lines, line) newmsg.Lines = append(newmsg.Lines, line)
getInceptionMsg(newmsg) getInceptionEnum(newmsg)
newmsg.Enums = append(newmsg.Enums, newmsg) fmtmsg.Enums = append(fmtmsg.Enums, newmsg)
continue continue
} }
*/
newmsg := new(FormatMsg)
newmsg.MaxVarname = bigName
newmsg.MaxVartype = bigType
newmsg.Header = line
getInceptionEnum(newmsg)
continue
} }
if strings.HasPrefix(line, "message ") { if strings.HasPrefix(line, "message ") {
newmsg := FormatMsg{
MaxVarname: bigName,
MaxVartype: bigType,
Header: line,
}
getInceptionMsg(&newmsg)
for _, newline := range formatMessage2(&newmsg) {
newfile += fmt.Sprintln(newline)
}
/*
if inMessage { if inMessage {
// message inception. search for the architect. don't forget your totem // message inception. search for the architect. don't forget your totem
newmsg := new(FormatMsg) newmsg := new(FormatMsg)
@ -105,7 +133,7 @@ func protoReformat(filename string) error {
newmsg.MaxVartype = bigType newmsg.MaxVartype = bigType
newmsg.Lines = append(newmsg.Lines, line) newmsg.Lines = append(newmsg.Lines, line)
getInceptionMsg(newmsg) getInceptionMsg(newmsg)
newmsg.InceptionMsgs = append(newmsg.InceptionMsgs, newmsg) fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
continue continue
} }
@ -121,9 +149,11 @@ func protoReformat(filename string) error {
line = fmt.Sprintf(hmm, start, " ", end) line = fmt.Sprintf(hmm, start, " ", end)
} }
newfile += fmt.Sprintln(line) newfile += fmt.Sprintln(line)
*/
continue continue
} }
/*
// find the end of the message // find the end of the message
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
inMessage = false inMessage = false
@ -137,6 +167,7 @@ func protoReformat(filename string) error {
fmtmsg.MaxVartype = bigType fmtmsg.MaxVartype = bigType
continue continue
} }
*/
// don't format or change anything when not in a "message {" section // don't format or change anything when not in a "message {" section
if !inMessage { if !inMessage {
@ -144,7 +175,7 @@ func protoReformat(filename string) error {
continue 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) 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 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 // returns vartype, varname, id, end
func tokenMsgVar(line string) (string, string, string, string) { func tokenMsgVar(line string) (string, string, string, string) {
parts := strings.Split(line, ";") 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() { for allTheLines.Scan() {
line := allTheLines.Next() line := allTheLines.Next()
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
@ -218,6 +288,7 @@ func getInceptionMsg(curmsg *FormatMsg) {
func formatMessage2(curmsg *FormatMsg) []string { func formatMessage2(curmsg *FormatMsg) []string {
var newmsg []string var newmsg []string
newmsg = append(newmsg, curmsg.Header) // +" //header")
// find the max length of varname and vartype // find the max length of varname and vartype
for _, line := range curmsg.Lines { for _, line := range curmsg.Lines {
parts := strings.Split(line, ";") parts := strings.Split(line, ";")
@ -261,6 +332,7 @@ func formatMessage2(curmsg *FormatMsg) []string {
newline = strings.TrimRight(newline, " ") newline = strings.TrimRight(newline, " ")
newmsg = append(newmsg, newline) newmsg = append(newmsg, newline)
} }
newmsg = append(newmsg, curmsg.Footer) // +" //footer")
return newmsg return newmsg
} }