diff --git a/file.proto b/file.proto index 461b69f..e4e6a4f 100644 --- a/file.proto +++ b/file.proto @@ -85,8 +85,7 @@ message Find { bool needAll = 4; // } -message File { - // `autogenpb:var:w io.Writer` +message File { // `autogenpb:var:w io.Writer` string Package = 1; // whatever the package name is at the top of the .go file string filename = 2; // yellow.proto string pbfilename = 3; // yellow.pb.go diff --git a/protoReformat.go b/protoReformat.go index adb9764..07baf76 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -20,10 +20,12 @@ var allTheLines *LinesScanner type EnumMessage struct { msgPB *FormatMsg + all []Message } type StdMessage struct { msgPB *FormatMsg + all []Message } func (msg *EnumMessage) name() string { @@ -31,12 +33,17 @@ func (msg *EnumMessage) name() string { } func (msg *StdMessage) name() string { + if msg.msgPB != nil { + return msg.msgPB.Header + } return "fuckit std" } -type Messages interface { +type Message interface { format() []string name() string + load() + addMsg(Message) } func protoReformat(filename string) error { @@ -104,8 +111,8 @@ func protoReformat(filename string) error { for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "oneof ") { - newmsg := newStdMessage(fmtmsg, line) - loadMsgDefinition(newmsg) + newmsg := fmtmsg.newOneofMessage(line) + newmsg.load() for _, newline := range newmsg.format() { newfile += fmt.Sprintln(newline) } @@ -113,8 +120,9 @@ func protoReformat(filename string) error { } if strings.HasPrefix(line, "enum ") { - newmsg := newEnumMessage(fmtmsg, line) - loadEnumDefinition(newmsg) + newmsg := fmtmsg.newEnumMessage(line) + newmsg.load() + // loadEnumDefinition(newmsg) for _, newline := range newmsg.format() { newfile += fmt.Sprintln(newline) } @@ -122,28 +130,12 @@ func protoReformat(filename string) error { } if strings.HasPrefix(line, "message ") { - newmsg := newStdMessage(fmtmsg, line) - + newmsg := fmtmsg.newStdMessage(line) + newmsg.load() 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) - } + for _, newline := range newmsg.format() { + newfile += fmt.Sprintln(newline) } - /* - 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 } @@ -167,11 +159,21 @@ func saveFile(filename string, data string) error { return nil } -func newStdMessage(fmtmsg *FormatMsg, header string) *StdMessage { +func newDepth(fmtmsg *FormatMsg, header string) *FormatMsg { newmsg := new(FormatMsg) newmsg.MaxVarname = fmtmsg.MaxVarname newmsg.MaxVartype = fmtmsg.MaxVartype newmsg.Header = header + newmsg.Depth = fmtmsg.Depth + 1 + + return newmsg +} + +// func newStdMessage(fmtmsg *FormatMsg, header string) *StdMessage { +func (msgPB *FormatMsg) newStdMessage(header string) *StdMessage { + newmsg := newDepth(msgPB, header) + newmsg.Type = FormatMsg_MESSAGE + msgPB.Msgs = append(msgPB.Msgs, newmsg) newstd := new(StdMessage) newstd.msgPB = newmsg @@ -179,11 +181,21 @@ func newStdMessage(fmtmsg *FormatMsg, header string) *StdMessage { return newstd } -func newEnumMessage(fmtmsg *FormatMsg, header string) *EnumMessage { - newmsg := new(FormatMsg) - newmsg.MaxVarname = fmtmsg.MaxVarname - newmsg.MaxVartype = fmtmsg.MaxVartype - newmsg.Header = header +func (msgPB *FormatMsg) newOneofMessage(header string) *StdMessage { + newmsg := newDepth(msgPB, header) + newmsg.Type = FormatMsg_ONEOF + msgPB.Msgs = append(msgPB.Msgs, newmsg) + + newstd := new(StdMessage) + newstd.msgPB = newmsg + + return newstd +} + +func (msgPB *FormatMsg) newEnumMessage(header string) *EnumMessage { + newmsg := newDepth(msgPB, header) + newmsg.Type = FormatMsg_ENUM + msgPB.Msgs = append(msgPB.Msgs, newmsg) newstd := new(EnumMessage) newstd.msgPB = newmsg @@ -191,44 +203,44 @@ func newEnumMessage(fmtmsg *FormatMsg, header string) *EnumMessage { return newstd } -func loadMsgDefinition(msg *StdMessage) []Messages { - var allMessages []Messages - allMessages = append(allMessages, msg) - - fmtmsg := msg.msgPB +// proto files can be defined as trees +// func loadMsgDefinition(msg *StdMessage) { +// func (newMsg *EnumMessage) load() { +// func (msg *StdMessage) loadMsgDefinition(msg *StdMessage) { +func (msg *StdMessage) load() { + // fmtmsg := msg.msgPB + curPB := msg.msgPB for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "oneof ") { - newmsg := newStdMessage(fmtmsg, line) - allMessages = append(allMessages, newmsg) - // fmtmsg.Oneofs = append(fmtmsg.Oneofs, newmsg) + newmsg := msg.msgPB.newOneofMessage(line) + newmsg.load() + curPB = newmsg.msgPB continue } if strings.HasPrefix(line, "enum ") { - newmsg := newEnumMessage(fmtmsg, line) - loadEnumDefinition(newmsg) - allMessages = append(allMessages, newmsg) - // fmtmsg.Enums = append(fmtmsg.Enums, newmsg) - // log.Info("got here:", line) - // os.Exit(-1) + newmsg := msg.msgPB.newEnumMessage(line) + newmsg.load() + curPB = newmsg.msgPB + // loadEnumDefinition(newmsg) continue } if strings.HasPrefix(line, "message ") { // message inception. search for the architect. don't forget your totem - newmsg := newStdMessage(fmtmsg, line) - newAll := loadMsgDefinition(newmsg) - allMessages = append(allMessages, newAll...) - // fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg) + newmsg := msg.msgPB.newStdMessage(line) + newmsg.load() + curPB = newmsg.msgPB continue } if strings.HasPrefix(line, "}") { - fmtmsg.Footer = line - return allMessages + msg.msgPB.Footer = line + return } - fmtmsg.Lines = append(fmtmsg.Lines, line) + curPB.Notes = append(curPB.Notes, line) + // fmtmsg.Lines = append(fmtmsg.Lines, line) } - return allMessages + return } // returns vartype, varname, id, end @@ -275,19 +287,17 @@ func makeLineIter(data []byte) iter.Seq[string] { } } -func loadEnumDefinition(newMsg *EnumMessage) *EnumMessage { - curmsg := newMsg.msgPB +// func loadEnumDefinition(newMsg *EnumMessage) *EnumMessage { +func (newMsg *EnumMessage) load() { + curPB := newMsg.msgPB for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "}") { - curmsg.Footer = line - newMsg.msgPB = curmsg - return newMsg + curPB.Footer = line + return } - curmsg.Lines = append(curmsg.Lines, line) + curPB.Lines = append(curPB.Lines, line) } - newMsg.msgPB = curmsg - return newMsg } // find the max length of varname and vartype