adding help

This commit is contained in:
Jeff Carr 2025-01-12 06:13:42 -06:00
parent 5b4491f17b
commit 08cd2afd86
9 changed files with 85 additions and 52 deletions

View File

@ -9,6 +9,9 @@ simple: test
full: install clean auto goimports vet build test install full: install clean auto goimports vet build test install
@echo everything worked and the example ran @echo everything worked and the example ran
# does a help example to debug the help logic
help: build
make -C example help
# if this passes, it should be OK to 'go install' # if this passes, it should be OK to 'go install'
test: test:

28
README
View File

@ -1,23 +1,13 @@
This app will autogenerate protobuf Sort() and Marshal() functions # This app will autogenerate Sort() and Marshal() functions for .proto files
It was designed to work on .proto files designed with a standard # It was designed to work on .proto files designed with a .proto standard
way that utilizes this method:
apple.proto should have * This will generate:
* Marshal() functions (to protoWIRE, protoTEXT and protoJSON)
* SortBy() functions
* FindBy() functions
* DeleteBy() functions
* AppendBy() functions
message Apples { # See the examples/ for a sample fruit.proto file that documents what is needed
string uuid
string version
repeaded Apple Apples
}
message Apple {
<whatever you want here>
}
This "scheme" as it were will be familar to others as a common way to pluralize data formats.
In general, this is turning out to be a good way to handle protocol buffers for me so far.
There are several things that can be put in the protobuf file to trigger what files are made.
See the examples for how to do this.

View File

@ -18,6 +18,9 @@ testProtoc:
../autogenpb --proto fruit.proto --package main # inserts mutex into protoc .pb.go file ../autogenpb --proto fruit.proto --package main # inserts mutex into protoc .pb.go file
make build make build
help:
../autogenpb --package main --proto bad.proto
modproto: clean withMutex goimports vet build modproto: clean withMutex goimports vet build
./example ./example

16
example/bad.proto Normal file
View File

@ -0,0 +1,16 @@
syntax = "proto3";
// this file is wrong and will give you errors
package main;
// "Fruit" must exist. you can put anything in it
message Bad {
string something = 1;
}
message Badthing {
string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Bad Bads = 3;
}

View File

@ -193,34 +193,6 @@ func (pb *Files) makeNewSortfile(pf *File) error {
*/ */
} }
// make delete()
/*
for _, msg := range pf.allMsg() {
PARENT := msg.Name
for _, v := range msg.Vars {
if !v.HasUnique {
continue
}
funcname := "func (x *" + PARENT + ") Delete" + msg.Name + "By" + v.VarName + "(" + v.VarType + ") bool"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
FRUIT := PARENT
APPLES := msg.Name
APPLE := msg.VarName
COLOR := v.VarName
FUNCNAME := "Delete" + msg.Name + "By" + v.VarName
var funcdef string
if argv.Delete {
funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME)
} else {
funcdef = msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME)
}
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
}
}
*/
// add Find() Delete() Append() Insert() // add Find() Delete() Append() Insert()
log.Printf(" %-2s %20s %20s %20s %20s\n", "", "PARENT STRUCT", "VAR STRUCT TYPE", "VAR NAME", "LOCK") log.Printf(" %-2s %20s %20s %20s %20s\n", "", "PARENT STRUCT", "VAR STRUCT TYPE", "VAR NAME", "LOCK")
for i, s := range pf.ToSort { for i, s := range pf.ToSort {
@ -295,7 +267,7 @@ func (pb *Files) makeNewSortfile(pf *File) error {
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
} }
// AppendBy() functions. these maybe need to be rethought // AppendBy() functions. todo: fix these so Append() is for simple things and Insert() is for unique keys
var ucount int var ucount int
for _, v := range msg.Vars { for _, v := range msg.Vars {
if v.IsRepeated { if v.IsRepeated {
@ -316,6 +288,7 @@ func (pb *Files) makeNewSortfile(pf *File) error {
} }
} }
// TODO: do this next // x *Repos) InsertPath( string) *Repo // returns existing record or new record if path != exists
if ucount == 1 { if ucount == 1 {
for _, v := range msg.Vars { for _, v := range msg.Vars {
if !v.HasUnique { if !v.HasUnique {
@ -329,7 +302,6 @@ func (pb *Files) makeNewSortfile(pf *File) error {
funcname = "func (x *" + PARENT + ") Insert(a *" + CHILD + ") (*" + CHILD + ", isNew bool) // todo" funcname = "func (x *" + PARENT + ") Insert(a *" + CHILD + ") (*" + CHILD + ", isNew bool) // todo"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
} }
} }
return nil return nil
} }

View File

@ -25,6 +25,7 @@ func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
/* FIX THSE as Import()
func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) {
LOCK := msg.getLockname("x") LOCK := msg.getLockname("x")
@ -66,6 +67,7 @@ func (msg *MsgName) appendUniqueCOLOR(w io.Writer, FRUIT, APPLES, APPLE, COLOR s
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
*/
// Unique Append. rejects Append() if value is already defined // Unique Append. rejects Append() if value is already defined
// compares the field 'COLOR' in the STRUCT with VARNAME // compares the field 'COLOR' in the STRUCT with VARNAME

View File

@ -20,6 +20,8 @@ func (pf *File) syncLock(w io.Writer) {
fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex") fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
/* /*
// this was a note, but the note is wrong. it seems to work fine. my example/ code was wrong. I think. notsure
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "// this is needed because it seems Marshal() fails if locks are in the structs (?)") fmt.Fprintln(w, "// this is needed because it seems Marshal() fails if locks are in the structs (?)")
fmt.Fprintln(w, "// this might just be a syntactical runtime error. notsure.") fmt.Fprintln(w, "// this might just be a syntactical runtime error. notsure.")

View File

@ -8,9 +8,6 @@ import (
// This was just going to be a function to print the results to stdout // This was just going to be a function to print the results to stdout
// instead, it's the core logic of the whole app
// --dry-run on the command line will just print what would be generated
// print the protobuf in human form // print the protobuf in human form
func (pf *File) printMsgTable() error { func (pf *File) printMsgTable() error {
pf.Bases.printMsg() pf.Bases.printMsg()

View File

@ -37,6 +37,7 @@ func (pb *Files) hasPluralMessage(f *File) error {
line = scanner.Text() line = scanner.Text()
fields := strings.Fields(line) fields := strings.Fields(line)
if fields[0] == "string" && fields[1] != "uuid" { if fields[0] == "string" && fields[1] != "uuid" {
f.noUuid()
return fmt.Errorf("proto file does not have a UUID") return fmt.Errorf("proto file does not have a UUID")
} }
// ok, uuid is here // ok, uuid is here
@ -46,6 +47,7 @@ func (pb *Files) hasPluralMessage(f *File) error {
line = scanner.Text() line = scanner.Text()
fields = strings.Fields(line) fields = strings.Fields(line)
if fields[0] == "string" && fields[1] != "version" { if fields[0] == "string" && fields[1] != "version" {
f.noUuid()
return fmt.Errorf("proto file does not have a version") return fmt.Errorf("proto file does not have a version")
} }
// found "version", the .proto file conforms // found "version", the .proto file conforms
@ -53,9 +55,55 @@ func (pb *Files) hasPluralMessage(f *File) error {
log.Info("found Version:", line) log.Info("found Version:", line)
return nil return nil
} }
f.noPluralMessage()
return fmt.Errorf("proto file error %s", f.Filename) return fmt.Errorf("proto file error %s", f.Filename)
} }
func (pf *File) noPluralMessage() {
base := cases.Title(language.English, cases.NoLower).String(pf.Filebase)
log.Info("")
log.Info("###########################################################################")
log.Info("Your proto file", pf.Filename, "does not contain the correct 'message' definitions")
log.Info("")
log.Info("For autogenpb to work on your file", pf.Filename, ", you must have both names exactly:")
log.Info("")
log.Printf("message %s {\n", base)
log.Info("}")
log.Printf("message %s {\n", base+"s")
log.Info("}")
log.Info("")
log.Info("###########################################################################")
badExit(fmt.Errorf("proto file error %s", pf.Filename))
}
// message Fruits { // `autogenpb:marshal` `autogenpb:mutex`
// string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`
// string version = 2; // `autogenpb:version:v0.0.1`
// repeated Fruit Fruits = 3; // THIS MUST BE "Fruit" and then "Fruit" + "s"
// }
func (pf *File) noUuid() {
base := cases.Title(language.English, cases.NoLower).String(pf.Filebase)
log.Info("")
log.Info("###########################################################################")
log.Info("Your proto file", pf.Filename, "is incorrect for 'message' ", base+"s")
log.Info("")
log.Info("For autogenpb to work on your file", pf.Filename, ",", base+"s must be exactly:")
log.Info("")
log.Info("message Fruits { // `autogenpb:marshal` `autogenpb:mutex`")
log.Info(" string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`")
log.Info(" string version = 2; // `autogenpb:version:v0.0.1`")
log.Info(" repeated Fruit Fruits = 3; // THIS MUST BE ", base, " and then ", base+"s")
log.Info("}")
log.Info("")
log.Info("If you don't have a UUID, you can use the randomly generated one here")
log.Info("")
log.Info("###########################################################################")
badExit(fmt.Errorf("proto file error %s", pf.Filename))
}
func (pb *Files) protoParse(pf *File) error { func (pb *Files) protoParse(pf *File) error {
// does the file conform to the standard? (also reads in UUID & Version) // does the file conform to the standard? (also reads in UUID & Version)
if err := pb.hasPluralMessage(pf); err != nil { if err := pb.hasPluralMessage(pf); err != nil {