diff --git a/Makefile b/Makefile index 84c2924..6d054de 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ install: auto: rm -f auto.pb.go ./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: make -C testfiles full 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 } diff --git a/auto.proto b/auto.proto index 2549294..a44b63f 100644 --- a/auto.proto +++ b/auto.proto @@ -64,21 +64,24 @@ message MsgName { // string name = 1; // the name of the message aka struct. for this example: "Shelf" - bool doMarshal = 2; // if "Shelf" should have Marshal & Unmarshal functions - bool doMutex = 3; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?) - repeated string sort = 4; // "Book", "Picture", etc - repeated string unique = 5; // if the fields should have AppendUnique() functions + string lockname = 2; // ShelfMU + bool doMarshal = 3; // if "Shelf" should have Marshal & Unmarshal functions + bool doMutex = 4; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?) + 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` string Package = 1; // whatever the package name is at the top of the .go file string filename = 2; // yellow.proto - string filebase = 3; // yellow - string uuid = 4; // the uuid to use in a func NewMsgName() - int64 version = 5; // the version to use in a func NewMsgName() + string pbfilename = 3; // yellow.pb.go + string filebase = 4; // yellow + 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... - 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 diff --git a/main.go b/main.go index 81963b6..8feef5a 100644 --- a/main.go +++ b/main.go @@ -125,22 +125,23 @@ func main() { // this is helpful because the protoc-gen-go lines // 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 - if !shell.Exists(pbfile) { - if err := pb.protocBuild(f, pbfile); err != nil { - log.Info("protoc build error:", err) - os.Exit(-1) + if !shell.Exists(f.Pbfilename) { + if err := pb.protocBuild(f); err != nil { + badExit(err) } } // 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 !shell.Exists(pbfile) { - log.Info("protoc build error.", pbfile) + if !shell.Exists(f.Pbfilename) { + log.Info("protoc build error.", f.Pbfilename) badExit(errors.New("failed to be created with protoc and proto-gen-go")) } diff --git a/protoParse.go b/protoParse.go index c9feb47..8c36e57 100644 --- a/protoParse.go +++ b/protoParse.go @@ -83,6 +83,7 @@ func (f *File) parseForMessage(line string) *MsgName { msg := new(MsgName) f.MsgNames = append(f.MsgNames, msg) msg.Name = msgName + msg.Lockname = msgName + "Mu" if strings.Contains(line, "`autogenpb:mutex`") { msg.DoMutex = true diff --git a/protoc.go b/protoc.go index bab8c80..a390abd 100644 --- a/protoc.go +++ b/protoc.go @@ -35,7 +35,7 @@ import ( // --go_opt=MforgeConfig.proto=go.wit.com/apps/autogenpb/testautogen \ // forgeConfig.proto -func (pb *Files) protocBuild(f *File, pbfile string) error { +func (pb *Files) protocBuild(f *File) error { // read in the .proto file data, err := os.ReadFile(f.Filename) if err != nil { @@ -43,11 +43,11 @@ func (pb *Files) protocBuild(f *File, pbfile string) error { return err } - if shell.Exists(pbfile) { - log.Info("protoc file already created", pbfile) + if shell.Exists(f.Pbfilename) { + log.Info("protoc file already created", f.Pbfilename) 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()) pwd, _ := os.Getwd() log.Info("go.Getwd()", pwd)