summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go25
-rw-r--r--marshal.go16
-rw-r--r--parseProtoFile.go44
-rw-r--r--sort.go19
4 files changed, 95 insertions, 9 deletions
diff --git a/main.go b/main.go
index 18cff07..0ed518d 100644
--- a/main.go
+++ b/main.go
@@ -4,6 +4,7 @@
package main
import (
+ "errors"
"os"
"strings"
@@ -21,6 +22,8 @@ var BUILDTIME string
var sortmap map[string]string
var forge *forgepb.Forge // forgepb figures out how to run protoc correctly if it's needed
+var marshalKeys []string
+var uniqueKeys []string
func main() {
pp := arg.MustParse(&argv)
@@ -90,6 +93,12 @@ func main() {
os.Exit(0)
}
+ // parse sort & marshal options from the .proto file
+ if err := findAutogenpb(sortmap); err != nil {
+ log.Info("autogenpb parse error:", err)
+ os.Exit(-1)
+ }
+
// try to make foo.pb.go with protoc if it's not here
sortmap["protoc"] = protobase + ".pb.go"
if !shell.Exists(sortmap["protoc"]) {
@@ -103,7 +112,7 @@ func main() {
// exit here
if !shell.Exists(sortmap["protoc"]) {
log.Info("protoc build error.", sortmap["protoc"], "failed to be created with protoc and proto-gen-go")
- os.Exit(-1)
+ badExit(errors.New("failed to be created with protoc and proto-gen-go"))
}
// add mutex
@@ -118,14 +127,28 @@ func main() {
if argv.NoSort {
log.Info("not making sort.pb.go file (--no-sort == true)")
} else {
+ if len(uniqueKeys) != 0 {
+ }
makeSortfile()
}
if argv.NoMarshal {
log.Info("not making marshal.pb.go file (--no-marshal == true)")
} else {
+ if len(marshalKeys) != 0 {
+ }
// make the foo.marshal.pb.go file
marshal(sortmap)
}
}
+
+func okExit(s string) {
+ log.Info("autogenpb ok", s)
+ os.Exit(0)
+}
+
+func badExit(err error) {
+ log.Info("autogenpb error:", err)
+ os.Exit(-1)
+}
diff --git a/marshal.go b/marshal.go
index 346115b..d7166fa 100644
--- a/marshal.go
+++ b/marshal.go
@@ -28,13 +28,15 @@ func marshal(names map[string]string) {
fmt.Fprintln(w, ")")
fmt.Fprintln(w, "")
- if len(argv.Marshal) == 0 {
- marshalThing(w, names["Base"])
- marshalThing(w, names["Bases"])
- } else {
- for _, v := range argv.Marshal {
- marshalThing(w, v)
- }
+ for _, v := range marshalKeys {
+ log.Info("found marshal key in .proto", v)
+ marshalThing(w, v)
+ }
+
+ // marshalThing(w, names["Base"])
+ // marshalThing(w, names["Bases"])
+ for _, v := range argv.Marshal {
+ marshalThing(w, v)
}
}
diff --git a/parseProtoFile.go b/parseProtoFile.go
new file mode 100644
index 0000000..682234d
--- /dev/null
+++ b/parseProtoFile.go
@@ -0,0 +1,44 @@
+package main
+
+// auto run protoc with the correct args
+
+import (
+ "os"
+ "strings"
+
+ "go.wit.com/log"
+ "golang.org/x/text/cases"
+ "golang.org/x/text/language"
+)
+
+// finds autogenpb:marshal and autogenpb:unique in the .proto file
+//
+// adds fields to []marshal and []unique
+func findAutogenpb(names map[string]string) error {
+ log.Info("starting findAutogenpb() on", names["protofile"])
+ // read in the .proto file
+ data, err := os.ReadFile(names["protofile"])
+ if err != nil {
+ // log.Info("open config file :", err)
+ return err
+ }
+
+ // look for included proto files
+ lines := strings.Split(string(data), "\n")
+ for _, line := range lines {
+ // log.Info("line:", line)
+ parts := strings.Fields(line)
+ if strings.Contains(line, "autogenpb:marshal") {
+ newm := parts[1]
+ log.Info("found marshal", newm)
+ marshalKeys = append(marshalKeys, newm)
+ }
+ if strings.Contains(line, "autogenpb:unique") {
+ newu := parts[1]
+ newu = cases.Title(language.English, cases.NoLower).String(newu)
+ log.Info("found unique field", newu)
+ uniqueKeys = append(uniqueKeys, newu)
+ }
+ }
+ return nil
+}
diff --git a/sort.go b/sort.go
index 4409eba..56b2fdd 100644
--- a/sort.go
+++ b/sort.go
@@ -5,6 +5,8 @@ import (
"io"
"os"
"strings"
+
+ "go.wit.com/log"
)
func makeSortfile() {
@@ -22,6 +24,21 @@ func makeSortfile() {
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]
@@ -133,7 +150,7 @@ func iterSortAll(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, "func (all *"+names["Bases"]+") SortBy"+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))")