fix mutex marshal nil panic

This commit is contained in:
Jeff Carr 2024-12-01 10:42:12 -06:00
parent 3261af8db3
commit 2c8a813b71
4 changed files with 61 additions and 24 deletions

View File

@ -34,7 +34,7 @@ func addMutex(names map[string]string) error {
found = true
log.Info("FOUND line:", line)
fmt.Fprintln(w, line)
fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
fmt.Fprintln(w, "")
} else {
fmt.Fprintln(w, line)

View File

@ -17,6 +17,7 @@ type args struct {
Marshal []string `arg:"--marshal" help:"what to marshal on"`
NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"`
NoSort bool `arg:"--no-sort" help:"do not make a sort.pb.go file"`
Mutex bool `arg:"--mutex" help:"try mutex hack (breaks proto.Marshal()"`
DryRun bool `arg:"--dry-run" help:"show what would be run"`
}

View File

@ -109,8 +109,8 @@ func main() {
// experiment to add a mutex to the structs.
// this might fix my other not so great lock implementation on sort (?)
// not sure though because I haven't tried it. leave it here until
// I can test it
// seems to work, but proto.Marshal() breaks with nil reference
if argv.Mutex {
if err := addMutex(sortmap); err == nil {
log.Info("adding mutex to existing protoc-gen-go file worked")
sortmap["mutex"] = "true"
@ -120,6 +120,7 @@ func main() {
sortmap["mutex"] = "false"
}
}
}
// if foo.pb.go still doesn't exist, protoc failed
// exit here

55
sort.go
View File

@ -153,8 +153,13 @@ func iterSortAll(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Len() int {")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " return len(all."+names["Bases"]+")")
fmt.Fprintln(w, "}")
@ -183,8 +188,13 @@ func iterSortBy(w io.Writer, names map[string]string) {
func iterEnd(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// safely returns a slice of pointers to the "+names["Base"]+" protobufs")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") selectAll"+names["Base"]+"() []*"+names["Base"]+" {")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+names["Base"]+"")
fmt.Fprintln(w, " var aStuff []*"+names["Base"]+"")
@ -209,8 +219,13 @@ func iterAppend(w io.Writer, names map[string]string) {
// fmt.Fprintln(w, "func (all *"+names["Bases"]+") Append(newP *"+names["Base"]+") bool { // todo: make unique name here")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") AppendUnique"+names["append"]+"(newP *"+names["Base"]+") bool {")
}
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
if names["append"] != "" {
fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {")
@ -233,8 +248,13 @@ func iterReplace(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// enforces "+names["append"]+" is unique")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Replace"+names["append"]+"(newP *"+names["Base"]+") bool { // todo: make unique name here")
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if p."+names["append"]+" == newP."+names["append"]+" {")
@ -250,8 +270,13 @@ func iterReplace(w io.Writer, names map[string]string) {
func iterDelete(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) bool {")
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
@ -268,8 +293,13 @@ func iterDelete(w io.Writer, names map[string]string) {
// this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct
func iterDeleteWithCopy(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) *"+names["Base"]+" {")
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " var newr "+names["Base"])
fmt.Fprintln(w, "")
@ -289,8 +319,13 @@ func iterDeleteWithCopy(w io.Writer, names map[string]string) {
func iterFind(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// find a dependancy by the go path")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") FindBy"+names["append"]+"(s string) *"+names["Base"]+" {")
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")