Add clean export function for updater.Resource

This commit is contained in:
Daniel 2022-06-21 16:57:37 +02:00
parent 38d3c839ef
commit 8421b8fba8
2 changed files with 22 additions and 3 deletions

View file

@ -1,7 +1,6 @@
package updater
// Export exports the list of resources. All resources must be
// locked when accessed.
// Export exports the list of resources.
func (reg *ResourceRegistry) Export() map[string]*Resource {
reg.RLock()
defer reg.RUnlock()
@ -9,7 +8,7 @@ func (reg *ResourceRegistry) Export() map[string]*Resource {
// copy the map
copiedResources := make(map[string]*Resource)
for key, val := range reg.resources {
copiedResources[key] = val
copiedResources[key] = val.Export()
}
return copiedResources

View file

@ -117,6 +117,26 @@ func (rv *ResourceVersion) isBetaVersionNumber() bool { //nolint:unused
}
}
// Export makes a copy of the resource with only the exposed information.
func (res *Resource) Export() *Resource {
res.Lock()
defer res.Unlock()
// Copy attibutes.
export := &Resource{
Identifier: res.Identifier,
Versions: make([]*ResourceVersion, len(res.Versions)),
ActiveVersion: res.ActiveVersion,
SelectedVersion: res.SelectedVersion,
}
// Copy Versions slice.
for i := 0; i < len(res.Versions); i++ {
export.Versions[i] = res.Versions[i]
}
return export
}
// Len is the number of elements in the collection.
// It implements sort.Interface for ResourceVersion.
func (res *Resource) Len() int {