From 6f354d8d9f2d34505794f5f842de967297a78616 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 9 Jan 2025 03:44:09 -0600 Subject: rename file --- auto.proto | 18 ++++---- parseProtoFile.go | 130 ------------------------------------------------------ protoParse.go | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 139 deletions(-) delete mode 100644 parseProtoFile.go create mode 100644 protoParse.go diff --git a/auto.proto b/auto.proto index 9ed008e..2549294 100644 --- a/auto.proto +++ b/auto.proto @@ -21,11 +21,11 @@ message Apple { // `autogenpb:marshal` } message Apples { // `autogenpb:marshal` `autogenpb:mutex` - string uuid = 1; // `autogen:default:b2a2de35-07b6-443b-8188-709e27bee8a7` - string version = 2; // `autogen:default:2` - repeated Apple Apples = 3; // `autogen:sort` - repeated Pears More = 4; // `autogen:sort` - repeated string Color = 5; // `autogen:sort` `autogen:unique` + string uuid = 1; // `autogenpb:default:b2a2de35-07b6-443b-8188-709e27bee8a7` + string version = 2; // `autogenpb:default:2` + repeated Apple Apples = 3; // `autogenpb:sort` + repeated Pears More = 4; // `autogenpb:sort` + repeated string Color = 5; // `autogenpb:sort` `autogenpb:unique` } message Pears { @@ -70,7 +70,7 @@ message MsgName { repeated string unique = 5; // if the fields should have AppendUnique() functions } -message File { // `autogen:nomarshal` +message File { // `autogenpb:nomarshal` string Package = 1; // whatever the package name is at the top of the .go file string filename = 2; // yellow.proto string filebase = 3; // yellow @@ -78,7 +78,7 @@ message File { // `autogen:nomarshal` int64 version = 5; // the version to use in a func NewMsgName() // every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc... - repeated MsgName msgNames = 6; // `autogen:unique` // in this file + repeated MsgName msgNames = 6; // `autogenpb:unique` // in this file } // I know, I know, the whole point of using protobuf @@ -87,7 +87,7 @@ message File { // `autogen:nomarshal` // trivial and empty protobuf message can marshal and identify all the files // also, this could be used to modify /usr/bin/file /usr/share/magic to identify the files // maybe this is already been done and is pointless, but it seems like a good idea -message Files { // `autogen:marshal` +message Files { // `autogenpb:marshal` string uuid = 1; // if you use this scheme, autogen will be able to identify your int64 version = 2; // protobuf files from the command line. repeated File Files = 3; // an array of each .proto file in the working directory @@ -95,7 +95,7 @@ message Files { // `autogen:marshal` // this generic message is used by autogen to identify and // then dump the uuid and version from any arbitrary .pb file -message Identify { // `autogen:marshal` +message Identify { // `autogenpb:marshal` string uuid = 1; // int64 version = 2; // } diff --git a/parseProtoFile.go b/parseProtoFile.go deleted file mode 100644 index 49f382c..0000000 --- a/parseProtoFile.go +++ /dev/null @@ -1,130 +0,0 @@ -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" -) - -// this parses the .proto file and handles anything with `autogenpb: ` - -// finds autogenpb:marshal and autogenpb:unique in the .proto file -// -// adds fields to []marshal and []unique -func (pb *Files) findAutogenpb(f *File) error { - // log.Info("starting findAutogenpb() on", names["protofile"]) - // read in the .proto file - data, err := os.ReadFile(f.Filename) - if err != nil { - // log.Info("open config file :", err) - return err - } - - var curmsg *MsgName - - // parse the proto file for message struct names - for _, line := range strings.Split(string(data), "\n") { - if strings.HasPrefix(line, "message ") { - curmsg = f.parseForMessage(line) - } - if strings.HasPrefix(line, "}") { - curmsg = nil - } - - // log.Info("line:", line) - parts := strings.Fields(line) - - if strings.Contains(line, "autogenpb:sort") { - if parts[0] == "repeated" { - newm := parts[1] - if curmsg == nil { - log.Info("Error: Found Sort for:", newm, "however, this struct can't be used") - // log.Info("found marshal", newm) - marshalKeys = append(marshalKeys, newm) - } else { - log.Info("Found Sort for:", newm, "in struct", curmsg.Name) - } - } 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" { - // log.Info("Found Unique for:", parts) - newu := parts[1] - newu = cases.Title(language.English, cases.NoLower).String(newu) - log.Info("found unique field", newu, "in struct", curmsg.Name) - // uniqueKeys = append(uniqueKeys, newu) - } else { - log.Info("Error:", line) - log.Info("Error: can not append on non repeated fields") - } - } - } - return nil -} - -// looks for mutex and marshal entries -func (f *File) parseForMessage(line string) *MsgName { - fields := strings.Fields(line) - if fields[0] != "message" { - return nil - } - msgName := fields[1] - msg := new(MsgName) - f.MsgNames = append(f.MsgNames, msg) - msg.Name = msgName - - if strings.Contains(line, "`autogenpb:mutex`") { - msg.DoMutex = true - log.Info("Found Mutex for:", msg.Name) - } - if strings.Contains(line, "`autogenpb:marshal`") { - msg.DoMarshal = true - log.Info("Found Marshal for:", msg.Name) - } - return msg -} - -func (pb *Files) findGlobalAutogenpb(f *File) error { - // log.Info("starting findAutogenpb() on", filename) - // read in the .proto file - data, err := os.ReadFile(f.Filename) - if err != nil { - // log.Info("open config file :", err) - return err - } - - lines := strings.Split(string(data), "\n") - for _, line := range lines { - if strings.Contains(line, "autogenpb:ignoreproto") { - // ignore this protofile completely (don't make foo.pb.go) - os.Exit(0) - } - if strings.Contains(line, "autogenpb:no-marshal") { - // don't marshal anything (don't make foo.marshal.pb.go) - argv.NoMarshal = true - } - if strings.Contains(line, "autogenpb:no-sort") { - // don't sort anything (don't make foo.sort.pb.go) - argv.NoSort = true - } - if strings.Contains(line, "autogenpb:mutex") { - // try the mutex hack - argv.Mutex = true - } - if strings.Contains(line, "autogenpb:gover:") { - // todo: parse the output here - parts := strings.Split(line, "autogenpb:gover:") - log.Info("found gover:", parts[1]) - argv.Mutex = true - } - } - return nil -} diff --git a/protoParse.go b/protoParse.go new file mode 100644 index 0000000..49f382c --- /dev/null +++ b/protoParse.go @@ -0,0 +1,130 @@ +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" +) + +// this parses the .proto file and handles anything with `autogenpb: ` + +// finds autogenpb:marshal and autogenpb:unique in the .proto file +// +// adds fields to []marshal and []unique +func (pb *Files) findAutogenpb(f *File) error { + // log.Info("starting findAutogenpb() on", names["protofile"]) + // read in the .proto file + data, err := os.ReadFile(f.Filename) + if err != nil { + // log.Info("open config file :", err) + return err + } + + var curmsg *MsgName + + // parse the proto file for message struct names + for _, line := range strings.Split(string(data), "\n") { + if strings.HasPrefix(line, "message ") { + curmsg = f.parseForMessage(line) + } + if strings.HasPrefix(line, "}") { + curmsg = nil + } + + // log.Info("line:", line) + parts := strings.Fields(line) + + if strings.Contains(line, "autogenpb:sort") { + if parts[0] == "repeated" { + newm := parts[1] + if curmsg == nil { + log.Info("Error: Found Sort for:", newm, "however, this struct can't be used") + // log.Info("found marshal", newm) + marshalKeys = append(marshalKeys, newm) + } else { + log.Info("Found Sort for:", newm, "in struct", curmsg.Name) + } + } 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" { + // log.Info("Found Unique for:", parts) + newu := parts[1] + newu = cases.Title(language.English, cases.NoLower).String(newu) + log.Info("found unique field", newu, "in struct", curmsg.Name) + // uniqueKeys = append(uniqueKeys, newu) + } else { + log.Info("Error:", line) + log.Info("Error: can not append on non repeated fields") + } + } + } + return nil +} + +// looks for mutex and marshal entries +func (f *File) parseForMessage(line string) *MsgName { + fields := strings.Fields(line) + if fields[0] != "message" { + return nil + } + msgName := fields[1] + msg := new(MsgName) + f.MsgNames = append(f.MsgNames, msg) + msg.Name = msgName + + if strings.Contains(line, "`autogenpb:mutex`") { + msg.DoMutex = true + log.Info("Found Mutex for:", msg.Name) + } + if strings.Contains(line, "`autogenpb:marshal`") { + msg.DoMarshal = true + log.Info("Found Marshal for:", msg.Name) + } + return msg +} + +func (pb *Files) findGlobalAutogenpb(f *File) error { + // log.Info("starting findAutogenpb() on", filename) + // read in the .proto file + data, err := os.ReadFile(f.Filename) + if err != nil { + // log.Info("open config file :", err) + return err + } + + lines := strings.Split(string(data), "\n") + for _, line := range lines { + if strings.Contains(line, "autogenpb:ignoreproto") { + // ignore this protofile completely (don't make foo.pb.go) + os.Exit(0) + } + if strings.Contains(line, "autogenpb:no-marshal") { + // don't marshal anything (don't make foo.marshal.pb.go) + argv.NoMarshal = true + } + if strings.Contains(line, "autogenpb:no-sort") { + // don't sort anything (don't make foo.sort.pb.go) + argv.NoSort = true + } + if strings.Contains(line, "autogenpb:mutex") { + // try the mutex hack + argv.Mutex = true + } + if strings.Contains(line, "autogenpb:gover:") { + // todo: parse the output here + parts := strings.Split(line, "autogenpb:gover:") + log.Info("found gover:", parts[1]) + argv.Mutex = true + } + } + return nil +} -- cgit v1.2.3