Skip to content

Commit

Permalink
Optimises Bundle.MediaType()
Browse files Browse the repository at this point in the history
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 <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Sep 14, 2023
1 parent ca95325 commit 4543bea
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/catalogmetadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4543bea

Please sign in to comment.