Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions commands/blob/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ var (
errBothVariants = errors.New("InvalidUnion", "union holds both variants")
)

// SpecFromBlob returns the spec of a blob whose digest is known. Its digest
// code is the digest's own; a digest that is not a multihash has code 0.
func SpecFromBlob(b Blob) BlobSpec {
s := BlobSpec{digest: b.Digest, size: b.Size, valid: true}
if d, err := multihash.Decode(b.Digest); err == nil {
// SpecFromDigest returns the spec of a blob of size bytes whose digest is
// known. Its digest code is the digest's own; a digest that is not a multihash
// has code 0.
func SpecFromDigest(digest multihash.Multihash, size uint64) BlobSpec {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added SpecFromDigest to the BlobSpec constructor documentation in commit 28089c5.

s := BlobSpec{digest: digest, size: size, valid: true}
if d, err := multihash.Decode(digest); err == nil {
s.code = d.Code
}
return s
}

// SpecFromBlob returns the spec of a blob whose digest is known.
func SpecFromBlob(b Blob) BlobSpec {
return SpecFromDigest(b.Digest, b.Size)
}

// SpecFromDigestCode returns the spec of a blob of size bytes whose digest is
// to be computed with the multihash function code.
func SpecFromDigestCode(code, size uint64) BlobSpec {
Expand Down
3 changes: 2 additions & 1 deletion commands/blob/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func TestBlobSpecRoundTrip(t *testing.T) {

func TestBlobSpecAccessors(t *testing.T) {
digest := testutil.RandomMultihash(t)
byDigest := blob.SpecFromBlob(blob.Blob{Digest: digest, Size: 7})
byDigest := blob.SpecFromDigest(digest, 7)
require.Equal(t, blob.SpecFromBlob(blob.Blob{Digest: digest, Size: 7}), byDigest, "a Blob specifies the same blob")
got, ok := byDigest.Digest()
require.True(t, ok)
require.Equal(t, digest, got)
Expand Down
8 changes: 4 additions & 4 deletions commands/blob/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type Blob struct {
// compute it as the data streams. A spec naming a digest has the digest's
// code too.
//
// Construct one with [SpecFromBlob] or [SpecFromDigestCode]. Encoding the zero
// value fails, as does decoding a spec that names both a digest and a digest
// code, or neither. The codec is hand-written (codec.go), over the generated
// codec of [BlobSpecModel].
// Construct one with [SpecFromBlob], [SpecFromDigest] or [SpecFromDigestCode].
// Encoding the zero value fails, as does decoding a spec that names both a
// digest and a digest code, or neither. The codec is hand-written (codec.go),
// over the generated codec of [BlobSpecModel].
type BlobSpec struct {
digest multihash.Multihash
code uint64
Expand Down
Loading