test works

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 10:20:06 -06:00
parent 766c2e1674
commit fb79d5b17d
7 changed files with 109 additions and 290 deletions

3
.gitignore vendored
View File

@ -1,7 +1,8 @@
*.swp *.swp
*.pb.go
go.mod go.mod
go.sum go.sum
/files/* /files/*
/work/* /work/*
autogenpb autogenpb
test.sort.pb.go testSort/testSort

View File

@ -2,7 +2,8 @@ VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d) BUILDTIME = $(shell date +%Y.%m.%d)
run: clean build run: clean build
make -C testautogen/ make -C testautogen/ all
make -C testSort/
vet: vet:
@GO111MODULE=off go vet @GO111MODULE=off go vet
@ -30,4 +31,6 @@ reset:
reset reset
clean: clean:
-rm autogenpb -rm -f autogenpb
make -C testautogen/ clean
make -C testSort/ clean

117
main.go
View File

@ -6,12 +6,27 @@ import (
"os" "os"
) )
var names map[string]string
func main() { func main() {
f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600) f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
names = make(map[string]string)
names["package"] = "testautogen"
names["lock"] = "gitTagslock"
names["Base"] = "GitTag"
names["Bases"] = "GitTags"
names["base"] = "gitTag"
names["sortBy"] = "ByPath"
names["sortKey"] = "Refname"
header(f, "testautogen") header(f, "testautogen")
syncLock(f, "gitTagslock") syncLock(f, "gitTagslock")
iterTop(f, "GitTag") iterTop(f, "GitTag")
iterNext(f, "GitTag") iterNext(f, "GitTag")
iterSort(f)
iterAppend(f)
iterEnd(f)
} }
func header(w io.Writer, name string) { func header(w io.Writer, name string) {
@ -81,49 +96,71 @@ func iterNext(w io.Writer, name string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
/*
func (r *Repos) All() *RepoIterator { func iterSort(w io.Writer) {
repoPointers := r.selectAllRepo() fmt.Fprintln(w, "func (all *" + names["Bases"] + ") All() *" + names["Base"] + "Iterator {")
fmt.Fprintln(w, " " + names["base"] + "Pointers := all.selectAll" + names["Base"] + "()")
iterator := NewRepoIterator(repoPointers) fmt.Fprintln(w, "")
return iterator fmt.Fprintln(w, " iterator := New" + names["Base"] + "Iterator(" + names["base"] + "Pointers)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Sort" + names["sortBy"] + "() *" + names["Base"] + "Iterator {")
fmt.Fprintln(w, " packs := all.selectAll" + names["Base"] + "()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " sort.Sort(" + names["Base"] + "" + names["sortBy"] + "(packs))")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " iterator := New" + names["Base"] + "Iterator(packs)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Len() int {")
fmt.Fprintln(w, " " + names["lock"] + ".RLock()")
fmt.Fprintln(w, " defer " + names["lock"] + ".RUnlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " return len(all." + names["Bases"] + ")")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
} }
func (r *Repos) SortByPath() *RepoIterator { func iterEnd(w io.Writer) {
packs := r.selectAllRepo() fmt.Fprintln(w, "type " + names["Base"] + "" + names["sortBy"] + " []*" + names["Base"] + "")
fmt.Fprintln(w, "")
sort.Sort(RepoByPath(packs)) fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Len() int { return len(a) }")
fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Less(i, j int) bool { return a[i]." + names["sortKey"] + " < a[j]." + names["sortKey"] + " }")
iterator := NewRepoIterator(packs) fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
return iterator fmt.Fprintln(w, "")
fmt.Fprintln(w, "// safely returns a slice of pointers to the " + names["Base"] + " protobufs")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") selectAll" + names["Base"] + "() []*" + names["Base"] + " {")
fmt.Fprintln(w, " " + names["lock"] + ".RLock()")
fmt.Fprintln(w, " defer " + names["lock"] + ".RUnlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " // Create a new slice to hold pointers to each " + names["Base"] + "")
fmt.Fprintln(w, " var aStuff []*" + names["Base"] + "")
fmt.Fprintln(w, " aStuff = make([]*" + names["Base"] + ", len(all." + names["Bases"] + "))")
fmt.Fprintln(w, " for i, p := range all." + names["Bases"] + " {")
fmt.Fprintln(w, " aStuff[i] = p // Copy pointers for safe iteration")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " return aStuff")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
} }
func (all *Repos) Len() int { func iterAppend(w io.Writer) {
repolock.RLock() fmt.Fprintln(w, "// enforces no duplicate Refname names")
defer repolock.RUnlock() fmt.Fprintln(w, "func (all *GitTags) Append(newP *GitTag) bool {")
fmt.Fprintln(w, " " + names["lock"] + ".Lock()")
return len(all.Repos) fmt.Fprintln(w, " defer " + names["lock"] + ".Unlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range all.GitTags {")
fmt.Fprintln(w, " if p.Refname == newP.Refname {")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " all.GitTags = append(all.GitTags, newP)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
} }
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
}
*/

View File

@ -1,7 +1,7 @@
VERSION = $(shell git describe --tags) VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d) BUILDTIME = $(shell date +%Y.%m.%d)
run: clean test.pb.go build run: clean build
./testSort ./testSort
vet: vet:
@ -31,4 +31,4 @@ reset:
reset reset
clean: clean:
-rm *.pb.go testSort -rm -f *.pb.go testSort

View File

@ -1,7 +1,27 @@
package main package main
import "log" import "log"
import "go.wit.com/apps/autogenpb/testautogen"
func main() { func main() {
log.Println("hello testSort") all := new(testautogen.GitTags)
// newt := new(testautogen.GitTag)
new1 := testautogen.GitTag{
Refname: "master",
Objectname: "blah",
}
all.Append(&new1)
new2 := testautogen.GitTag{
Refname: "devel",
Objectname: "j23jljalse",
}
all.Append(&new2)
loop := all.SortByPath()
for loop.Scan() {
t := loop.Next()
log.Println("hello testSort t.Refname =", t.Refname, t.Objectname)
}
} }

View File

@ -1,6 +1,8 @@
VERSION = $(shell git describe --tags) VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d) BUILDTIME = $(shell date +%Y.%m.%d)
test: vet
all: clean test.pb.go run goimports vet all: clean test.pb.go run goimports vet
run: run:

View File

@ -1,244 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.35.2-devel
// protoc v3.21.12
// source: test.proto
package testautogen
import (
reflect "reflect"
sync "sync"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type GitTag struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Refname string `protobuf:"bytes,1,opt,name=refname,proto3" json:"refname,omitempty"` // tag name. treated as unique
Creatordate *timestamppb.Timestamp `protobuf:"bytes,2,opt,name=creatordate,proto3" json:"creatordate,omitempty"` // git creatordate
Authordate *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=authordate,proto3" json:"authordate,omitempty"` // git creatordate
Objectname string `protobuf:"bytes,4,opt,name=objectname,proto3" json:"objectname,omitempty"` // git hash
Subject string `protobuf:"bytes,5,opt,name=subject,proto3" json:"subject,omitempty"` // git tag subject
}
func (x *GitTag) Reset() {
*x = GitTag{}
mi := &file_test_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GitTag) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GitTag) ProtoMessage() {}
func (x *GitTag) ProtoReflect() protoreflect.Message {
mi := &file_test_proto_msgTypes[0]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GitTag.ProtoReflect.Descriptor instead.
func (*GitTag) Descriptor() ([]byte, []int) {
return file_test_proto_rawDescGZIP(), []int{0}
}
func (x *GitTag) GetRefname() string {
if x != nil {
return x.Refname
}
return ""
}
func (x *GitTag) GetCreatordate() *timestamppb.Timestamp {
if x != nil {
return x.Creatordate
}
return nil
}
func (x *GitTag) GetAuthordate() *timestamppb.Timestamp {
if x != nil {
return x.Authordate
}
return nil
}
func (x *GitTag) GetObjectname() string {
if x != nil {
return x.Objectname
}
return ""
}
func (x *GitTag) GetSubject() string {
if x != nil {
return x.Subject
}
return ""
}
type GitTags struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Uuid string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"` // I guess why not just have this on each file
Version string `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"` // maybe can be used for protobuf schema change violations
GitTags []*GitTag `protobuf:"bytes,3,rep,name=gitTags,proto3" json:"gitTags,omitempty"`
}
func (x *GitTags) Reset() {
*x = GitTags{}
mi := &file_test_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GitTags) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GitTags) ProtoMessage() {}
func (x *GitTags) ProtoReflect() protoreflect.Message {
mi := &file_test_proto_msgTypes[1]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GitTags.ProtoReflect.Descriptor instead.
func (*GitTags) Descriptor() ([]byte, []int) {
return file_test_proto_rawDescGZIP(), []int{1}
}
func (x *GitTags) GetUuid() string {
if x != nil {
return x.Uuid
}
return ""
}
func (x *GitTags) GetVersion() string {
if x != nil {
return x.Version
}
return ""
}
func (x *GitTags) GetGitTags() []*GitTag {
if x != nil {
return x.GitTags
}
return nil
}
var File_test_proto protoreflect.FileDescriptor
var file_test_proto_rawDesc = []byte{
0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x67, 0x69,
0x74, 0x70, 0x62, 0x1a, 0x1f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x22, 0xd6, 0x01, 0x0a, 0x06, 0x47, 0x69, 0x74, 0x54, 0x61, 0x67, 0x12,
0x18, 0x0a, 0x07, 0x72, 0x65, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x07, 0x72, 0x65, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x3c, 0x0a, 0x0b, 0x63, 0x72, 0x65,
0x61, 0x74, 0x6f, 0x72, 0x64, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a,
0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0b, 0x63, 0x72, 0x65, 0x61,
0x74, 0x6f, 0x72, 0x64, 0x61, 0x74, 0x65, 0x12, 0x3a, 0x0a, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x6f,
0x72, 0x64, 0x61, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f,
0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69,
0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x64,
0x61, 0x74, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x6e, 0x61, 0x6d,
0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x6e,
0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x05,
0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x22, 0x60, 0x0a,
0x07, 0x47, 0x69, 0x74, 0x54, 0x61, 0x67, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, 0x69, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07,
0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76,
0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x27, 0x0a, 0x07, 0x67, 0x69, 0x74, 0x54, 0x61, 0x67,
0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x67, 0x69, 0x74, 0x70, 0x62, 0x2e,
0x47, 0x69, 0x74, 0x54, 0x61, 0x67, 0x52, 0x07, 0x67, 0x69, 0x74, 0x54, 0x61, 0x67, 0x73, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_test_proto_rawDescOnce sync.Once
file_test_proto_rawDescData = file_test_proto_rawDesc
)
func file_test_proto_rawDescGZIP() []byte {
file_test_proto_rawDescOnce.Do(func() {
file_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_proto_rawDescData)
})
return file_test_proto_rawDescData
}
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_test_proto_goTypes = []any{
(*GitTag)(nil), // 0: gitpb.GitTag
(*GitTags)(nil), // 1: gitpb.GitTags
(*timestamppb.Timestamp)(nil), // 2: google.protobuf.Timestamp
}
var file_test_proto_depIdxs = []int32{
2, // 0: gitpb.GitTag.creatordate:type_name -> google.protobuf.Timestamp
2, // 1: gitpb.GitTag.authordate:type_name -> google.protobuf.Timestamp
0, // 2: gitpb.GitTags.gitTags:type_name -> gitpb.GitTag
3, // [3:3] is the sub-list for method output_type
3, // [3:3] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension type_name
3, // [3:3] is the sub-list for extension extendee
0, // [0:3] is the sub-list for field type_name
}
func init() { file_test_proto_init() }
func file_test_proto_init() {
if File_test_proto != nil {
return
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_test_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_test_proto_goTypes,
DependencyIndexes: file_test_proto_depIdxs,
MessageInfos: file_test_proto_msgTypes,
}.Build()
File_test_proto = out.File
file_test_proto_rawDesc = nil
file_test_proto_goTypes = nil
file_test_proto_depIdxs = nil
}