summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go65
-rw-r--r--testautogen/Makefile2
2 files changed, 62 insertions, 5 deletions
diff --git a/main.go b/main.go
index 2a7d0fd..2e1240a 100644
--- a/main.go
+++ b/main.go
@@ -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"] != "" {
+ 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, "// does not enforce any unique fields")
+ 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, "")
+}
diff --git a/testautogen/Makefile b/testautogen/Makefile
index 0e85afd..4bbcdef 100644
--- a/testautogen/Makefile
+++ b/testautogen/Makefile
@@ -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