From 57f8f88ae9b6622e4df2fe6f1720d711ae8a8819 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 Jan 2025 17:49:14 -0600 Subject: [PATCH] simple example build test is correct for both mutex --- Makefile | 3 ++- example/Makefile | 5 +++- sort.go | 8 +------ sortFunc.go | 19 ---------------- sortNew.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 sortNew.go diff --git a/Makefile b/Makefile index be9856e..4b825f6 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d_%H%M) simple: build - make -C example simple goimports vet + make -C example clean simpleMutexGlobal goimports vet + make -C example clean simpleMutexProtoc goimports vet full: install clean auto goimports vet build test install @echo everything worked and the example ran diff --git a/example/Makefile b/example/Makefile index e2673c3..552c332 100644 --- a/example/Makefile +++ b/example/Makefile @@ -16,9 +16,12 @@ vet: build: GO111MODULE=off go build -v -simple: +simpleMutexProtoc: ../autogenpb --proto fruit.proto --package main +simpleMutexGlobal: + ../autogenpb --proto fruit.proto --package main --mutex=false + withMutex: ../autogenpb --proto fruit.proto --package main ../autogenpb --proto file.proto --package main diff --git a/sort.go b/sort.go index d553852..ad59978 100644 --- a/sort.go +++ b/sort.go @@ -90,13 +90,7 @@ func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writ var APPLES string = cases.Title(language.English, cases.NoLower).String(find.VarName) var APPLE string = find.VarType var COLOR string = newS - var FruitLock string - if argv.Mutex { - FruitLock = "x.Lock" // uses mutex frum protoc.pb.go file - } else { - FruitLock = FRUIT + ".Lock" // ugly global lock hack. should probably deprecate - } - appendUniqueBy(wFind, FRUIT, APPLES, APPLE, COLOR, FruitLock) + parent.appendUniqueBy(wFind, FRUIT, APPLES, APPLE, COLOR) log.Printf("\t(x %s) FindBy%s(string) *%s\n", parent.Name, newS, find.VarType) if v.VarType == "string" { log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, find.VarType) diff --git a/sortFunc.go b/sortFunc.go index df52114..869a2ab 100644 --- a/sortFunc.go +++ b/sortFunc.go @@ -193,25 +193,6 @@ func (pf *File) appendUnique(w io.Writer) { fmt.Fprintln(w, "") } -func appendUniqueBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FruitLock string) { - fmt.Fprintln(w, "// TESTING") - fmt.Fprintln(w, "// enforces "+APPLE+"."+COLOR+" is unique in "+FRUIT+"."+APPLES) - fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUniqueBy"+COLOR+"(newP *"+APPLE+") bool {") - fmt.Fprintln(w, " "+FruitLock+".Lock()") - fmt.Fprintln(w, " defer "+FruitLock+".Unlock()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") - fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {") - fmt.Fprintln(w, " return false") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)") - fmt.Fprintln(w, " return true") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") -} - func (pf *File) replaceFunc(w io.Writer) { var MSG string = pf.Bases.Name var BASE string = pf.Base.Name diff --git a/sortNew.go b/sortNew.go new file mode 100644 index 0000000..d2c05b1 --- /dev/null +++ b/sortNew.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "io" +) + +func (msg *MsgName) getLockname(s string) string { + if argv.Mutex { + // use the mutex lock from the modified protoc.pb.go file + return s + ".Lock" + } + // a single global lock by struct name + return msg.Lockname +} + +func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { + LOCK := msg.getLockname("x") + + fmt.Fprintln(w, "// TESTING") + fmt.Fprintln(w, "// enforces "+APPLE+" is unique in "+FRUIT+"."+APPLES) + fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUnique(newP *"+APPLE+") bool {") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + for _, COLOR := range COLORS { + fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") + fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {") + fmt.Fprintln(w, " return false") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + } + fmt.Fprintln(w, "") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +} + +func (msg *MsgName) appendUniqueBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { + LOCK := msg.getLockname("x") + + fmt.Fprintln(w, "// TESTING") + fmt.Fprintln(w, "// enforces "+APPLE+"."+COLOR+" is unique in "+FRUIT+"."+APPLES) + fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUniqueBy"+COLOR+"(newP *"+APPLE+") bool {") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") + fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {") + fmt.Fprintln(w, " return false") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +}