autogenpb/addMutex.go

64 lines
1.5 KiB
Go
Raw Normal View History

package main
// will this help things?
// this is a hack for testing for now
// cram a mutex in the pb.go file
import (
"fmt"
"os"
"strings"
"go.wit.com/log"
)
func (pb *Files) addMutex(f *File) error {
fullname := f.Pbfilename
log.Info("pb filename:", fullname)
data, err := os.ReadFile(fullname)
if err != nil {
log.Info("pb filename failed to read:", err)
return err
}
w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "package ") {
log.Info("CHANGING package:", line, "to package:", f.Package)
fmt.Fprintln(w, "package "+f.Package)
// log.Info("CHANGING package:", line, "to package:main")
// fmt.Fprintln(w, "package "+"main")
continue
}
var found bool
for _, msg := range f.MsgNames {
start := "type " + msg.Name + " struct {"
// marshalThing(w, msg.Name)
// log.Info("line:", line)
if strings.HasSuffix(line, start) {
if msg.DoMutex {
msg.MutexFound = true
found = true
log.Info("Adding Mutex to line:", line)
fmt.Fprintln(w, line)
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
fmt.Fprintln(w, "")
} else {
log.Info("Skipping. DoMutex = false for", msg.Name)
}
}
}
if !found {
fmt.Fprintln(w, line)
}
}
for _, msg := range f.MsgNames {
if !msg.MutexFound && msg.DoMutex {
return fmt.Errorf("addMutex() parse didn't work for %s", msg.Name)
}
}
return nil
}