2024-12-27 14:43:21 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2025-01-10 04:22:53 -06:00
|
|
|
"fmt"
|
2025-01-10 12:19:22 -06:00
|
|
|
"io"
|
2024-12-27 14:43:21 -06:00
|
|
|
"os"
|
2025-01-10 11:22:08 -06:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
"golang.org/x/text/cases"
|
|
|
|
"golang.org/x/text/language"
|
2024-12-27 14:43:21 -06:00
|
|
|
)
|
|
|
|
|
2025-01-10 04:22:53 -06:00
|
|
|
// this file is named poorly. It has more than Sort()
|
|
|
|
|
2025-01-09 15:51:45 -06:00
|
|
|
func (pb *Files) makeNewSortfile(pf *File) error {
|
2025-01-10 11:22:08 -06:00
|
|
|
wSort, _ := os.OpenFile(pf.Filebase+".sort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
defer wSort.Close()
|
|
|
|
wFind, _ := os.OpenFile(pf.Filebase+".find.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
defer wFind.Close()
|
|
|
|
|
|
|
|
header(wSort, pf)
|
2025-01-09 15:29:27 -06:00
|
|
|
|
2025-01-10 11:22:08 -06:00
|
|
|
pf.Base.iterTop(wSort)
|
|
|
|
pf.Base.iterNext(wSort)
|
|
|
|
pf.selectAllFunc(wSort)
|
|
|
|
pf.iterSelect(wSort)
|
2025-01-10 04:22:53 -06:00
|
|
|
|
2025-01-10 11:22:08 -06:00
|
|
|
pf.sortByFunc(wSort)
|
2025-01-10 21:33:00 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
pf.appendUnique(wFind) // Append() enforce no unique keys
|
|
|
|
if argv.Delete {
|
|
|
|
pf.deleteWithCopyFunc(wFind)
|
|
|
|
} else {
|
|
|
|
pf.deleteFunc(wFind)
|
|
|
|
}
|
|
|
|
pf.findFunc(wFind)
|
|
|
|
*/
|
2025-01-10 11:22:08 -06:00
|
|
|
|
2025-01-11 03:27:53 -06:00
|
|
|
header(wFind, pf)
|
|
|
|
pf.syncLock(wFind)
|
|
|
|
pf.specialBases(wFind)
|
2025-01-10 23:55:12 -06:00
|
|
|
|
2025-01-10 11:22:08 -06:00
|
|
|
// attempt to add sort functions for pf.Base
|
2025-01-10 22:27:38 -06:00
|
|
|
if err := pf.processMessage(pf.Bases, wSort, wFind); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := pf.processMessage(pf.Base, wSort, wFind); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-01-10 11:22:08 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-11 04:41:14 -06:00
|
|
|
/*
|
|
|
|
func newGenerate(parent *MsgName, child *MsgName)
|
|
|
|
var FRUIT string = cases.Title(language.English, cases.NoLower).String(parent.Name)
|
|
|
|
var LOCK string = parent.Lockname
|
|
|
|
var APPLES string = cases.Title(language.English, cases.NoLower).String(pf.Bases.Name)
|
|
|
|
var APPLE string = cases.Title(language.English, cases.NoLower).String(pf.Base.Name)
|
|
|
|
var COLORS []string
|
|
|
|
for _, v := range parent.Vars {
|
|
|
|
if v.HasUnique {
|
|
|
|
var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName)
|
|
|
|
COLORS = append(COLORS, COLOR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2025-01-11 03:27:53 -06:00
|
|
|
func (pf *File) specialBases(wFind io.Writer) {
|
2025-01-10 23:55:12 -06:00
|
|
|
var FRUIT string = cases.Title(language.English, cases.NoLower).String(pf.Bases.Name)
|
|
|
|
var APPLES string = cases.Title(language.English, cases.NoLower).String(pf.Bases.Name)
|
|
|
|
var APPLE string = cases.Title(language.English, cases.NoLower).String(pf.Base.Name)
|
|
|
|
|
|
|
|
pf.Bases.simpleAppend(wFind, FRUIT, APPLES, APPLE)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-01-10 22:27:38 -06:00
|
|
|
// to simplify being able to read the code, FRUIT, APPLES and APPLE are used
|
|
|
|
// 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
|
2025-01-11 04:27:45 -06:00
|
|
|
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
|
2025-01-10 22:27:38 -06:00
|
|
|
|
2025-01-10 21:33:00 -06:00
|
|
|
log.Printf("Generating functions for %s\n", FRUIT)
|
2025-01-10 22:27:38 -06:00
|
|
|
|
2025-01-11 04:27:45 -06:00
|
|
|
for _, v := range parent.Vars {
|
2025-01-10 11:22:08 -06:00
|
|
|
if !v.IsRepeated {
|
2025-01-10 19:37:32 -06:00
|
|
|
log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType)
|
2025-01-10 11:22:08 -06:00
|
|
|
continue
|
|
|
|
}
|
2025-01-10 22:27:38 -06:00
|
|
|
log.Printf("\tFOUND REPEATED %s %s\n", v.VarName, v.VarType)
|
2025-01-10 21:33:00 -06:00
|
|
|
// use easier to read variable names APPLE and APPLES in the code
|
|
|
|
var APPLE string = v.VarType
|
|
|
|
var APPLES string = cases.Title(language.English, cases.NoLower).String(v.VarName)
|
|
|
|
|
|
|
|
// try and find the message struct for APPLE
|
|
|
|
var found *MsgName
|
2025-01-10 22:27:38 -06:00
|
|
|
if pf.Base.Name == APPLE {
|
|
|
|
found = pf.Base
|
|
|
|
}
|
2025-01-10 21:33:00 -06:00
|
|
|
for _, m := range pf.MsgNames {
|
|
|
|
if m.Name == APPLE {
|
|
|
|
found = m
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found == nil {
|
|
|
|
return fmt.Errorf("failed to find struct %s", APPLE)
|
2025-01-10 11:22:08 -06:00
|
|
|
}
|
2025-01-10 21:33:00 -06:00
|
|
|
|
2025-01-10 22:27:38 -06:00
|
|
|
log.Printf("FOUND: %s %s for %s\n", APPLES, APPLE, FRUIT)
|
|
|
|
|
2025-01-11 04:27:45 -06:00
|
|
|
found.addFindByMsg(wFind, FRUIT, APPLES, APPLE, LOCK)
|
2025-01-10 22:27:38 -06:00
|
|
|
found.addAppendByMsg(wFind, FRUIT, APPLES, APPLE)
|
|
|
|
found.addDeleteByMsg(wFind, FRUIT, APPLES, APPLE)
|
|
|
|
found.addInsertByMsg(wFind, FRUIT, APPLES, APPLE) // new idea
|
|
|
|
|
|
|
|
found.addSortByMsg(wSort, FRUIT, APPLES, APPLE)
|
2025-01-10 04:22:53 -06:00
|
|
|
}
|
2025-01-09 15:51:45 -06:00
|
|
|
return nil
|
2024-12-27 14:43:21 -06:00
|
|
|
}
|
2025-01-10 11:22:08 -06:00
|
|
|
|
2025-01-11 04:27:45 -06:00
|
|
|
func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK string) {
|
2025-01-11 04:03:41 -06:00
|
|
|
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)
|
2025-01-11 04:41:14 -06:00
|
|
|
log.Printf("\t\t(x %s) FindBy%s(string) *%s\n", FRUIT, COLOR, APPLE)
|
|
|
|
generateFindBy(w, FRUIT, APPLES, APPLE, COLOR, LOCK)
|
2025-01-11 04:03:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-10 22:27:38 -06:00
|
|
|
func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
|
|
|
|
log.Printf("\tDELETE: %s %s for %s\n", APPLES, APPLE, FRUIT)
|
2025-01-10 18:26:44 -06:00
|
|
|
var COLORS []string
|
2025-01-10 22:27:38 -06:00
|
|
|
for _, v := range parent.Vars {
|
|
|
|
if !v.HasUnique {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName)
|
|
|
|
COLORS = append(COLORS, COLOR)
|
2025-01-10 18:26:44 -06:00
|
|
|
|
2025-01-10 22:27:38 -06:00
|
|
|
log.Printf("\t\t(x %s) DeleteBy%s(string) *%s\n", FRUIT, COLOR, APPLE)
|
|
|
|
if argv.Delete {
|
|
|
|
parent.deleteByWithCopy(w, FRUIT, APPLES, APPLE, COLOR)
|
|
|
|
} else {
|
|
|
|
parent.deleteBy(w, FRUIT, APPLES, APPLE, COLOR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parent *MsgName) addInsertByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
|
|
|
|
log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT)
|
2025-01-10 21:33:00 -06:00
|
|
|
for _, v := range parent.Vars {
|
2025-01-10 22:27:38 -06:00
|
|
|
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)
|
2025-01-11 04:03:41 -06:00
|
|
|
parent.insertBy(w, FRUIT, APPLES, APPLE, COLOR)
|
2025-01-10 11:22:08 -06:00
|
|
|
}
|
2025-01-10 22:27:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parent *MsgName) addAppendByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
|
|
|
|
log.Printf("\tAPPEND!: %s %s for %s\n", APPLES, APPLE, FRUIT)
|
|
|
|
var COLORS []string
|
|
|
|
|
|
|
|
for _, v := range parent.Vars {
|
2025-01-10 11:22:08 -06:00
|
|
|
if v.HasUnique {
|
2025-01-10 22:27:38 -06:00
|
|
|
var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName)
|
2025-01-10 18:26:44 -06:00
|
|
|
COLORS = append(COLORS, COLOR)
|
|
|
|
|
2025-01-10 22:27:38 -06:00
|
|
|
log.Printf("\t\t(x %s) AppendUniqueBy%s(%s)\n", FRUIT, COLOR, APPLE)
|
2025-01-11 04:27:45 -06:00
|
|
|
parent.appendUniqueCOLOR(w, FRUIT, APPLES, APPLE, COLOR)
|
2025-01-10 11:22:08 -06:00
|
|
|
}
|
|
|
|
}
|
2025-01-10 18:26:44 -06:00
|
|
|
if len(COLORS) > 0 {
|
2025-01-10 22:27:38 -06:00
|
|
|
parent.appendUnique(w, FRUIT, APPLES, APPLE, COLORS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parent *MsgName) addSortByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
|
|
|
|
log.Printf("\tSORT!: %s %s for %s\n", APPLES, APPLE, FRUIT)
|
|
|
|
|
|
|
|
for _, v := range parent.Vars {
|
|
|
|
if v.HasSort {
|
|
|
|
// log.Printf("\tSort!: %s %s for %s\n", APPLES, APPLE, v.VarName)
|
|
|
|
newS := cases.Title(language.English, cases.NoLower).String(v.VarName)
|
|
|
|
log.Printf("\t\t(x %s) SortdBy%s() *%sIter\n", FRUIT, newS, APPLE)
|
|
|
|
}
|
2025-01-10 18:26:44 -06:00
|
|
|
}
|
2025-01-10 11:22:08 -06:00
|
|
|
}
|