From c8e134433692e5f9d852f2a8f50aa47b7a7a1229 Mon Sep 17 00:00:00 2001 From: Mikalai Radchuk Date: Thu, 14 Sep 2023 10:31:25 +0100 Subject: [PATCH] Optimises `Bundle.MediaType()` Media type is an optional field and can be an empty string after looading. This change avoids unmarshalling every time we call MediaType() method by comparing to `nil`. If value is `nil` - we need to load. If anything else (even an empty string) we already lazy-loaded the value. Signed-off-by: Mikalai Radchuk --- internal/catalogmetadata/types.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/catalogmetadata/types.go b/internal/catalogmetadata/types.go index dacca4d03..eeb836661 100644 --- a/internal/catalogmetadata/types.go +++ b/internal/catalogmetadata/types.go @@ -45,7 +45,7 @@ type Bundle struct { bundlePackage *property.Package semVersion *bsemver.Version requiredPackages []PackageRequired - mediaType string + mediaType *string } func (b *Bundle) Version() (*bsemver.Version, error) { @@ -67,7 +67,7 @@ func (b *Bundle) MediaType() (string, error) { return "", err } - return b.mediaType, nil + return *b.mediaType, nil } func (b *Bundle) loadPackage() error { @@ -118,12 +118,12 @@ func (b *Bundle) loadRequiredPackages() error { func (b *Bundle) loadMediaType() error { b.mu.Lock() defer b.mu.Unlock() - if b.mediaType == "" { + if b.mediaType == nil { mediaType, err := loadFromProps[string](b, PropertyBundleMediaType, false) if err != nil { return fmt.Errorf("error determining bundle mediatype for bundle %q: %s", b.Name, err) } - b.mediaType = mediaType + b.mediaType = &mediaType } return nil }