From 08cd2afd86557f06d6b3f6e006d686fef4c7b2ee Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 12 Jan 2025 06:13:42 -0600 Subject: [PATCH] adding help --- Makefile | 3 +++ README | 28 +++++++++------------------ example/Makefile | 3 +++ example/bad.proto | 16 ++++++++++++++++ generate.go | 32 ++----------------------------- generateAppend.go | 2 ++ generateMutex.go | 2 ++ human.go | 3 --- protoParse.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 85 insertions(+), 52 deletions(-) create mode 100644 example/bad.proto diff --git a/Makefile b/Makefile index 9f445bd..e260d8b 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ simple: test full: install clean auto goimports vet build test install @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' test: diff --git a/README b/README index da987ea..e14b614 100644 --- a/README +++ b/README @@ -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 -way that utilizes this method: +# It was designed to work on .proto files designed with a .proto standard -apple.proto should have +* This will generate: +* Marshal() functions (to protoWIRE, protoTEXT and protoJSON) +* SortBy() functions +* FindBy() functions +* DeleteBy() functions +* AppendBy() functions -message Apples { - string uuid - string version - repeaded Apple Apples -} +# See the examples/ for a sample fruit.proto file that documents what is needed -message Apple { - -} - -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. diff --git a/example/Makefile b/example/Makefile index 3a045bd..40c0146 100644 --- a/example/Makefile +++ b/example/Makefile @@ -18,6 +18,9 @@ testProtoc: ../autogenpb --proto fruit.proto --package main # inserts mutex into protoc .pb.go file make build +help: + ../autogenpb --package main --proto bad.proto + modproto: clean withMutex goimports vet build ./example diff --git a/example/bad.proto b/example/bad.proto new file mode 100644 index 0000000..b9198e7 --- /dev/null +++ b/example/bad.proto @@ -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; +} diff --git a/generate.go b/generate.go index 3dc70c7..43e5092 100644 --- a/generate.go +++ b/generate.go @@ -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() log.Printf(" %-2s %20s %20s %20s %20s\n", "", "PARENT STRUCT", "VAR STRUCT TYPE", "VAR NAME", "LOCK") 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) } - // 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 for _, v := range msg.Vars { 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 { for _, v := range msg.Vars { 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" log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) } - } return nil } diff --git a/generateAppend.go b/generateAppend.go index cd34a4e..d233bfc 100644 --- a/generateAppend.go +++ b/generateAppend.go @@ -25,6 +25,7 @@ func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) { fmt.Fprintln(w, "") } +/* FIX THSE as Import() func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { 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, "") } +*/ // Unique Append. rejects Append() if value is already defined // compares the field 'COLOR' in the STRUCT with VARNAME diff --git a/generateMutex.go b/generateMutex.go index aaa68a5..45d0d2e 100644 --- a/generateMutex.go +++ b/generateMutex.go @@ -20,6 +20,8 @@ func (pf *File) syncLock(w io.Writer) { fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex") 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, "// 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.") diff --git a/human.go b/human.go index c735bf4..bd27580 100644 --- a/human.go +++ b/human.go @@ -8,9 +8,6 @@ import ( // 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 func (pf *File) printMsgTable() error { pf.Bases.printMsg() diff --git a/protoParse.go b/protoParse.go index 2cefc80..c18c87d 100644 --- a/protoParse.go +++ b/protoParse.go @@ -37,6 +37,7 @@ func (pb *Files) hasPluralMessage(f *File) error { line = scanner.Text() fields := strings.Fields(line) if fields[0] == "string" && fields[1] != "uuid" { + f.noUuid() return fmt.Errorf("proto file does not have a UUID") } // ok, uuid is here @@ -46,6 +47,7 @@ func (pb *Files) hasPluralMessage(f *File) error { line = scanner.Text() fields = strings.Fields(line) if fields[0] == "string" && fields[1] != "version" { + f.noUuid() return fmt.Errorf("proto file does not have a version") } // found "version", the .proto file conforms @@ -53,9 +55,55 @@ func (pb *Files) hasPluralMessage(f *File) error { log.Info("found Version:", line) return nil } + f.noPluralMessage() 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 { // does the file conform to the standard? (also reads in UUID & Version) if err := pb.hasPluralMessage(pf); err != nil {