Splits up blob create options definitions to be package-specific

Redefines privately in both storage and client packages

Signed-off-by: Brian Bland <brian.bland@docker.com>
This commit is contained in:
Brian Bland 2016-01-14 10:08:52 -08:00
parent e90578b27d
commit 67aef89bc0
3 changed files with 37 additions and 8 deletions

View file

@ -18,7 +18,6 @@ import (
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/docker/distribution/registry/api/v2" "github.com/docker/distribution/registry/api/v2"
"github.com/docker/distribution/registry/client/transport" "github.com/docker/distribution/registry/client/transport"
"github.com/docker/distribution/registry/storage"
"github.com/docker/distribution/registry/storage/cache" "github.com/docker/distribution/registry/storage/cache"
"github.com/docker/distribution/registry/storage/cache/memory" "github.com/docker/distribution/registry/storage/cache/memory"
) )
@ -573,8 +572,39 @@ func (bs *blobs) Put(ctx context.Context, mediaType string, p []byte) (distribut
return writer.Commit(ctx, desc) return writer.Commit(ctx, desc)
} }
// createOptions is a collection of blob creation modifiers relevant to general
// blob storage intended to be configured by the BlobCreateOption.Apply method.
type createOptions struct {
Mount struct {
ShouldMount bool
From reference.Canonical
}
}
type optionFunc func(interface{}) error
func (f optionFunc) Apply(v interface{}) error {
return f(v)
}
// WithMountFrom returns a BlobCreateOption which designates that the blob should be
// mounted from the given canonical reference.
func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption {
return optionFunc(func(v interface{}) error {
opts, ok := v.(*createOptions)
if !ok {
return fmt.Errorf("unexpected options type: %T", v)
}
opts.Mount.ShouldMount = true
opts.Mount.From = ref
return nil
})
}
func (bs *blobs) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) { func (bs *blobs) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) {
var opts storage.CreateOptions var opts createOptions
for _, option := range options { for _, option := range options {
err := option.Apply(&opts) err := option.Apply(&opts)

View file

@ -20,7 +20,6 @@ import (
"github.com/docker/distribution/manifest/schema1" "github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/docker/distribution/registry/api/errcode" "github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/storage"
"github.com/docker/distribution/testutil" "github.com/docker/distribution/testutil"
"github.com/docker/distribution/uuid" "github.com/docker/distribution/uuid"
"github.com/docker/libtrust" "github.com/docker/libtrust"
@ -523,7 +522,7 @@ func TestBlobMount(t *testing.T) {
l := r.Blobs(ctx) l := r.Blobs(ctx)
bw, err := l.Create(ctx, storage.WithMountFrom(canonicalRef)) bw, err := l.Create(ctx, WithMountFrom(canonicalRef))
if bw != nil { if bw != nil {
t.Fatalf("Expected blob writer to be nil, was %v", bw) t.Fatalf("Expected blob writer to be nil, was %v", bw)
} }

View file

@ -97,9 +97,9 @@ func (lbs *linkedBlobStore) Put(ctx context.Context, mediaType string, p []byte)
return desc, lbs.linkBlob(ctx, desc) return desc, lbs.linkBlob(ctx, desc)
} }
// CreateOptions is a collection of blob creation modifiers relevant to general // createOptions is a collection of blob creation modifiers relevant to general
// blob storage intended to be configured by the BlobCreateOption.Apply method. // blob storage intended to be configured by the BlobCreateOption.Apply method.
type CreateOptions struct { type createOptions struct {
Mount struct { Mount struct {
ShouldMount bool ShouldMount bool
From reference.Canonical From reference.Canonical
@ -116,7 +116,7 @@ func (f optionFunc) Apply(v interface{}) error {
// mounted from the given canonical reference. // mounted from the given canonical reference.
func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption { func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption {
return optionFunc(func(v interface{}) error { return optionFunc(func(v interface{}) error {
opts, ok := v.(*CreateOptions) opts, ok := v.(*createOptions)
if !ok { if !ok {
return fmt.Errorf("unexpected options type: %T", v) return fmt.Errorf("unexpected options type: %T", v)
} }
@ -132,7 +132,7 @@ func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption {
func (lbs *linkedBlobStore) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) { func (lbs *linkedBlobStore) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) {
context.GetLogger(ctx).Debug("(*linkedBlobStore).Writer") context.GetLogger(ctx).Debug("(*linkedBlobStore).Writer")
var opts CreateOptions var opts createOptions
for _, option := range options { for _, option := range options {
err := option.Apply(&opts) err := option.Apply(&opts)