diff --git a/Makefile b/Makefile index 4b825f6..9562c0e 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d_%H%M) simple: build - make -C example clean simpleMutexGlobal goimports vet + # make -C example clean simpleMutexGlobal goimports vet make -C example clean simpleMutexProtoc goimports vet full: install clean auto goimports vet build test install diff --git a/addMutex.go b/addMutex.go index ff2aec5..cfd31b3 100644 --- a/addMutex.go +++ b/addMutex.go @@ -69,8 +69,25 @@ func (pb *Files) addMutex(f *File) error { // is this struct supposed to have a Mutex added? func (pf *File) structMatch(line string) bool { - for _, msg := range pf.MsgNames { - start := "type " + msg.Name + " struct {" + var msg *MsgName + 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) { msg.MutexFound = true return true diff --git a/human.go b/human.go new file mode 100644 index 0000000..0aebb80 --- /dev/null +++ b/human.go @@ -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) + } +} diff --git a/protoParse.go b/protoParse.go index 832601d..173dd6f 100644 --- a/protoParse.go +++ b/protoParse.go @@ -33,8 +33,6 @@ func (pb *Files) hasPluralMessage(f *File) error { // nope, not this line continue } - // found the matching message - f.Bases = f.parseForMessage(line) line = scanner.Text() fields := strings.Fields(line) @@ -75,24 +73,19 @@ func (pb *Files) protoParse(f *File) error { // parse the proto file for message struct names for _, line := range strings.Split(string(data), "\n") { - base := cases.Title(language.English, cases.NoLower).String(f.Filebase) if strings.HasPrefix(line, "message ") { 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, "}") { curmsg = nil } if curmsg == nil { + // log.Info("curmsg == nil", line) // can't contiue on nil below here continue } + // log.Info("curmsg != nil", line) parts := strings.Fields(line) msgvar := parseMsgVar(line) @@ -151,16 +144,31 @@ func parseMsgVar(line string) *MsgVar { } // looks for mutex and marshal entries -func (f *File) parseForMessage(line string) *MsgName { +func (pf *File) parseForMessage(line string) *MsgName { fields := strings.Fields(line) - if fields[0] != "message" { + if len(fields) == 0 || fields[0] != "message" { 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]) log.Info("found messge:", msgName) - msg := new(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`") { msg.DoMutex = true diff --git a/protoc.go b/protoc.go index a390abd..f8297bc 100644 --- a/protoc.go +++ b/protoc.go @@ -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) diff --git a/sort.go b/sort.go index e10f1b1..8e4fda9 100644 --- a/sort.go +++ b/sort.go @@ -45,15 +45,16 @@ func (pb *Files) makeNewSortfile(pf *File) error { pf.findFunc(wFind) // 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 } 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 { if !v.IsRepeated { - // log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) + log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) continue } 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 { - // 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 for _, msg := range pf.MsgNames { if msg.Name == find.VarType { @@ -108,22 +109,3 @@ func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writ } 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) - } - } -}