almost done removing old map var

This commit is contained in:
Jeff Carr 2025-01-10 03:53:07 -06:00
parent b111cdd1f9
commit 5bb3846a3b
3 changed files with 69 additions and 72 deletions

View File

@ -18,7 +18,7 @@ type args struct {
Marshal []string `arg:"--marshal" help:"what to marshal on"` Marshal []string `arg:"--marshal" help:"what to marshal on"`
NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"` 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"` 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"` 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"` 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"` GoPath string `arg:"--gopath" help:"the gopath of this repo"`

View File

@ -22,5 +22,6 @@ func (pb *Files) makeNewSortfile(pf *File) error {
pf.iterSortBy(f) pf.iterSortBy(f)
pf.iterAll(f) pf.iterAll(f)
pf.iterDelete(f) pf.iterDelete(f)
pf.iterFind(f)
return nil return nil
} }

138
sort.go
View File

@ -127,9 +127,6 @@ func (pf *File) iterSelect(w io.Writer) {
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
} }
func iterAppend(w io.Writer, names map[string]string) {
}
func (pf *File) appendUnique(w io.Writer) { func (pf *File) appendUnique(w io.Writer) {
var MSG string = pf.Bases.Name var MSG string = pf.Bases.Name
var BASE string = pf.Base.Name var BASE string = pf.Base.Name
@ -196,38 +193,35 @@ func (pf *File) appendUnique(w io.Writer) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func iterReplace(w io.Writer, names map[string]string) { func (pf *File) iterReplace(w io.Writer) {
if names["append"] == "" { var MSG string = pf.Bases.Name
// can't continue without a key field var BASE string = pf.Base.Name
} var LOCK string = pf.Bases.Lockname
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")
if sortmap["lock"] == "all" { for _, KEY := range pf.Base.Unique {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") fmt.Fprintln(w, "// enforces "+KEY+" is unique")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") fmt.Fprintln(w, "func (all *"+MSG+") Replace"+KEY+"(newP *"+BASE+") bool { // todo: make unique name here")
} else { fmt.Fprintln(w, " "+LOCK+".RLock()")
fmt.Fprintln(w, " "+names["lock"]+".RLock()") fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
fmt.Fprintln(w, " defer "+names["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) { func (pf *File) iterDelete(w io.Writer) {
var MSG string = pf.Bases.Name var MSG string = pf.Bases.Name
var LOCK string = pf.Bases.Lockname 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, "func (all *"+MSG+") DeleteBy"+KEY+"(s string) bool {")
fmt.Fprintln(w, " "+LOCK+".RLock()") fmt.Fprintln(w, " "+LOCK+".RLock()")
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") 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 // 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) { func (pf *File) iterDeleteWithCopy(w io.Writer) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) *"+names["Base"]+" {") var MSG string = pf.Bases.Name
if sortmap["lock"] == "all" { var BASE string = pf.Base.Name
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") var LOCK string = pf.Bases.Lockname
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else { for _, KEY := range pf.Base.Unique {
fmt.Fprintln(w, " "+names["lock"]+".RLock()") fmt.Fprintln(w, "func (all *"+MSG+") DeleteBy"+KEY+"(s string) *"+BASE+" {")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") 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) { func (pf *File) iterFind(w io.Writer) {
fmt.Fprintln(w, "// find a dependancy by the go path") var MSG string = pf.Bases.Name
fmt.Fprintln(w, "func (all *"+names["Bases"]+") FindBy"+names["append"]+"(s string) *"+names["Base"]+" {") var BASE string = pf.Base.Name
fmt.Fprintln(w, " if all == nil {") var LOCK string = pf.Bases.Lockname
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, " }") for _, KEY := range pf.Base.Unique {
fmt.Fprintln(w, "") fmt.Fprintln(w, "// find a dependancy by the go path")
if sortmap["lock"] == "all" { fmt.Fprintln(w, "func (all *"+MSG+") FindBy"+KEY+"(s string) *"+BASE+" {")
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") fmt.Fprintln(w, " if all == nil {")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") fmt.Fprintln(w, " return nil")
} else { fmt.Fprintln(w, " }")
fmt.Fprintln(w, " "+names["lock"]+".RLock()") fmt.Fprintln(w, "")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") 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, "")
} }