diff --git a/.gitignore b/.gitignore index e334c52..abcb734 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ go.sum autogenpb* testfiles/testfiles example/example +small/small diff --git a/Makefile b/Makefile index 2b9bb7d..068d8a1 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ BUILDTIME = $(shell date +%Y.%m.%d_%H%M) simple: build # make -C example clean simpleMutexGlobal goimports vet - # make -C example clean simpleMutexProtoc 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/addMutex.go b/addMutex.go index 0bc4e19..dfb091c 100644 --- a/addMutex.go +++ b/addMutex.go @@ -105,10 +105,7 @@ func (pf *File) structMatch(line string) bool { start = "type " + msg.Name + " struct {" if strings.HasPrefix(line, start) { msg.MutexFound = true - if argv.Marshal { - // msg.Lockname = "fruitMu.Lock" - return false - } + // msg.Lockname = "x.Lock" return true } @@ -116,10 +113,7 @@ func (pf *File) structMatch(line string) bool { start = "type " + msg.Name + " struct {" if strings.HasPrefix(line, start) { msg.MutexFound = true - if argv.Marshal { - // msg.Lockname = "fruitMu.Lock" - return false - } + // msg.Lockname = "x.Lock" return true } } diff --git a/argv.go b/argv.go index d50dfad..5670f61 100644 --- a/argv.go +++ b/argv.go @@ -12,9 +12,8 @@ type args struct { Package string `arg:"--package" help:"the package name"` Proto string `arg:"--proto" help:"the .proto filename"` Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"` - Marshal bool `arg:"--marshal" default:"true" help:"if you need Marshal(), per-message Mutex's fail for some reason"` Delete bool `arg:"--delete" help:"use delete with copy experiment"` - DryRun bool `arg:"--dry-run" help:"show what would be run"` + DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"` GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` GoPath string `arg:"--gopath" help:"the gopath of this repo"` Identify string `arg:"--identify" help:"identify file"` diff --git a/example/Makefile b/example/Makefile index 66edeaf..c8b849f 100644 --- a/example/Makefile +++ b/example/Makefile @@ -46,4 +46,4 @@ goimports: clean: -rm -f go.* -rm -f *.pb.go - -rm -f example basket.pb + -rm -f example diff --git a/example/main.go b/example/main.go index 078dbce..490421e 100644 --- a/example/main.go +++ b/example/main.go @@ -1,6 +1,3 @@ -//go:build go1.20 -// +build go1.20 - package main import ( @@ -15,33 +12,75 @@ import ( var VERSION string var BUILDTIME string -var sortmap map[string]string -var marshalKeys []string -var uniqueKeys []string - var counter int = rand.Intn(100) func main() { var pb *Fruits - // pb = NewFruits() pb = new(Fruits) - // attempt to load basket.pb if err := pb.loadBasket(); err != nil { - log.Info("load basket failed. running this for the first time?") - a := pb.getNewFruit() - pb.Fruits = append(pb.Fruits, a) - pb.printTable() + log.Info("First time user?") } - if err := pb.saveBasket(); err != nil { + // attempt to load basket.pb + fruit := &Fruit{ + Brand: "mom", + City: "New NewYork", + UPC: "2000", + } + pb.Fruits = append(pb.Fruits, fruit) + + a := &Fruit{ + Brand: "fry", + City: "the moon", + UPC: "2001", + } + pb.Fruits = append(pb.Fruits, a) + + b := pb.getNewFruit() + pb.Fruits = append(pb.Fruits, b) + + pb.saveBasket() + + if err := pb.loadBasket(); err != nil { badExit(err) } - if pb == nil { - badExit(fmt.Errorf("pb == nil This should not have happened")) - } - pb.addThings() pb.printTable() - pb.saveBasket() - pb.sortTable(100) +} + +func (pb *Fruits) addFruits(i int) { + var x int + for x < i { + x += 1 + b := pb.getNewFruit() + pb.Fruits = append(pb.Fruits, b) + } +} + +func (pb *Fruits) getNewFruit() *Fruit { + counter += rand.Intn(10) + upc := fmt.Sprintf("%d", counter) + + a := &Fruit{ + Brand: "fry", + City: "the moon", + UPC: upc, + } + return a +} + +func (pb *Fruits) saveBasket() error { + data, err := pb.Marshal() + if err != nil { + return err + } + + w, err := os.OpenFile("basket.pb", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + w.Write(data) + w.Close() + log.Info("saved basket.pb ok") + return nil } func badExit(err error) { @@ -49,36 +88,29 @@ func badExit(err error) { os.Exit(-1) } -func (pb *Fruits) getNewFruit() *Fruit { - counter += rand.Intn(100) - upc := fmt.Sprintf("%d", counter) - fut := &Fruit{ - Brand: "mom", - City: "New NewYork", - UPC: upc, +func (pb *Fruits) loadBasket() error { + if pb == nil { + pb = new(Fruits) } - return fut + data, err := os.ReadFile("basket.pb") + if err != nil { + return err + } + + if err := pb.Unmarshal(data); err != nil { + return err + } + log.Info("loaded basket.pb ok") + return nil } - -/* - x := new(Fruit) - x = &Fruit{ - Brand: "fry", - City: "Germany", - } - pb.printTable() - - testAppend(x) -*/ - func (pb *Fruits) printTable() { var count int all := pb.All() for all.Scan() { tmp := all.Next() count += 1 - log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City) - if count > 5 { + log.Printf("found %d %s %s %s\n", count, tmp.Brand, tmp.City, tmp.UPC) + if count > 10 { break } } @@ -98,74 +130,3 @@ func (pb *Fruits) sortTable(i int) { } log.Printf("Total entries: %d\n", pb.Len()) } - -// adds 10 new entries -func (pb *Fruits) addThings() { - var added int - var count int = rand.Intn(10000) - // var base string = "bender" - for { - count += rand.Intn(1000) - var found bool - name := fmt.Sprintf("bender%d", count) - all := pb.All() - for all.Scan() { - tmp := all.Next() - if tmp.Brand == name { - // log.Printf("tmp name EQUAL %d %s vs %s city = %s len=%d\n", count, tmp.Brand, name, tmp.City, pb.Len()) - found = true - break - } - // log.Printf("DID NOT EXIST %d %s vs %s city = %s len=%d\n", count, tmp.Brand, name, tmp.City, pb.Len()) - } - if found { - continue - } - // log.Printf("DID NOT EXIST %d %s len=%d\n", count, name, pb.Len()) - tmp := new(Fruit) - tmp.Brand = name - tmp.City = "paris" - if pb.AppendUniqueByBrand(tmp) { - // log.Printf("AppendUniqueBrand() ok len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len()) - } else { - badExit(fmt.Errorf("AppendUniqueByBrand() ERROR len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len())) - } - pb.Append(tmp) - added += 1 - if added > 10 { - return - } - } -} - -func (pb *Fruits) saveBasket() error { - data, err := pb.Marshal() - if err != nil { - return err - } - - w, err := os.OpenFile("basket.pb", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - return err - } - w.Write(data) - w.Close() - log.Info("saved basket.pb ok") - return nil -} - -func (pb *Fruits) loadBasket() error { - if pb == nil { - pb = new(Fruits) - } - data, err := os.ReadFile("basket.pb") - if err != nil { - return err - } - - if err := pb.Unmarshal(data); err != nil { - return err - } - log.Info("loaded basket.pb ok") - return nil -}