Skip to content

Commit

Permalink
fix: added a custom error type for getContent's use
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle M. Tarplee <[email protected]>
  • Loading branch information
ktarplee committed Dec 29, 2023
1 parent 6f0c1f0 commit 4becd16
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (opts *CopyGraphOptions) WithMount(sourceRepository string, mounter registr

// To avoid needing a content.Fetcher as an input argument we simply fall back to the default behavior
// as if getContent was nil
return nil, errdef.ErrUnsupported
return nil, registry.UseSourceRepository
}

// Mount or copy
Expand Down
5 changes: 3 additions & 2 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"oras.land/oras-go/v2/internal/cas"
"oras.land/oras-go/v2/internal/docker"
"oras.land/oras-go/v2/internal/spec"
"oras.land/oras-go/v2/registry"
)

// storageTracker tracks storage API counts.
Expand Down Expand Up @@ -1549,8 +1550,8 @@ func TestCopyGraph_WithOptions(t *testing.T) {
}

_, err := getContent()
if !errors.Is(err, errdef.ErrUnsupported) {
t.Fatalf("Expected error %v", errdef.ErrUnsupported)
if !errors.Is(err, registry.UseSourceRepository) {
t.Fatalf("Expected error %v", registry.UseSourceRepository)
}
rc, err := src.Fetch(ctx, desc)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion registry/remote/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ func (s *blobStore) Mount(ctx context.Context, desc ocispec.Descriptor, fromRepo
var r io.ReadCloser
if getContent != nil {
r, err = getContent()
if errors.Is(err, errdef.ErrUnsupported) {
if errors.Is(err, registry.UseSourceRepository) {
// getContent can return a ErrUnsupported to fallback to the default copy operation
r, err = s.sibling(fromRepo).Fetch(ctx, desc)
}
Expand Down
4 changes: 2 additions & 2 deletions registry/remote/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ func TestRepository_Mount_Fallback(t *testing.T) {
}
})

t.Run("getContent is ErrUnsupported", func(t *testing.T) {
t.Run("getContent is UseSourceRepository", func(t *testing.T) {
sequence = ""

err = repo.Mount(ctx, blobDesc, "test", func() (io.ReadCloser, error) {
return nil, errdef.ErrUnsupported
return nil, registry.UseSourceRepository
})
if err != nil {
t.Fatalf("Repository.Push() error = %v", err)
Expand Down
5 changes: 5 additions & 0 deletions registry/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package registry
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -128,6 +129,10 @@ type Mounter interface {
) error
}

// UseSourceRepository signals to Mount implementations that the content from the source repository should be used. This may be returned from the getContent function of the Mounter.Mount method.
// This is useful to avoid having to know that source repository in the definition of getContent.
var UseSourceRepository = errors.New("use source repository")

// Tags lists the tags available in the repository.
func Tags(ctx context.Context, repo TagLister) ([]string, error) {
var res []string
Expand Down

0 comments on commit 4becd16

Please sign in to comment.