added Delete<Key>() ?

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 13:35:13 -06:00
parent 4b64696a29
commit 908c905675
2 changed files with 63 additions and 6 deletions

67
main.go
View File

@ -56,7 +56,7 @@ func main() {
sortmap["package"] = packageName
sortmap["protobase"] = protobase
sortmap["base"] = argv.LoBase
sortmap["lock"] = sortmap["base"] + "slock"
sortmap["lock"] = sortmap["base"] + "sLock" // is nonglobal and plural
sortmap["Base"] = argv.UpBase
sortmap["Bases"] = sortmap["Base"] + "s"
@ -67,6 +67,7 @@ func main() {
// will keep this key unique if defined
sortmap["append"] = argv.Append
if argv.DryRun {
for k, v := range sortmap {
log.Info(k, "=", v)
@ -82,6 +83,13 @@ func main() {
iterNext(f, sortmap)
iterSort(f, sortmap)
iterAppend(f, sortmap)
if argv.Append == "" {
// do nothing. enforce no unique keys
} else {
// add ReplaceKey()
iterDelete(f, sortmap)
iterReplace(f, sortmap)
}
iterEnd(f, sortmap)
if argv.NoMarshal {
@ -116,6 +124,8 @@ func header(w io.Writer, names map[string]string) {
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?")
fmt.Fprintln(w, "// sync.RWMutex or sync.Mutex?")
fmt.Fprintln(w, "var "+names["lock"]+" sync.RWMutex")
fmt.Fprintln(w, "")
}
@ -217,12 +227,17 @@ func iterEnd(w io.Writer, names map[string]string) {
}
func iterAppend(w io.Writer, names map[string]string) {
if names["append"] != "" {
fmt.Fprintln(w, "// enforces "+names["append"]+" is unique")
} else {
if names["append"] == "" {
fmt.Fprintln(w, "// does not enforce any unique fields")
} else {
fmt.Fprintln(w, "// enforces "+names["append"]+" is unique")
}
if names["append"] == "" {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Append(newP *"+names["Base"]+") bool {")
} else {
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(newP *"+names["Base"]+") bool {")
}
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Append(newP *"+names["Base"]+") bool {")
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
fmt.Fprintln(w, "")
@ -239,3 +254,45 @@ func iterAppend(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "}")
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")
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
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 iterDelete(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()")
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, "")
}

View File

@ -3,7 +3,7 @@ BUILDTIME = $(shell date +%Y.%m.%d)
test: vet
all: clean test.pb.go run vet
all: clean test.pb.go run
run:
../autogenpb --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname" --marshal GitTags --append Refname