From 5bb3846a3b8267399dabaa532a0b1c4d31cf7791 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 Jan 2025 03:53:07 -0600 Subject: [PATCH] almost done removing old map var --- argv.go | 2 +- newsort.go | 1 + sort.go | 138 ++++++++++++++++++++++++++--------------------------- 3 files changed, 69 insertions(+), 72 deletions(-) diff --git a/argv.go b/argv.go index 29c5e57..6d496f1 100644 --- a/argv.go +++ b/argv.go @@ -18,7 +18,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() ???)"` + Mutex bool `arg:"--mutex" default:"true" help:"add mutex in protoc autogen file"` DryRun bool `arg:"--dry-run" help:"show what would be run"` GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` GoPath string `arg:"--gopath" help:"the gopath of this repo"` diff --git a/newsort.go b/newsort.go index 2c67c25..5d591c2 100644 --- a/newsort.go +++ b/newsort.go @@ -22,5 +22,6 @@ func (pb *Files) makeNewSortfile(pf *File) error { pf.iterSortBy(f) pf.iterAll(f) pf.iterDelete(f) + pf.iterFind(f) return nil } diff --git a/sort.go b/sort.go index ee5d7a9..2bcff18 100644 --- a/sort.go +++ b/sort.go @@ -127,9 +127,6 @@ func (pf *File) iterSelect(w io.Writer) { fmt.Fprintln(w, "}") } -func iterAppend(w io.Writer, names map[string]string) { -} - func (pf *File) appendUnique(w io.Writer) { var MSG string = pf.Bases.Name var BASE string = pf.Base.Name @@ -196,38 +193,35 @@ func (pf *File) appendUnique(w io.Writer) { fmt.Fprintln(w, "") } -func iterReplace(w io.Writer, names map[string]string) { - if names["append"] == "" { - // can't continue without a key field - } - 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") +func (pf *File) iterReplace(w io.Writer) { + var MSG string = pf.Bases.Name + var BASE string = pf.Base.Name + var LOCK string = pf.Bases.Lockname - 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()") + for _, KEY := range pf.Base.Unique { + fmt.Fprintln(w, "// enforces "+KEY+" is unique") + fmt.Fprintln(w, "func (all *"+MSG+") Replace"+KEY+"(newP *"+BASE+") bool { // todo: make unique name here") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range all."+MSG+" {") + fmt.Fprintln(w, " if p."+KEY+" == newP."+KEY+" {") + fmt.Fprintln(w, " return false") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") } - fmt.Fprintln(w, "") - fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {") - fmt.Fprintln(w, " if p."+names["append"]+" == newP."+names["append"]+" {") - fmt.Fprintln(w, " return false") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " all."+names["Bases"]+" = append(all."+names["Bases"]+", newP)") - fmt.Fprintln(w, " return true") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") } func (pf *File) iterDelete(w io.Writer) { var MSG string = pf.Bases.Name var LOCK string = pf.Bases.Lockname - for _, KEY := range pf.Base.Sort { + for _, KEY := range pf.Base.Unique { fmt.Fprintln(w, "func (all *"+MSG+") DeleteBy"+KEY+"(s string) bool {") fmt.Fprintln(w, " "+LOCK+".RLock()") fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") @@ -246,52 +240,54 @@ func (pf *File) iterDelete(w io.Writer) { } // 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"]+" {") - 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()") +func (pf *File) iterDeleteWithCopy(w io.Writer) { + var MSG string = pf.Bases.Name + var BASE string = pf.Base.Name + var LOCK string = pf.Bases.Lockname + + for _, KEY := range pf.Base.Unique { + fmt.Fprintln(w, "func (all *"+MSG+") DeleteBy"+KEY+"(s string) *"+BASE+" {") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " var newr "+BASE) + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for i, _ := range all."+MSG+" {") + fmt.Fprintln(w, " if all."+MSG+"[i]."+KEY+" == s {") + fmt.Fprintln(w, " newr = *all."+MSG+"[i]") + fmt.Fprintln(w, " all."+MSG+"[i] = all."+MSG+"[len(all."+MSG+")-1]") + fmt.Fprintln(w, " all."+MSG+" = all."+MSG+"[:len(all."+MSG+")-1]") + fmt.Fprintln(w, " return &newr") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " return nil") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") } - fmt.Fprintln(w, "") - fmt.Fprintln(w, " var newr "+names["Base"]) - 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, " newr = *all."+names["Bases"]+"[i]") - 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 &newr") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " return nil") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") } -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, " if all == nil {") - fmt.Fprintln(w, " return nil") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") - 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()") +func (pf *File) iterFind(w io.Writer) { + var MSG string = pf.Bases.Name + var BASE string = pf.Base.Name + var LOCK string = pf.Bases.Lockname + + for _, KEY := range pf.Base.Unique { + fmt.Fprintln(w, "// find a dependancy by the go path") + fmt.Fprintln(w, "func (all *"+MSG+") FindBy"+KEY+"(s string) *"+BASE+" {") + fmt.Fprintln(w, " if all == nil {") + fmt.Fprintln(w, " return nil") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for i, _ := range all."+MSG+" {") + fmt.Fprintln(w, " if all."+MSG+"[i]."+KEY+" == s {") + fmt.Fprintln(w, " return all."+MSG+"[i]") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " return nil") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") } - 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, " return all."+names["Bases"]+"[i]") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " return nil") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") }