add 'enum' and 'oneof' detection
This commit is contained in:
parent
ae29d5cc67
commit
cb8d3f624c
|
@ -66,6 +66,7 @@ message FormatMsg {
|
|||
int64 maxVartype = 3; // max string length of var types
|
||||
repeated FormatMsg inceptionMsgs = 4; // messages inside messages
|
||||
repeated FormatMsg enums = 5; // locally defined enums
|
||||
repeated FormatMsg oneofs = 6; // locally defined oneofs
|
||||
}
|
||||
|
||||
message Find {
|
||||
|
|
109
protoReformat.go
109
protoReformat.go
|
@ -13,10 +13,9 @@ import (
|
|||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// like 'goimport' but for .proto files
|
||||
// like 'goimport', but for .proto files
|
||||
|
||||
// var maxVarname int
|
||||
// var maxVartype int
|
||||
var allTheLines *LinesScanner
|
||||
|
||||
func protoReformat(filename string) error {
|
||||
// read in the .proto file
|
||||
|
@ -69,12 +68,46 @@ func protoReformat(filename string) error {
|
|||
fmtmsg.MaxVartype = bigType
|
||||
|
||||
// write out the messages
|
||||
all := newLinesScanner(strings.Split(string(data), "\n"))
|
||||
for all.Scan() {
|
||||
line := all.Next()
|
||||
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 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 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
|
||||
|
||||
}
|
||||
inMessage = true
|
||||
parts := strings.Fields(line)
|
||||
|
@ -127,57 +160,6 @@ func protoReformat(filename string) error {
|
|||
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
|
||||
func tokenMsgVar(line string) (string, string, string, string) {
|
||||
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 {
|
||||
var newmsg []string
|
||||
|
||||
|
@ -299,7 +292,7 @@ func (it *LinesScanner) Next() string {
|
|||
if it.index-1 == len(it.things) {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue