Skip to content

Commit dfcd4b7

Browse files
committed
bundles: make some functions BundleList methods
Make 'addBundleToList()' (renamed 'addBundle()') and 'getSortedCreationTokens()' (renamed 'sortedCreationTokens()') methods of the 'BundleList' struct rather than standalone functions. Both of these functions are more appropriately represented as modifiers/accessors of the bundle list, rather than an external computation on a bundle list. Signed-off-by: Victoria Dye <[email protected]>
1 parent 5d94036 commit dfcd4b7

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

internal/bundles/bundles.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,21 @@ type BundleList struct {
3939
Bundles map[int64]Bundle
4040
}
4141

42-
func addBundleToList(bundle Bundle, list *BundleList) {
42+
func (list *BundleList) addBundle(bundle Bundle) {
4343
list.Bundles[bundle.CreationToken] = bundle
4444
}
4545

46+
func (list *BundleList) sortedCreationTokens() []int64 {
47+
keys := make([]int64, 0, len(list.Bundles))
48+
for timestamp := range list.Bundles {
49+
keys = append(keys, timestamp)
50+
}
51+
52+
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
53+
54+
return keys
55+
}
56+
4657
type BundleProvider interface {
4758
CreateInitialBundle(ctx context.Context, repo *core.Repository) Bundle
4859
CreateIncrementalBundle(ctx context.Context, repo *core.Repository, list *BundleList) (*Bundle, error)
@@ -79,7 +90,7 @@ func (b *bundleProvider) CreateInitialBundle(ctx context.Context, repo *core.Rep
7990
func (b *bundleProvider) createDistinctBundle(repo *core.Repository, list *BundleList) Bundle {
8091
timestamp := time.Now().UTC().Unix()
8192

82-
keys := b.getSortedCreationTokens(list)
93+
keys := list.sortedCreationTokens()
8394

8495
maxTimestamp := keys[len(keys)-1]
8596
if timestamp <= maxTimestamp {
@@ -100,7 +111,7 @@ func (b *bundleProvider) createDistinctBundle(repo *core.Repository, list *Bundl
100111
func (b *bundleProvider) CreateSingletonList(ctx context.Context, bundle Bundle) *BundleList {
101112
list := BundleList{1, "all", make(map[int64]Bundle)}
102113

103-
addBundleToList(bundle, &list)
114+
list.addBundle(bundle)
104115

105116
return &list
106117
}
@@ -122,7 +133,7 @@ func (b *bundleProvider) WriteBundleList(ctx context.Context, list *BundleList,
122133
out, "[bundle]\n\tversion = %d\n\tmode = %s\n\n",
123134
list.Version, list.Mode)
124135

125-
keys := b.getSortedCreationTokens(list)
136+
keys := list.sortedCreationTokens()
126137

127138
for _, token := range keys {
128139
bundle := list.Bundles[token]
@@ -298,7 +309,7 @@ func (b *bundleProvider) CollapseList(ctx context.Context, repo *core.Repository
298309
return nil
299310
}
300311

301-
keys := b.getSortedCreationTokens(list)
312+
keys := list.sortedCreationTokens()
302313

303314
refs := make(map[string]string)
304315

@@ -349,14 +360,3 @@ func (b *bundleProvider) CollapseList(ctx context.Context, repo *core.Repository
349360
list.Bundles[maxTimestamp] = bundle
350361
return nil
351362
}
352-
353-
func (b *bundleProvider) getSortedCreationTokens(list *BundleList) []int64 {
354-
keys := make([]int64, 0, len(list.Bundles))
355-
for timestamp := range list.Bundles {
356-
keys = append(keys, timestamp)
357-
}
358-
359-
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
360-
361-
return keys
362-
}

0 commit comments

Comments
 (0)