Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 11:08:11 -06:00
parent 6e29eb7862
commit 28daefa6c5
4 changed files with 111 additions and 213 deletions

46
argv.go Normal file
View File

@ -0,0 +1,46 @@
package main
/*
this parses the command line arguements
this enables command line options from other packages like 'gui' and 'log'
*/
var argv args
type args struct {
Base string `arg:"--base" help:"basename"`
Proto string `arg:"--proto" help:"the .proto filename"`
}
func (a args) Description() string {
return `
autogenpb tries to generate a sort.pb.go file for a protobuf
Install with:
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
`
}
func (args) Version() string {
return "go-clone " + VERSION + " Built on " + BUILDTIME
}

View File

@ -1,174 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.35.2-devel
// protoc v3.21.12
// source: gitTags.proto
package autogen
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
reflect "reflect"
sync "sync"
)
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_gitTags_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_gitTags_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_gitTags_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 ""
}
var File_gitTags_proto protoreflect.FileDescriptor
var file_gitTags_proto_rawDesc = []byte{
0x0a, 0x0d, 0x67, 0x69, 0x74, 0x54, 0x61, 0x67, 0x73, 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,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_gitTags_proto_rawDescOnce sync.Once
file_gitTags_proto_rawDescData = file_gitTags_proto_rawDesc
)
func file_gitTags_proto_rawDescGZIP() []byte {
file_gitTags_proto_rawDescOnce.Do(func() {
file_gitTags_proto_rawDescData = protoimpl.X.CompressGZIP(file_gitTags_proto_rawDescData)
})
return file_gitTags_proto_rawDescData
}
var file_gitTags_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_gitTags_proto_goTypes = []any{
(*GitTag)(nil), // 0: gitpb.GitTag
(*timestamppb.Timestamp)(nil), // 1: google.protobuf.Timestamp
}
var file_gitTags_proto_depIdxs = []int32{
1, // 0: gitpb.GitTag.creatordate:type_name -> google.protobuf.Timestamp
1, // 1: gitpb.GitTag.authordate:type_name -> google.protobuf.Timestamp
2, // [2:2] is the sub-list for method output_type
2, // [2:2] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
}
func init() { file_gitTags_proto_init() }
func file_gitTags_proto_init() {
if File_gitTags_proto != nil {
return
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_gitTags_proto_rawDesc,
NumEnums: 0,
NumMessages: 1,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_gitTags_proto_goTypes,
DependencyIndexes: file_gitTags_proto_depIdxs,
MessageInfos: file_gitTags_proto_msgTypes,
}.Build()
File_gitTags_proto = out.File
file_gitTags_proto_rawDesc = nil
file_gitTags_proto_goTypes = nil
file_gitTags_proto_depIdxs = nil
}

93
main.go
View File

@ -4,19 +4,43 @@ import (
"fmt"
"io"
"os"
"strings"
"github.com/alexflint/go-arg"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
// sent via -ldflags
var VERSION string
var BUILDTIME string
func main() {
pp := arg.MustParse(&argv)
// for very new users or users unfamilar with the command line, this may help them
if argv.Base == "help" || argv.Base == "?" {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
cmd := []string{"go", "list", "-f", "'{{.Name}}'"}
result := shell.Run(cmd)
packageName := strings.Join(result.Stdout, "\n")
packageName = strings.TrimSpace(packageName)
packageName = strings.Trim(packageName, "'")
log.Info("packageName == ", packageName)
f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
sortmap := make(map[string]string)
sortmap["package"] = "testautogen"
sortmap["lock"] = "gitTagslock"
sortmap["Base"] = "GitTag"
sortmap["Bases"] = "GitTags"
sortmap["package"] = packageName
sortmap["base"] = "gitTag"
sortmap["lock"] = sortmap["base"] + "slock"
sortmap["Base"] = "GitTag"
sortmap["Bases"] = sortmap["Base"] + "s"
sortmap["sortBy"] = "ByPath"
sortmap["sortKey"] = "Refname"
@ -82,7 +106,7 @@ func iterTop(w io.Writer, names map[string]string) {
func iterNext(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// Next() returns the next thing in the array")
fmt.Fprintln(w, "func (it *" + names["Base"] + "Iterator) Next() *" + names["Base"] + " {")
fmt.Fprintln(w, "func (it *"+names["Base"]+"Iterator) Next() *"+names["Base"]+" {")
fmt.Fprintln(w, " if it.packs[it.index-1] == nil {")
fmt.Fprintln(w, " for i, d := range it.packs {")
fmt.Fprintln(w, " fmt.Println(\"i =\", i, d)")
@ -96,49 +120,48 @@ func iterNext(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "")
}
func iterSort(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") All() *" + names["Base"] + "Iterator {")
fmt.Fprintln(w, " " + names["base"] + "Pointers := all.selectAll" + names["Base"] + "()")
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, " 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, "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, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(packs))")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " iterator := New" + names["Base"] + "Iterator(packs)")
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, "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, " return len(all."+names["Bases"]+")")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterEnd(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "type " + names["Base"] + "" + names["sortBy"] + " []*" + names["Base"] + "")
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, "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, "// 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, " // 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, "")
@ -149,17 +172,17 @@ func iterEnd(w io.Writer, names map[string]string) {
func iterAppend(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// enforces no duplicate Refname names")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Append(newP *" + names["Base"] + ") bool {")
fmt.Fprintln(w, " " + names["lock"] + ".Lock()")
fmt.Fprintln(w, " defer " + names["lock"] + ".Unlock()")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Append(newP *"+names["Base"]+") bool {")
fmt.Fprintln(w, " "+names["lock"]+".Lock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range all." + names["Bases"] + " {")
fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {")
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." + names["Bases"] + " = append(all." + names["Bases"] + ", newP)")
fmt.Fprintln(w, " all."+names["Bases"]+" = append(all."+names["Bases"]+", newP)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")

View File

@ -1,7 +1,10 @@
package main
import "log"
import "go.wit.com/apps/autogenpb/testautogen"
import (
"log"
"go.wit.com/apps/autogenpb/testautogen"
)
func main() {
all := new(testautogen.GitTags)