diff --git a/main.go b/main.go index a9d1972..4438297 100644 --- a/main.go +++ b/main.go @@ -103,11 +103,21 @@ func main() { protobase := strings.TrimSuffix(argv.Proto, ".proto") f.Filebase = protobase - if err := pb.findGlobalAutogenpb(f); err != nil { + // parse the .proto file + if err := pb.protoParse(f); err != nil { log.Info("autogenpb parse error:", err) os.Exit(-1) } + // parse sort & marshal options from the .proto file + // this goes through the .proto files and looks + // for `autogenpb: ` lines + if err := pb.protoParseNew(f); err != nil { + log.Info("autogenpb parse error:", err) + os.Exit(-1) + } + + // this should be garbage soon sortmap = make(map[string]string) sortmap["package"] = packageName sortmap["protofile"] = argv.Proto @@ -134,14 +144,6 @@ func main() { os.Exit(0) } - // parse sort & marshal options from the .proto file - // this goes through the .proto files and looks - // for `autogenpb: ` lines - if err := pb.findAutogenpb(f); err != nil { - log.Info("autogenpb parse error:", err) - os.Exit(-1) - } - // try to make foo.pb.go with protoc if it's not here // this is helpful because the protoc-gen-go lines // are also annoying to code by hand diff --git a/protoParse.go b/protoParse.go index a27a4fb..c9feb47 100644 --- a/protoParse.go +++ b/protoParse.go @@ -16,7 +16,7 @@ import ( // finds autogenpb:marshal and autogenpb:unique in the .proto file // // adds fields to []marshal and []unique -func (pb *Files) findAutogenpb(f *File) error { +func (pb *Files) protoParseNew(f *File) error { // log.Info("starting findAutogenpb() on", names["protofile"]) // read in the .proto file data, err := os.ReadFile(f.Filename) @@ -41,22 +41,29 @@ func (pb *Files) findAutogenpb(f *File) error { if strings.Contains(line, "autogenpb:sort") { if parts[0] == "repeated" { - newm := parts[1] + newS := parts[1] if curmsg == nil { - log.Info("Error: Found Sort for:", newm, "however, this struct can't be used") + log.Info("Error: Found Sort for:", newS, "however, this struct can't be used") } else { - log.Info("Found Sort for:", newm, "in struct", curmsg.Name) + log.Info("Addded Sort:", newS, "in struct", curmsg.Name) + curmsg.Sort = append(curmsg.Sort, newS) } } else { log.Info("Error:", line) log.Info("Error: can not sort on non repeated fields") } } + if strings.Contains(line, "autogenpb:unique") { if parts[0] == "repeated" { - newu := parts[1] - newu = cases.Title(language.English, cases.NoLower).String(newu) - log.Info("Found unique field", newu, "in struct", curmsg.Name) + newU := parts[1] + newU = cases.Title(language.English, cases.NoLower).String(newU) + if curmsg == nil { + log.Info("Error: Found Unique for:", newU, "however, this struct can't be used") + } else { + log.Info("Added Unique:", newU, "in struct", curmsg.Name) + curmsg.Unique = append(curmsg.Unique, newU) + } } else { log.Info("Error:", line) log.Info("Error: can not append on non repeated fields") @@ -88,7 +95,8 @@ func (f *File) parseForMessage(line string) *MsgName { return msg } -func (pb *Files) findGlobalAutogenpb(f *File) error { +// this doesn't do anything anymore (?) +func (pb *Files) protoParse(f *File) error { // log.Info("starting findAutogenpb() on", filename) // read in the .proto file data, err := os.ReadFile(f.Filename)