lala. working again
This commit is contained in:
parent
9349ab95fc
commit
502826cf8c
|
@ -8,3 +8,4 @@ go.sum
|
||||||
autogenpb*
|
autogenpb*
|
||||||
testfiles/testfiles
|
testfiles/testfiles
|
||||||
example/example
|
example/example
|
||||||
|
small/small
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@ BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
|
||||||
|
|
||||||
simple: build
|
simple: build
|
||||||
# make -C example clean simpleMutexGlobal goimports vet
|
# 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
|
full: install clean auto goimports vet build test install
|
||||||
@echo everything worked and the example ran
|
@echo everything worked and the example ran
|
||||||
|
|
10
addMutex.go
10
addMutex.go
|
@ -105,10 +105,7 @@ func (pf *File) structMatch(line string) bool {
|
||||||
start = "type " + msg.Name + " struct {"
|
start = "type " + msg.Name + " struct {"
|
||||||
if strings.HasPrefix(line, start) {
|
if strings.HasPrefix(line, start) {
|
||||||
msg.MutexFound = true
|
msg.MutexFound = true
|
||||||
if argv.Marshal {
|
// msg.Lockname = "x.Lock"
|
||||||
// msg.Lockname = "fruitMu.Lock"
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,10 +113,7 @@ func (pf *File) structMatch(line string) bool {
|
||||||
start = "type " + msg.Name + " struct {"
|
start = "type " + msg.Name + " struct {"
|
||||||
if strings.HasPrefix(line, start) {
|
if strings.HasPrefix(line, start) {
|
||||||
msg.MutexFound = true
|
msg.MutexFound = true
|
||||||
if argv.Marshal {
|
// msg.Lockname = "x.Lock"
|
||||||
// msg.Lockname = "fruitMu.Lock"
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
3
argv.go
3
argv.go
|
@ -12,9 +12,8 @@ type args struct {
|
||||||
Package string `arg:"--package" help:"the package name"`
|
Package string `arg:"--package" help:"the package name"`
|
||||||
Proto string `arg:"--proto" help:"the .proto filename"`
|
Proto string `arg:"--proto" help:"the .proto filename"`
|
||||||
Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"`
|
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"`
|
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"`
|
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"`
|
GoPath string `arg:"--gopath" help:"the gopath of this repo"`
|
||||||
Identify string `arg:"--identify" help:"identify file"`
|
Identify string `arg:"--identify" help:"identify file"`
|
||||||
|
|
|
@ -46,4 +46,4 @@ goimports:
|
||||||
clean:
|
clean:
|
||||||
-rm -f go.*
|
-rm -f go.*
|
||||||
-rm -f *.pb.go
|
-rm -f *.pb.go
|
||||||
-rm -f example basket.pb
|
-rm -f example
|
||||||
|
|
183
example/main.go
183
example/main.go
|
@ -1,6 +1,3 @@
|
||||||
//go:build go1.20
|
|
||||||
// +build go1.20
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -15,33 +12,75 @@ import (
|
||||||
var VERSION string
|
var VERSION string
|
||||||
var BUILDTIME string
|
var BUILDTIME string
|
||||||
|
|
||||||
var sortmap map[string]string
|
|
||||||
var marshalKeys []string
|
|
||||||
var uniqueKeys []string
|
|
||||||
|
|
||||||
var counter int = rand.Intn(100)
|
var counter int = rand.Intn(100)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var pb *Fruits
|
var pb *Fruits
|
||||||
// pb = NewFruits()
|
|
||||||
pb = new(Fruits)
|
pb = new(Fruits)
|
||||||
// attempt to load basket.pb
|
|
||||||
if err := pb.loadBasket(); err != nil {
|
if err := pb.loadBasket(); err != nil {
|
||||||
log.Info("load basket failed. running this for the first time?")
|
log.Info("First time user?")
|
||||||
a := pb.getNewFruit()
|
|
||||||
pb.Fruits = append(pb.Fruits, a)
|
|
||||||
pb.printTable()
|
|
||||||
}
|
}
|
||||||
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)
|
badExit(err)
|
||||||
}
|
}
|
||||||
if pb == nil {
|
|
||||||
badExit(fmt.Errorf("pb == nil This should not have happened"))
|
|
||||||
}
|
|
||||||
pb.addThings()
|
|
||||||
pb.printTable()
|
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) {
|
func badExit(err error) {
|
||||||
|
@ -49,36 +88,29 @@ func badExit(err error) {
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pb *Fruits) getNewFruit() *Fruit {
|
func (pb *Fruits) loadBasket() error {
|
||||||
counter += rand.Intn(100)
|
if pb == nil {
|
||||||
upc := fmt.Sprintf("%d", counter)
|
pb = new(Fruits)
|
||||||
fut := &Fruit{
|
|
||||||
Brand: "mom",
|
|
||||||
City: "New NewYork",
|
|
||||||
UPC: upc,
|
|
||||||
}
|
}
|
||||||
return fut
|
data, err := os.ReadFile("basket.pb")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if err := pb.Unmarshal(data); err != nil {
|
||||||
x := new(Fruit)
|
return err
|
||||||
x = &Fruit{
|
}
|
||||||
Brand: "fry",
|
log.Info("loaded basket.pb ok")
|
||||||
City: "Germany",
|
return nil
|
||||||
}
|
}
|
||||||
pb.printTable()
|
|
||||||
|
|
||||||
testAppend(x)
|
|
||||||
*/
|
|
||||||
|
|
||||||
func (pb *Fruits) printTable() {
|
func (pb *Fruits) printTable() {
|
||||||
var count int
|
var count int
|
||||||
all := pb.All()
|
all := pb.All()
|
||||||
for all.Scan() {
|
for all.Scan() {
|
||||||
tmp := all.Next()
|
tmp := all.Next()
|
||||||
count += 1
|
count += 1
|
||||||
log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City)
|
log.Printf("found %d %s %s %s\n", count, tmp.Brand, tmp.City, tmp.UPC)
|
||||||
if count > 5 {
|
if count > 10 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,74 +130,3 @@ func (pb *Fruits) sortTable(i int) {
|
||||||
}
|
}
|
||||||
log.Printf("Total entries: %d\n", pb.Len())
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue