FIX THINGS. THIS IS BETTER!

This commit is contained in:
Jeff Carr 2025-01-12 19:32:52 -06:00
parent b5002b158b
commit 9cb5064906
3 changed files with 56 additions and 34 deletions

View File

@ -21,11 +21,23 @@ message Apple {
google.protobuf.Timestamp ctime = 3; // when the apple was born google.protobuf.Timestamp ctime = 3; // when the apple was born
} }
message Apples {
string name = 1; // `autogenpb:unique` // generates SortByxxx() and AppendUnique() functions
string genus = 2; // `autogenpb:unique` // generates same thing here but SortByGenus()
repeated Apple apples = 3;
}
message Pear { // `autogenpb:nomutex` message Pear { // `autogenpb:nomutex`
string name = 1; // `autogenpb:sort` string name = 1; // `autogenpb:sort`
string favorite = 2; // `autogenpb:sort` `autogenpb:unique` string favorite = 2; // `autogenpb:sort` `autogenpb:unique`
} }
message Pears { // `autogenpb:nomutex`
string name = 1; // `autogenpb:sort`
string favorite = 2; // `autogenpb:sort` `autogenpb:unique`
repeated Pear pears = 3;
}
message Banana { // `autogenpb:nomutex` message Banana { // `autogenpb:nomutex`
repeated string name = 1; // `autogenpb:sort` repeated string name = 1; // `autogenpb:sort`
string favorite = 2; // `autogenpb:sort` `autogenpb:unique` string favorite = 2; // `autogenpb:sort` `autogenpb:unique`
@ -43,14 +55,14 @@ message Basket { // `autogenpb:nomutex`
// "Fruit" must exist. you can put anything in it // "Fruit" must exist. you can put anything in it
message Fruit { message Fruit {
string brand = 1; // `autogenpb:unique` `autogenpb:sort` string brand = 1; // `autogenpb:unique` `autogenpb:sort`
repeated Apple apples = 2; Apple apples = 2;
repeated Pear pears = 3; repeated Pear pears = 3;
string UPC = 4; // `autogenpb:sort` `autogenpb:unique` string UPC = 4; // `autogenpb:sort` `autogenpb:unique`
string city = 5; // `autogenpb:sort` string city = 5; // `autogenpb:sort`
repeated Pear notpears = 6; Pears notpears = 6;
repeated Pear fakepears = 7; Pears fakepears = 7;
repeated Basket gifts = 8; repeated Basket gifts = 8;
} }
// "Fruits" MUST EXIST and start exactly this way // "Fruits" MUST EXIST and start exactly this way

View File

@ -107,13 +107,14 @@ func (pb *Files) makeNewSortfile(pf *File) error {
// func newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELECT string) string { // func newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELECT string) string {
if PARENT == VARNAME { if PARENT == VARNAME {
sortby = "SortBy" + v.VarName sortby = "SortBy" + v.VarName
sortname := s.VarType + v.VarName
selectName := "selectAll" + VARNAME
funcdef := newSortBy(wSort, PARENT, s.VarType, sortname, sortby, selectName, v.VarName)
log.Printf("Adding %s\n", funcdef)
} else { } else {
// deprecate this THIS DOES NOT MAKE SENSE TO DO
sortby = "Sort" + VARNAME + "By" + v.VarName sortby = "Sort" + VARNAME + "By" + v.VarName
} }
sortname := s.VarType + v.VarName
selectName := "selectAll" + VARNAME
funcdef := newSortBy(wSort, PARENT, s.VarType, sortname, sortby, selectName, v.VarName)
log.Printf("Adding %s\n", funcdef)
} else { } else {
// funcdef := "func (x *" + FUNCTYPE + ") SortBy" + v.VarName + "(" + v.VarType + ") *[]iter" + s.VarType + " # can not do this yet" // funcdef := "func (x *" + FUNCTYPE + ") SortBy" + v.VarName + "(" + v.VarType + ") *[]iter" + s.VarType + " # can not do this yet"
} }
@ -124,7 +125,7 @@ func (pb *Files) makeNewSortfile(pf *File) error {
fmt.Fprintf(wSort, "\n") fmt.Fprintf(wSort, "\n")
fmt.Fprintf(wSort, "// END SORT\n") fmt.Fprintf(wSort, "// END SORT\n")
// make Len() // make Len() and Append()
for _, msg := range pf.allMsg() { for _, msg := range pf.allMsg() {
PARENT := msg.Name PARENT := msg.Name
LOCK := msg.Lockname LOCK := msg.Lockname
@ -142,14 +143,14 @@ func (pb *Files) makeNewSortfile(pf *File) error {
} }
if PARENT == VARNAME { if PARENT == VARNAME {
// FUNCNAME := "Append" // FUNCNAME := "Append"
msg.simpleAppend(wSort, PARENT, v.VarName, v.VarType) funcdef := msg.simpleAppend(wSort, PARENT, v.VarName, v.VarType)
log.Printf("Adding %s\n", funcdef)
// funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, PARENT, s.VarType) // funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, PARENT, s.VarType)
// log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) // log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
} }
} }
} }
} }
log.Printf("\n")
// add All() // add All()
for _, s := range pf.ToSort { for _, s := range pf.ToSort {
@ -202,13 +203,14 @@ func (pb *Files) makeNewSortfile(pf *File) error {
if PARENT == VARNAME { if PARENT == VARNAME {
// special case because of the enforced .proto format // special case because of the enforced .proto format
FUNCNAME = "FindBy" + v.VarName FUNCNAME = "FindBy" + v.VarName
funcdef := msg.generateFindBy(wSort, FUNCNAME, PARENT, VARNAME, s.VarType, v.VarName)
// func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string {
log.Printf("Adding %s\n", funcdef)
} else { } else {
// deprecate these
FUNCNAME = "Find" + VARNAME + "By" + v.VarName FUNCNAME = "Find" + VARNAME + "By" + v.VarName
} }
funcdef := msg.generateFindBy(wSort, FUNCNAME, PARENT, VARNAME, s.VarType, v.VarName)
// func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string {
log.Printf("Adding %s\n", funcdef)
} else { } else {
if v.VarType == "string" { if v.VarType == "string" {
// probably dumb. move to a database if you need this // probably dumb. move to a database if you need this
@ -234,18 +236,19 @@ func (pb *Files) makeNewSortfile(pf *File) error {
COLOR := v.VarType COLOR := v.VarType
var FUNCNAME string var FUNCNAME string
if PARENT == VARNAME { if PARENT == VARNAME {
// special case because of the enforced .proto format // special case because of the enforced .proto format // ONLY USE CASE THAT MAKES SENSE
FUNCNAME = "DeleteBy" + v.VarName FUNCNAME = "DeleteBy" + v.VarName
var funcdef string
if argv.Delete {
funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName)
} else {
funcdef = msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName)
}
log.Printf("Adding %s\n", funcdef)
} else { } else {
// deprecate these
FUNCNAME = "Delete" + VARNAME + "By" + v.VarName FUNCNAME = "Delete" + VARNAME + "By" + v.VarName
} }
var funcdef string
if argv.Delete {
funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName)
} else {
funcdef = msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName)
}
log.Printf("Adding %s\n", funcdef)
} }
// AppendBy() functions. todo: fix these so Append() is for simple things and Insert() is for unique keys // AppendBy() functions. todo: fix these so Append() is for simple things and Insert() is for unique keys
@ -260,12 +263,17 @@ func (pb *Files) makeNewSortfile(pf *File) error {
ucount += 1 ucount += 1
var FUNCNAME string var FUNCNAME string
if PARENT == VARNAME { if PARENT == VARNAME {
// special case because of the enforced .proto format // special case because of the enforced .proto format // ONLY SUPPORT THIS
FUNCNAME = "AppendBy" + v.VarName FUNCNAME = "AppendBy" + v.VarName
} else {
FUNCNAME = "Append" + VARNAME + "By" + v.VarName
funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, v.VarName, s.VarType) funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, v.VarName, s.VarType)
log.Printf("Adding %s\n", funcdef) log.Printf("Adding %s\n", funcdef)
} else {
// deprecate this
/*
FUNCNAME = "Append" + VARNAME + "By" + v.VarName
funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, v.VarName, s.VarType)
log.Printf("Adding %s\n", funcdef)
*/
} }
} }

View File

@ -3,26 +3,28 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"go.wit.com/log"
) )
// generates Append() // generates Append()
// I like these functions the best. // I like these functions the best.
func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) { func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) string {
LOCK := msg.getLockname("x") LOCK := msg.getLockname("x")
log.Printf("\t\t(x %s) APPEND(%s)\n", FRUIT, APPLE) funcdef := "func (x *" + FRUIT + ") Append(y *" + APPLE + ")"
// log.Printf("\t\t(x %s) APPEND(%s)\n", FRUIT, APPLE)
// append -- no check at all // append -- no check at all
fmt.Fprintln(w, "// just a simple Append() shortcut (but still uses the mutex lock)") 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, funcdef, "{")
fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " "+LOCK+".Lock()")
fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", y)") fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", y)")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
return funcdef
} }
/* FIX THSE as Import() /* FIX THSE as Import()