autogenpb/protoParse.go

137 lines
3.4 KiB
Go
Raw Normal View History

package main
// auto run protoc with the correct args
import (
2025-01-09 15:03:05 -06:00
"bufio"
"fmt"
"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: `
2025-01-09 15:03:05 -06:00
// 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)
2025-01-09 15:29:27 -06:00
prefix := "message " + base + "s {" // to conform, it must have an added 's'
2025-01-09 15:03:05 -06:00
if !strings.HasPrefix(line, prefix) {
// log.Info("nope", prefix, "line", line)
// nope, not this line
continue
}
// found the matching message
2025-01-09 15:29:27 -06:00
f.Bases = f.parseForMessage(line)
2025-01-09 15:03:05 -06:00
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
2025-01-08 20:30:33 -06:00
data, err := os.ReadFile(f.Filename)
if err != nil {
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") {
2025-01-09 15:03:05 -06:00
base := cases.Title(language.English, cases.NoLower).String(f.Filebase)
2025-01-08 20:52:47 -06:00
if strings.HasPrefix(line, "message ") {
2025-01-08 20:58:29 -06:00
curmsg = f.parseForMessage(line)
2025-01-09 15:29:27 -06:00
prefix := "message " + base + " {" // only look for this for now
2025-01-09 15:03:05 -06:00
if strings.HasPrefix(line, prefix) {
2025-01-09 15:29:27 -06:00
f.Base = curmsg
2025-01-09 15:03:05 -06:00
} else {
f.MsgNames = append(f.MsgNames, curmsg)
}
2025-01-08 20:58:29 -06:00
}
if strings.HasPrefix(line, "}") {
curmsg = nil
2025-01-08 20:52:47 -06:00
}
2025-01-09 15:03:05 -06:00
if curmsg == nil {
// can't contiue on nil below here
continue
}
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") {
2025-01-09 15:03:05 -06:00
newS := parts[1]
log.Info("Addded Sort:", newS, "in struct", curmsg.Name)
curmsg.Sort = append(curmsg.Sort, newS)
}
2025-01-09 04:22:11 -06:00
if strings.Contains(line, "autogenpb:unique") {
2025-01-09 15:03:05 -06:00
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
}
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
}
2025-01-09 15:29:27 -06:00
msgName := cases.Title(language.English, cases.NoLower).String(fields[1])
2025-01-09 15:03:05 -06:00
log.Info("found messge:", msgName)
2025-01-08 20:52:47 -06:00
msg := new(MsgName)
msg.Name = msgName
msg.Lockname = msgName + "Mu"
2025-01-08 20:52:47 -06:00
if strings.Contains(line, "`autogenpb:mutex`") {
msg.DoMutex = true
2025-01-09 04:03:30 -06:00
log.Info("Added Mutex=true:", msg.Name)
2025-01-08 20:52:47 -06:00
}
if strings.Contains(line, "`autogenpb:marshal`") {
msg.DoMarshal = true
2025-01-09 04:03:30 -06:00
log.Info("Added Marshal=true:", msg.Name)
2025-01-08 20:52:47 -06:00
}
2025-01-08 20:58:29 -06:00
return msg
2025-01-08 20:52:47 -06:00
}