From 84a042cb15b456e5e7c37fbc922395becf7f071d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 29 Nov 2024 13:27:32 -0600 Subject: [PATCH] autogenpb marshal * sort Signed-off-by: Jeff Carr --- Makefile | 3 + forgeConfig.marshal.go | 50 -------------- forgeConfig.proto | 6 +- forgeConfig.sort.go | 151 ----------------------------------------- forgeConfig/main.go | 2 +- settings.go | 2 +- 6 files changed, 8 insertions(+), 206 deletions(-) delete mode 100644 forgeConfig.marshal.go delete mode 100644 forgeConfig.sort.go diff --git a/Makefile b/Makefile index c73a6c5..a0c6343 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,9 @@ clean: install: make -C forgeConfig install +autogenpb: + autogenpb --proto forgeConfig.proto --lobase forgeConfig --upbase ForgeConfig --sort "ByPath,GoPath" --marshal ForgeConfigs --append GoPath + forgeConfig.pb.go: forgeConfig.proto # I'm using version v1.35.x from google.golang.org/protobuf/cmd/protoc-gen-go cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/forgepb \ diff --git a/forgeConfig.marshal.go b/forgeConfig.marshal.go deleted file mode 100644 index 63fa744..0000000 --- a/forgeConfig.marshal.go +++ /dev/null @@ -1,50 +0,0 @@ -package forgepb - -// TODO: autogen this -// functions to import and export the protobuf -// data to and from config files - -import ( - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/protobuf/encoding/prototext" - "google.golang.org/protobuf/proto" - // "google.golang.org/protobuf/proto" -) - -// human readable JSON -func (p *ForgeConfigs) FormatJSON() string { - return protojson.Format(p) -} - -// apparently this isn't supposed to be used? -// https://protobuf.dev/reference/go/faq/#unstable-text -// this is a shame because this is much nicer output than JSON Format() -// TODO: fix things so this is the default -func (p *ForgeConfigs) FormatTEXT() string { - return prototext.Format(p) -} - -// unmarshalTEXT -func (p *ForgeConfigs) UnmarshalTEXT(data []byte) error { - return prototext.Unmarshal(data, p) -} - -// marshal json -func (p *ForgeConfigs) MarshalJSON() ([]byte, error) { - return protojson.Marshal(p) -} - -// unmarshal -func (p *ForgeConfigs) UnmarshalJSON(data []byte) error { - return protojson.Unmarshal(data, p) -} - -// marshal to wire -func (m *ForgeConfigs) Marshal() ([]byte, error) { - return proto.Marshal(m) -} - -// unmarshal from wire -func (m *ForgeConfigs) Unmarshal(data []byte) error { - return proto.Unmarshal(data, m) -} diff --git a/forgeConfig.proto b/forgeConfig.proto index 5d3b25a..5195b09 100644 --- a/forgeConfig.proto +++ b/forgeConfig.proto @@ -20,9 +20,9 @@ message ForgeConfig { bool favorite = 6; // you like this. always git clone/go clone this repo bool interesting = 7; // this is something interesting you found and want to remember it - string masterBranch = 8; // git 'main' or 'master' branch name - string develBranch = 9; // whatever the git 'devel' branch name is - string userBranch = 10; // whatever your username branch is + string masterBranchName = 8; // git 'main' or 'master' branch name + string develBranchName = 9; // whatever the git 'devel' branch name is + string userBranchName = 10; // whatever your username branch is string debName = 11; // the actual name used with 'apt install' (or distro apt equivalent. // todo: appeal to everyone to alias 'apt' on rhat, gentoo, arch, etc to alias 'apt install' diff --git a/forgeConfig.sort.go b/forgeConfig.sort.go deleted file mode 100644 index d8fc4a1..0000000 --- a/forgeConfig.sort.go +++ /dev/null @@ -1,151 +0,0 @@ -package forgepb - -// TODO: autogen this? (probably not feasible. need go-arglike tricks in proto) - -import ( - "fmt" - "os" - "sort" - sync "sync" - "time" -) - -// bad global lock until I figure out some other plan -var forgeConfigsLock sync.RWMutex - -type ForgeConfigIterator struct { - sync.RWMutex - - packs []*ForgeConfig - index int -} - -// newForgeConfigIterator initializes a new iterator. -func newForgeConfigIterator(packs []*ForgeConfig) *ForgeConfigIterator { - return &ForgeConfigIterator{packs: packs} -} - -// Scan moves to the next element and returns false if there are no more packs. -func (it *ForgeConfigIterator) Scan() bool { - if it.index >= len(it.packs) { - return false - } - it.index++ - return true -} - -// ForgeConfig returns the current forgeConfig. -func (it *ForgeConfigIterator) Next() *ForgeConfig { - if it.packs[it.index-1] == nil { - for i, d := range it.packs { - fmt.Println("i =", i, d) - } - fmt.Println("len =", len(it.packs)) - fmt.Println("forgeConfig == nil", it.index, it.index-1) - os.Exit(-1) - } - return it.packs[it.index-1] -} - -// Use Scan() in a loop, similar to a while loop -// -// for iterator.Scan() { -// d := iterator.ForgeConfig() -// fmt.Println("ForgeConfig UUID:", d.Uuid) -// } - -func (r *ForgeConfigs) All() *ForgeConfigIterator { - forgeConfigPointers := r.selectAllForgeConfigs() - - iterator := newForgeConfigIterator(forgeConfigPointers) - return iterator -} - -func (r *ForgeConfigs) SortByPath() *ForgeConfigIterator { - packs := r.selectAllForgeConfigs() - - sort.Sort(byForgeConfigPath(packs)) - - iterator := newForgeConfigIterator(packs) - return iterator -} - -// enforces no duplicate forgeConfig paths -func (r *ForgeConfigs) Append(newP *ForgeConfig) bool { - forgeConfigsLock.Lock() - defer forgeConfigsLock.Unlock() - - for _, p := range r.ForgeConfigs { - if p.GoPath == newP.GoPath { - return false - } - } - - r.ForgeConfigs = append(r.ForgeConfigs, newP) - return true -} - -// returns time.Duration since last Update() -func (r *ForgeConfig) Age(newP *ForgeConfig) time.Duration { - t := time.Since(r.Verstamp.AsTime()) - return t -} - -// find a forgeConfig by path -func (r *ForgeConfigs) FindByPath(gopath string) *ForgeConfig { - forgeConfigsLock.RLock() - defer forgeConfigsLock.RUnlock() - - for _, p := range r.ForgeConfigs { - if p.GoPath == gopath { - return p - } - } - - return nil -} - -func (r *ForgeConfigs) Len() int { - forgeConfigsLock.RLock() - defer forgeConfigsLock.RUnlock() - - return len(r.ForgeConfigs) -} - -type byForgeConfigPath []*ForgeConfig - -func (a byForgeConfigPath) Len() int { return len(a) } -func (a byForgeConfigPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath } -func (a byForgeConfigPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] } - -func (all *ForgeConfigs) DeleteByPath(gopath string) *ForgeConfig { - forgeConfigsLock.Lock() - defer forgeConfigsLock.Unlock() - - var newr ForgeConfig - - for i, _ := range all.ForgeConfigs { - if all.ForgeConfigs[i].GoPath == gopath { - newr = *all.ForgeConfigs[i] - all.ForgeConfigs[i] = all.ForgeConfigs[len(all.ForgeConfigs)-1] - all.ForgeConfigs = all.ForgeConfigs[:len(all.ForgeConfigs)-1] - return &newr - } - } - return nil -} - -// safely returns a slice of pointers to the ForgeConfig protobufs -func (r *ForgeConfigs) selectAllForgeConfigs() []*ForgeConfig { - forgeConfigsLock.RLock() - defer forgeConfigsLock.RUnlock() - - // Create a new slice to hold pointers to each ForgeConfig - var allPacks []*ForgeConfig - allPacks = make([]*ForgeConfig, len(r.ForgeConfigs)) - for i, p := range r.ForgeConfigs { - allPacks[i] = p // Copy pointers for safe iteration - } - - return allPacks -} diff --git a/forgeConfig/main.go b/forgeConfig/main.go index 6e9fe1a..3ce6c7d 100644 --- a/forgeConfig/main.go +++ b/forgeConfig/main.go @@ -25,7 +25,7 @@ func main() { // try to delete, then save config and exit if argv.Delete { - if oldr := f.Config.DeleteByPath(argv.GoPath); oldr == nil { + if oldr := f.Config.DeleteByGoPath(argv.GoPath); oldr == nil { log.Info("deleted", argv.GoPath, "did not exist. did nothing") os.Exit(0) } diff --git a/settings.go b/settings.go index c024d81..c1a7cb8 100644 --- a/settings.go +++ b/settings.go @@ -15,7 +15,7 @@ import ( ) func (all *ForgeConfigs) UpdateGoPath(name string, gopath string) bool { - oldr := all.DeleteByPath(name) + oldr := all.DeleteByGoPath(name) if oldr == nil { // nothing to update return false