allow setting the mutex lock variable name

This commit is contained in:
Jeff Carr 2025-01-13 05:55:36 -06:00
parent adcc0385f4
commit 1626a2a501
3 changed files with 50 additions and 32 deletions

View File

@ -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*

View File

@ -12,6 +12,7 @@ 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"`
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"`

View File

@ -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)
if msg := f.structMatch(line); msg == nil {
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")
} 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
}