allow setting the mutex lock variable name
This commit is contained in:
parent
adcc0385f4
commit
1626a2a501
4
Makefile
4
Makefile
|
@ -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*
|
||||||
|
|
1
argv.go
1
argv.go
|
@ -12,6 +12,7 @@ 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"`
|
||||||
|
MutexName string `arg:"--mutex-name" help:"use a var name for the mutex"`
|
||||||
Delete bool `arg:"--delete" help:"use delete with copy experiment"`
|
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"`
|
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"`
|
GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"`
|
||||||
|
|
|
@ -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 {
|
|
||||||
// log.Info("Adding Mutex to:", line)
|
|
||||||
fmt.Fprintln(w, 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?
|
} else {
|
||||||
// fmt.Fprintln(w, "\t// auto-added by go.wit.com/apps/autogenpb")
|
fmt.Fprintln(w, line)
|
||||||
// fmt.Fprintln(w, "\tsync.RWMutex")
|
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, "")
|
fmt.Fprintln(w, "")
|
||||||
} else {
|
} else {
|
||||||
// log.Info("Skipping. Mutex = false for:", line)
|
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, line)
|
|
||||||
fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
|
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
fmt.Fprintln(w, line)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue