diff --git a/bug.marshal.pb.go b/bug.marshal.pb.go new file mode 100644 index 0000000..4295ffe --- /dev/null +++ b/bug.marshal.pb.go @@ -0,0 +1,56 @@ +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.69-2-g626b737 2025-05-23_19:46:37_UTC +// go install go.wit.com/apps/autogenpb@latest +// +// define which structs (messages) you want to use in the .proto file +// Then sort.pb.go and marshal.pb.go files are autogenerated +// +// autogenpb uses it and has an example .proto file with instructions +// + +package bugpb + +import ( + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/encoding/prototext" + "google.golang.org/protobuf/proto" +) + +// human readable JSON +func (v *Bugs) FormatJSON() string { + return protojson.Format(v) +} + +// marshal json +func (v *Bugs) MarshalJSON() ([]byte, error) { + return protojson.Marshal(v) +} + +// unmarshal json +func (v *Bugs) UnmarshalJSON(data []byte) error { + return protojson.Unmarshal(data, v) +} + +// apparently this isn't stable, but it's awesomely better +// https://protobuf.dev/reference/go/faq/#unstable-text +// it's brilliant for config files! +func (v *Bugs) FormatTEXT() string { + v.fixUuid() + return prototext.Format(v) +} + +// unmarshalTEXT. This reads the .text config file back in after the user edits it +func (v *Bugs) UnmarshalTEXT(data []byte) error { + return prototext.Unmarshal(data, v) +} + +// marshal to wire. This is called winning. +func (v *Bugs) Marshal() ([]byte, error) { + v.fixUuid() + return proto.Marshal(v) +} + +// unmarshal from wire. You have won. +func (v *Bugs) Unmarshal(data []byte) error { + return proto.Unmarshal(data, v) +} diff --git a/bug.pb.go b/bug.pb.go new file mode 100644 index 0000000..5225dbd --- /dev/null +++ b/bug.pb.go @@ -0,0 +1,249 @@ +// Code modified by go.wit.com/apps/autogenpb DO NOT EDIT. +// +// user defined Mutex locks were auto added +// +// autogenpb version & build time: v0.0.69-2-g626b737 2025-05-23_19:46:37_UTC +// autogenpb auto generates Sort(), Unique() and Marshal() functions +// go install go.wit.com/apps/autogenpb@latest + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.33.0 +// protoc v3.21.12 +// source: bug.proto + +// protobufs for git-bug + +package bugpb // autogenpb changed the package name + +import ( + reflect "reflect" + sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + _ "google.golang.org/protobuf/types/known/anypb" +) + +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 Bug struct { + sync.RWMutex // auto-added by go.wit.com/apps/autogenpb + + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Uuid string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *Bug) Reset() { + *x = Bug{} + if protoimpl.UnsafeEnabled { + mi := &file_bug_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Bug) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Bug) ProtoMessage() {} + +func (x *Bug) ProtoReflect() protoreflect.Message { + mi := &file_bug_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Bug.ProtoReflect.Descriptor instead. +func (*Bug) Descriptor() ([]byte, []int) { + return file_bug_proto_rawDescGZIP(), []int{0} +} + +func (x *Bug) GetUuid() string { + if x != nil { + return x.Uuid + } + return "" +} + +func (x *Bug) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +type Bugs struct { + sync.RWMutex // auto-added by go.wit.com/apps/autogenpb + + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Uuid string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"` // `autogenpb:uuid:cdf6711e-f948-4376-9e72-cf1912a96cee` + Version string `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"` // `autogenpb:version:v1` + Bugs []*Bug `protobuf:"bytes,3,rep,name=Bugs,proto3" json:"Bugs,omitempty"` // this never gets used, but it's not worth violating the autogenpb standard +} + +func (x *Bugs) Reset() { + *x = Bugs{} + if protoimpl.UnsafeEnabled { + mi := &file_bug_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Bugs) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Bugs) ProtoMessage() {} + +func (x *Bugs) ProtoReflect() protoreflect.Message { + mi := &file_bug_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Bugs.ProtoReflect.Descriptor instead. +func (*Bugs) Descriptor() ([]byte, []int) { + return file_bug_proto_rawDescGZIP(), []int{1} +} + +func (x *Bugs) GetUuid() string { + if x != nil { + return x.Uuid + } + return "" +} + +func (x *Bugs) GetVersion() string { + if x != nil { + return x.Version + } + return "" +} + +func (x *Bugs) GetBugs() []*Bug { + if x != nil { + return x.Bugs + } + return nil +} + +var File_bug_proto protoreflect.FileDescriptor + +var file_bug_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x62, 0x75, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x62, 0x75, 0x67, + 0x70, 0x62, 0x1a, 0x19, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x75, 0x66, 0x2f, 0x61, 0x6e, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2d, 0x0a, + 0x03, 0x42, 0x75, 0x67, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x54, 0x0a, 0x04, + 0x42, 0x75, 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, 0x1e, 0x0a, 0x04, 0x42, 0x75, 0x67, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x0a, 0x2e, 0x62, 0x75, 0x67, 0x70, 0x62, 0x2e, 0x42, 0x75, 0x67, 0x52, 0x04, 0x42, 0x75, + 0x67, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_bug_proto_rawDescOnce sync.Once + file_bug_proto_rawDescData = file_bug_proto_rawDesc +) + +func file_bug_proto_rawDescGZIP() []byte { + file_bug_proto_rawDescOnce.Do(func() { + file_bug_proto_rawDescData = protoimpl.X.CompressGZIP(file_bug_proto_rawDescData) + }) + return file_bug_proto_rawDescData +} + +var file_bug_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_bug_proto_goTypes = []interface{}{ + (*Bug)(nil), // 0: bugpb.Bug + (*Bugs)(nil), // 1: bugpb.Bugs +} +var file_bug_proto_depIdxs = []int32{ + 0, // 0: bugpb.Bugs.Bugs:type_name -> bugpb.Bug + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_bug_proto_init() } +func file_bug_proto_init() { + if File_bug_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_bug_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Bug); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_bug_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Bugs); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_bug_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_bug_proto_goTypes, + DependencyIndexes: file_bug_proto_depIdxs, + MessageInfos: file_bug_proto_msgTypes, + }.Build() + File_bug_proto = out.File + file_bug_proto_rawDesc = nil + file_bug_proto_goTypes = nil + file_bug_proto_depIdxs = nil +} diff --git a/bug.sort.pb.go b/bug.sort.pb.go new file mode 100644 index 0000000..a3f1269 --- /dev/null +++ b/bug.sort.pb.go @@ -0,0 +1,186 @@ +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.69-2-g626b737 2025-05-23_19:46:37_UTC +// go install go.wit.com/apps/autogenpb@latest +// +// define which structs (messages) you want to use in the .proto file +// Then sort.pb.go and marshal.pb.go files are autogenerated +// +// autogenpb uses it and has an example .proto file with instructions +// + +package bugpb + +import ( + "fmt" + "iter" + "sync" +) + +// a simple global lock +var bugMu sync.RWMutex + +func (x *Bugs) fixUuid() { + if x == nil { + return + } + if x.Uuid == "cdf6711e-f948-4376-9e72-cf1912a96cee" { + return + } + x.Uuid = "cdf6711e-f948-4376-9e72-cf1912a96cee" + x.Version = "v1 go.wit.com/lib/protobuf/bugpb" +} + +func NewBugs() *Bugs { + x := new(Bugs) + x.Uuid = "cdf6711e-f948-4376-9e72-cf1912a96cee" + x.Version = "v1 go.wit.com/lib/protobuf/bugpb" + return x +} + +// START SORT + +// DEFINE THE Bugs SCANNER. +// itializes a new scanner. +func newBugsScanner(things []*Bugs) *BugsScanner { + return &BugsScanner{things: things} +} + +type BugsScanner struct { + sync.Mutex + + things []*Bugs + index int +} + +func (it *BugsScanner) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.Lock() + it.index++ + it.Unlock() + return true +} + +// Next() returns the next thing in the array +func (it *BugsScanner) Next() *Bugs { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in BugsScanner", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE SCANNER + +// DEFINE THE Bug SCANNER. +// itializes a new scanner. +func newBugScanner(things []*Bug) *BugScanner { + return &BugScanner{things: things} +} + +type BugScanner struct { + sync.Mutex + + things []*Bug + index int +} + +func (it *BugScanner) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.Lock() + it.index++ + it.Unlock() + return true +} + +// Next() returns the next thing in the array +func (it *BugScanner) Next() *Bug { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in BugScanner", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE SCANNER + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *Bugs) allBugs() []*Bug { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*Bug + tmp = make([]*Bug, len(x.Bugs)) + for i, p := range x.Bugs { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the Bug protobufs +func (x *Bugs) selectAllBugs() []*Bug { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each Bug + var tmp []*Bug + tmp = make([]*Bug, len(x.Bugs)) + for i, p := range x.Bugs { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// END SORT + +func (x *Bugs) Len() int { + x.RLock() + defer x.RUnlock() + + return len(x.Bugs) +} + +// just a simple Append() shortcut (but still uses the mutex lock) +func (x *Bugs) Append(y *Bug) { + x.Lock() + defer x.Unlock() + + x.Bugs = append(x.Bugs, y) +} + +func (x *Bugs) All() *BugScanner { + BugPointers := x.selectAllBugs() + + scanner := newBugScanner(BugPointers) + return scanner +} + +// Iterate 'for x := range' syntax using the awesome golang 1.24 'iter' +func (x *Bugs) IterAll() iter.Seq[*Bug] { + items := x.selectAllBugs() + // log.Println("Made All() Iter.Seq[] with length", len(items)) + return func(yield func(*Bug) bool) { + for _, v := range items { + if !yield(v) { + return + } + } + } +} +func (x *Bugs) Delete(y *Bug) bool { + x.Lock() + defer x.Unlock() + + for i, _ := range x.Bugs { + if x.Bugs[i] == y { + x.Bugs[i] = x.Bugs[len(x.Bugs)-1] + x.Bugs = x.Bugs[:len(x.Bugs)-1] + return true + } + } + return false +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e69b74d --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go.wit.com/lib/protobuf/bugpb + +go 1.24.1 + +require google.golang.org/protobuf v1.36.6 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4665b5a --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= +google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=