summaryrefslogtreecommitdiff
path: root/generateMutex.go
diff options
context:
space:
mode:
Diffstat (limited to 'generateMutex.go')
-rw-r--r--generateMutex.go57
1 files changed, 35 insertions, 22 deletions
diff --git a/generateMutex.go b/generateMutex.go
index c5eeb54..9ec0be4 100644
--- a/generateMutex.go
+++ b/generateMutex.go
@@ -75,7 +75,9 @@ func (pb *Files) addMutex(f *File) error {
for _, line := range lines {
if strings.HasPrefix(line, "package ") {
// log.Info("CHANGING package:", line, "to package:", f.Package)
- fmt.Fprintln(w, "package "+f.Package)
+ if f.Package != "" {
+ fmt.Fprintln(w, "package", f.Package, "// autogenpb changed the package name")
+ }
// log.Info("CHANGING package:", line, "to package:main")
// fmt.Fprintln(w, "package "+"main")
continue
@@ -87,22 +89,27 @@ func (pb *Files) addMutex(f *File) error {
continue
}
- 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") // this must be 'Lock' or Marshal() panics?
- // fmt.Fprintln(w, "\t// auto-added by go.wit.com/apps/autogenpb")
- // fmt.Fprintln(w, "\tsync.RWMutex")
+ if msg := f.structMatch(line); msg == nil {
+ fmt.Fprintln(w, line)
+ } else {
+ fmt.Fprintln(w, line)
+ if !argv.Mutex {
+ fmt.Fprintln(w, "\t// sync.RWMutex // skipped. argv was --mutex=false")
+ fmt.Fprintln(w, "")
+ continue
+ }
+ if msg.NoMutex {
+ fmt.Fprintln(w, "\t// sync.RWMutex // skipped. protobuf file has `autogenpb:nomutex`")
+ fmt.Fprintln(w, "")
+ continue
+ }
+ if argv.MutexName == "" {
+ fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb") // this must be 'Lock' or Marshal() panics?
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.Fprintf(w, "\t%s sync.RWMutex // auto-added by go.wit.com/apps/autogenpb\n", argv.MutexName) // this must be 'Lock' or Marshal() panics?
fmt.Fprintln(w, "")
}
- } else {
- fmt.Fprintln(w, line)
}
}
if argv.Mutex {
@@ -117,14 +124,15 @@ func (pb *Files) addMutex(f *File) error {
}
// is this struct supposed to have a Mutex added?
-func (pf *File) structMatch(line string) bool {
+func (pf *File) structMatch(line string) *MsgName {
var msg *MsgName
var start string
msg = pf.Bases
start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) {
- return msg.setupMutex(pf.Filebase + "Mu")
+ msg.setupMutex(pf.Filebase + "Mu")
+ return msg
}
// ONLY PASS THIS IF YOU DO NOT WANT TO USE MARSHAL()
@@ -132,26 +140,31 @@ func (pf *File) structMatch(line string) bool {
msg = pf.Base
start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) {
- return msg.setupMutex(pf.Filebase + "Mu")
+ msg.setupMutex(pf.Filebase + "Mu")
+ return msg
}
for _, msg = range pf.MsgNames {
start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) {
- return msg.setupMutex(pf.Filebase + "Mu")
+ msg.setupMutex(pf.Filebase + "Mu")
+ return msg
}
}
- return false
+ return nil
}
// nameMu should probably be lowercase.
// notsure if it ever makes sense to export a mutex or even if you can
-func (msg *MsgName) setupMutex(nameMu string) bool {
+func (msg *MsgName) setupMutex(nameMu string) {
msg.MutexFound = true
if msg.NoMutex {
msg.Lockname = nameMu
- return false
+ return
+ }
+ if argv.MutexName == "" {
+ msg.Lockname = "x"
+ } else {
+ msg.Lockname = "x." + argv.MutexName
}
- msg.Lockname = "x.Lock"
- return true
}