example is working okay
This commit is contained in:
parent
948af64ea2
commit
3f81cca18c
|
@ -1,4 +1,5 @@
|
||||||
*.swp
|
*.swp
|
||||||
|
*.pb
|
||||||
*.pb.go
|
*.pb.go
|
||||||
go.mod
|
go.mod
|
||||||
go.sum
|
go.sum
|
||||||
|
|
11
addMutex.go
11
addMutex.go
|
@ -40,15 +40,18 @@ func (pb *Files) addMutex(f *File) error {
|
||||||
// marshalThing(w, msg.Name)
|
// marshalThing(w, msg.Name)
|
||||||
// log.Info("line:", line)
|
// log.Info("line:", line)
|
||||||
if strings.HasSuffix(line, start) {
|
if strings.HasSuffix(line, start) {
|
||||||
if msg.DoMutex {
|
msg.MutexFound = true
|
||||||
msg.MutexFound = true
|
found = true
|
||||||
found = true
|
if argv.Mutex {
|
||||||
log.Info("Adding Mutex to line:", line)
|
log.Info("Adding Mutex to line:", line)
|
||||||
fmt.Fprintln(w, line)
|
fmt.Fprintln(w, line)
|
||||||
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
|
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
} else {
|
} else {
|
||||||
log.Info("Skipping. DoMutex = false for", msg.Name)
|
log.Info("Skipping. Mutex = false for", msg.Name)
|
||||||
|
fmt.Fprintln(w, line)
|
||||||
|
fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
|
||||||
|
fmt.Fprintln(w, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
argv.go
6
argv.go
|
@ -9,9 +9,9 @@ package main
|
||||||
var argv args
|
var argv args
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
Package string `arg:"--package" help:"the package name"`
|
Package string `arg:"--package" help:"the package name"`
|
||||||
LoBase string `arg:"--lobase" help:"lowercase basename"`
|
// LoBase string `arg:"--lobase" help:"lowercase basename"`
|
||||||
UpBase string `arg:"--upbase" help:"uppercase basename"`
|
// UpBase string `arg:"--upbase" help:"uppercase basename"`
|
||||||
Proto string `arg:"--proto" help:"the .proto filename"`
|
Proto string `arg:"--proto" help:"the .proto filename"`
|
||||||
Append string `arg:"--append" help:"will keep this key unique on append"`
|
Append string `arg:"--append" help:"will keep this key unique on append"`
|
||||||
Sort []string `arg:"-s,--sort,separate" help:"how and what to sort on"`
|
Sort []string `arg:"-s,--sort,separate" help:"how and what to sort on"`
|
||||||
|
|
|
@ -14,7 +14,7 @@ vet:
|
||||||
@GO111MODULE=off go vet
|
@GO111MODULE=off go vet
|
||||||
|
|
||||||
build:
|
build:
|
||||||
GO111MODULE=off go build
|
GO111MODULE=off go build -v
|
||||||
|
|
||||||
withMutex:
|
withMutex:
|
||||||
../autogenpb --proto fruit.proto --package main --mutex
|
../autogenpb --proto fruit.proto --package main --mutex
|
||||||
|
|
|
@ -28,7 +28,7 @@ message Pear {
|
||||||
|
|
||||||
// "Fruit" must exist. you can put anything in it
|
// "Fruit" must exist. you can put anything in it
|
||||||
message Fruit { // `autogenpb:marshal`
|
message Fruit { // `autogenpb:marshal`
|
||||||
string brand = 1; // `autogenpb:unique`
|
string brand = 1; // `autogenpb:unique` `autogenpb:sort`
|
||||||
repeated Apple apples = 2;
|
repeated Apple apples = 2;
|
||||||
repeated Pear pears = 3;
|
repeated Pear pears = 3;
|
||||||
string UPC = 4; // `autogenpb:sort` `autogenpb:unique`
|
string UPC = 4; // `autogenpb:sort` `autogenpb:unique`
|
||||||
|
|
138
example/main.go
138
example/main.go
|
@ -4,6 +4,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
|
@ -20,25 +22,105 @@ var uniqueKeys []string
|
||||||
var pb *Fruits
|
var pb *Fruits
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// pb = NewFruits()
|
pb = NewFruits()
|
||||||
pb = new(Fruits)
|
// attempt to load basket.pb
|
||||||
pb.Uuid = "test"
|
if err := pb.loadBasket(); err != nil {
|
||||||
pb.Version = "v0.0.2"
|
log.Info("load basket failed. running this for the first time?")
|
||||||
|
fruit := &Fruit{
|
||||||
fruit := &Fruit{
|
Brand: "mom",
|
||||||
Brand: "mom",
|
City: "New NewYork",
|
||||||
City: "New NewYork",
|
}
|
||||||
|
testAppend(fruit)
|
||||||
|
appendByUPC(fruit)
|
||||||
}
|
}
|
||||||
|
if pb == nil {
|
||||||
|
log.Info("This should not have happened")
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
pb.printTable()
|
||||||
|
pb.addThings()
|
||||||
|
pb.printTable()
|
||||||
|
pb.saveBasket()
|
||||||
|
pb.sortTable(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
x := new(Fruit)
|
x := new(Fruit)
|
||||||
x = &Fruit{
|
x = &Fruit{
|
||||||
Brand: "dad",
|
Brand: "fry",
|
||||||
City: "Germany",
|
City: "Germany",
|
||||||
}
|
}
|
||||||
appendByUPC(x)
|
pb.printTable()
|
||||||
appendByUPC(fruit)
|
|
||||||
|
|
||||||
testAppend(fruit)
|
|
||||||
testAppend(x)
|
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 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("Total entries: %d\n", pb.Len())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pb *Fruits) sortTable(i int) {
|
||||||
|
var count int
|
||||||
|
all := pb.SortByBrand()
|
||||||
|
for all.Scan() {
|
||||||
|
tmp := all.Next()
|
||||||
|
count += 1
|
||||||
|
log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City)
|
||||||
|
if count > i {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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.AppendUniqueBrand(tmp) {
|
||||||
|
// log.Printf("AppendUniqueBrand() ok len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len())
|
||||||
|
} else {
|
||||||
|
log.Printf("AppendUniqueBrand() ERROR len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len())
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
pb.Append(tmp)
|
||||||
|
added += 1
|
||||||
|
if added > 10 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAppend(fruit *Fruit) {
|
func testAppend(fruit *Fruit) {
|
||||||
|
@ -70,3 +152,35 @@ func appendByUPC(fruit *Fruit) {
|
||||||
log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City)
|
log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
3
main.go
3
main.go
|
@ -27,7 +27,8 @@ var BUILDTIME string
|
||||||
|
|
||||||
var sortmap map[string]string
|
var sortmap map[string]string
|
||||||
var marshalKeys []string
|
var marshalKeys []string
|
||||||
var uniqueKeys []string
|
|
||||||
|
// var uniqueKeys []string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pp := arg.MustParse(&argv)
|
pp := arg.MustParse(&argv)
|
||||||
|
|
21
sort.go
21
sort.go
|
@ -117,13 +117,13 @@ func (pf *File) iterSelect(w io.Writer) {
|
||||||
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+BASE+"")
|
fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+BASE+"")
|
||||||
fmt.Fprintln(w, " var aStuff []*"+BASE+"")
|
fmt.Fprintln(w, " var tmp []*"+BASE+"")
|
||||||
fmt.Fprintln(w, " aStuff = make([]*"+BASE+", len(all."+BASES+"))")
|
fmt.Fprintln(w, " tmp = make([]*"+BASE+", len(all."+BASES+"))")
|
||||||
fmt.Fprintln(w, " for i, p := range all."+BASES+" {")
|
fmt.Fprintln(w, " for i, p := range all."+BASES+" {")
|
||||||
fmt.Fprintln(w, " aStuff[i] = p // Copy pointers for safe iteration")
|
fmt.Fprintln(w, " tmp[i] = p // Copy pointers for safe iteration")
|
||||||
fmt.Fprintln(w, " }")
|
fmt.Fprintln(w, " }")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, " return aStuff")
|
fmt.Fprintln(w, " return tmp")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,6 +142,7 @@ func (pf *File) appendUnique(w io.Writer) {
|
||||||
LOCK = pf.Bases.Lockname
|
LOCK = pf.Bases.Lockname
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append check for every key
|
||||||
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
|
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
|
||||||
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique(newP *"+BASE+") bool {")
|
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique(newP *"+BASE+") bool {")
|
||||||
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
||||||
|
@ -160,6 +161,7 @@ func (pf *File) appendUnique(w io.Writer) {
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
|
|
||||||
|
// append for single keys
|
||||||
for _, KEY := range pf.Base.Unique {
|
for _, KEY := range pf.Base.Unique {
|
||||||
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
|
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
|
||||||
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique"+KEY+"(newP *"+BASE+") bool {")
|
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique"+KEY+"(newP *"+BASE+") bool {")
|
||||||
|
@ -177,6 +179,17 @@ func (pf *File) appendUnique(w io.Writer) {
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append -- no check at all
|
||||||
|
fmt.Fprintln(w, "// just a simple Append() with no checking (but still uses the mutex lock)")
|
||||||
|
fmt.Fprintln(w, "func (all *"+MSG+") Append(newP *"+BASE+") bool {")
|
||||||
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
||||||
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
||||||
|
fmt.Fprintln(w, "")
|
||||||
|
fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)")
|
||||||
|
fmt.Fprintln(w, " return true")
|
||||||
|
fmt.Fprintln(w, "}")
|
||||||
|
fmt.Fprintln(w, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func iterReplace(w io.Writer, names map[string]string) {
|
func iterReplace(w io.Writer, names map[string]string) {
|
||||||
|
|
Loading…
Reference in New Issue