git/odb/pack: introduce *Object to encapsulate typed data

This commit is contained in:
Taylor Blau 2017-09-05 17:02:31 -04:00
parent ef5037021a
commit 9ea23ca017
2 changed files with 74 additions and 0 deletions

27
git/odb/pack/object.go Normal file

@ -0,0 +1,27 @@
package pack
// Object is an encapsulation of an object found in a packfile, or a packed
// object.
type Object struct {
// data is the front-most element of the delta-base chain, and when
// resolved, yields the uncompressed data of this object.
data Chain
// typ is the underlying object's type. It is not the type of the
// front-most chain element, rather, the type of the actual object.
typ PackedObjectType
}
// Unpack resolves the delta-base chain and returns an uncompressed, unpacked,
// and full representation of the data encoded by this object.
//
// If there was any error in unpacking this object, it is returned immediately,
// and the object's data can be assumed to be corrupt.
func (o *Object) Unpack() ([]byte, error) {
return o.data.Unpack()
}
// Type returns the underlying object's type. Rather than the type of the
// front-most delta-base component, it is the type of the object itself.
func (o *Object) Type() PackedObjectType {
return o.typ
}

@ -0,0 +1,47 @@
package pack
import (
"testing"
"github.com/git-lfs/git-lfs/errors"
"github.com/stretchr/testify/assert"
)
func TestObjectTypeReturnsObjectType(t *testing.T) {
o := &Object{
typ: TypeCommit,
}
assert.Equal(t, TypeCommit, o.Type())
}
func TestObjectUnpackUnpacksData(t *testing.T) {
expected := []byte{0x1, 0x2, 0x3, 0x4}
o := &Object{
data: &ChainSimple{
X: expected,
},
}
data, err := o.Unpack()
assert.Equal(t, expected, data)
assert.NoError(t, err)
}
func TestObjectUnpackPropogatesErrors(t *testing.T) {
expected := errors.New("git/odb/pack: testing")
o := &Object{
data: &ChainSimple{
Err: expected,
},
}
data, err := o.Unpack()
assert.Nil(t, data)
assert.Equal(t, expected, err)
}