summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-01-13 05:55:36 -0600
committerJeff Carr <[email protected]>2025-01-13 05:55:36 -0600
commit1626a2a50191ea6e0981c3e60c8791e42dec7357 (patch)
tree56cb1406418d42c93f23095a324545ce6dba7d5f
parentadcc0385f4198d80ae0b2c7ac5c791920dfcda5a (diff)
allow setting the mutex lock variable name
-rw-r--r--Makefile4
-rw-r--r--argv.go17
-rw-r--r--generateMutex.go57
3 files changed, 48 insertions, 30 deletions
diff --git a/Makefile b/Makefile
index a9ba393..7466598 100644
--- a/Makefile
+++ b/Makefile
@@ -81,3 +81,7 @@ clean:
-rm -f go.*
-rm -f *.pb.go
-rm -f autogenpb
+
+clean-more:
+ ls -l autogenpb autogenpb.last
+ -rm -f autogenpb.2*
diff --git a/argv.go b/argv.go
index 5670f61..4ae5570 100644
--- a/argv.go
+++ b/argv.go
@@ -9,14 +9,15 @@ package main
var argv args
type args struct {
- Package string `arg:"--package" help:"the package name"`
- Proto string `arg:"--proto" help:"the .proto filename"`
- Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"`
- 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"`
- 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"`
- Identify string `arg:"--identify" help:"identify file"`
+ Package string `arg:"--package" help:"the package name"`
+ Proto string `arg:"--proto" help:"the .proto filename"`
+ Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"`
+ MutexName string `arg:"--mutex-name" help:"use a var name for the mutex"`
+ 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"`
+ 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"`
+ Identify string `arg:"--identify" help:"identify file"`
}
func (a args) Description() string {
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
}