example doesn't seem to work. why? notsure
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
7c10ff958e
commit
948af64ea2
|
@ -1,6 +1,9 @@
|
|||
VERSION = $(shell git describe --tags)
|
||||
BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
|
||||
|
||||
test: goimports build
|
||||
./example
|
||||
|
||||
modproto: clean withMutex goimports vet build
|
||||
./example
|
||||
|
||||
|
|
|
@ -4,5 +4,6 @@ func NewFruits() *Fruits {
|
|||
x := new(Fruits)
|
||||
x.Uuid = "test"
|
||||
x.Version = "v0.0.2"
|
||||
// x.Fruits =
|
||||
return x
|
||||
}
|
||||
|
|
|
@ -20,22 +20,53 @@ var uniqueKeys []string
|
|||
var pb *Fruits
|
||||
|
||||
func main() {
|
||||
pb = NewFruits()
|
||||
// pb = NewFruits()
|
||||
pb = new(Fruits)
|
||||
pb.Uuid = "test"
|
||||
pb.Version = "v0.0.2"
|
||||
|
||||
fruit := &Fruit{
|
||||
Brand: "mom",
|
||||
City: "New NewYork",
|
||||
}
|
||||
x := new(Fruit)
|
||||
x = &Fruit{
|
||||
Brand: "dad",
|
||||
City: "Germany",
|
||||
}
|
||||
appendByUPC(x)
|
||||
appendByUPC(fruit)
|
||||
|
||||
testAppend(fruit)
|
||||
testAppend(x)
|
||||
}
|
||||
|
||||
func testAppend(fruit *Fruit) {
|
||||
if pb.AppendUnique(fruit) {
|
||||
log.Info("AppendUnique() ok")
|
||||
log.Info("AppendUnique() test1 ok", fruit.Brand, fruit.City)
|
||||
} else {
|
||||
log.Info("AppendUnique() failed")
|
||||
log.Info("AppendUnique() test1 failed", fruit.Brand, fruit.City)
|
||||
os.Exit(-1)
|
||||
}
|
||||
if pb.AppendUnique(fruit) {
|
||||
log.Info("AppendUnique() worked but should not have")
|
||||
log.Info("AppendUnique() test2 worked but should not have", fruit.Brand, fruit.City)
|
||||
os.Exit(-1)
|
||||
} else {
|
||||
log.Info("AppendUnique() failed ok")
|
||||
log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City)
|
||||
}
|
||||
}
|
||||
|
||||
func appendByUPC(fruit *Fruit) {
|
||||
if pb.AppendUniqueUPC(fruit) {
|
||||
log.Info("AppendUnique() test1 ok", fruit.Brand, fruit.City)
|
||||
} else {
|
||||
log.Info("AppendUnique() test1 failed", fruit.Brand, fruit.City)
|
||||
os.Exit(-1)
|
||||
}
|
||||
if pb.AppendUniqueUPC(fruit) {
|
||||
log.Info("AppendUnique() test2 worked but should not have", fruit.Brand, fruit.City)
|
||||
os.Exit(-1)
|
||||
} else {
|
||||
log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City)
|
||||
}
|
||||
}
|
||||
|
|
39
main.go
39
main.go
|
@ -10,6 +10,7 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -18,8 +19,6 @@ import (
|
|||
"github.com/go-cmd/cmd"
|
||||
"go.wit.com/lib/gui/shell"
|
||||
"go.wit.com/log"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
// sent via -ldflags
|
||||
|
@ -45,10 +44,8 @@ func main() {
|
|||
|
||||
if !shell.Exists(argv.Proto) {
|
||||
log.Info("protobuf", argv.Proto, "is missing")
|
||||
if !argv.DryRun {
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(argv.Proto, ".proto") {
|
||||
log.Info("protobuf", argv.Proto, "must end in .proto")
|
||||
|
@ -113,11 +110,18 @@ func main() {
|
|||
// for `autogenpb: ` lines
|
||||
if err := pb.protoParse(f); err != nil {
|
||||
log.Info("autogenpb parse error:", err)
|
||||
os.Exit(-1)
|
||||
badExit(err)
|
||||
}
|
||||
|
||||
if f.Bases == nil {
|
||||
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", f.Filebase))
|
||||
}
|
||||
if f.Base == nil {
|
||||
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", f.Filebase))
|
||||
}
|
||||
|
||||
if argv.DryRun {
|
||||
os.Exit(0)
|
||||
okExit("")
|
||||
}
|
||||
|
||||
// try to make foo.pb.go with protoc if it's not here
|
||||
|
@ -144,29 +148,10 @@ func main() {
|
|||
badExit(errors.New("failed to be created with protoc and proto-gen-go"))
|
||||
}
|
||||
|
||||
// make the marshal.pb.go file
|
||||
pb.marshal(f)
|
||||
|
||||
// this should be garbage soon
|
||||
sortmap = make(map[string]string)
|
||||
sortmap["package"] = packageName
|
||||
sortmap["protofile"] = argv.Proto
|
||||
sortmap["protobase"] = protobase
|
||||
if argv.LoBase == "" {
|
||||
// if not set, assumed to be protobase
|
||||
sortmap["base"] = protobase
|
||||
} else {
|
||||
sortmap["base"] = argv.LoBase
|
||||
}
|
||||
sortmap["lock"] = sortmap["base"] + "sMu" // is nonglobal and plural
|
||||
if argv.UpBase == "" {
|
||||
sortmap["Base"] = cases.Title(language.English, cases.NoLower).String(protobase)
|
||||
sortmap["Bases"] = sortmap["Base"] + "s"
|
||||
} else {
|
||||
sortmap["Base"] = argv.UpBase
|
||||
sortmap["Bases"] = sortmap["Base"] + "s"
|
||||
}
|
||||
|
||||
// pb.makeSortfile(f)
|
||||
// make the sort.pb.go file
|
||||
pb.makeNewSortfile(f)
|
||||
}
|
||||
|
||||
|
|
27
newsort.go
27
newsort.go
|
@ -1,43 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (pb *Files) makeNewSortfile(pf *File) error {
|
||||
f, _ := os.OpenFile(pf.Filebase+".newsort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
|
||||
header(f, pf)
|
||||
|
||||
pf.syncLock(f)
|
||||
if pf.Bases == nil {
|
||||
return fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase)
|
||||
}
|
||||
if pf.Base == nil {
|
||||
return fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase)
|
||||
|
||||
if argv.Mutex {
|
||||
// use the mutex lock from the modified protoc.pb.go file
|
||||
pf.Bases.Lockname = "all.Lock"
|
||||
}
|
||||
|
||||
pf.Base.iterTop(f)
|
||||
pf.Base.iterNext(f)
|
||||
pf.iterSelect(f)
|
||||
pf.appendUnique(f) // Append() enforce no unique keys
|
||||
pf.iterSortBy(f)
|
||||
pf.iterAll(f)
|
||||
// pf.iterSortAll(f, sortmap)
|
||||
|
||||
return nil
|
||||
// iterSortAll(f, sortmap)
|
||||
|
||||
if argv.Append != "" {
|
||||
sortmap["append"] = string(argv.Append)
|
||||
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
|
||||
}
|
||||
|
||||
/*
|
||||
for _, s := range uniqueKeys {
|
||||
// log.Info("found unique key in .proto", s)
|
||||
sortmap["sortBy"] = s
|
||||
sortmap["sortKey"] = s
|
||||
|
||||
iterSortBy(f, sortmap)
|
||||
|
||||
sortmap["append"] = sortmap["sortKey"]
|
||||
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
|
||||
|
||||
|
@ -60,6 +53,6 @@ func (pb *Files) makeNewSortfile(pf *File) error {
|
|||
iterReplace(f, sortmap)
|
||||
iterFind(f, sortmap)
|
||||
}
|
||||
iterEnd(f, sortmap)
|
||||
*/
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -97,7 +97,7 @@ func (pb *Files) protoParse(f *File) error {
|
|||
parts := strings.Fields(line)
|
||||
|
||||
if strings.Contains(line, "autogenpb:sort") {
|
||||
newS := parts[1]
|
||||
newS := cases.Title(language.English, cases.NoLower).String(parts[1])
|
||||
log.Info("Addded Sort:", newS, "in struct", curmsg.Name)
|
||||
curmsg.Sort = append(curmsg.Sort, newS)
|
||||
}
|
||||
|
|
133
sort.go
133
sort.go
|
@ -3,65 +3,9 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// passes in the protobuf file protobuf
|
||||
func (pb *Files) makeSortfile(pf *File) {
|
||||
f, _ := os.OpenFile(pf.Filebase+".sort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
|
||||
// header(f, pf)
|
||||
|
||||
// pf.iterTop(f, sortmap["base"])
|
||||
// iterNext(f, sortmap)
|
||||
iterAppend(f, sortmap) // Append() enforce no unique keys
|
||||
iterSortAll(f, sortmap)
|
||||
|
||||
if argv.Append != "" {
|
||||
sortmap["append"] = string(argv.Append)
|
||||
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
|
||||
}
|
||||
|
||||
for _, s := range uniqueKeys {
|
||||
// log.Info("found unique key in .proto", s)
|
||||
sortmap["sortBy"] = s
|
||||
sortmap["sortKey"] = s
|
||||
|
||||
iterSortBy(f, sortmap)
|
||||
|
||||
sortmap["append"] = sortmap["sortKey"]
|
||||
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
|
||||
|
||||
iterDelete(f, sortmap)
|
||||
iterReplace(f, sortmap)
|
||||
iterFind(f, sortmap)
|
||||
}
|
||||
|
||||
for _, s := range argv.Sort {
|
||||
sortparts := strings.Split(s, ",")
|
||||
sortmap["sortBy"] = sortparts[0]
|
||||
sortmap["sortKey"] = sortparts[1]
|
||||
|
||||
iterSortBy(f, sortmap)
|
||||
|
||||
sortmap["append"] = sortmap["sortKey"]
|
||||
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
|
||||
|
||||
iterDelete(f, sortmap)
|
||||
iterReplace(f, sortmap)
|
||||
iterFind(f, sortmap)
|
||||
}
|
||||
iterEnd(f, sortmap)
|
||||
}
|
||||
|
||||
func (pf *File) syncLock(w io.Writer) {
|
||||
if pf.Bases == nil {
|
||||
log.Info("BASES == nil in syncLock")
|
||||
return
|
||||
}
|
||||
var LOCK string = pf.Bases.Lockname
|
||||
|
||||
fmt.Fprintln(w, "// bad global lock until modifying the .pb.go file is tested")
|
||||
|
@ -111,70 +55,71 @@ func (msg *MsgName) iterNext(w io.Writer) {
|
|||
fmt.Fprintln(w, " for i, d := range it.things {")
|
||||
fmt.Fprintln(w, " fmt.Println(\"i =\", i, d)")
|
||||
fmt.Fprintln(w, " }")
|
||||
fmt.Fprintln(w, " // fmt.Println(\"protobuf autogenpb sort error len =\", len(it.things))")
|
||||
fmt.Fprintln(w, " // fmt.Println(\"protobuf autogenpb sort error next == nil\", it.index, it.index-1)")
|
||||
fmt.Fprintln(w, " }")
|
||||
fmt.Fprintln(w, " return it.things[it.index-1]")
|
||||
fmt.Fprintln(w, "}")
|
||||
fmt.Fprintln(w, "")
|
||||
}
|
||||
|
||||
func iterSortAll(w io.Writer, names map[string]string) {
|
||||
fmt.Fprintln(w, "func (all *"+names["Bases"]+") All() *"+names["Base"]+"Iterator {")
|
||||
fmt.Fprintln(w, " "+names["base"]+"Pointers := all.selectAll"+names["Base"]+"()")
|
||||
func (pf *File) iterAll(w io.Writer) {
|
||||
var BASES string = pf.Bases.Name
|
||||
var BASE string = pf.Base.Name
|
||||
var LOCK string = pf.Bases.Lockname
|
||||
|
||||
fmt.Fprintln(w, "func (all *"+BASES+") All() *"+BASE+"Iterator {")
|
||||
fmt.Fprintln(w, " "+BASE+"Pointers := all.selectAll"+BASE+"()")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator("+names["base"]+"Pointers)")
|
||||
fmt.Fprintln(w, " iterator := New"+BASE+"Iterator("+BASE+"Pointers)")
|
||||
fmt.Fprintln(w, " return iterator")
|
||||
fmt.Fprintln(w, "}")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Len() int {")
|
||||
if sortmap["lock"] == "all" {
|
||||
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
|
||||
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
|
||||
} else {
|
||||
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
|
||||
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
|
||||
}
|
||||
fmt.Fprintln(w, "func (all *"+BASES+") Len() int {")
|
||||
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
||||
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, " return len(all."+names["Bases"]+")")
|
||||
fmt.Fprintln(w, " return len(all."+BASES+")")
|
||||
fmt.Fprintln(w, "}")
|
||||
fmt.Fprintln(w, "")
|
||||
}
|
||||
|
||||
func iterSortBy(w io.Writer, names map[string]string) {
|
||||
fmt.Fprintln(w, "func (all *"+names["Bases"]+") SortBy"+names["sortBy"]+"() *"+names["Base"]+"Iterator {")
|
||||
fmt.Fprintln(w, " things := all.selectAll"+names["Base"]+"()")
|
||||
func (pf *File) iterSortBy(w io.Writer) {
|
||||
var BASES string = pf.Bases.Name
|
||||
var BASE string = pf.Base.Name
|
||||
|
||||
for _, SORT := range pf.Base.Sort {
|
||||
fmt.Fprintln(w, "func (all *"+BASES+") SortBy"+SORT+"() *"+BASE+"Iterator {")
|
||||
fmt.Fprintln(w, " things := all.selectAll"+BASE+"()")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(things))")
|
||||
fmt.Fprintln(w, " sort.Sort("+BASE+""+SORT+"(things))")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(things)")
|
||||
fmt.Fprintln(w, " iterator := New"+BASE+"Iterator(things)")
|
||||
fmt.Fprintln(w, " return iterator")
|
||||
fmt.Fprintln(w, "}")
|
||||
fmt.Fprintln(w, "")
|
||||
|
||||
fmt.Fprintln(w, "type "+names["Base"]+""+names["sortBy"]+" []*"+names["Base"]+"")
|
||||
fmt.Fprintln(w, "type "+BASE+""+SORT+" []*"+BASE+"")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, "func (a "+names["Base"]+""+names["sortBy"]+") Len() int { return len(a) }")
|
||||
fmt.Fprintln(w, "func (a "+names["Base"]+""+names["sortBy"]+") Less(i, j int) bool { return a[i]."+names["sortKey"]+" < a[j]."+names["sortKey"]+" }")
|
||||
fmt.Fprintln(w, "func (a "+names["Base"]+""+names["sortBy"]+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
|
||||
fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Len() int { return len(a) }")
|
||||
fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Less(i, j int) bool { return a[i]."+SORT+" < a[j]."+SORT+" }")
|
||||
fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
|
||||
fmt.Fprintln(w, "")
|
||||
}
|
||||
}
|
||||
|
||||
func iterEnd(w io.Writer, names map[string]string) {
|
||||
fmt.Fprintln(w, "// safely returns a slice of pointers to the "+names["Base"]+" protobufs")
|
||||
fmt.Fprintln(w, "func (all *"+names["Bases"]+") selectAll"+names["Base"]+"() []*"+names["Base"]+" {")
|
||||
if sortmap["lock"] == "all" {
|
||||
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
|
||||
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
|
||||
} else {
|
||||
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
|
||||
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
|
||||
}
|
||||
func (pf *File) iterSelect(w io.Writer) {
|
||||
var BASES string = pf.Bases.Name
|
||||
var BASE string = pf.Base.Name
|
||||
var LOCK string = pf.Bases.Lockname
|
||||
|
||||
fmt.Fprintln(w, "// safely returns a slice of pointers to the "+BASE+" protobufs")
|
||||
fmt.Fprintln(w, "func (all *"+BASES+") selectAll"+BASE+"() []*"+BASE+" {")
|
||||
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
||||
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+names["Base"]+"")
|
||||
fmt.Fprintln(w, " var aStuff []*"+names["Base"]+"")
|
||||
fmt.Fprintln(w, " aStuff = make([]*"+names["Base"]+", len(all."+names["Bases"]+"))")
|
||||
fmt.Fprintln(w, " for i, p := range all."+names["Bases"]+" {")
|
||||
fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+BASE+"")
|
||||
fmt.Fprintln(w, " var aStuff []*"+BASE+"")
|
||||
fmt.Fprintln(w, " aStuff = make([]*"+BASE+", len(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, " }")
|
||||
fmt.Fprintln(w, "")
|
||||
|
|
Loading…
Reference in New Issue