diff --git a/argv.go b/argv.go index ca49bf1..472425d 100644 --- a/argv.go +++ b/argv.go @@ -13,7 +13,7 @@ type args struct { UpBase string `arg:"--upbase" help:"uppercase basename"` Proto string `arg:"--proto" help:"the .proto filename"` Append string `arg:"--append" help:"will keep this key unique on append"` - Sort []string `arg:"--sort" help:"how and what to sort on"` + Sort []string `arg:"-s,--sort,separate" help:"how and what to sort on"` Marshal []string `arg:"--marshal" help:"what to marshal on"` NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"` NoSort bool `arg:"--no-sort" help:"do not make a sort.pb.go file"` diff --git a/sort.go b/sort.go index 871632b..ff7026b 100644 --- a/sort.go +++ b/sort.go @@ -14,31 +14,21 @@ func makeSortfile() { syncLock(f, sortmap) iterTop(f, sortmap) iterNext(f, sortmap) - // setup Sort() functions - if len(argv.Sort) == 0 { - // don't do any sorting - // setup Append() functions - if argv.Append == "" { - iterAppend(f, sortmap) // Append() enforce no unique keys - } else { - iterAppend(f, sortmap) // Append() enforce no unique keys - sortmap["append"] = argv.Append + 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 - } - } else { - sortparts := strings.Split(argv.Sort[0], ",") + } + + for _, s := range argv.Sort { + sortparts := strings.Split(s, ",") sortmap["sortBy"] = sortparts[0] sortmap["sortKey"] = sortparts[1] - iterSort(f, sortmap) + iterSortBy(f, sortmap) - if argv.Append == "" { - iterAppend(f, sortmap) // Append() enforce no unique keys - } else { - iterAppend(f, sortmap) // Append() enforce no unique keys - sortmap["append"] = argv.Append - iterAppend(f, sortmap) // Append() enforce unique key argv.Append - } sortmap["append"] = sortmap["sortKey"] iterAppend(f, sortmap) // Append() enforce unique key argv.Append @@ -125,7 +115,7 @@ func iterNext(w io.Writer, names map[string]string) { fmt.Fprintln(w, "") } -func iterSort(w io.Writer, names map[string]string) { +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"]+"()") fmt.Fprintln(w, "") @@ -133,15 +123,6 @@ func iterSort(w io.Writer, names map[string]string) { fmt.Fprintln(w, " return iterator") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") - fmt.Fprintln(w, "func (all *"+names["Bases"]+") Sort"+names["sortBy"]+"() *"+names["Base"]+"Iterator {") - fmt.Fprintln(w, " packs := all.selectAll"+names["Base"]+"()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(packs))") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(packs)") - fmt.Fprintln(w, " return iterator") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") fmt.Fprintln(w, "func (all *"+names["Bases"]+") Len() int {") fmt.Fprintln(w, " "+names["lock"]+".RLock()") fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") @@ -151,13 +132,26 @@ func iterSort(w io.Writer, names map[string]string) { fmt.Fprintln(w, "") } -func iterEnd(w io.Writer, names map[string]string) { +func iterSortBy(w io.Writer, names map[string]string) { + fmt.Fprintln(w, "func (all *"+names["Bases"]+") Sort"+names["sortBy"]+"() *"+names["Base"]+"Iterator {") + fmt.Fprintln(w, " packs := all.selectAll"+names["Base"]+"()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(packs))") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(packs)") + fmt.Fprintln(w, " return iterator") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + 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, "") +} + +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"]+" {") fmt.Fprintln(w, " "+names["lock"]+".RLock()") diff --git a/testautogen/Makefile b/testautogen/Makefile index 476e3d0..a2bafb3 100644 --- a/testautogen/Makefile +++ b/testautogen/Makefile @@ -6,7 +6,7 @@ test: vet all: clean test.pb.go forgeConfig.pb.go run run: - ../autogenpb --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname" --marshal GitTags --append Subject + ../autogenpb --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname" --sort "BySubject,Subject" --marshal GitTags --append Bling ../autogenpb --proto forgeConfig.proto --sort "ByPath,GoPath" # --append GoPath vet: diff --git a/testautogen/test.proto b/testautogen/test.proto index 00dc92c..8d60852 100644 --- a/testautogen/test.proto +++ b/testautogen/test.proto @@ -10,6 +10,8 @@ message GitTag { google.protobuf.Timestamp authordate = 3; // git creatordate string objectname = 4; // git hash string subject = 5; // git tag subject + string author = 6; // author + string bling = 7; // bling } message GitTags {