autogenpb/protoParse.go

131 lines
3.4 KiB
Go
Raw Normal View History

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"
)
2025-01-08 20:30:33 -06:00
// 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
2025-01-08 20:30:33 -06:00
func (pb *Files) findAutogenpb(f *File) error {
2024-12-01 22:21:09 -06:00
// log.Info("starting findAutogenpb() on", names["protofile"])
// read in the .proto file
2025-01-08 20:30:33 -06:00
data, err := os.ReadFile(f.Filename)
if err != nil {
// log.Info("open config file :", err)
return err
}
2025-01-08 20:58:29 -06:00
var curmsg *MsgName
// parse the proto file for message struct names
2025-01-08 20:52:47 -06:00
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "message ") {
2025-01-08 20:58:29 -06:00
curmsg = f.parseForMessage(line)
}
if strings.HasPrefix(line, "}") {
curmsg = nil
2025-01-08 20:52:47 -06:00
}
// log.Info("line:", line)
parts := strings.Fields(line)
2025-01-09 03:42:29 -06:00
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")
2025-01-08 20:58:29 -06:00
}
}
if strings.Contains(line, "autogenpb:unique") {
2025-01-09 03:42:29 -06:00
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
}
2024-12-01 22:21:09 -06:00
2025-01-08 20:52:47 -06:00
// looks for mutex and marshal entries
2025-01-08 20:58:29 -06:00
func (f *File) parseForMessage(line string) *MsgName {
2025-01-08 20:52:47 -06:00
fields := strings.Fields(line)
if fields[0] != "message" {
2025-01-08 20:58:29 -06:00
return nil
2025-01-08 20:52:47 -06:00
}
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)
}
2025-01-08 20:58:29 -06:00
return msg
2025-01-08 20:52:47 -06:00
}
func (pb *Files) findGlobalAutogenpb(f *File) error {
2024-12-01 22:21:09 -06:00
// log.Info("starting findAutogenpb() on", filename)
// read in the .proto file
data, err := os.ReadFile(f.Filename)
2024-12-01 22:21:09 -06:00
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
}