autogenpb marshal.pb.go and sort.pb.go

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 16:22:19 -06:00
parent 3359a44996
commit bdcaba32f8
5 changed files with 15 additions and 151 deletions

View File

@ -5,7 +5,7 @@
# go install
all: refs.pb.go gitTags.pb.go godep.pb.go repos.pb.go vet
all: refs.pb.go gitTags.pb.go godep.pb.go repo.pb.go
make -C scanGoSrc/
vet: lint
@ -44,10 +44,11 @@ godep.pb.go: godep.proto
--go_opt=Mgodep.proto=go.wit.com/lib/protobuf/gitpb \
godep.proto
repos.pb.go: repos.proto
repo.pb.go: repo.proto
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mrefs.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mgodep.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mrepos.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mrepo.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=MgitTags.proto=go.wit.com/lib/protobuf/gitpb \
repos.proto
repo.proto
autogenpb --proto repo.proto --sort "ByPath,GoPath"

View File

@ -23,6 +23,10 @@ message Repo {
bool goPrimitive = 9; // if this is a golang primitive
repeated GitTag gitTags = 10;
string masterBranchName = 11; // git 'main' or 'master' branch name
string develBranchName = 12; // whatever the git 'devel' branch name is
string userBranchName = 13; // whatever your username branch is
}
message Repos {

View File

@ -3,8 +3,8 @@ package gitpb
// delete a gopath:
// myrepos.DeleteByPath("go.wit.com/apps/go-clone")
func (all *Repos) DeleteByPath(gopath string) *Repo {
repolock.Lock()
defer repolock.Unlock()
reposMu.Lock()
defer reposMu.Unlock()
for i, _ := range all.Repos {
if all.Repos[i].GoPath == gopath {
@ -18,8 +18,8 @@ func (all *Repos) DeleteByPath(gopath string) *Repo {
// find a package by gopath
func (all *Repos) FindByGoPath(gopath string) *Repo {
repolock.RLock()
defer repolock.RUnlock()
reposMu.RLock()
defer reposMu.RUnlock()
for _, p := range all.Repos {
if p.GoPath == gopath {
@ -32,8 +32,8 @@ func (all *Repos) FindByGoPath(gopath string) *Repo {
// enforces no duplicate gopath's
func (all *Repos) add(newP *Repo) bool {
repolock.Lock()
defer repolock.Unlock()
reposMu.Lock()
defer reposMu.Unlock()
for _, p := range all.Repos {
if p.GoPath == newP.GoPath {

View File

@ -1,42 +0,0 @@
package gitpb
// todo: autogen this
// functions to import and export the protobuf
import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
// human readable JSON
func (r *Repo) FormatJSON() string {
return protojson.Format(r)
}
// 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()
func (r *Repo) FormatTEXT() string {
return prototext.Format(r)
}
// marshal json
func (r *Repo) MarshalJSON() ([]byte, error) {
return protojson.Marshal(r)
}
// unmarshal
func (r *Repo) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, r)
}
// marshal to wire
func (r *Repo) Marshal() ([]byte, error) {
return proto.Marshal(r)
}
// unmarshal from wire
func (r *Repo) Unmarshal(data []byte) error {
return proto.Unmarshal(data, r)
}

View File

@ -1,99 +0,0 @@
package gitpb
// this is becoming a standard format
// todo: autogenerate this from the .proto file?
import (
"fmt"
"os"
"sort"
sync "sync"
)
// bad global lock until I figure out some other plan
var repolock sync.RWMutex
type RepoIterator struct {
sync.RWMutex
packs []*Repo
index int
}
// NewRepoIterator initializes a new iterator.
func NewRepoIterator(packs []*Repo) *RepoIterator {
return &RepoIterator{packs: packs}
}
// Scan moves to the next element and returns false if there are no more packs.
func (it *RepoIterator) Scan() bool {
if it.index >= len(it.packs) {
return false
}
it.index++
return true
}
// Repo returns the current repo.
func (it *RepoIterator) Next() *Repo {
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("repo == 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.Repo()
// fmt.Println("Repo UUID:", d.Uuid)
// }
func (r *Repos) All() *RepoIterator {
repoPointers := r.selectAllRepo()
iterator := NewRepoIterator(repoPointers)
return iterator
}
func (r *Repos) SortByPath() *RepoIterator {
packs := r.selectAllRepo()
sort.Sort(RepoByPath(packs))
iterator := NewRepoIterator(packs)
return iterator
}
func (all *Repos) Len() int {
repolock.RLock()
defer repolock.RUnlock()
return len(all.Repos)
}
type RepoByPath []*Repo
func (a RepoByPath) Len() int { return len(a) }
func (a RepoByPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
func (a RepoByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the Repo protobufs
func (all *Repos) selectAllRepo() []*Repo {
repolock.RLock()
defer repolock.RUnlock()
// Create a new slice to hold pointers to each Repo
var aRepos []*Repo
aRepos = make([]*Repo, len(all.Repos))
for i, p := range all.Repos {
aRepos[i] = p // Copy pointers for safe iteration
}
return aRepos
}