From 88e4359cdec41a29750586202b15fa2dabb89e55 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 Jan 2025 04:22:53 -0600 Subject: [PATCH] cleanup as much junk as possible --- argv.go | 48 ++++++++++++++---------------------------------- main.go | 5 ----- sort.go | 34 ++++++++++++++++++++++------------ sortFunc.go | 12 ++++++------ 4 files changed, 42 insertions(+), 57 deletions(-) diff --git a/argv.go b/argv.go index 6d496f1..7357f6b 100644 --- a/argv.go +++ b/argv.go @@ -10,48 +10,28 @@ var argv args type args struct { Package string `arg:"--package" help:"the package name"` - // LoBase string `arg:"--lobase" help:"lowercase basename"` - // UpBase string `arg:"--upbase" help:"uppercase basename"` - Proto string `arg:"--proto" help:"the .proto filename"` - Append string `arg:"--append" help:"will keep this key unique on append"` - Sort []string `arg:"-s,--sort,separate" help:"how and what to sort on"` - Marshal []string `arg:"--marshal" help:"what to marshal on"` - NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"` - NoSort bool `arg:"--no-sort" help:"do not make a sort.pb.go file"` - Mutex bool `arg:"--mutex" default:"true" help:"add mutex in protoc autogen file"` - DryRun bool `arg:"--dry-run" help:"show what would be run"` - GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` - GoPath string `arg:"--gopath" help:"the gopath of this repo"` + Proto string `arg:"--proto" help:"the .proto filename"` + Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"` + Delete bool `arg:"--delete" help:"use delete with copy experiment"` + DryRun bool `arg:"--dry-run" help:"show what would be run"` + GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` + GoPath string `arg:"--gopath" help:"the gopath of this repo"` } func (a args) Description() string { - return ` -autogenpb tries to generate a sort.pb.go file for a protobuf + return "autogenpb " + VERSION + " Built on " + BUILDTIME + ` -Install with: +Auto Generate protocol buffer Sort() and Marshal() functions - go install go.wit.com/apps/autogenpb@latest +go install go.wit.com/apps/autogenpb@latest -More information at: - - https://go.wit.com/ - -The protobuf is assumed to have a 'standard' format. - -That is the .proto file: - -* only defines one thing and it's the same name as the file -* uses the concept of 'plural' (like ruby on rails) - This means, the file should be "apples.proto" and inside - have 'message Apple' and 'message Apples' - The "message Apples" should have a repeated Apple apples - -There is an example in the code: - - go-clone go.wit.com/apps/autogenpb +The protobuf requires a 'standard' .proto format. +See the git sources for an example .proto file. ` } +/* func (args) Version() string { - return "go-clone " + VERSION + " Built on " + BUILDTIME + return "autogenpb " + VERSION + " Built on " + BUILDTIME } +*/ diff --git a/main.go b/main.go index 2e324e2..4b1b770 100644 --- a/main.go +++ b/main.go @@ -25,11 +25,6 @@ import ( var VERSION string var BUILDTIME string -var sortmap map[string]string -var marshalKeys []string - -// var uniqueKeys []string - func main() { pp := arg.MustParse(&argv) diff --git a/sort.go b/sort.go index 5d591c2..44b0798 100644 --- a/sort.go +++ b/sort.go @@ -1,27 +1,37 @@ package main import ( + "fmt" "os" ) -func (pb *Files) makeNewSortfile(pf *File) error { - f, _ := os.OpenFile(pf.Filebase+".newsort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) +// this file is named poorly. It has more than Sort() - header(f, pf) - pf.syncLock(f) +func (pb *Files) makeNewSortfile(pf *File) error { + w, _ := os.OpenFile(pf.Filebase+".sort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + + header(w, pf) + pf.syncLock(w) if argv.Mutex { // use the mutex lock from the modified protoc.pb.go file pf.Bases.Lockname = "all.Lock" } - pf.Base.iterTop(f) - pf.Base.iterNext(f) - pf.iterSelect(f) - pf.appendUnique(f) // Append() enforce no unique keys - pf.iterSortBy(f) - pf.iterAll(f) - pf.iterDelete(f) - pf.iterFind(f) + pf.Base.iterTop(w) + pf.Base.iterNext(w) + pf.selectAllFunc(w) + pf.iterSelect(w) + + fmt.Fprintln(w, "// maybe seperate files here?") + + pf.appendUnique(w) // Append() enforce no unique keys + pf.sortByFunc(w) + if argv.Delete { + pf.deleteWithCopyFunc(w) + } else { + pf.deleteFunc(w) + } + pf.findFunc(w) return nil } diff --git a/sortFunc.go b/sortFunc.go index 2bcff18..869a2ab 100644 --- a/sortFunc.go +++ b/sortFunc.go @@ -61,7 +61,7 @@ func (msg *MsgName) iterNext(w io.Writer) { fmt.Fprintln(w, "") } -func (pf *File) iterAll(w io.Writer) { +func (pf *File) selectAllFunc(w io.Writer) { var BASES string = pf.Bases.Name var BASE string = pf.Base.Name var LOCK string = pf.Bases.Lockname @@ -82,7 +82,7 @@ func (pf *File) iterAll(w io.Writer) { fmt.Fprintln(w, "") } -func (pf *File) iterSortBy(w io.Writer) { +func (pf *File) sortByFunc(w io.Writer) { var BASES string = pf.Bases.Name var BASE string = pf.Base.Name @@ -193,7 +193,7 @@ func (pf *File) appendUnique(w io.Writer) { fmt.Fprintln(w, "") } -func (pf *File) iterReplace(w io.Writer) { +func (pf *File) replaceFunc(w io.Writer) { var MSG string = pf.Bases.Name var BASE string = pf.Base.Name var LOCK string = pf.Bases.Lockname @@ -217,7 +217,7 @@ func (pf *File) iterReplace(w io.Writer) { } } -func (pf *File) iterDelete(w io.Writer) { +func (pf *File) deleteFunc(w io.Writer) { var MSG string = pf.Bases.Name var LOCK string = pf.Bases.Lockname @@ -240,7 +240,7 @@ func (pf *File) iterDelete(w io.Writer) { } // this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct -func (pf *File) iterDeleteWithCopy(w io.Writer) { +func (pf *File) deleteWithCopyFunc(w io.Writer) { var MSG string = pf.Bases.Name var BASE string = pf.Base.Name var LOCK string = pf.Bases.Lockname @@ -266,7 +266,7 @@ func (pf *File) iterDeleteWithCopy(w io.Writer) { } } -func (pf *File) iterFind(w io.Writer) { +func (pf *File) findFunc(w io.Writer) { var MSG string = pf.Bases.Name var BASE string = pf.Base.Name var LOCK string = pf.Bases.Lockname