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 go.*
-rm -f *.pb.go -rm -f *.pb.go
-rm -f autogenpb -rm -f autogenpb
clean-more:
ls -l autogenpb autogenpb.last
-rm -f autogenpb.2*

17
argv.go
View File

@ -9,14 +9,15 @@ package main
var argv args var argv args
type args struct { type args struct {
Package string `arg:"--package" help:"the package name"` Package string `arg:"--package" help:"the package name"`
Proto string `arg:"--proto" help:"the .proto filename"` Proto string `arg:"--proto" help:"the .proto filename"`
Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"` 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"` MutexName string `arg:"--mutex-name" help:"use a var name for the mutex"`
DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"` Delete bool `arg:"--delete" help:"use delete with copy experiment"`
GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"`
GoPath string `arg:"--gopath" help:"the gopath of this repo"` GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"`
Identify string `arg:"--identify" help:"identify file"` GoPath string `arg:"--gopath" help:"the gopath of this repo"`
Identify string `arg:"--identify" help:"identify file"`
} }
func (a args) Description() string { func (a args) Description() string {

View File

@ -75,7 +75,9 @@ func (pb *Files) addMutex(f *File) error {
for _, line := range lines { for _, line := range lines {
if strings.HasPrefix(line, "package ") { if strings.HasPrefix(line, "package ") {
// log.Info("CHANGING package:", line, "to package:", f.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") // log.Info("CHANGING package:", line, "to package:main")
// fmt.Fprintln(w, "package "+"main") // fmt.Fprintln(w, "package "+"main")
continue continue
@ -87,22 +89,27 @@ func (pb *Files) addMutex(f *File) error {
continue continue
} }
if f.structMatch(line) { if msg := f.structMatch(line); msg == nil {
if argv.Mutex { fmt.Fprintln(w, line)
// 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")
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, "")
}
} else { } else {
fmt.Fprintln(w, line) 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 {
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, "")
}
} }
} }
if argv.Mutex { if argv.Mutex {
@ -117,14 +124,15 @@ func (pb *Files) addMutex(f *File) error {
} }
// is this struct supposed to have a Mutex added? // 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 msg *MsgName
var start string var start string
msg = pf.Bases msg = pf.Bases
start = "type " + msg.Name + " struct {" start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) { 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() // 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 msg = pf.Base
start = "type " + msg.Name + " struct {" start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) { if strings.HasPrefix(line, start) {
return msg.setupMutex(pf.Filebase + "Mu") msg.setupMutex(pf.Filebase + "Mu")
return msg
} }
for _, msg = range pf.MsgNames { for _, msg = range pf.MsgNames {
start = "type " + msg.Name + " struct {" start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) { 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. // nameMu should probably be lowercase.
// notsure if it ever makes sense to export a mutex or even if you can // 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 msg.MutexFound = true
if msg.NoMutex { if msg.NoMutex {
msg.Lockname = nameMu msg.Lockname = nameMu
return false return
}
if argv.MutexName == "" {
msg.Lockname = "x"
} else {
msg.Lockname = "x." + argv.MutexName
} }
msg.Lockname = "x.Lock"
return true
} }