diff --git a/header.go b/header.go index 1947705..8d5fbfb 100644 --- a/header.go +++ b/header.go @@ -7,7 +7,7 @@ import ( func pbHeaderComment(w io.Writer) { // technically this should be the first line and in this exact format: - fmt.Fprintln(w, "// Code modified by go.wit.com/apps/autogenpb. DO NOT EDIT.") + fmt.Fprintln(w, "// Code modified by go.wit.com/apps/autogenpb DO NOT EDIT.") fmt.Fprintln(w, "//") fmt.Fprintln(w, "// user defined Mutex locks were auto added") fmt.Fprintln(w, "//") @@ -19,18 +19,14 @@ func pbHeaderComment(w io.Writer) { func headerComment(w io.Writer) { // technically this should be the first line and in this exact format: - fmt.Fprintln(w, "// Code generated by go.wit.com/apps/autogenpb. DO NOT EDIT.") + fmt.Fprintln(w, "// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.") fmt.Fprintln(w, "// This file was autogenerated with autogenpb", VERSION, BUILDTIME) fmt.Fprintln(w, "// go install go.wit.com/apps/autogenpb@latest") fmt.Fprintln(w, "//") - 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, "// define which structs (messages) you want to use in the .proto file") + fmt.Fprintln(w, "// Then sort.pb.go and marshal.pb.go files are autogenerated") 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, "// autogenpb uses it and has an example .proto file with instructions") fmt.Fprintln(w, "//") fmt.Fprintln(w, "") } diff --git a/main.go b/main.go index e0d22c6..6e85d5b 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,11 @@ //go:build go1.20 // +build go1.20 +// protobuf the way I am using them, require GO 1.20. I think. I could be wrong. + +// go:generate go-mod-clean +// go:generate autogenpb auto.proto + package main import ( diff --git a/newsort.go b/newsort.go index 5b6084b..aa6a8c2 100644 --- a/newsort.go +++ b/newsort.go @@ -3,6 +3,8 @@ package main import ( "os" "strings" + + "go.wit.com/log" ) func (pb *Files) makeNewSortfile(pf *File) { @@ -10,12 +12,15 @@ func (pb *Files) makeNewSortfile(pf *File) { header(f, pf) - if sortmap["lock"] == "all" { - // if the lock is set to 'all' this means the mutex was put in the protoc-gen-go struct - } else { - syncLock(f, sortmap) + for _, msg := range pf.MsgNames { + if msg.DoMutex { + pf.syncLock(f, msg.Lockname) + pf.iterTop(f, msg.Name) + } else { + log.Info("Skipping syncLock() for", msg.Name, "DoMutex = false") + } } - iterTop(f, sortmap) + iterNext(f, sortmap) iterAppend(f, sortmap) // Append() enforce no unique keys iterSortAll(f, sortmap) diff --git a/sort.go b/sort.go index 1d202a6..b098897 100644 --- a/sort.go +++ b/sort.go @@ -13,12 +13,7 @@ func (pb *Files) makeSortfile(pf *File) { header(f, pf) - if sortmap["lock"] == "all" { - // if the lock is set to 'all' this means the mutex was put in the protoc-gen-go struct - } else { - syncLock(f, sortmap) - } - iterTop(f, sortmap) + pf.iterTop(f, sortmap["base"]) iterNext(f, sortmap) iterAppend(f, sortmap) // Append() enforce no unique keys iterSortAll(f, sortmap) @@ -60,25 +55,28 @@ func (pb *Files) makeSortfile(pf *File) { iterEnd(f, sortmap) } -func syncLock(w io.Writer, names map[string]string) { - fmt.Fprintln(w, "// bad global lock until I figure out some other plan") - fmt.Fprintln(w, "// redo/fix protoc-gen-go 1.35 and do it there?") +func (pf *File) syncLock(w io.Writer, lock string) { + var LOCK string = lock + + fmt.Fprintln(w, "// bad global lock until modifying the .pb.go file is tested") fmt.Fprintln(w, "// sync.RWMutex or sync.Mutex?") - fmt.Fprintln(w, "var "+names["lock"]+" sync.RWMutex") + fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex") fmt.Fprintln(w, "") } -func iterTop(w io.Writer, names map[string]string) { - fmt.Fprintln(w, "type "+names["Base"]+"Iterator struct {") +func (pf *File) iterTop(w io.Writer, base string) { + var BASE string = base + + fmt.Fprintln(w, "type "+BASE+"Iterator struct {") fmt.Fprintln(w, " sync.RWMutex") fmt.Fprintln(w, "") - fmt.Fprintln(w, " things []*"+names["Base"]) + fmt.Fprintln(w, " things []*"+BASE) fmt.Fprintln(w, " index int") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") - fmt.Fprintln(w, "// New"+names["Base"]+"Iterator initializes a new iterator.") - fmt.Fprintln(w, "func New"+names["Base"]+"Iterator(things []*"+names["Base"]+") *"+names["Base"]+"Iterator {") - fmt.Fprintln(w, " return &"+names["Base"]+"Iterator{things: things}") + fmt.Fprintln(w, "// New"+BASE+"Iterator initializes a new iterator.") + fmt.Fprintln(w, "func New"+BASE+"Iterator(things []*"+BASE+") *"+BASE+"Iterator {") + fmt.Fprintln(w, " return &"+BASE+"Iterator{things: things}") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more things.") @@ -88,7 +86,7 @@ func iterTop(w io.Writer, names map[string]string) { fmt.Fprintln(w, "// d := iterator.Next(") fmt.Fprintln(w, "// fmt.Println(\"found UUID:\", d.Uuid") fmt.Fprintln(w, "// }") - fmt.Fprintln(w, "func (it *"+names["Base"]+"Iterator) Scan() bool {") + fmt.Fprintln(w, "func (it *"+BASE+"Iterator) Scan() bool {") fmt.Fprintln(w, " if it.index >= len(it.things) {") fmt.Fprintln(w, " return false") fmt.Fprintln(w, " }")