diff --git a/generate.go b/generate.go index 43a86d4..3bc74f1 100644 --- a/generate.go +++ b/generate.go @@ -64,12 +64,13 @@ func (pf *File) specialBases(wFind io.Writer) { // FRUIT == the string name of the message in the protobuf file // APPLE == the type of the repeated variable // APPLES == the variable name of the repeated struct -func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error { - var FRUIT string = cases.Title(language.English, cases.NoLower).String(msg.Name) +func (pf *File) processMessage(parent *MsgName, wSort, wFind io.Writer) error { + var FRUIT string = cases.Title(language.English, cases.NoLower).String(parent.Name) + var LOCK string = parent.Lockname log.Printf("Generating functions for %s\n", FRUIT) - for _, v := range msg.Vars { + for _, v := range parent.Vars { if !v.IsRepeated { log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) continue @@ -96,7 +97,7 @@ func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error { log.Printf("FOUND: %s %s for %s\n", APPLES, APPLE, FRUIT) - found.addFindByMsg(wFind, FRUIT, APPLES, APPLE) + found.addFindByMsg(wFind, FRUIT, APPLES, APPLE, LOCK) found.addAppendByMsg(wFind, FRUIT, APPLES, APPLE) found.addDeleteByMsg(wFind, FRUIT, APPLES, APPLE) found.addInsertByMsg(wFind, FRUIT, APPLES, APPLE) // new idea @@ -106,13 +107,13 @@ func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error { return nil } -func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { +func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK string) { log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT) for _, v := range parent.Vars { if v.HasUnique { var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName) log.Printf("\t\t(x %s) InsertBy%s(string) *%s\n", FRUIT, COLOR, APPLE) - parent.findBy(w, FRUIT, APPLES, APPLE, COLOR) + parent.findBy(w, FRUIT, APPLES, APPLE, COLOR, LOCK) } } } @@ -157,7 +158,7 @@ func (parent *MsgName) addAppendByMsg(w io.Writer, FRUIT, APPLES, APPLE string) COLORS = append(COLORS, COLOR) log.Printf("\t\t(x %s) AppendUniqueBy%s(%s)\n", FRUIT, COLOR, APPLE) - parent.appendUniqueBy(w, FRUIT, APPLES, APPLE, COLOR) + parent.appendUniqueCOLOR(w, FRUIT, APPLES, APPLE, COLOR) } } if len(COLORS) > 0 { diff --git a/generateAppend.go b/generateAppend.go new file mode 100644 index 0000000..5c70316 --- /dev/null +++ b/generateAppend.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "io" + + "go.wit.com/log" +) + +// generates Append() + +// I like these functions the best. +func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) { + LOCK := msg.getLockname("x") + + log.Printf("\t\t(x %s) APPEND(%s)\n", FRUIT, APPLE) + // append -- no check at all + fmt.Fprintln(w, "// just a simple Append() shortcut (but still uses the mutex lock)") + fmt.Fprintln(w, "func (x *"+FRUIT+") Append(y *"+APPLE+") {") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", y)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +} + +func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { + LOCK := msg.getLockname("x") + + fmt.Fprintln(w, "// enforces "+APPLE+" is unique in "+FRUIT+"."+APPLES) + fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUnique(newP *"+APPLE+") bool {") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") + for _, COLOR := range COLORS { + fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {") + fmt.Fprintln(w, " return false") + fmt.Fprintln(w, " }") + } + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +} + +func (msg *MsgName) appendUniqueCOLOR(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { + LOCK := msg.getLockname("x") + + fmt.Fprintln(w, "// enforces "+APPLE+"."+COLOR+" is unique in "+FRUIT+"."+APPLES) + fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUnique"+COLOR+"(newP *"+APPLE+") bool {") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") + fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {") + fmt.Fprintln(w, " return false") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +} diff --git a/generateFind.go b/generateFind.go index c731fec..f2ecff1 100644 --- a/generateFind.go +++ b/generateFind.go @@ -3,13 +3,9 @@ package main import ( "fmt" "io" - - "go.wit.com/log" ) -// generates Find() - -// but really it has Append(), Delete() and some other stuff +// generates Find() and some other stuff func (msg *MsgName) getLockname(s string) string { // leave this function stubbed in for development of autogenpb @@ -22,10 +18,8 @@ func (msg *MsgName) getLockname(s string) string { return msg.Lockname } -func (msg *MsgName) findBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { - LOCK := msg.getLockname("x") - - fmt.Fprintln(w, "// find a dependancy by the go path") +func (msg *MsgName) findBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, LOCK string) { + fmt.Fprintln(w, "// lookup a", FRUIT, "by the ", COLOR, msg.Lockname, msg.Name) fmt.Fprintln(w, "func (x *"+FRUIT+") FindBy"+COLOR+"(s string) *"+APPLE+" {") fmt.Fprintln(w, " if x == nil {") fmt.Fprintln(w, " return nil") @@ -44,64 +38,6 @@ func (msg *MsgName) findBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { fmt.Fprintln(w, "") } -// I like these functions the best. -func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) { - LOCK := msg.getLockname("x") - - log.Printf("\t\t(x %s) APPEND(%s)\n", FRUIT, APPLE) - // append -- no check at all - fmt.Fprintln(w, "// just a simple Append() shortcut (but still uses the mutex lock)") - fmt.Fprintln(w, "func (x *"+FRUIT+") Append(y *"+APPLE+") {") - fmt.Fprintln(w, " "+LOCK+".Lock()") - fmt.Fprintln(w, " defer "+LOCK+".Unlock()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", y)") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") -} - -func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { - LOCK := msg.getLockname("x") - - fmt.Fprintln(w, "// enforces "+APPLE+" is unique in "+FRUIT+"."+APPLES) - fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUnique(newP *"+APPLE+") bool {") - fmt.Fprintln(w, " "+LOCK+".Lock()") - fmt.Fprintln(w, " defer "+LOCK+".Unlock()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") - for _, COLOR := range COLORS { - fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {") - fmt.Fprintln(w, " return false") - fmt.Fprintln(w, " }") - } - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)") - fmt.Fprintln(w, " return true") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") -} - -func (msg *MsgName) appendUniqueBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { - LOCK := msg.getLockname("x") - - fmt.Fprintln(w, "// enforces "+APPLE+"."+COLOR+" is unique in "+FRUIT+"."+APPLES) - fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUniqueBy"+COLOR+"(newP *"+APPLE+") bool {") - fmt.Fprintln(w, " "+LOCK+".Lock()") - fmt.Fprintln(w, " defer "+LOCK+".Unlock()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") - fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {") - fmt.Fprintln(w, " return false") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)") - fmt.Fprintln(w, " return true") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") -} - func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { LOCK := msg.getLockname("x") @@ -145,10 +81,10 @@ func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR st fmt.Fprintln(w, "") } +// new 2025 idea. useful? TODO: look at this again in 1y func (msg *MsgName) insertBy(w io.Writer, FRUIT, APPLES, APPLE string, COLOR string) { LOCK := msg.getLockname("x") - fmt.Fprintln(w, "// useful? remindme:1y") fmt.Fprintln(w, "// returns an "+APPLE+" if "+COLOR+" matches, otherwise create") fmt.Fprintln(w, "func (x *"+FRUIT+") InsertBy"+COLOR+" (y string) *"+APPLE+" {") fmt.Fprintln(w, " "+LOCK+".Lock()") diff --git a/generateMutex.go b/generateMutex.go index f9e143e..aaa68a5 100644 --- a/generateMutex.go +++ b/generateMutex.go @@ -17,16 +17,17 @@ func (pf *File) syncLock(w io.Writer) { var LOCK string = pf.Base.Lockname // if the Marshall code changes, this will have to change 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, "") + /* + 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") + */ } func (pb *Files) addMutex(f *File) error {