diff --git a/example/Makefile b/example/Makefile index c744d23..6aaad50 100644 --- a/example/Makefile +++ b/example/Makefile @@ -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 diff --git a/example/fruit.New.go b/example/fruit.New.go index 6d32ded..0be9b07 100644 --- a/example/fruit.New.go +++ b/example/fruit.New.go @@ -4,5 +4,6 @@ func NewFruits() *Fruits { x := new(Fruits) x.Uuid = "test" x.Version = "v0.0.2" + // x.Fruits = return x } diff --git a/example/main.go b/example/main.go index 6222701..90efe25 100644 --- a/example/main.go +++ b/example/main.go @@ -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) } } diff --git a/main.go b/main.go index f9f62da..5a96f25 100644 --- a/main.go +++ b/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,9 +44,7 @@ func main() { if !shell.Exists(argv.Proto) { log.Info("protobuf", argv.Proto, "is missing") - if !argv.DryRun { - os.Exit(-1) - } + os.Exit(-1) } if !strings.HasSuffix(argv.Proto, ".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) } diff --git a/newsort.go b/newsort.go index 863bb2e..b931977 100644 --- a/newsort.go +++ b/newsort.go @@ -1,65 +1,58 @@ 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 - for _, s := range uniqueKeys { - // log.Info("found unique key in .proto", s) - sortmap["sortBy"] = s - sortmap["sortKey"] = s + sortmap["append"] = sortmap["sortKey"] + iterAppend(f, sortmap) // Append() enforce unique key argv.Append - iterSortBy(f, sortmap) + iterDelete(f, sortmap) + iterReplace(f, sortmap) + iterFind(f, sortmap) + } - sortmap["append"] = sortmap["sortKey"] - iterAppend(f, sortmap) // Append() enforce unique key argv.Append + for _, s := range argv.Sort { + sortparts := strings.Split(s, ",") + sortmap["sortBy"] = sortparts[0] + sortmap["sortKey"] = sortparts[1] - iterDelete(f, sortmap) - iterReplace(f, sortmap) - iterFind(f, sortmap) - } + iterSortBy(f, sortmap) - for _, s := range argv.Sort { - sortparts := strings.Split(s, ",") - sortmap["sortBy"] = sortparts[0] - sortmap["sortKey"] = sortparts[1] + sortmap["append"] = sortmap["sortKey"] + iterAppend(f, sortmap) // Append() enforce unique key argv.Append - 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) + iterDelete(f, sortmap) + iterReplace(f, sortmap) + iterFind(f, sortmap) + } + */ return nil } diff --git a/protoParse.go b/protoParse.go index 024125d..6403346 100644 --- a/protoParse.go +++ b/protoParse.go @@ -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) } diff --git a/sort.go b/sort.go index 1c3ac9a..8704d4e 100644 --- a/sort.go +++ b/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"]+"()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(things))") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(things)") - fmt.Fprintln(w, " return iterator") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") +func (pf *File) iterSortBy(w io.Writer) { + var BASES string = pf.Bases.Name + var BASE string = pf.Base.Name - fmt.Fprintln(w, "type "+names["Base"]+""+names["sortBy"]+" []*"+names["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, "") + 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("+BASE+""+SORT+"(things))") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " iterator := New"+BASE+"Iterator(things)") + fmt.Fprintln(w, " return iterator") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + + fmt.Fprintln(w, "type "+BASE+""+SORT+" []*"+BASE+"") + fmt.Fprintln(w, "") + 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, "")