diff options
| author | Jeff Carr <[email protected]> | 2025-01-09 15:03:05 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-01-09 15:03:05 -0600 |
| commit | ff1721c250420fb4f1ce24f13d2e20721e40a07b (patch) | |
| tree | 3a61ac908c628077368ad27158746b726f0a5659 /protoParse.go | |
| parent | e725c0cc8011ab6d2c418f83d9b86c7897fabef4 (diff) | |
compiles again
Diffstat (limited to 'protoParse.go')
| -rw-r--r-- | protoParse.go | 131 |
1 files changed, 71 insertions, 60 deletions
diff --git a/protoParse.go b/protoParse.go index 31e2f6d..bd7db81 100644 --- a/protoParse.go +++ b/protoParse.go @@ -3,6 +3,8 @@ package main // auto run protoc with the correct args import ( + "bufio" + "fmt" "os" "strings" @@ -13,15 +15,59 @@ import ( // 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) protoParseNew(f *File) error { - // log.Info("starting findAutogenpb() on", names["protofile"]) +// does the fruit.proto file have "message Fruits" +func (pb *Files) hasPluralMessage(f *File) error { + file, err := os.Open(f.Filename) + if err != nil { + return err + } + defer file.Close() + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + + base := cases.Title(language.English, cases.NoLower).String(f.Filebase) + prefix := "message " + base + "s" // to conform, it must have an added 's' + if !strings.HasPrefix(line, prefix) { + // log.Info("nope", prefix, "line", line) + // nope, not this line + continue + } + // found the matching message + // f.Bases = f.parseForMessage(line) + + line = scanner.Text() + fields := strings.Fields(line) + if fields[0] == "string" && fields[1] != "uuid" { + return fmt.Errorf("proto file does not have a UUID") + } + // ok, uuid is here + f.Uuid = line + log.Info("found UUID:", line) + + line = scanner.Text() + fields = strings.Fields(line) + if fields[0] == "string" && fields[1] != "version" { + return fmt.Errorf("proto file does not have a version") + } + // found "version", the .proto file conforms + f.Version = line + log.Info("found Version:", line) + return nil + } + return fmt.Errorf("proto file error %s", f.Filename) +} + +func (pb *Files) protoParse(f *File) error { + // does the file conform to the standard? (also reads in UUID & Version) + if err := pb.hasPluralMessage(f); err != nil { + return err + } + log.Info(f.Filename, "is valid so far") + // read in the .proto file data, err := os.ReadFile(f.Filename) if err != nil { - // log.Info("open config file :", err) return err } @@ -29,45 +75,38 @@ func (pb *Files) protoParseNew(f *File) error { // parse the proto file for message struct names for _, line := range strings.Split(string(data), "\n") { + base := cases.Title(language.English, cases.NoLower).String(f.Filebase) if strings.HasPrefix(line, "message ") { curmsg = f.parseForMessage(line) + prefix := "message " + base // only look for this for now + if strings.HasPrefix(line, prefix) { + // f.Base = curmsg + } else { + f.MsgNames = append(f.MsgNames, curmsg) + } } if strings.HasPrefix(line, "}") { curmsg = nil } + if curmsg == nil { + // can't contiue on nil below here + continue + } // log.Info("line:", line) parts := strings.Fields(line) if strings.Contains(line, "autogenpb:sort") { - if parts[0] == "repeated" { - newS := parts[1] - if curmsg == nil { - log.Info("Error: Found Sort for:", newS, "however, this struct can't be used") - } else { - 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") - } + newS := parts[1] + log.Info("Addded Sort:", newS, "in struct", curmsg.Name) + curmsg.Sort = append(curmsg.Sort, newS) } if strings.Contains(line, "autogenpb:unique") { - if parts[0] == "repeated" { - 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") - } + newU := parts[1] + newU = cases.Title(language.English, cases.NoLower).String(newU) + log.Info("Added Unique:", newU, "in struct", curmsg.Name) + curmsg.Unique = append(curmsg.Unique, newU) } } return nil @@ -80,8 +119,8 @@ func (f *File) parseForMessage(line string) *MsgName { return nil } msgName := fields[1] + log.Info("found messge:", msgName) msg := new(MsgName) - f.MsgNames = append(f.MsgNames, msg) msg.Name = msgName msg.Lockname = msgName + "Mu" @@ -95,31 +134,3 @@ func (f *File) parseForMessage(line string) *MsgName { } return msg } - -// 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) - 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 - } - } - return nil -} |
