summaryrefslogtreecommitdiff
path: root/addMutex.go
diff options
context:
space:
mode:
Diffstat (limited to 'addMutex.go')
-rw-r--r--addMutex.go56
1 files changed, 33 insertions, 23 deletions
diff --git a/addMutex.go b/addMutex.go
index 5e374f8..12ea1d0 100644
--- a/addMutex.go
+++ b/addMutex.go
@@ -21,6 +21,14 @@ func (pb *Files) addMutex(f *File) error {
return err
}
+ // check if autogenpb has already looked at this file
+ for _, line := range strings.Split(string(data), "\n") {
+ if strings.Contains(line, "autogenpb DO NOT EDIT") {
+ log.Info("autogenpb has already been run")
+ return nil
+ }
+ }
+
w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
pbHeaderComment(w)
@@ -34,30 +42,20 @@ func (pb *Files) addMutex(f *File) error {
// fmt.Fprintln(w, "package "+"main")
continue
}
- var found bool
- for _, msg := range f.MsgNames {
- start := "type " + msg.Name + " struct {"
- // marshalThing(w, msg.Name)
- // log.Info("line:", line)
- if strings.HasPrefix(line, start) {
- msg.MutexFound = true
- found = true
- if argv.Mutex {
- log.Info("Adding Mutex to line:", line)
- fmt.Fprintln(w, line)
- fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
- fmt.Fprintln(w, "")
- } else {
- log.Info("Skipping. Mutex = false for", msg.Name)
- fmt.Fprintln(w, line)
- fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
- fmt.Fprintln(w, "")
- }
- // important to exit here. somehow this matched twice at one point. notsure how
- break
+
+ if f.structMatch(line) {
+ if argv.Mutex {
+ log.Info("Adding Mutex to:", line)
+ fmt.Fprintln(w, line)
+ fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
+ fmt.Fprintln(w, "")
+ } else {
+ log.Info("Skipping. Mutex = false for:", line)
+ fmt.Fprintln(w, line)
+ fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
+ fmt.Fprintln(w, "")
}
- }
- if !found {
+ } else {
fmt.Fprintln(w, line)
}
}
@@ -68,3 +66,15 @@ func (pb *Files) addMutex(f *File) error {
}
return nil
}
+
+// 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 {"
+ if strings.HasPrefix(line, start) {
+ msg.MutexFound = true
+ return true
+ }
+ }
+ return false
+}