diff options
Diffstat (limited to 'addMutex.go')
| -rw-r--r-- | addMutex.go | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/addMutex.go b/addMutex.go index fc1af97..01cb908 100644 --- a/addMutex.go +++ b/addMutex.go @@ -5,7 +5,6 @@ package main // cram a mutex in the pb.go file import ( - "errors" "fmt" "os" "strings" @@ -14,17 +13,15 @@ import ( ) func (pb *Files) addMutex(f *File) error { - fullname := f.Filebase + ".pb.go" - log.Info("fullname:", fullname) + fullname := f.Pbfilename + log.Info("pb filename:", fullname) data, err := os.ReadFile(fullname) if err != nil { - // log.Info("open config file :", err) + log.Info("pb filename failed to read:", err) return err } - w, _ := os.OpenFile(f.Filebase+".pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - - var found bool + 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 { @@ -35,21 +32,32 @@ func (pb *Files) addMutex(f *File) error { // fmt.Fprintln(w, "package "+"main") continue } - // log.Info("line:", line) - start := "type " + "sunshine" + " struct {" - if strings.HasSuffix(line, start) { - found = true - log.Info("FOUND line:", line) - fmt.Fprintln(w, line) - fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") - fmt.Fprintln(w, "") - } else { + 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) } } - // os.Exit(-1) - if found { - return nil + for _, msg := range f.MsgNames { + if !msg.MutexFound && msg.DoMutex { + return fmt.Errorf("addMutex() parse didn't work for %s", msg.Name) + } } - return errors.New("addMutex() parse didn't work") + return nil } |
