unique tag setting for Append()

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 12:59:26 -06:00
parent bdd91505e0
commit 4b64696a29
3 changed files with 18 additions and 8 deletions

View File

@ -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"`

23
main.go
View File

@ -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, "}")

View File

@ -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