git/odb: introduce const ObjectType enumeration

This commit is contained in:
Taylor Blau 2017-05-15 15:53:17 -06:00
parent 8ba719962a
commit 054986da5d
2 changed files with 80 additions and 0 deletions

45
git/odb/object_type.go Normal file

@ -0,0 +1,45 @@
package odb
import "strings"
// ObjectType is a constant enumeration type for identifying the kind of object
// type an implementing instance of the Object interface is.
type ObjectType uint8
const (
UnknownObjectType ObjectType = iota
BlobObjectType
TreeObjectType
CommitObjectType
)
// ObjectTypeFromString converts from a given string to an ObjectType
// enumeration instance.
func ObjectTypeFromString(s string) ObjectType {
switch strings.ToLower(s) {
case "blob":
return BlobObjectType
case "tree":
return TreeObjectType
case "commit":
return CommitObjectType
default:
return UnknownObjectType
}
}
// String implements the fmt.Stringer interface and returns a string
// representation of the ObjectType enumeration instance.
func (t ObjectType) String() string {
switch t {
case UnknownObjectType:
return "unknown"
case BlobObjectType:
return "blob"
case TreeObjectType:
return "tree"
case CommitObjectType:
return "commit"
}
return "<unknown>"
}

@ -0,0 +1,35 @@
package odb
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestObjectTypeFromString(t *testing.T) {
for str, typ := range map[string]ObjectType{
"blob": BlobObjectType,
"tree": TreeObjectType,
"commit": CommitObjectType,
"something else": UnknownObjectType,
} {
t.Run(str, func(t *testing.T) {
assert.Equal(t, typ, ObjectTypeFromString(str))
})
}
}
func TestObjectTypeToString(t *testing.T) {
for typ, str := range map[ObjectType]string{
BlobObjectType: "blob",
TreeObjectType: "tree",
CommitObjectType: "commit",
UnknownObjectType: "unknown",
ObjectType(math.MaxUint8): "<unknown>",
} {
t.Run(str, func(t *testing.T) {
assert.Equal(t, str, typ.String())
})
}
}