a real world example

This commit is contained in:
Jeff Carr 2025-03-27 15:03:11 -05:00
parent be94cbe320
commit 4b7f420045
4 changed files with 1412 additions and 71 deletions

View File

@ -19,7 +19,8 @@ type args struct {
Regret bool `arg:"--regret" help:"ignore needed UUID. You will eventually regret this."` Regret bool `arg:"--regret" help:"ignore needed UUID. You will eventually regret this."`
Delete bool `arg:"--delete" help:"use delete with copy experiment"` Delete bool `arg:"--delete" help:"use delete with copy experiment"`
DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"` DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"`
Format bool `arg:"--format" help:"eformat the .proto file and exit"` Format bool `arg:"--format" help:"format the .proto file and exit"`
Comments bool `arg:"--format-comments" help:"enforce parseable comments in a .proto file"`
NoFormat bool `arg:"--no-format" help:"do not auto-reformat the .proto file"` NoFormat bool `arg:"--no-format" help:"do not auto-reformat the .proto file"`
GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"`
GoPath string `arg:"--gopath" help:"the gopath of this repo"` GoPath string `arg:"--gopath" help:"the gopath of this repo"`

File diff suppressed because it is too large Load Diff

View File

@ -66,6 +66,10 @@ func main() {
protoReformat(argv.Proto) protoReformat(argv.Proto)
okExit("") okExit("")
} }
if argv.Comments {
protoReformatComments(argv.Proto)
okExit("")
}
if argv.Regret { if argv.Regret {
// this will override the manditory Uuid checks // this will override the manditory Uuid checks
@ -86,6 +90,7 @@ func main() {
log.Info("autogenpb parse error:", err) log.Info("autogenpb parse error:", err)
badExit(err) badExit(err)
} }
if !argv.NoFormat { if !argv.NoFormat {
protoReformat(argv.Proto) protoReformat(argv.Proto)
} }

View File

@ -40,12 +40,31 @@ func (msg *StdMessage) name() string {
} }
type Message interface { type Message interface {
format() []string
name() string name() string
load() load()
addMsg(Message) addMsg(Message)
} }
func protoReformatComments(filename string) error {
// read in the .proto file
data, err := os.ReadFile(filename)
if err != nil {
log.Info("file read failed", filename, err)
return err
}
var newfile string
log.Info("filename", filename)
alltest := makeLineIter(data)
// gets the max vartype and varname
for line := range alltest {
newfile += fmt.Sprintln(commentPreprocessor(line))
}
saveFile(filename, newfile)
return nil
}
func protoReformat(filename string) error { func protoReformat(filename string) error {
// read in the .proto file // read in the .proto file
data, err := os.ReadFile(filename) data, err := os.ReadFile(filename)
@ -56,17 +75,6 @@ func protoReformat(filename string) error {
var newfile string var newfile string
/* check the comment preprocessor
log.Info("filename", filename)
alltest := makeLineIter(data)
// gets the max vartype and varname
for line := range alltest {
newfile += fmt.Sprintln(commentPreprocessor(line))
}
saveFile(filename, newfile)
os.Exit(-1)
*/
var fmtmsg *FormatMsg var fmtmsg *FormatMsg
fmtmsg = new(FormatMsg) fmtmsg = new(FormatMsg)
@ -109,11 +117,12 @@ func protoReformat(filename string) error {
// 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.NextRaw()
if strings.HasPrefix(line, "oneof ") { if strings.HasPrefix(line, "oneof ") {
newmsg := fmtmsg.newOneofMessage(line) newmsg := fmtmsg.newOneofMessage(line)
newmsg.load() newmsg.load()
for _, newline := range newmsg.format() { for _, newline := range newmsg.msgPB.format() {
newfile += fmt.Sprintln(newline) newfile += fmt.Sprintln(newline)
} }
continue continue
@ -123,7 +132,7 @@ func protoReformat(filename string) error {
newmsg := fmtmsg.newEnumMessage(line) newmsg := fmtmsg.newEnumMessage(line)
newmsg.load() newmsg.load()
// loadEnumDefinition(newmsg) // loadEnumDefinition(newmsg)
for _, newline := range newmsg.format() { for _, newline := range newmsg.msgPB.format() {
newfile += fmt.Sprintln(newline) newfile += fmt.Sprintln(newline)
} }
continue continue
@ -133,7 +142,7 @@ func protoReformat(filename string) error {
newmsg := fmtmsg.newStdMessage(line) newmsg := fmtmsg.newStdMessage(line)
newmsg.load() newmsg.load()
log.Info("got to message", line) log.Info("got to message", line)
for _, newline := range newmsg.format() { for _, newline := range newmsg.msgPB.format() {
newfile += fmt.Sprintln(newline) newfile += fmt.Sprintln(newline)
} }
continue continue
@ -215,13 +224,13 @@ func (msg *StdMessage) load() {
if strings.HasPrefix(line, "oneof ") { if strings.HasPrefix(line, "oneof ") {
newmsg := msg.msgPB.newOneofMessage(line) newmsg := msg.msgPB.newOneofMessage(line)
newmsg.load() newmsg.load()
curPB = newmsg.msgPB // curPB = newmsg.msgPB
continue continue
} }
if strings.HasPrefix(line, "enum ") { if strings.HasPrefix(line, "enum ") {
newmsg := msg.msgPB.newEnumMessage(line) newmsg := msg.msgPB.newEnumMessage(line)
newmsg.load() newmsg.load()
curPB = newmsg.msgPB // curPB = newmsg.msgPB
// loadEnumDefinition(newmsg) // loadEnumDefinition(newmsg)
continue continue
} }
@ -229,14 +238,14 @@ func (msg *StdMessage) load() {
// message inception. search for the architect. don't forget your totem // message inception. search for the architect. don't forget your totem
newmsg := msg.msgPB.newStdMessage(line) newmsg := msg.msgPB.newStdMessage(line)
newmsg.load() newmsg.load()
curPB = newmsg.msgPB // curPB = newmsg.msgPB
continue continue
} }
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
msg.msgPB.Footer = line msg.msgPB.Footer = line
return return
} }
curPB.Notes = append(curPB.Notes, line) curPB.Lines = append(curPB.Lines, line)
// fmtmsg.Lines = append(fmtmsg.Lines, line) // fmtmsg.Lines = append(fmtmsg.Lines, line)
} }
@ -319,36 +328,54 @@ func setMaxSizes(curmsg *FormatMsg) {
} }
} }
func (curmsg *FormatMsg) format() []string { // use this for header and footer lines
return formatEnum(curmsg) func (msg *FormatMsg) padBase() string {
var pad string
for i := 1; i < int(msg.Depth); i += 1 {
pad += fmt.Sprintf("%8s", " ")
}
return pad
} }
func (curmsg *EnumMessage) format() []string { // use this for lines inside the message
return formatEnum(curmsg.msgPB) func (msg *FormatMsg) pad() string {
var pad string
for i := 0; i < int(msg.Depth); i += 1 {
pad += fmt.Sprintf("%8s", " ")
}
return pad
} }
func formatEnum(curmsg *FormatMsg) []string { func formatEnum(curmsg *FormatMsg) []string {
var newmsg []string var newmsg []string
newmsg = append(newmsg, curmsg.Header) // +" //header") header := fmt.Sprintf("%s%s // enum depth=%d", curmsg.padBase(), curmsg.Header, curmsg.Depth)
newmsg = append(newmsg, header)
for _, line := range curmsg.Lines { for _, line := range curmsg.Lines {
line = " " + strings.TrimSpace(line) line = fmt.Sprintf("%s%s", curmsg.pad(), line)
newmsg = append(newmsg, line) newmsg = append(newmsg, line)
} }
newmsg = append(newmsg, curmsg.Footer) // +" //footer") footer := fmt.Sprintf("%s%s // enum footer depth=%d", curmsg.padBase(), curmsg.Footer, curmsg.Depth)
newmsg = append(newmsg, footer)
return newmsg return newmsg
} }
func (curmsg *StdMessage) format() []string { func (msg *FormatMsg) format() []string {
return formatMessage(curmsg.msgPB) switch msg.Type {
case FormatMsg_ENUM:
return formatEnum(msg)
case FormatMsg_MESSAGE:
return formatMessage(msg)
}
return formatMessage(msg)
} }
func formatMessage(curmsg *FormatMsg) []string { func formatMessage(curmsg *FormatMsg) []string {
var newmsg []string var newmsg []string
if curmsg.Header != "" { if curmsg.Header != "" {
line := curmsg.Header line := fmt.Sprintf("%s%s // msg depth=%d", curmsg.padBase(), curmsg.Header, curmsg.Depth)
parts := strings.Fields(line) parts := strings.Fields(line)
if len(parts) > 3 { if len(parts) > 3 {
// hack to actually indent comments on the message line itself. you're welcome // hack to actually indent comments on the message line itself. you're welcome
@ -356,51 +383,34 @@ func formatMessage(curmsg *FormatMsg) []string {
end := strings.Join(parts[3:], " ") end := strings.Join(parts[3:], " ")
offset := int(curmsg.MaxVarname) + int(curmsg.MaxVartype) + 16 - len(start) offset := int(curmsg.MaxVarname) + int(curmsg.MaxVartype) + 16 - len(start)
pad := fmt.Sprintf("%d", offset) pad := fmt.Sprintf("%d", offset)
hmm := "%s %" + pad + "s %s" hmm := "%s %" + pad + "s %s // depth=%d"
line = fmt.Sprintf(hmm, start, " ", end) line = fmt.Sprintf(hmm, start, " ", end, curmsg.Depth)
} else {
line = fmt.Sprintf("%s // len(parts)=%d depth=%d", line, len(parts), curmsg.Depth)
} }
newmsg = append(newmsg, line) // +" //header") newmsg = append(newmsg, line) // " //header")
} else {
newmsg = append(newmsg, "// ERROR: header was blank") // +" //header")
} }
// find the max length of varname and vartype // find the max length of varname and vartype
setMaxSizes(curmsg) setMaxSizes(curmsg)
/*
for _, line := range curmsg.Lines {
parts := strings.Split(line, ";")
if len(parts) < 2 {
// line is blank or just a comment
continue
}
vartype, varname, _, _ := tokenMsgVar(line)
if len(vartype) > int(curmsg.MaxVartype) {
curmsg.MaxVartype = int64(len(vartype))
}
if len(varname) > int(curmsg.MaxVarname) {
curmsg.MaxVarname = int64(len(varname))
}
}
*/
/*
for _, msg := range curmsg.Enums {
for _, newline := range formatEnum(msg) {
newmsg = append(newmsg, newline)
}
}
for _, msg := range curmsg.Oneofs {
for _, newline := range formatEnum(msg) {
newmsg = append(newmsg, newline)
}
}
for _, msg := range curmsg.Msgs { for _, msg := range curmsg.Msgs {
for _, newline := range msg.format() { switch msg.Type {
newmsg = append(newmsg, newline) case FormatMsg_ENUM:
for _, line := range formatEnum(msg) {
line = fmt.Sprintf("%s%s", curmsg.pad(), line)
newmsg = append(newmsg, line)
}
case FormatMsg_MESSAGE:
for _, line := range formatMessage(msg) {
line = fmt.Sprintf("%s%s", curmsg.pad(), line)
newmsg = append(newmsg, line)
}
default:
} }
} }
*/
for _, line := range curmsg.Lines { for _, line := range curmsg.Lines {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
@ -455,7 +465,15 @@ func (it *LinesScanner) Scan() bool {
return true return true
} }
// Next() returns the next thing in the array // does no cleaning of the data
func (it *LinesScanner) NextRaw() string {
if it.index-1 == len(it.things) {
fmt.Println("Next() error in LinesScanner", it.index)
}
return it.things[it.index-1]
}
// cleans out comments
func (it *LinesScanner) Next() string { 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)
@ -463,8 +481,8 @@ func (it *LinesScanner) Next() string {
// out := commentPreprocessor(it.things[it.index-1]) // out := commentPreprocessor(it.things[it.index-1])
out := it.things[it.index-1] out := it.things[it.index-1]
out = commentPreprocessor(out) out = commentPreprocessor(out)
// return strings.TrimSpace(out) return strings.TrimSpace(out)
return out // return out
} }
// END DEFINE THE ITERATOR // END DEFINE THE ITERATOR
@ -481,11 +499,13 @@ func commentPreprocessor(line string) string {
var comments []string var comments []string
for _, match := range matches { for _, match := range matches {
comments = append(comments, strings.TrimSpace(match[1])) comments = append(comments, strings.TrimSpace(match[1]))
// comments = append(comments, match[1])
} }
// Remove the block comments from the original line // Remove the block comments from the original line
line = re.ReplaceAllString(line, "") line = re.ReplaceAllString(line, "")
line = strings.TrimSpace(line) // line = strings.TrimSpace(line)
line = strings.TrimSuffix(line, " ")
// Append comments at the end with // // Append comments at the end with //
for _, comment := range comments { for _, comment := range comments {