diff --git a/sort.go b/generate.go similarity index 90% rename from sort.go rename to generate.go index 90e7f5a..43a86d4 100644 --- a/sort.go +++ b/generate.go @@ -96,6 +96,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.addAppendByMsg(wFind, FRUIT, APPLES, APPLE) found.addDeleteByMsg(wFind, FRUIT, APPLES, APPLE) found.addInsertByMsg(wFind, FRUIT, APPLES, APPLE) // new idea @@ -105,6 +106,17 @@ func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error { return nil } +func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE 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) + } + } +} + func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { log.Printf("\tDELETE: %s %s for %s\n", APPLES, APPLE, FRUIT) var COLORS []string @@ -130,7 +142,7 @@ func (parent *MsgName) addInsertByMsg(w io.Writer, FRUIT, APPLES, APPLE string) 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.insertByColor(w, FRUIT, APPLES, APPLE, COLOR) + parent.insertBy(w, FRUIT, APPLES, APPLE, COLOR) } } } diff --git a/sortNew.go b/generateFind.go similarity index 94% rename from sortNew.go rename to generateFind.go index 72370ba..c731fec 100644 --- a/sortNew.go +++ b/generateFind.go @@ -7,7 +7,12 @@ import ( "go.wit.com/log" ) +// generates Find() + +// but really it has Append(), Delete() and some other stuff + func (msg *MsgName) getLockname(s string) string { + // leave this function stubbed in for development of autogenpb // if argv.Mutex { // // use the mutex lock from the modified protoc.pb.go file // return s + ".Lock" @@ -17,13 +22,34 @@ 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") + fmt.Fprintln(w, "func (x *"+FRUIT+") FindBy"+COLOR+"(s string) *"+APPLE+" {") + fmt.Fprintln(w, " if x == nil {") + fmt.Fprintln(w, " return nil") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {") + fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == s {") + fmt.Fprintln(w, " return x."+APPLES+"[i]") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " return nil") + fmt.Fprintln(w, "}") + 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, "// TESTING 2") 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()") @@ -34,33 +60,9 @@ func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) { fmt.Fprintln(w, "") } -func (msg *MsgName) insertByColor(w io.Writer, FRUIT, APPLES, APPLE string, COLOR string) { - LOCK := msg.getLockname("x") - - fmt.Fprintln(w, "// TESTING") - 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()") - fmt.Fprintln(w, " defer "+LOCK+".Unlock()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") - fmt.Fprintln(w, " if p."+COLOR+" == y {") - fmt.Fprintln(w, " return p") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " z := new("+APPLE+")") - fmt.Fprintln(w, " z."+COLOR+" = y") - fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", z)") - fmt.Fprintln(w, " return z") - 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, "// TESTING") 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()") @@ -83,7 +85,6 @@ func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLOR func (msg *MsgName) appendUniqueBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { LOCK := msg.getLockname("x") - fmt.Fprintln(w, "// TESTING") 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()") @@ -101,33 +102,9 @@ func (msg *MsgName) appendUniqueBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR stri fmt.Fprintln(w, "") } -func (msg *MsgName) findBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { - LOCK := msg.getLockname("x") - - fmt.Fprintln(w, "// TESTING") - fmt.Fprintln(w, "// find a dependancy by the go path") - fmt.Fprintln(w, "func (x *"+FRUIT+") FindBy"+COLOR+"(s string) *"+APPLE+" {") - fmt.Fprintln(w, " if x == nil {") - fmt.Fprintln(w, " return nil") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " "+LOCK+".RLock()") - fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {") - fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == s {") - fmt.Fprintln(w, " return x."+APPLES+"[i]") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, " return nil") - fmt.Fprintln(w, "}") - fmt.Fprintln(w, "") -} - func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { LOCK := msg.getLockname("x") - fmt.Fprintln(w, "// TESTING") fmt.Fprintln(w, "func (x *"+FRUIT+") DeleteBy"+COLOR+"(s string) bool {") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") @@ -148,7 +125,7 @@ func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { LOCK := msg.getLockname("x") - fmt.Fprintln(w, "// TESTING") + fmt.Fprintln(w, "// TESTING fails with 'go vet' warning") fmt.Fprintln(w, "func (x *"+FRUIT+") DeleteBy"+COLOR+"(s string) *"+APPLE+" {") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") @@ -167,3 +144,26 @@ func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR st fmt.Fprintln(w, "}") fmt.Fprintln(w, "") } + +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()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") + fmt.Fprintln(w, " if p."+COLOR+" == y {") + fmt.Fprintln(w, " return p") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " z := new("+APPLE+")") + fmt.Fprintln(w, " z."+COLOR+" = y") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", z)") + fmt.Fprintln(w, " return z") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +} diff --git a/header.go b/generateHeader.go similarity index 100% rename from header.go rename to generateHeader.go diff --git a/marshal.go b/generateMarshal.go similarity index 100% rename from marshal.go rename to generateMarshal.go diff --git a/addMutex.go b/generateMutex.go similarity index 100% rename from addMutex.go rename to generateMutex.go diff --git a/sortFunc.go b/generateSort.go similarity index 100% rename from sortFunc.go rename to generateSort.go