add 'enum' and 'oneof' detection
This commit is contained in:
parent
ae29d5cc67
commit
cb8d3f624c
|
@ -14,7 +14,7 @@ syntax = "proto3";
|
||||||
// You can generate Marshal & Unmarshal for any struct (message) you want
|
// You can generate Marshal & Unmarshal for any struct (message) you want
|
||||||
// You can generate SortBy and Append functions ONLY FOR 'repeated <message>'
|
// You can generate SortBy and Append functions ONLY FOR 'repeated <message>'
|
||||||
// Also, those structs must be defined in the same file
|
// Also, those structs must be defined in the same file
|
||||||
// Additionally, you must use `autogenpb:mutex` on the parent struct.
|
// Additionally, you must use `autogenpb:mutex` on the parent struct.
|
||||||
// The autogenerated code requires a RW mutex and autogenpb will insert it into the struct
|
// The autogenerated code requires a RW mutex and autogenpb will insert it into the struct
|
||||||
|
|
||||||
package main;
|
package main;
|
||||||
|
@ -66,6 +66,7 @@ message FormatMsg {
|
||||||
int64 maxVartype = 3; // max string length of var types
|
int64 maxVartype = 3; // max string length of var types
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
message Find {
|
message Find {
|
||||||
|
|
109
protoReformat.go
109
protoReformat.go
|
@ -13,10 +13,9 @@ import (
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// like 'goimport' but for .proto files
|
// like 'goimport', but for .proto files
|
||||||
|
|
||||||
// var maxVarname int
|
var allTheLines *LinesScanner
|
||||||
// var maxVartype int
|
|
||||||
|
|
||||||
func protoReformat(filename string) error {
|
func protoReformat(filename string) error {
|
||||||
// read in the .proto file
|
// read in the .proto file
|
||||||
|
@ -69,12 +68,46 @@ func protoReformat(filename string) error {
|
||||||
fmtmsg.MaxVartype = bigType
|
fmtmsg.MaxVartype = bigType
|
||||||
|
|
||||||
// write out the messages
|
// write out the messages
|
||||||
all := newLinesScanner(strings.Split(string(data), "\n"))
|
allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
|
||||||
for all.Scan() {
|
for allTheLines.Scan() {
|
||||||
line := all.Next()
|
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 strings.HasPrefix(line, "message ") {
|
||||||
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.MaxVarname = bigName
|
||||||
|
newmsg.MaxVartype = bigType
|
||||||
|
newmsg.Lines = append(newmsg.Lines, line)
|
||||||
|
getInceptionMsg(newmsg)
|
||||||
|
newmsg.InceptionMsgs = append(newmsg.InceptionMsgs, newmsg)
|
||||||
|
continue
|
||||||
|
|
||||||
}
|
}
|
||||||
inMessage = true
|
inMessage = true
|
||||||
parts := strings.Fields(line)
|
parts := strings.Fields(line)
|
||||||
|
@ -127,57 +160,6 @@ func protoReformat(filename string) error {
|
||||||
return nil
|
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
|
// 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, ";")
|
||||||
|
@ -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 {
|
func formatMessage2(curmsg *FormatMsg) []string {
|
||||||
var newmsg []string
|
var newmsg []string
|
||||||
|
|
||||||
|
@ -299,7 +292,7 @@ func (it *LinesScanner) Next() string {
|
||||||
if it.index-1 == len(it.things) {
|
if it.index-1 == len(it.things) {
|
||||||
fmt.Println("Next() error in LinesScanner", it.index)
|
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
|
// END DEFINE THE ITERATOR
|
||||||
|
|
Loading…
Reference in New Issue