-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.go
305 lines (250 loc) · 9.17 KB
/
gallery.go
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package gallery
import (
"errors"
"fmt"
"github.com/modernice/media-entity/image"
"github.com/modernice/media-entity/internal"
"github.com/modernice/media-entity/internal/slicex"
"golang.org/x/exp/slices"
)
var (
// ErrEmptyID is returned when trying to add an [Image] with an empty id to
// a [Stack], or when trying to create a [Stack] with an empty id.
ErrEmptyID = errors.New("empty id")
// ErrDuplicateID returned when trying to add a [Stack] with an ID that
// already exists, or when trying to add an [Image] to a [Stack] that
// already contains an [Image] with the same id.
ErrDuplicateID = errors.New("duplicate id")
// ErrStackNotFound is returned when a [Stack] cannot be found in a gallery.
ErrStackNotFound = errors.New("stack not found in gallery")
// ErrVariantNotFound is returned when an [Image] cannot be found in a [Stack].
ErrVariantNotFound = errors.New("variant not found in stack")
)
// ID is the type constraint for [Stack]s and [Image]s of a gallery.
type ID interface {
comparable
fmt.Stringer
}
// Base provides the core implementation for image galleries.
type Base[StackID, ImageID ID] struct {
DTO[StackID, ImageID]
}
// DTO provides the fields for [*Base].
type DTO[StackID, ImageID ID] struct {
Stacks []Stack[StackID, ImageID] `json:"stacks"`
}
// Stack returns the [Stack] with the given id, or false if no the gallery does
// not contain a [Stack] with the id.
func (dto DTO[StackID, ImageID]) Stack(id StackID) (Stack[StackID, ImageID], bool) {
for _, stack := range dto.Stacks {
if stack.ID == id {
return stack, true
}
}
return zeroStack[StackID, ImageID](), false
}
// New returns a new gallery [*Base] that can be embedded into structs build
// galleries. The ID type of the gallery's stacks is specified by the StackID
// type parameter.
//
// type MyGallery struct {
// *gallery.Base[string]
// }
//
// func NewGallery() *MyGallery {
// return &MyGallery{Base: gallery.New[string]()}
// }
func New[StackID, ImageID ID]() *Base[StackID, ImageID] {
return &Base[StackID, ImageID]{}
}
// NewStack adds a new [Stack] to the gallery. The provided [Image] will
// be the original image of the [Stack], with its [image.Metadata.Original]
// field set to `true`. If the gallery already contains a [Stack] with the same
// id, the [Stack] is not added to the gallery, and an error that satisfies
// errors.Is(err, ErrDuplicateID) is returned. If the provided stack id or the
// provided image id is empty (zero value), an error that satisfies
// errors.Is(err, ErrEmptyID) is returned.
func (g *Base[StackID, ImageID]) NewStack(id StackID, img Image[ImageID]) (Stack[StackID, ImageID], error) {
if id == internal.Zero[StackID]() {
return zeroStack[StackID, ImageID](), fmt.Errorf("stack id: %w", ErrEmptyID)
}
if img.ID == internal.Zero[ImageID]() {
return zeroStack[StackID, ImageID](), fmt.Errorf("image id: %w", ErrEmptyID)
}
// Gallery already contains a Stack with the same id.
if _, ok := g.Stack(id); ok {
return zeroStack[StackID, ImageID](), fmt.Errorf("stack id: %w", ErrDuplicateID)
}
// Force initialize the "Names" and "Descriptions" fields of the image.
img.Image = img.Normalize()
// Mark as the original image.
img.Original = true
stack := Stack[StackID, ImageID]{
ID: id,
Variants: []Image[ImageID]{img},
Tags: make(Tags, 0),
}
g.Stacks = append(g.Stacks, stack)
return stack, nil
}
// RemoveStack removes the [Stack] with the given id from the gallery. If the
// gallery does not contain a [Stack] with the given id, an error that satisfies
// errors.Is(err, ErrStackNotFound) is returned.
func (g *Base[StackID, ImageID]) RemoveStack(id StackID) (Stack[StackID, ImageID], error) {
for i, s := range g.Stacks {
if s.ID == id {
g.Stacks = append(g.Stacks[:i], g.Stacks[i+1:]...)
return s, nil
}
}
return zeroStack[StackID, ImageID](), ErrStackNotFound
}
// NewVariant adds an image as a new variant to the [Stack] with the given id,
// and returns the updated [Stack] that contains the new [Image]. If the gallery
// does not contain a [Stack] with the given id, an error that satisfies
// errors.Is(err, ErrStackNotFound) is returned.
func (g *Base[StackID, ImageID]) NewVariant(stackID StackID, variantID ImageID, img image.Image) (Stack[StackID, ImageID], error) {
stack, ok := g.Stack(stackID)
if !ok {
return zeroStack[StackID, ImageID](), ErrStackNotFound
}
if _, ok := stack.Variant(variantID); ok {
return zeroStack[StackID, ImageID](), fmt.Errorf("variant id: %w", ErrDuplicateID)
}
variant, err := stack.NewVariant(variantID, img)
if err != nil {
return zeroStack[StackID, ImageID](), err
}
stack.Variants = append(stack.Variants, variant)
g.replaceStack(stack.ID, stack)
return stack, nil
}
// RemoveVariant removes a variant from the given [Stack] and returns the
// removed variant. If the gallery does not contain a [Stack] with the given id,
// an error that satisfies errors.Is(err, ErrStackNotFound) is returned. Similarly,
// if the [Stack] does not contain an [Image] with the given ImageID, an error that
// satisfies errors.Is(err, ErrImageNotFound) is returned.
func (g *Base[StackID, ImageID]) RemoveVariant(stackID StackID, imageID ImageID) (Image[ImageID], error) {
stack, ok := g.Stack(stackID)
if !ok {
return zeroImage[ImageID](), ErrStackNotFound
}
img, ok := stack.Image(imageID)
if !ok {
return zeroImage[ImageID](), ErrVariantNotFound
}
stack.Variants = slicex.Filter(stack.Variants, func(v Image[ImageID]) bool {
return v.ID != imageID
})
g.replaceStack(stack.ID, stack)
return img, nil
}
// ReplaceVariant replaces a variant of the [Stack] with the given StackID.
// If the gallery does not contain a [Stack] with the given id, an error that
// satisfies errors.Is(err, ErrStackNotFound) is returned. Similarly, if the
// [Stack] does not contain an [Image] with the same id as the provided [Image],
// an error that satisfies errors.Is(err, ErrImageNotFound) is returned.
func (g *Base[StackID, ImageID]) ReplaceVariant(stackID StackID, variant Image[ImageID]) (Stack[StackID, ImageID], error) {
stack, ok := g.Stack(stackID)
if !ok {
return zeroStack[StackID, ImageID](), ErrStackNotFound
}
variant.Image = variant.Normalize()
for i, img := range stack.Variants {
if img.ID == variant.ID {
stack.Variants[i] = variant
g.replaceStack(stack.ID, stack)
return stack, nil
}
}
return stack, ErrVariantNotFound
}
// Tag adds the given tags to the [Stack] with the given id, and returns the
// updated [Stack]. If the gallery does not contain a [Stack] with the given id,
// an error that satisfies errors.Is(err, ErrStackNotFound) is returned.
func (g *Base[StackID, ImageID]) Tag(stackID StackID, tags ...string) (Stack[StackID, ImageID], error) {
stack, ok := g.Stack(stackID)
if !ok {
return zeroStack[StackID, ImageID](), ErrStackNotFound
}
stack.Tags = stack.Tags.With(tags...)
g.replaceStack(stack.ID, stack)
return stack, nil
}
// Untag removes the given tags from the [Stack] with the given id, and returns
// the updated [Stack]. If the gallery does not contain a [Stack] with the given
// id, an error that satisfies errors.Is(err, ErrStackNotFound) is returned.
func (g *Base[StackID, ImageID]) Untag(stackID StackID, tags ...string) (Stack[StackID, ImageID], error) {
stack, ok := g.Stack(stackID)
if !ok {
return zeroStack[StackID, ImageID](), ErrStackNotFound
}
stack.Tags = stack.Tags.Without(tags...)
g.replaceStack(stack.ID, stack)
return stack, nil
}
// Sort sorts the gallery's stacks by the given sorting order.
func (g *Base[StackID, ImageID]) Sort(sorting []StackID) {
// Filter out invalid stack ids.
sorting = slicex.Filter(sorting, func(id StackID) bool {
return slicex.ContainsFunc(g.Stacks, func(s Stack[StackID, ImageID]) bool {
return s.ID == id
})
})
if len(sorting) == 0 {
return
}
previous := slicex.Map(g.Stacks, func(s Stack[StackID, ImageID]) StackID { return s.ID })
slices.SortFunc(g.Stacks, func(a, b Stack[StackID, ImageID]) int {
idxA := slices.Index(sorting, a.ID)
idxB := slices.Index(sorting, b.ID)
if idxA == -1 && idxB == -1 {
idxA = slices.Index(previous, a.ID)
idxB = slices.Index(previous, b.ID)
}
if idxB < 0 {
return -1
}
if idxA < 0 {
return 1
}
if idxA < idxB {
return -1
}
if idxA > idxB {
return 1
}
return 0
})
}
// Clear removes all stacks from the gallery.
func (g *Base[StackID, ImageID]) Clear() {
g.Stacks = make([]Stack[StackID, ImageID], 0)
}
// DryRun executes the given function and returns the error that is returned by
// that function. Any changes made to the gallery's stacks are reverted before
// returning.
func (g *Base[StackID, ImageID]) DryRun(fn func(*Base[StackID, ImageID]) error) error {
backup := cloneStacks(g.Stacks)
err := fn(g)
g.Stacks = backup
return err
}
func (g *Base[StackID, ImageID]) replaceStack(id StackID, stack Stack[StackID, ImageID]) {
for i, s := range g.Stacks {
if s.ID == id {
g.Stacks[i] = stack
return
}
}
}
func zeroStack[StackID, ImageID ID]() (zero Stack[StackID, ImageID]) {
return zero
}
func cloneStacks[StackID, ImageID ID](stacks []Stack[StackID, ImageID]) []Stack[StackID, ImageID] {
out := make([]Stack[StackID, ImageID], len(stacks))
for i, stack := range stacks {
out[i] = stack.Clone()
}
return out
}