From 4b64696a291e437c4d7219318000636dbda8a0f9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 29 Nov 2024 12:59:26 -0600 Subject: [PATCH] unique tag setting for Append() Signed-off-by: Jeff Carr --- argv.go | 1 + main.go | 23 ++++++++++++++++------- testautogen/Makefile | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/argv.go b/argv.go index fd9f766..52f1728 100644 --- a/argv.go +++ b/argv.go @@ -12,6 +12,7 @@ type args struct { LoBase string `arg:"--lobase" help:"lowercase basename"` UpBase string `arg:"--upbase" help:"uppercase basename"` Proto string `arg:"--proto" help:"the .proto filename"` + Append string `arg:"--append" help:"will keep this key unique on append"` Sort []string `arg:"--sort" help:"how and what to sort on"` Marshal []string `arg:"--marshal" help:"what to marshal on"` NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"` diff --git a/main.go b/main.go index 0763542..2a7d0fd 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,9 @@ func main() { sortmap["sortBy"] = sortparts[0] sortmap["sortKey"] = sortparts[1] + // will keep this key unique if defined + sortmap["append"] = argv.Append + if argv.DryRun { for k, v := range sortmap { log.Info(k, "=", v) @@ -214,17 +217,23 @@ func iterEnd(w io.Writer, names map[string]string) { } func iterAppend(w io.Writer, names map[string]string) { - fmt.Fprintln(w, "// enforces no duplicate Refname names") + if names["append"] != "" { + fmt.Fprintln(w, "// enforces "+names["append"]+" is unique") + } else { + fmt.Fprintln(w, "// does not enforce any unique fields") + } 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, "") - fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {") - fmt.Fprintln(w, " if p.Refname == newP.Refname {") - fmt.Fprintln(w, " return false") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") + if names["append"] != "" { + 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, "}") diff --git a/testautogen/Makefile b/testautogen/Makefile index b0557a9..0e85afd 100644 --- a/testautogen/Makefile +++ b/testautogen/Makefile @@ -6,7 +6,7 @@ test: vet all: clean test.pb.go run vet run: - ../autogenpb --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname" --marshal GitTags + ../autogenpb --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname" --marshal GitTags --append Refname vet: @GO111MODULE=off go vet