correctly identify the two primary structs

This commit is contained in:
Jeff Carr 2025-01-10 19:37:32 -06:00
parent dc86ae010f
commit 2265143869
6 changed files with 88 additions and 52 deletions

View File

@ -2,7 +2,7 @@ VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d_%H%M) BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
simple: build simple: build
make -C example clean simpleMutexGlobal goimports vet # make -C example clean simpleMutexGlobal goimports vet
make -C example clean simpleMutexProtoc goimports vet make -C example clean simpleMutexProtoc goimports vet
full: install clean auto goimports vet build test install full: install clean auto goimports vet build test install

View File

@ -69,8 +69,25 @@ func (pb *Files) addMutex(f *File) error {
// is this struct supposed to have a Mutex added? // is this struct supposed to have a Mutex added?
func (pf *File) structMatch(line string) bool { func (pf *File) structMatch(line string) bool {
for _, msg := range pf.MsgNames { var msg *MsgName
start := "type " + msg.Name + " struct {" var start string
msg = pf.Bases
start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) {
msg.MutexFound = true
return true
}
msg = pf.Base
start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) {
msg.MutexFound = true
return true
}
for _, msg = range pf.MsgNames {
start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) { if strings.HasPrefix(line, start) {
msg.MutexFound = true msg.MutexFound = true
return true return true

41
human.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"go.wit.com/log"
)
// print the protobuf in human form
func (pf *File) printMsgTable() {
pf.Bases.printMsg()
pf.Base.printMsg()
// everything else
for _, msg := range pf.MsgNames {
msg.printMsg()
}
}
func (msg *MsgName) printMsg() {
var s string
if msg.DoMutex {
s += "(mutex) "
}
if msg.DoMarshal {
s += "(marshal) "
}
log.Printf("%s %s\n", msg.Name, s)
for _, v := range msg.Vars {
var end string
if v.IsRepeated {
end += "(repeated) "
}
if v.HasSort {
end += "(sort) "
}
if v.HasUnique {
end += "(unique) "
}
log.Printf("\t%s %s %s\n", v.VarName, v.VarType, end)
}
}

View File

@ -33,8 +33,6 @@ func (pb *Files) hasPluralMessage(f *File) error {
// nope, not this line // nope, not this line
continue continue
} }
// found the matching message
f.Bases = f.parseForMessage(line)
line = scanner.Text() line = scanner.Text()
fields := strings.Fields(line) fields := strings.Fields(line)
@ -75,24 +73,19 @@ func (pb *Files) protoParse(f *File) error {
// parse the proto file for message struct names // parse the proto file for message struct names
for _, line := range strings.Split(string(data), "\n") { for _, line := range strings.Split(string(data), "\n") {
base := cases.Title(language.English, cases.NoLower).String(f.Filebase)
if strings.HasPrefix(line, "message ") { if strings.HasPrefix(line, "message ") {
curmsg = f.parseForMessage(line) curmsg = f.parseForMessage(line)
prefix := "message " + base + " {" // only look for this for now
if strings.HasPrefix(line, prefix) {
f.Base = curmsg
f.MsgNames = append(f.MsgNames, curmsg)
} else {
f.MsgNames = append(f.MsgNames, curmsg)
}
} }
// this logic isn't right. find end of message with more bravado
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
curmsg = nil curmsg = nil
} }
if curmsg == nil { if curmsg == nil {
// log.Info("curmsg == nil", line)
// can't contiue on nil below here // can't contiue on nil below here
continue continue
} }
// log.Info("curmsg != nil", line)
parts := strings.Fields(line) parts := strings.Fields(line)
msgvar := parseMsgVar(line) msgvar := parseMsgVar(line)
@ -151,16 +144,31 @@ func parseMsgVar(line string) *MsgVar {
} }
// looks for mutex and marshal entries // looks for mutex and marshal entries
func (f *File) parseForMessage(line string) *MsgName { func (pf *File) parseForMessage(line string) *MsgName {
fields := strings.Fields(line) fields := strings.Fields(line)
if fields[0] != "message" { if len(fields) == 0 || fields[0] != "message" {
return nil return nil
} }
var msg *MsgName
msg = new(MsgName)
base := cases.Title(language.English, cases.NoLower).String(pf.Filebase)
prefix := "message " + base + "s {" // only look for this for now
if strings.HasPrefix(line, prefix) {
pf.Bases = msg
} else {
prefix := "message " + base + " {" // only look for this for now
if strings.HasPrefix(line, prefix) {
pf.Base = msg
} else {
pf.MsgNames = append(pf.MsgNames, msg)
}
}
msgName := cases.Title(language.English, cases.NoLower).String(fields[1]) msgName := cases.Title(language.English, cases.NoLower).String(fields[1])
log.Info("found messge:", msgName) log.Info("found messge:", msgName)
msg := new(MsgName)
msg.Name = msgName msg.Name = msgName
msg.Lockname = f.Filebase + "Mu" // this should be lowercase. do not export the Mutex msg.Lockname = pf.Filebase + "Mu" // this should be lowercase. do not export the Mutex
if strings.Contains(line, "`autogenpb:mutex`") { if strings.Contains(line, "`autogenpb:mutex`") {
msg.DoMutex = true msg.DoMutex = true

View File

@ -86,18 +86,6 @@ func (pb *Files) protocBuild(f *File) error {
} }
} }
} }
/*
start := "type " + names["Bases"] + " struct {"
if strings.HasSuffix(line, start) {
found = true
log.Info("FOUND line:", line)
fmt.Fprintln(w, line)
fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
fmt.Fprintln(w, "")
} else {
fmt.Fprintln(w, line)
}
*/
} }
cmd = append(cmd, f.Filename) cmd = append(cmd, f.Filename)

28
sort.go
View File

@ -45,15 +45,16 @@ func (pb *Files) makeNewSortfile(pf *File) error {
pf.findFunc(wFind) pf.findFunc(wFind)
// attempt to add sort functions for pf.Base // attempt to add sort functions for pf.Base
pf.processMessage(pf.Base, wSort, wFind) pf.processMessage(pf.Bases, wSort, wFind)
// pf.processMessage(pf.Base, wSort, wFind)
return nil return nil
} }
func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error { func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error {
log.Printf("%s\n", msg.Name) log.Printf("ADDING FIND AND SORT FOR MESSAGE %s WITH %+v\n", msg.Name, msg.Vars)
for _, v := range msg.Vars { for _, v := range msg.Vars {
if !v.IsRepeated { if !v.IsRepeated {
// log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType)
continue continue
} }
if err := pf.addSortByMsg(msg, v, wSort, wFind); err != nil { if err := pf.addSortByMsg(msg, v, wSort, wFind); err != nil {
@ -64,7 +65,7 @@ func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error {
} }
func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writer) error { func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writer) error {
// log.Printf("\tLOOK HERE: %s %s\n", find.VarName, find.VarType) log.Printf("\tLOOK HERE: %s %s\n", find.VarName, find.VarType)
var found *MsgName var found *MsgName
for _, msg := range pf.MsgNames { for _, msg := range pf.MsgNames {
if msg.Name == find.VarType { if msg.Name == find.VarType {
@ -108,22 +109,3 @@ func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writ
} }
return nil return nil
} }
func (pf *File) printMsgTable() {
for _, msg := range pf.MsgNames {
log.Printf("%s\n", msg.Name)
for _, v := range msg.Vars {
var end string
if v.IsRepeated {
end += "(repeated) "
}
if v.HasSort {
end += "(sort) "
}
if v.HasUnique {
end += "(unique) "
}
log.Printf("\t%s %s %s\n", v.VarName, v.VarType, end)
}
}
}