2024-11-29 15:19:04 -06:00
|
|
|
package main
|
|
|
|
|
2024-11-30 12:17:38 -06:00
|
|
|
// will this help things?
|
|
|
|
// this is a hack for testing for now
|
|
|
|
// cram a mutex in the pb.go file
|
2024-11-29 15:19:04 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2025-01-11 01:30:08 -06:00
|
|
|
"io"
|
2024-11-29 15:19:04 -06:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2025-01-11 01:30:08 -06:00
|
|
|
func (pf *File) syncLock(w io.Writer) {
|
2025-01-11 02:44:21 -06:00
|
|
|
var LOCK string = pf.Base.Lockname // if the Marshall code changes, this will have to change
|
2025-01-11 01:30:08 -06:00
|
|
|
|
|
|
|
fmt.Fprintln(w, "// a simple global lock")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "// this is needed because it seems Marshal() fails if locks are in the structs (?)")
|
|
|
|
fmt.Fprintln(w, "// this might just be a syntactical runtime error. notsure.")
|
|
|
|
fmt.Fprintln(w, "// maybe this autogen tool will help someone that actually knows what is going on inside")
|
|
|
|
fmt.Fprintln(w, "// go/src/google.golang.org/protobuf/proto/proto_methods.go")
|
|
|
|
fmt.Fprintln(w, "// go/src/google.golang.org/protobuf/proto/encode.go")
|
|
|
|
fmt.Fprintln(w, "// my guess is that Marshal() needs to be told to ignore sync.RWMutex as it ephemeral and can't be stored")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
2025-01-08 19:45:48 -06:00
|
|
|
func (pb *Files) addMutex(f *File) error {
|
2025-01-09 05:49:23 -06:00
|
|
|
fullname := f.Pbfilename
|
|
|
|
log.Info("pb filename:", fullname)
|
2024-11-29 15:19:04 -06:00
|
|
|
data, err := os.ReadFile(fullname)
|
|
|
|
if err != nil {
|
2025-01-09 05:49:23 -06:00
|
|
|
log.Info("pb filename failed to read:", err)
|
2024-11-29 15:19:04 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-01-10 07:40:24 -06:00
|
|
|
// check if autogenpb has already looked at this file
|
|
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
|
|
if strings.Contains(line, "autogenpb DO NOT EDIT") {
|
2025-01-10 11:22:08 -06:00
|
|
|
log.Info("autogenpb has already been run on", fullname)
|
2025-01-10 07:40:24 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-09 05:49:23 -06:00
|
|
|
w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
2024-11-29 15:19:04 -06:00
|
|
|
|
2025-01-09 06:05:38 -06:00
|
|
|
pbHeaderComment(w)
|
|
|
|
|
2024-11-29 15:19:04 -06:00
|
|
|
lines := strings.Split(string(data), "\n")
|
|
|
|
for _, line := range lines {
|
2025-01-08 19:45:48 -06:00
|
|
|
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
|
|
|
|
}
|
2025-01-10 07:40:24 -06:00
|
|
|
|
|
|
|
if f.structMatch(line) {
|
|
|
|
if argv.Mutex {
|
|
|
|
log.Info("Adding Mutex to:", line)
|
|
|
|
fmt.Fprintln(w, line)
|
2025-01-11 01:30:08 -06:00
|
|
|
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") // this must be 'Lock' or Marshal() panics?
|
|
|
|
// fmt.Fprintln(w, "\t// auto-added by go.wit.com/apps/autogenpb")
|
|
|
|
// fmt.Fprintln(w, "\tsync.RWMutex")
|
2025-01-10 07:40:24 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
} else {
|
|
|
|
log.Info("Skipping. Mutex = false for:", line)
|
|
|
|
fmt.Fprintln(w, line)
|
|
|
|
fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 05:49:23 -06:00
|
|
|
}
|
2025-01-10 07:40:24 -06:00
|
|
|
} else {
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, line)
|
|
|
|
}
|
|
|
|
}
|
2025-01-09 05:49:23 -06:00
|
|
|
for _, msg := range f.MsgNames {
|
|
|
|
if !msg.MutexFound && msg.DoMutex {
|
|
|
|
return fmt.Errorf("addMutex() parse didn't work for %s", msg.Name)
|
|
|
|
}
|
2024-11-29 15:19:04 -06:00
|
|
|
}
|
2025-01-09 05:49:23 -06:00
|
|
|
return nil
|
2024-11-29 15:19:04 -06:00
|
|
|
}
|
2025-01-10 07:40:24 -06:00
|
|
|
|
|
|
|
// is this struct supposed to have a Mutex added?
|
|
|
|
func (pf *File) structMatch(line string) bool {
|
2025-01-10 19:37:32 -06:00
|
|
|
var msg *MsgName
|
|
|
|
var start string
|
|
|
|
|
|
|
|
msg = pf.Bases
|
|
|
|
start = "type " + msg.Name + " struct {"
|
|
|
|
if strings.HasPrefix(line, start) {
|
|
|
|
msg.MutexFound = true
|
2025-01-11 02:44:21 -06:00
|
|
|
msg.Lockname = "x.Lock"
|
2025-01-10 19:37:32 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2025-01-11 01:30:08 -06:00
|
|
|
// ONLY PASS THIS IF YOU DO NOT WANT TO USE MARSHAL()
|
|
|
|
|
2025-01-10 19:37:32 -06:00
|
|
|
msg = pf.Base
|
|
|
|
start = "type " + msg.Name + " struct {"
|
|
|
|
if strings.HasPrefix(line, start) {
|
|
|
|
msg.MutexFound = true
|
2025-01-11 02:44:21 -06:00
|
|
|
if argv.Marshal {
|
|
|
|
// msg.Lockname = "fruitMu.Lock"
|
|
|
|
return false
|
|
|
|
}
|
2025-01-10 19:37:32 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, msg = range pf.MsgNames {
|
|
|
|
start = "type " + msg.Name + " struct {"
|
2025-01-10 07:40:24 -06:00
|
|
|
if strings.HasPrefix(line, start) {
|
|
|
|
msg.MutexFound = true
|
2025-01-11 02:44:21 -06:00
|
|
|
if argv.Marshal {
|
|
|
|
// msg.Lockname = "fruitMu.Lock"
|
|
|
|
return false
|
|
|
|
}
|
2025-01-10 07:40:24 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|