simple example build test is correct for both mutex

This commit is contained in:
Jeff Carr 2025-01-10 17:49:14 -06:00
parent e676c213b1
commit 57f8f88ae9
5 changed files with 66 additions and 28 deletions

View File

@ -2,7 +2,8 @@ VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d_%H%M) BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
simple: build 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 full: install clean auto goimports vet build test install
@echo everything worked and the example ran @echo everything worked and the example ran

View File

@ -16,9 +16,12 @@ vet:
build: build:
GO111MODULE=off go build -v GO111MODULE=off go build -v
simple: simpleMutexProtoc:
../autogenpb --proto fruit.proto --package main ../autogenpb --proto fruit.proto --package main
simpleMutexGlobal:
../autogenpb --proto fruit.proto --package main --mutex=false
withMutex: withMutex:
../autogenpb --proto fruit.proto --package main ../autogenpb --proto fruit.proto --package main
../autogenpb --proto file.proto --package main ../autogenpb --proto file.proto --package main

View File

@ -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 APPLES string = cases.Title(language.English, cases.NoLower).String(find.VarName)
var APPLE string = find.VarType var APPLE string = find.VarType
var COLOR string = newS var COLOR string = newS
var FruitLock string parent.appendUniqueBy(wFind, FRUIT, APPLES, APPLE, COLOR)
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)
log.Printf("\t(x %s) FindBy%s(string) *%s\n", parent.Name, newS, find.VarType) log.Printf("\t(x %s) FindBy%s(string) *%s\n", parent.Name, newS, find.VarType)
if v.VarType == "string" { if v.VarType == "string" {
log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, find.VarType) log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, find.VarType)

View File

@ -193,25 +193,6 @@ func (pf *File) appendUnique(w io.Writer) {
fmt.Fprintln(w, "") 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) { func (pf *File) replaceFunc(w io.Writer) {
var MSG string = pf.Bases.Name var MSG string = pf.Bases.Name
var BASE string = pf.Base.Name var BASE string = pf.Base.Name

59
sortNew.go Normal file
View File

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