blob: bf0c4b4db8388d220fc017f310b761040de4f8df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
// This file was autogenerated with autogenpb v0.5.14 2025-09-28_00:11:07_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 prep
import (
"fmt"
"iter"
"sync"
"google.golang.org/protobuf/proto"
)
// a simple global lock
var autoMu sync.RWMutex
func (x *Autos) fixUuid() {
if x == nil {
return
}
if x.Uuid == "94210ebf-a534-4b33-aadd-2f5e1f56ae38" {
return
}
x.Uuid = "94210ebf-a534-4b33-aadd-2f5e1f56ae38"
x.Version = "v0.0.1 go.wit.com/lib/gui/prep"
}
func NewAutos() *Autos {
x := new(Autos)
x.Uuid = "94210ebf-a534-4b33-aadd-2f5e1f56ae38"
x.Version = "v0.0.1 go.wit.com/lib/gui/prep"
return x
}
// START SORT
// DEFINE THE Autos SCANNER.
// itializes a new scanner.
func newAutosScanner(things []*Autos) *AutosScanner {
return &AutosScanner{things: things}
}
type AutosScanner struct {
sync.Mutex
things []*Autos
index int
}
func (it *AutosScanner) 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 *AutosScanner) Next() *Autos {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in AutosScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// DEFINE THE Auto SCANNER.
// itializes a new scanner.
func newAutoScanner(things []*Auto) *AutoScanner {
return &AutoScanner{things: things}
}
type AutoScanner struct {
sync.Mutex
things []*Auto
index int
}
func (it *AutoScanner) 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 *AutoScanner) Next() *Auto {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in AutoScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// safely returns a slice of pointers to the FRUIT protobufs
func (x *Autos) allAutos() []*Auto {
autoMu.RLock()
defer autoMu.RUnlock()
// Create a new slice to hold pointers to each FRUIT
var tmp []*Auto
tmp = make([]*Auto, len(x.Autos))
for i, p := range x.Autos {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// safely returns a slice of pointers to the Auto protobufs
func (x *Autos) selectAllAutos() []*Auto {
autoMu.RLock()
defer autoMu.RUnlock()
// Create a new slice to hold pointers to each Auto
var tmp []*Auto
tmp = make([]*Auto, len(x.Autos))
for i, p := range x.Autos {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// END SORT
func (x *Autos) Len() int {
autoMu.RLock()
defer autoMu.RUnlock()
return len(x.Autos)
}
// a Append() shortcut (that does Clone() with a mutex) notsure if it really works
func (x *Autos) Append(y *Auto) *Auto {
autoMu.Lock()
defer autoMu.Unlock()
z := proto.Clone(y).(*Auto)
x.Autos = append(x.Autos, z)
return z
}
func (x *Autos) All() *AutoScanner {
AutoPointers := x.selectAllAutos()
scanner := newAutoScanner(AutoPointers)
return scanner
}
// Iterate 'for x := range' syntax using the awesome golang 1.24 'iter'
func (x *Autos) IterAll() iter.Seq[*Auto] {
items := x.selectAllAutos()
// log.Println("Made All() Iter.Seq[] with length", len(items))
return func(yield func(*Auto) bool) {
for _, v := range items {
if !yield(v) {
return
}
}
}
}
func (x *Autos) Delete(y *Auto) bool {
autoMu.Lock()
defer autoMu.Unlock()
for i, _ := range x.Autos {
if x.Autos[i] == y {
x.Autos[i] = x.Autos[len(x.Autos)-1]
x.Autos = x.Autos[:len(x.Autos)-1]
return true
}
}
return false
}
|