only do mutex after proto

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-30 23:20:10 -06:00
parent 3a4e0c1046
commit 9a34961d3d
2 changed files with 40 additions and 12 deletions

21
main.go
View File

@ -106,6 +106,18 @@ func main() {
log.Info("protoc build error:", err)
os.Exit(-1)
}
// 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
if err := addMutex(sortmap); err == nil {
log.Info("adding mutex to existing protoc-gen-go file worked")
sortmap["mutex"] = "true"
} else {
log.Info("adding mutex to existing protoc-gen-go file did not work")
sortmap["mutex"] = "false"
}
}
// if foo.pb.go still doesn't exist, protoc failed
@ -115,15 +127,6 @@ func main() {
badExit(errors.New("failed to be created with protoc and proto-gen-go"))
}
// add mutex
if err := addMutex(sortmap); err == nil {
log.Info("adding mutex to existing protoc-gen-go file worked")
sortmap["mutex"] = "true"
} else {
log.Info("adding mutex to existing protoc-gen-go file did not work")
sortmap["mutex"] = "false"
}
if argv.NoSort {
log.Info("not making sort.pb.go file (--no-sort == true)")
} else {

31
sort.go
View File

@ -58,11 +58,18 @@ func makeSortfile() {
func headerComment(w io.Writer) {
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// this file was autogenerated with autogenpb")
fmt.Fprintln(w, "// This file was autogenerated with autogenpb.")
fmt.Fprintln(w, "// go install go.wit.com/apps/autogenpb@latest")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// you might be able to use it on simple, strictly defined protobuf files")
fmt.Fprintln(w, "// You can use it on simple protobuf files")
fmt.Fprintln(w, "// The .proto file must have a singular and plural form of a message")
fmt.Fprintln(w, "// (for those of you that know ruby on rails, it's like that)")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// You can mark which repos you want to auto generate sort.pb.go and marshal.pb.go files for")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// For an example,")
fmt.Fprintln(w, "// go-clone go.wit.com/lib/protobuf/gitpb")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// go install go.wit.com/apps/autogenpb@latest")
fmt.Fprintln(w, "")
}
@ -238,6 +245,24 @@ 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()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
fmt.Fprintln(w, " all."+names["Bases"]+"[i] = all."+names["Bases"]+"[len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " all."+names["Bases"]+" = all."+names["Bases"]+"[:len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
// 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()")