diff --git a/protoReformat.go b/protoReformat.go index 818153e..d54ece1 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -18,11 +18,26 @@ import ( var allTheLines *LinesScanner -type Messages interface { - format() []string +type EnumMessage struct { + msgPB *FormatMsg } -var allMessages []Messages +type StdMessage struct { + msgPB *FormatMsg +} + +func (msg *EnumMessage) name() string { + return "fuckit enum" +} + +func (msg *StdMessage) name() string { + return "fuckit std" +} + +type Messages interface { + format() []string + name() string +} func protoReformat(filename string) error { // read in the .proto file @@ -83,45 +98,40 @@ func protoReformat(filename string) error { } fmtmsg.Lines = append(fmtmsg.Lines, line) } + fmtmsg.MaxVarname = bigName + fmtmsg.MaxVartype = bigType // write out the messages allTheLines = newLinesScanner(strings.Split(string(data), "\n")) for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "oneof ") { - newmsg := new(FormatMsg) - newmsg.MaxVarname = bigName - newmsg.MaxVartype = bigType - newmsg.Header = line - loadEnumDefinition(newmsg) - allMessages = append(allMessages, newmsg) - for _, newline := range formatMessage(newmsg) { + newmsg := newStdMessage(fmtmsg, line) + loadMsgDefinition(newmsg) + for _, newline := range newmsg.format() { newfile += fmt.Sprintln(newline) } continue } if strings.HasPrefix(line, "enum ") { - newmsg := new(FormatMsg) - newmsg.MaxVarname = bigName - newmsg.MaxVartype = bigType - newmsg.Header = line + newmsg := newEnumMessage(fmtmsg, line) loadEnumDefinition(newmsg) - for _, newline := range formatEnum(newmsg) { + for _, newline := range newmsg.format() { newfile += fmt.Sprintln(newline) } continue } if strings.HasPrefix(line, "message ") { - newmsg := new(FormatMsg) - newmsg.MaxVarname = bigName - newmsg.MaxVartype = bigType - newmsg.Header = line + newmsg := newStdMessage(fmtmsg, line) - loadMsgDefinition(newmsg) - for _, newline := range formatMessage(newmsg) { - newfile += fmt.Sprintln(newline) + log.Info("got to message", line) + for i, msg := range loadMsgDefinition(newmsg) { + log.Info("got in", i, msg.name()) + for _, newline := range msg.format() { + newfile += fmt.Sprintln(newline) + } } /* parts := strings.Fields(line) @@ -159,45 +169,68 @@ func saveFile(filename string, data string) error { return nil } -func loadMsgDefinition(fmtmsg *FormatMsg) { +func newStdMessage(fmtmsg *FormatMsg, header string) *StdMessage { + newmsg := new(FormatMsg) + newmsg.MaxVarname = fmtmsg.MaxVarname + newmsg.MaxVartype = fmtmsg.MaxVartype + newmsg.Header = header + + newstd := new(StdMessage) + newstd.msgPB = newmsg + + return newstd +} + +func newEnumMessage(fmtmsg *FormatMsg, header string) *EnumMessage { + newmsg := new(FormatMsg) + newmsg.MaxVarname = fmtmsg.MaxVarname + newmsg.MaxVartype = fmtmsg.MaxVartype + newmsg.Header = header + + newstd := new(EnumMessage) + newstd.msgPB = newmsg + + return newstd +} + +func loadMsgDefinition(msg *StdMessage) []Messages { + var allMessages []Messages + allMessages = append(allMessages, msg) + + fmtmsg := msg.msgPB for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "oneof ") { - newmsg := new(FormatMsg) - newmsg.MaxVarname = fmtmsg.MaxVarname - newmsg.MaxVartype = fmtmsg.MaxVartype - newmsg.Header = line - loadEnumDefinition(newmsg) - fmtmsg.Oneofs = append(fmtmsg.Oneofs, newmsg) + newmsg := newStdMessage(fmtmsg, line) + allMessages = append(allMessages, newmsg) + // fmtmsg.Oneofs = append(fmtmsg.Oneofs, newmsg) continue } if strings.HasPrefix(line, "enum ") { - newmsg := new(FormatMsg) - newmsg.MaxVarname = fmtmsg.MaxVarname - newmsg.MaxVartype = fmtmsg.MaxVartype - newmsg.Header = line + newmsg := newEnumMessage(fmtmsg, line) loadEnumDefinition(newmsg) - fmtmsg.Enums = append(fmtmsg.Enums, newmsg) + allMessages = append(allMessages, newmsg) + // fmtmsg.Enums = append(fmtmsg.Enums, newmsg) // log.Info("got here:", line) // os.Exit(-1) 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 - loadMsgDefinition(newmsg) - fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg) + newmsg := newStdMessage(fmtmsg, line) + newAll := loadMsgDefinition(newmsg) + allMessages = append(allMessages, newAll...) + // fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg) continue } if strings.HasPrefix(line, "}") { fmtmsg.Footer = line - return + return allMessages } fmtmsg.Lines = append(fmtmsg.Lines, line) } + + return allMessages } // returns vartype, varname, id, end @@ -244,15 +277,19 @@ func makeLineIter(data []byte) iter.Seq[string] { } } -func loadEnumDefinition(curmsg *FormatMsg) { +func loadEnumDefinition(newMsg *EnumMessage) *EnumMessage { + curmsg := newMsg.msgPB for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "}") { curmsg.Footer = line - return + newMsg.msgPB = curmsg + return newMsg } curmsg.Lines = append(curmsg.Lines, line) } + newMsg.msgPB = curmsg + return newMsg } // find the max length of varname and vartype @@ -278,6 +315,10 @@ func (curmsg *FormatMsg) format() []string { return formatEnum(curmsg) } +func (curmsg *EnumMessage) format() []string { + return formatEnum(curmsg.msgPB) +} + func formatEnum(curmsg *FormatMsg) []string { var newmsg []string newmsg = append(newmsg, curmsg.Header) // +" //header") @@ -291,6 +332,10 @@ func formatEnum(curmsg *FormatMsg) []string { return newmsg } +func (curmsg *StdMessage) format() []string { + return formatMessage(curmsg.msgPB) +} + func formatMessage(curmsg *FormatMsg) []string { var newmsg []string