2024-11-29 15:19:04 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2025-01-09 15:29:27 -06:00
|
|
|
func (pf *File) syncLock(w io.Writer) {
|
|
|
|
var LOCK string = pf.Bases.Lockname
|
2025-01-09 06:47:35 -06:00
|
|
|
|
|
|
|
fmt.Fprintln(w, "// bad global lock until modifying the .pb.go file is tested")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "// sync.RWMutex or sync.Mutex?")
|
2025-01-09 06:47:35 -06:00
|
|
|
fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
2025-01-09 15:29:27 -06:00
|
|
|
func (msg *MsgName) iterTop(w io.Writer) {
|
|
|
|
var BASE string = msg.Name
|
|
|
|
|
2025-01-09 06:47:35 -06:00
|
|
|
fmt.Fprintln(w, "type "+BASE+"Iterator struct {")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, " sync.RWMutex")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 06:47:35 -06:00
|
|
|
fmt.Fprintln(w, " things []*"+BASE)
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, " index int")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 06:47:35 -06:00
|
|
|
fmt.Fprintln(w, "// New"+BASE+"Iterator initializes a new iterator.")
|
|
|
|
fmt.Fprintln(w, "func New"+BASE+"Iterator(things []*"+BASE+") *"+BASE+"Iterator {")
|
|
|
|
fmt.Fprintln(w, " return &"+BASE+"Iterator{things: things}")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-12-27 14:43:21 -06:00
|
|
|
fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more things.")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "// Use Scan() in a loop, similar to a while loop")
|
|
|
|
fmt.Fprintln(w, "//")
|
|
|
|
fmt.Fprintln(w, "// for iterator.Scan() ")
|
|
|
|
fmt.Fprintln(w, "// d := iterator.Next(")
|
|
|
|
fmt.Fprintln(w, "// fmt.Println(\"found UUID:\", d.Uuid")
|
|
|
|
fmt.Fprintln(w, "// }")
|
2025-01-09 06:47:35 -06:00
|
|
|
fmt.Fprintln(w, "func (it *"+BASE+"Iterator) Scan() bool {")
|
2024-12-27 14:43:21 -06:00
|
|
|
fmt.Fprintln(w, " if it.index >= len(it.things) {")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " it.index++")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
2025-01-09 15:29:27 -06:00
|
|
|
func (msg *MsgName) iterNext(w io.Writer) {
|
|
|
|
var BASE string = msg.Name
|
|
|
|
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "// Next() returns the next thing in the array")
|
2025-01-09 12:54:04 -06:00
|
|
|
fmt.Fprintln(w, "func (it *"+BASE+"Iterator) Next() *"+BASE+" {")
|
2024-12-27 14:43:21 -06:00
|
|
|
fmt.Fprintln(w, " if it.things[it.index-1] == nil {")
|
|
|
|
fmt.Fprintln(w, " for i, d := range it.things {")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, " fmt.Println(\"i =\", i, d)")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
2024-12-27 14:43:21 -06:00
|
|
|
fmt.Fprintln(w, " return it.things[it.index-1]")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
2025-01-09 17:15:53 -06:00
|
|
|
func (pf *File) iterAll(w io.Writer) {
|
|
|
|
var BASES string = pf.Bases.Name
|
|
|
|
var BASE string = pf.Base.Name
|
|
|
|
var LOCK string = pf.Bases.Lockname
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "func (all *"+BASES+") All() *"+BASE+"Iterator {")
|
|
|
|
fmt.Fprintln(w, " "+BASE+"Pointers := all.selectAll"+BASE+"()")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 17:15:53 -06:00
|
|
|
fmt.Fprintln(w, " iterator := New"+BASE+"Iterator("+BASE+"Pointers)")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, " return iterator")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 17:15:53 -06:00
|
|
|
fmt.Fprintln(w, "func (all *"+BASES+") Len() int {")
|
|
|
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
2024-11-29 17:31:49 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 17:15:53 -06:00
|
|
|
fmt.Fprintln(w, " return len(all."+BASES+")")
|
2024-11-29 17:31:49 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
2025-01-09 17:15:53 -06:00
|
|
|
func (pf *File) iterSortBy(w io.Writer) {
|
|
|
|
var BASES string = pf.Bases.Name
|
|
|
|
var BASE string = pf.Base.Name
|
2024-11-29 15:19:04 -06:00
|
|
|
|
2025-01-09 17:15:53 -06:00
|
|
|
for _, SORT := range pf.Base.Sort {
|
|
|
|
fmt.Fprintln(w, "func (all *"+BASES+") SortBy"+SORT+"() *"+BASE+"Iterator {")
|
|
|
|
fmt.Fprintln(w, " things := all.selectAll"+BASE+"()")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " sort.Sort("+BASE+""+SORT+"(things))")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " iterator := New"+BASE+"Iterator(things)")
|
|
|
|
fmt.Fprintln(w, " return iterator")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "type "+BASE+""+SORT+" []*"+BASE+"")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Len() int { return len(a) }")
|
|
|
|
fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Less(i, j int) bool { return a[i]."+SORT+" < a[j]."+SORT+" }")
|
|
|
|
fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
2024-11-29 17:31:49 -06:00
|
|
|
}
|
|
|
|
|
2025-01-09 17:15:53 -06:00
|
|
|
func (pf *File) iterSelect(w io.Writer) {
|
|
|
|
var BASES string = pf.Bases.Name
|
|
|
|
var BASE string = pf.Base.Name
|
|
|
|
var LOCK string = pf.Bases.Lockname
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "// safely returns a slice of pointers to the "+BASE+" protobufs")
|
|
|
|
fmt.Fprintln(w, "func (all *"+BASES+") selectAll"+BASE+"() []*"+BASE+" {")
|
|
|
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 17:15:53 -06:00
|
|
|
fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+BASE+"")
|
2025-01-09 20:08:22 -06:00
|
|
|
fmt.Fprintln(w, " var tmp []*"+BASE+"")
|
|
|
|
fmt.Fprintln(w, " tmp = make([]*"+BASE+", len(all."+BASES+"))")
|
2025-01-09 17:15:53 -06:00
|
|
|
fmt.Fprintln(w, " for i, p := range all."+BASES+" {")
|
2025-01-09 20:08:22 -06:00
|
|
|
fmt.Fprintln(w, " tmp[i] = p // Copy pointers for safe iteration")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 20:08:22 -06:00
|
|
|
fmt.Fprintln(w, " return tmp")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
}
|
|
|
|
|
|
|
|
func iterAppend(w io.Writer, names map[string]string) {
|
2025-01-09 12:54:04 -06:00
|
|
|
}
|
|
|
|
|
2025-01-09 16:18:06 -06:00
|
|
|
func (pf *File) appendUnique(w io.Writer) {
|
2025-01-09 15:51:45 -06:00
|
|
|
var MSG string = pf.Bases.Name
|
2025-01-09 16:18:06 -06:00
|
|
|
var BASE string = pf.Base.Name
|
2025-01-09 15:51:45 -06:00
|
|
|
var LOCK string = pf.Bases.Lockname
|
|
|
|
|
2025-01-09 12:54:04 -06:00
|
|
|
if argv.Mutex {
|
2025-01-09 16:02:53 -06:00
|
|
|
// use the mutex lock from the modified protoc.pb.go file
|
2025-01-09 15:51:45 -06:00
|
|
|
LOCK = "all.Lock"
|
2025-01-09 16:02:53 -06:00
|
|
|
} else {
|
|
|
|
LOCK = pf.Bases.Lockname
|
2024-12-01 10:42:12 -06:00
|
|
|
}
|
2025-01-09 12:54:04 -06:00
|
|
|
|
2025-01-09 20:40:00 -06:00
|
|
|
// append -- no check at all
|
|
|
|
fmt.Fprintln(w, "// just a simple Append() with no checking (but still uses the mutex lock)")
|
|
|
|
fmt.Fprintln(w, "func (all *"+MSG+") Append(newP *"+BASE+") bool {")
|
2025-01-09 12:54:04 -06:00
|
|
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 12:54:04 -06:00
|
|
|
fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)")
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 15:51:45 -06:00
|
|
|
|
2025-01-09 20:08:22 -06:00
|
|
|
// append for single keys
|
2025-01-09 15:51:45 -06:00
|
|
|
for _, KEY := range pf.Base.Unique {
|
|
|
|
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
|
|
|
|
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique"+KEY+"(newP *"+BASE+") bool {")
|
|
|
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " for _, p := range all."+MSG+" {")
|
|
|
|
fmt.Fprintln(w, " if p."+KEY+" == newP."+KEY+" {")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
2025-01-09 20:08:22 -06:00
|
|
|
|
2025-01-09 20:40:00 -06:00
|
|
|
// append check for every key
|
|
|
|
if len(pf.Base.Unique) == 0 {
|
|
|
|
// there are no keys defined
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
|
|
|
|
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique(newP *"+BASE+") bool {")
|
2025-01-09 20:08:22 -06:00
|
|
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 20:40:00 -06:00
|
|
|
fmt.Fprintln(w, " for _, p := range all."+MSG+" {")
|
|
|
|
for _, KEY := range pf.Base.Unique {
|
|
|
|
fmt.Fprintln(w, " if p."+KEY+" == newP."+KEY+" {")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-09 20:08:22 -06:00
|
|
|
fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 15:19:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func iterReplace(w io.Writer, names map[string]string) {
|
|
|
|
if names["append"] == "" {
|
|
|
|
// can't continue without a key field
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "// enforces "+names["append"]+" is unique")
|
|
|
|
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Replace"+names["append"]+"(newP *"+names["Base"]+") bool { // todo: make unique name here")
|
|
|
|
|
2024-12-01 10:42:12 -06:00
|
|
|
if sortmap["lock"] == "all" {
|
|
|
|
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
|
|
|
|
}
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {")
|
|
|
|
fmt.Fprintln(w, " if p."+names["append"]+" == newP."+names["append"]+" {")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " all."+names["Bases"]+" = append(all."+names["Bases"]+", newP)")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
2025-01-09 21:15:01 -06:00
|
|
|
func (pf *File) iterDelete(w io.Writer) {
|
|
|
|
var MSG string = pf.Bases.Name
|
|
|
|
var LOCK string = pf.Bases.Lockname
|
|
|
|
|
|
|
|
for _, KEY := range pf.Base.Sort {
|
|
|
|
fmt.Fprintln(w, "func (all *"+MSG+") DeleteBy"+KEY+"(s string) bool {")
|
|
|
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " for i, _ := range all."+MSG+" {")
|
|
|
|
fmt.Fprintln(w, " if all."+MSG+"[i]."+KEY+" == s {")
|
|
|
|
fmt.Fprintln(w, " all."+MSG+"[i] = all."+MSG+"[len(all."+MSG+")-1]")
|
|
|
|
fmt.Fprintln(w, " all."+MSG+" = all."+MSG+"[:len(all."+MSG+")-1]")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-12-01 10:42:12 -06:00
|
|
|
}
|
2024-11-30 23:20:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct
|
|
|
|
func iterDeleteWithCopy(w io.Writer, names map[string]string) {
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) *"+names["Base"]+" {")
|
2024-12-01 10:42:12 -06:00
|
|
|
if sortmap["lock"] == "all" {
|
|
|
|
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
|
|
|
|
}
|
2024-11-29 15:19:04 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " var newr "+names["Base"])
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
|
|
|
|
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
|
|
|
|
fmt.Fprintln(w, " newr = *all."+names["Bases"]+"[i]")
|
|
|
|
fmt.Fprintln(w, " all."+names["Bases"]+"[i] = all."+names["Bases"]+"[len(all."+names["Bases"]+")-1]")
|
|
|
|
fmt.Fprintln(w, " all."+names["Bases"]+" = all."+names["Bases"]+"[:len(all."+names["Bases"]+")-1]")
|
|
|
|
fmt.Fprintln(w, " return &newr")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " return nil")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
2024-11-29 17:45:17 -06:00
|
|
|
|
|
|
|
func iterFind(w io.Writer, names map[string]string) {
|
|
|
|
fmt.Fprintln(w, "// find a dependancy by the go path")
|
|
|
|
fmt.Fprintln(w, "func (all *"+names["Bases"]+") FindBy"+names["append"]+"(s string) *"+names["Base"]+" {")
|
2024-12-01 16:43:08 -06:00
|
|
|
fmt.Fprintln(w, " if all == nil {")
|
|
|
|
fmt.Fprintln(w, " return nil")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-12-01 10:42:12 -06:00
|
|
|
if sortmap["lock"] == "all" {
|
|
|
|
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
|
|
|
|
}
|
2024-11-29 17:45:17 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
|
|
|
|
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
|
|
|
|
fmt.Fprintln(w, " return all."+names["Bases"]+"[i]")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " return nil")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|