diff --git a/dir/dir.go b/dir/dir.go index 2a9fcb8..a351162 100644 --- a/dir/dir.go +++ b/dir/dir.go @@ -176,8 +176,11 @@ func (engine *Engine) Digests(ctx context.Context, algorithm digest.Algorithm, p } // Put implements Writer.Put. -func (engine *Engine) Put(ctx context.Context, reader io.Reader) (dig digest.Digest, err error) { - digester := engine.Algorithm.Digester() +func (engine *Engine) Put(ctx context.Context, algorithm digest.Algorithm, reader io.Reader) (dig digest.Digest, err error) { + if algorithm.String() == "" { + algorithm = engine.Algorithm + } + digester := algorithm.Digester() file, err := ioutil.TempFile(engine.temp, "blob-") if err != nil { diff --git a/dir/dir_test.go b/dir/dir_test.go index d2ec9d3..80cf68a 100644 --- a/dir/dir_test.go +++ b/dir/dir_test.go @@ -85,15 +85,10 @@ func TestGood(t *testing.T) { } defer engine.Close(ctx) - dirEngine, ok := engine.(*Engine) - if !ok { - t.Fatalf("template.New() did not return a *template.Engine") - } - var digestSha256 digest.Digest bodyIn := "Hello, World!" t.Run("put default algorithm", func(t *testing.T) { - digestSha256, err = engine.Put(ctx, strings.NewReader(bodyIn)) + digestSha256, err = engine.Put(ctx, "", strings.NewReader(bodyIn)) if err != nil { t.Fatal(err) } @@ -107,12 +102,7 @@ func TestGood(t *testing.T) { var digestSha512 digest.Digest t.Run("put SHA-512", func(t *testing.T) { - dirEngine.Algorithm = digest.SHA512 - defer func() { - dirEngine.Algorithm = digest.SHA256 - }() - - digestSha512, err = engine.Put(ctx, strings.NewReader(bodyIn)) + digestSha512, err = engine.Put(ctx, digest.SHA512, strings.NewReader(bodyIn)) if err != nil { t.Fatal(err) } @@ -215,7 +205,7 @@ func TestGood(t *testing.T) { t.Run("digests", func(t *testing.T) { // Inject sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 - _, err = engine.Put(ctx, strings.NewReader("")) + _, err = engine.Put(ctx, "", strings.NewReader("")) if err != nil { t.Fatal(err) } diff --git a/interface.go b/interface.go index f7d47c9..486d3f4 100644 --- a/interface.go +++ b/interface.go @@ -152,7 +152,12 @@ type Writer interface { // Put adds a new blob to the store. The action is idempotent; a // nil return means "that content is stored at DIGEST" without // implying "because of your Put()". - Put(ctx context.Context, reader io.Reader) (digest digest.Digest, err error) + // + + // The algorithm argument allows you to require a particular digest + // algorithm. Set to the empty string to allow the Writer to use + // its preferred algorithm. + Put(ctx context.Context, algorithm digest.Algorithm, reader io.Reader) (digest digest.Digest, err error) } // Deleter represents a content-addressable storage engine deleter.