diff --git a/.gitignore b/.gitignore index dcbace9..903d8f2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ *.swp +*.pb.go go.mod go.sum /files/* /work/* autogenpb -test.sort.pb.go +testSort/testSort diff --git a/Makefile b/Makefile index 47d003a..02f0903 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d) run: clean build - make -C testautogen/ + make -C testautogen/ all + make -C testSort/ vet: @GO111MODULE=off go vet @@ -30,4 +31,6 @@ reset: reset clean: - -rm autogenpb + -rm -f autogenpb + make -C testautogen/ clean + make -C testSort/ clean diff --git a/main.go b/main.go index bcf733d..b893c49 100644 --- a/main.go +++ b/main.go @@ -6,12 +6,27 @@ import ( "os" ) +var names map[string]string + func main() { 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") syncLock(f, "gitTagslock") iterTop(f, "GitTag") iterNext(f, "GitTag") + iterSort(f) + iterAppend(f) + iterEnd(f) } func header(w io.Writer, name string) { @@ -81,49 +96,71 @@ func iterNext(w io.Writer, name string) { fmt.Fprintln(w, "") } -/* -func (r *Repos) All() *RepoIterator { - repoPointers := r.selectAllRepo() - - iterator := NewRepoIterator(repoPointers) - return iterator +func iterSort(w io.Writer) { + fmt.Fprintln(w, "func (all *" + names["Bases"] + ") All() *" + names["Base"] + "Iterator {") + fmt.Fprintln(w, " " + names["base"] + "Pointers := all.selectAll" + names["Base"] + "()") + fmt.Fprintln(w, "") + 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 { - packs := r.selectAllRepo() - - sort.Sort(RepoByPath(packs)) - - iterator := NewRepoIterator(packs) - return iterator +func iterEnd(w io.Writer) { + fmt.Fprintln(w, "type " + names["Base"] + "" + names["sortBy"] + " []*" + names["Base"] + "") + fmt.Fprintln(w, "") + 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"] + " }") + fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Swap(i, j int) { a[i], a[j] = a[j], a[i] }") + 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 { - repolock.RLock() - defer repolock.RUnlock() - - return len(all.Repos) +func iterAppend(w io.Writer) { + fmt.Fprintln(w, "// enforces no duplicate Refname names") + fmt.Fprintln(w, "func (all *GitTags) Append(newP *GitTag) bool {") + fmt.Fprintln(w, " " + names["lock"] + ".Lock()") + 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 -} -*/ diff --git a/testSort/Makefile b/testSort/Makefile index 50afd9d..88a8320 100644 --- a/testSort/Makefile +++ b/testSort/Makefile @@ -1,7 +1,7 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d) -run: clean test.pb.go build +run: clean build ./testSort vet: @@ -31,4 +31,4 @@ reset: reset clean: - -rm *.pb.go testSort + -rm -f *.pb.go testSort diff --git a/testSort/main.go b/testSort/main.go index 01a7c39..332bdf7 100644 --- a/testSort/main.go +++ b/testSort/main.go @@ -1,7 +1,27 @@ package main import "log" +import "go.wit.com/apps/autogenpb/testautogen" 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) + } } diff --git a/testautogen/Makefile b/testautogen/Makefile index bad3d3f..749686c 100644 --- a/testautogen/Makefile +++ b/testautogen/Makefile @@ -1,6 +1,8 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d) +test: vet + all: clean test.pb.go run goimports vet run: diff --git a/testautogen/test.pb.go b/testautogen/test.pb.go deleted file mode 100644 index e10cafe..0000000 --- a/testautogen/test.pb.go +++ /dev/null @@ -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 -}