a real world example
This commit is contained in:
parent
be94cbe320
commit
4b7f420045
3
argv.go
3
argv.go
|
@ -19,7 +19,8 @@ type args struct {
|
|||
Regret bool `arg:"--regret" help:"ignore needed UUID. You will eventually regret this."`
|
||||
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"`
|
||||
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"`
|
||||
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"`
|
||||
|
|
File diff suppressed because it is too large
Load Diff
5
main.go
5
main.go
|
@ -66,6 +66,10 @@ func main() {
|
|||
protoReformat(argv.Proto)
|
||||
okExit("")
|
||||
}
|
||||
if argv.Comments {
|
||||
protoReformatComments(argv.Proto)
|
||||
okExit("")
|
||||
}
|
||||
|
||||
if argv.Regret {
|
||||
// this will override the manditory Uuid checks
|
||||
|
@ -86,6 +90,7 @@ func main() {
|
|||
log.Info("autogenpb parse error:", err)
|
||||
badExit(err)
|
||||
}
|
||||
|
||||
if !argv.NoFormat {
|
||||
protoReformat(argv.Proto)
|
||||
}
|
||||
|
|
160
protoReformat.go
160
protoReformat.go
|
@ -40,12 +40,31 @@ func (msg *StdMessage) name() string {
|
|||
}
|
||||
|
||||
type Message interface {
|
||||
format() []string
|
||||
name() string
|
||||
load()
|
||||
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 {
|
||||
// read in the .proto file
|
||||
data, err := os.ReadFile(filename)
|
||||
|
@ -56,17 +75,6 @@ func protoReformat(filename string) error {
|
|||
|
||||
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
|
||||
fmtmsg = new(FormatMsg)
|
||||
|
||||
|
@ -109,11 +117,12 @@ func protoReformat(filename string) error {
|
|||
// write out the messages
|
||||
allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
|
||||
for allTheLines.Scan() {
|
||||
line := allTheLines.Next()
|
||||
line := allTheLines.NextRaw()
|
||||
|
||||
if strings.HasPrefix(line, "oneof ") {
|
||||
newmsg := fmtmsg.newOneofMessage(line)
|
||||
newmsg.load()
|
||||
for _, newline := range newmsg.format() {
|
||||
for _, newline := range newmsg.msgPB.format() {
|
||||
newfile += fmt.Sprintln(newline)
|
||||
}
|
||||
continue
|
||||
|
@ -123,7 +132,7 @@ func protoReformat(filename string) error {
|
|||
newmsg := fmtmsg.newEnumMessage(line)
|
||||
newmsg.load()
|
||||
// loadEnumDefinition(newmsg)
|
||||
for _, newline := range newmsg.format() {
|
||||
for _, newline := range newmsg.msgPB.format() {
|
||||
newfile += fmt.Sprintln(newline)
|
||||
}
|
||||
continue
|
||||
|
@ -133,7 +142,7 @@ func protoReformat(filename string) error {
|
|||
newmsg := fmtmsg.newStdMessage(line)
|
||||
newmsg.load()
|
||||
log.Info("got to message", line)
|
||||
for _, newline := range newmsg.format() {
|
||||
for _, newline := range newmsg.msgPB.format() {
|
||||
newfile += fmt.Sprintln(newline)
|
||||
}
|
||||
continue
|
||||
|
@ -215,13 +224,13 @@ func (msg *StdMessage) load() {
|
|||
if strings.HasPrefix(line, "oneof ") {
|
||||
newmsg := msg.msgPB.newOneofMessage(line)
|
||||
newmsg.load()
|
||||
curPB = newmsg.msgPB
|
||||
// curPB = newmsg.msgPB
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, "enum ") {
|
||||
newmsg := msg.msgPB.newEnumMessage(line)
|
||||
newmsg.load()
|
||||
curPB = newmsg.msgPB
|
||||
// curPB = newmsg.msgPB
|
||||
// loadEnumDefinition(newmsg)
|
||||
continue
|
||||
}
|
||||
|
@ -229,14 +238,14 @@ func (msg *StdMessage) load() {
|
|||
// message inception. search for the architect. don't forget your totem
|
||||
newmsg := msg.msgPB.newStdMessage(line)
|
||||
newmsg.load()
|
||||
curPB = newmsg.msgPB
|
||||
// curPB = newmsg.msgPB
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, "}") {
|
||||
msg.msgPB.Footer = line
|
||||
return
|
||||
}
|
||||
curPB.Notes = append(curPB.Notes, line)
|
||||
curPB.Lines = append(curPB.Lines, line)
|
||||
// fmtmsg.Lines = append(fmtmsg.Lines, line)
|
||||
}
|
||||
|
||||
|
@ -319,36 +328,54 @@ func setMaxSizes(curmsg *FormatMsg) {
|
|||
}
|
||||
}
|
||||
|
||||
func (curmsg *FormatMsg) format() []string {
|
||||
return formatEnum(curmsg)
|
||||
// use this for header and footer lines
|
||||
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 {
|
||||
return formatEnum(curmsg.msgPB)
|
||||
// use this for lines inside the message
|
||||
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 {
|
||||
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 {
|
||||
line = " " + strings.TrimSpace(line)
|
||||
line = fmt.Sprintf("%s%s", curmsg.pad(), 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
|
||||
}
|
||||
|
||||
func (curmsg *StdMessage) format() []string {
|
||||
return formatMessage(curmsg.msgPB)
|
||||
func (msg *FormatMsg) format() []string {
|
||||
switch msg.Type {
|
||||
case FormatMsg_ENUM:
|
||||
return formatEnum(msg)
|
||||
case FormatMsg_MESSAGE:
|
||||
return formatMessage(msg)
|
||||
}
|
||||
return formatMessage(msg)
|
||||
}
|
||||
|
||||
func formatMessage(curmsg *FormatMsg) []string {
|
||||
var newmsg []string
|
||||
|
||||
if curmsg.Header != "" {
|
||||
line := curmsg.Header
|
||||
line := fmt.Sprintf("%s%s // msg depth=%d", curmsg.padBase(), curmsg.Header, curmsg.Depth)
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) > 3 {
|
||||
// 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:], " ")
|
||||
offset := int(curmsg.MaxVarname) + int(curmsg.MaxVartype) + 16 - len(start)
|
||||
pad := fmt.Sprintf("%d", offset)
|
||||
hmm := "%s %" + pad + "s %s"
|
||||
line = fmt.Sprintf(hmm, start, " ", end)
|
||||
hmm := "%s %" + pad + "s %s // depth=%d"
|
||||
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
|
||||
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))
|
||||
for _, msg := range curmsg.Msgs {
|
||||
switch msg.Type {
|
||||
case FormatMsg_ENUM:
|
||||
for _, line := range formatEnum(msg) {
|
||||
line = fmt.Sprintf("%s%s", curmsg.pad(), line)
|
||||
newmsg = append(newmsg, line)
|
||||
}
|
||||
if len(varname) > int(curmsg.MaxVarname) {
|
||||
curmsg.MaxVarname = int64(len(varname))
|
||||
case FormatMsg_MESSAGE:
|
||||
for _, line := range formatMessage(msg) {
|
||||
line = fmt.Sprintf("%s%s", curmsg.pad(), line)
|
||||
newmsg = append(newmsg, line)
|
||||
}
|
||||
default:
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
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 _, newline := range msg.format() {
|
||||
newmsg = append(newmsg, newline)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
for _, line := range curmsg.Lines {
|
||||
line = strings.TrimSpace(line)
|
||||
|
@ -455,7 +465,15 @@ func (it *LinesScanner) Scan() bool {
|
|||
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 {
|
||||
if it.index-1 == len(it.things) {
|
||||
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 := it.things[it.index-1]
|
||||
out = commentPreprocessor(out)
|
||||
// return strings.TrimSpace(out)
|
||||
return out
|
||||
return strings.TrimSpace(out)
|
||||
// return out
|
||||
}
|
||||
|
||||
// END DEFINE THE ITERATOR
|
||||
|
@ -481,11 +499,13 @@ func commentPreprocessor(line string) string {
|
|||
var comments []string
|
||||
for _, match := range matches {
|
||||
comments = append(comments, strings.TrimSpace(match[1]))
|
||||
// comments = append(comments, match[1])
|
||||
}
|
||||
|
||||
// Remove the block comments from the original line
|
||||
line = re.ReplaceAllString(line, "")
|
||||
line = strings.TrimSpace(line)
|
||||
// line = strings.TrimSpace(line)
|
||||
line = strings.TrimSuffix(line, " ")
|
||||
|
||||
// Append comments at the end with //
|
||||
for _, comment := range comments {
|
||||
|
|
Loading…
Reference in New Issue