package main import ( "fmt" "io" ) func (msg *MsgName) getLockname(s string) string { if argv.Mutex { // use the mutex lock from the modified protoc.pb.go file return s + ".Lock" } // a single global lock by struct name return msg.Lockname } // I like these functions the best. func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) { LOCK := msg.getLockname("x") // 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()") 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) insertByColors(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { LOCK := msg.getLockname("x") for _, COLOR := range COLORS { 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+" == "+COLOR+" {") 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 true") 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()") 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, "// 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()") 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) 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()") fmt.Fprintln(w, "") fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {") fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == s {") fmt.Fprintln(w, " x."+APPLES+"[i] = x."+APPLES+"[len(x."+APPLES+")-1]") fmt.Fprintln(w, " x."+APPLES+" = x."+APPLES+"[:len(x."+APPLES+")-1]") fmt.Fprintln(w, " return true") fmt.Fprintln(w, " }") fmt.Fprintln(w, " }") fmt.Fprintln(w, " return false") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") } // this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct func (msg *MsgName) deleteByWithCopy(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) *"+APPLE+" {") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, "") fmt.Fprintln(w, " var newr "+APPLE) fmt.Fprintln(w, "") fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {") fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == s {") fmt.Fprintln(w, " newr = *x."+APPLES+"[i]") fmt.Fprintln(w, " x."+APPLES+"[i] = x."+APPLES+"[len(x."+APPLES+")-1]") fmt.Fprintln(w, " x."+APPLES+" = x."+APPLES+"[:len(x."+APPLES+")-1]") fmt.Fprintln(w, " return &newr") fmt.Fprintln(w, " }") fmt.Fprintln(w, " }") fmt.Fprintln(w, " return nil") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") }