mutex's are being added to the protoc pb.go file

This commit is contained in:
Jeff Carr 2025-01-09 05:49:23 -06:00
parent 78bfce745b
commit a4beeca857
6 changed files with 54 additions and 41 deletions

View File

@ -26,7 +26,7 @@ install:
auto: auto:
rm -f auto.pb.go rm -f auto.pb.go
./autogenpb --proto auto.proto --package main ./autogenpb --proto auto.proto --package main
rm -f auto.sort.pb.go auto.marshal.pb.go rm -f auto.sort.pb.go auto.newsort.pb.go # auto.marshal.pb.go
test: test:
make -C testfiles full make -C testfiles full

View File

@ -5,7 +5,6 @@ package main
// cram a mutex in the pb.go file // cram a mutex in the pb.go file
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -14,17 +13,15 @@ import (
) )
func (pb *Files) addMutex(f *File) error { func (pb *Files) addMutex(f *File) error {
fullname := f.Filebase + ".pb.go" fullname := f.Pbfilename
log.Info("fullname:", fullname) log.Info("pb filename:", fullname)
data, err := os.ReadFile(fullname) data, err := os.ReadFile(fullname)
if err != nil { if err != nil {
// log.Info("open config file :", err) log.Info("pb filename failed to read:", err)
return err return err
} }
w, _ := os.OpenFile(f.Filebase+".pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
var found bool
lines := strings.Split(string(data), "\n") lines := strings.Split(string(data), "\n")
for _, line := range lines { for _, line := range lines {
@ -35,21 +32,32 @@ func (pb *Files) addMutex(f *File) error {
// fmt.Fprintln(w, "package "+"main") // fmt.Fprintln(w, "package "+"main")
continue continue
} }
var found bool
for _, msg := range f.MsgNames {
start := "type " + msg.Name + " struct {"
// marshalThing(w, msg.Name)
// log.Info("line:", line) // log.Info("line:", line)
start := "type " + "sunshine" + " struct {"
if strings.HasSuffix(line, start) { if strings.HasSuffix(line, start) {
if msg.DoMutex {
msg.MutexFound = true
found = true found = true
log.Info("FOUND line:", line) log.Info("Adding Mutex to line:", line)
fmt.Fprintln(w, line) fmt.Fprintln(w, line)
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} else { } else {
log.Info("Skipping. DoMutex = false for", msg.Name)
}
}
}
if !found {
fmt.Fprintln(w, line) fmt.Fprintln(w, line)
} }
} }
// os.Exit(-1) for _, msg := range f.MsgNames {
if found { if !msg.MutexFound && msg.DoMutex {
return nil return fmt.Errorf("addMutex() parse didn't work for %s", msg.Name)
} }
return errors.New("addMutex() parse didn't work") }
return nil
} }

View File

@ -64,21 +64,24 @@ message MsgName {
// //
string name = 1; // the name of the message aka struct. for this example: "Shelf" string name = 1; // the name of the message aka struct. for this example: "Shelf"
bool doMarshal = 2; // if "Shelf" should have Marshal & Unmarshal functions string lockname = 2; // ShelfMU
bool doMutex = 3; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?) bool doMarshal = 3; // if "Shelf" should have Marshal & Unmarshal functions
repeated string sort = 4; // "Book", "Picture", etc bool doMutex = 4; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?)
repeated string unique = 5; // if the fields should have AppendUnique() functions bool mutexFound = 5; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?)
repeated string sort = 6; // "Book", "Picture", etc
repeated string unique = 7; // if the fields should have AppendUnique() functions
} }
message File { // `autogenpb:nomarshal` message File { // `autogenpb:nomarshal`
string Package = 1; // whatever the package name is at the top of the .go file string Package = 1; // whatever the package name is at the top of the .go file
string filename = 2; // yellow.proto string filename = 2; // yellow.proto
string filebase = 3; // yellow string pbfilename = 3; // yellow.pb.go
string uuid = 4; // the uuid to use in a func NewMsgName() string filebase = 4; // yellow
int64 version = 5; // the version to use in a func NewMsgName() string uuid = 5; // the uuid to use in a func NewMsgName()
int64 version = 6; // the version to use in a func NewMsgName()
// every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc... // every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc...
repeated MsgName msgNames = 6; // `autogenpb:unique` // in this file repeated MsgName msgNames = 7; // `autogenpb:unique` // in this file
} }
// I know, I know, the whole point of using protobuf // I know, I know, the whole point of using protobuf

17
main.go
View File

@ -125,22 +125,23 @@ func main() {
// this is helpful because the protoc-gen-go lines // this is helpful because the protoc-gen-go lines
// are also annoying to code by hand // are also annoying to code by hand
pbfile := f.Filebase + ".pb.go" f.Pbfilename = f.Filebase + ".pb.go"
// try to create the foo.pb.go file using protoc if it is not there // try to create the foo.pb.go file using protoc if it is not there
if !shell.Exists(pbfile) { if !shell.Exists(f.Pbfilename) {
if err := pb.protocBuild(f, pbfile); err != nil { if err := pb.protocBuild(f); err != nil {
log.Info("protoc build error:", err) badExit(err)
os.Exit(-1)
} }
} }
// try to add the Mutex to the pb.go file // try to add the Mutex to the pb.go file
pb.addMutex(f) if err := pb.addMutex(f); err != nil {
badExit(err)
}
// if foo.pb.go still doesn't exist, protoc failed // if foo.pb.go still doesn't exist, protoc failed
if !shell.Exists(pbfile) { if !shell.Exists(f.Pbfilename) {
log.Info("protoc build error.", pbfile) log.Info("protoc build error.", f.Pbfilename)
badExit(errors.New("failed to be created with protoc and proto-gen-go")) badExit(errors.New("failed to be created with protoc and proto-gen-go"))
} }

View File

@ -83,6 +83,7 @@ func (f *File) parseForMessage(line string) *MsgName {
msg := new(MsgName) msg := new(MsgName)
f.MsgNames = append(f.MsgNames, msg) f.MsgNames = append(f.MsgNames, msg)
msg.Name = msgName msg.Name = msgName
msg.Lockname = msgName + "Mu"
if strings.Contains(line, "`autogenpb:mutex`") { if strings.Contains(line, "`autogenpb:mutex`") {
msg.DoMutex = true msg.DoMutex = true

View File

@ -35,7 +35,7 @@ import (
// --go_opt=MforgeConfig.proto=go.wit.com/apps/autogenpb/testautogen \ // --go_opt=MforgeConfig.proto=go.wit.com/apps/autogenpb/testautogen \
// forgeConfig.proto // forgeConfig.proto
func (pb *Files) protocBuild(f *File, pbfile string) error { func (pb *Files) protocBuild(f *File) error {
// read in the .proto file // read in the .proto file
data, err := os.ReadFile(f.Filename) data, err := os.ReadFile(f.Filename)
if err != nil { if err != nil {
@ -43,11 +43,11 @@ func (pb *Files) protocBuild(f *File, pbfile string) error {
return err return err
} }
if shell.Exists(pbfile) { if shell.Exists(f.Pbfilename) {
log.Info("protoc file already created", pbfile) log.Info("protoc file already created", f.Pbfilename)
return nil return nil
} }
log.Info("Attempt to generate the protoc file:", pbfile) log.Info("Attempt to generate the protoc file:", f.Pbfilename)
// log.Info("go src", forge.GetGoSrc()) // log.Info("go src", forge.GetGoSrc())
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
log.Info("go.Getwd()", pwd) log.Info("go.Getwd()", pwd)