aboutsummaryrefslogtreecommitdiff
path: root/object/id
diff options
context:
space:
mode:
Diffstat (limited to 'object/id')
-rw-r--r--object/id/object_format_ops.go49
-rw-r--r--object/id/object_id_parse.go49
-rw-r--r--object/id/object_id_test.go34
3 files changed, 65 insertions, 67 deletions
diff --git a/object/id/object_format_ops.go b/object/id/object_format_ops.go
index 76077235..51bcae82 100644
--- a/object/id/object_format_ops.go
+++ b/object/id/object_format_ops.go
@@ -1,6 +1,10 @@
package id
-import "hash"
+import (
+ "encoding/hex"
+ "fmt"
+ "hash"
+)
// HexLen returns the encoded hexadecimal length.
func (objectFormat ObjectFormat) HexLen() int {
@@ -22,6 +26,49 @@ func (objectFormat ObjectFormat) New() (hash.Hash, error) {
return newFn(), nil
}
+// FromBytes builds an object ID from raw bytes for this object format.
+func (objectFormat ObjectFormat) FromBytes(b []byte) (ObjectID, error) {
+ var id ObjectID
+ if objectFormat.Size() == 0 {
+ return id, ErrInvalidObjectFormat
+ }
+
+ if len(b) != objectFormat.Size() {
+ return id, fmt.Errorf("%w: got %d bytes, expected %d", ErrInvalidObjectID, len(b), objectFormat.Size())
+ }
+
+ copy(id.data[:], b)
+ id.objectFormat = objectFormat
+
+ return id, nil
+}
+
+// FromString parses an object ID from its canonical hex representation.
+func (objectFormat ObjectFormat) FromString(s string) (ObjectID, error) {
+ var id ObjectID
+ if objectFormat.Size() == 0 {
+ return id, ErrInvalidObjectFormat
+ }
+
+ if len(s)&1 != 0 {
+ return id, fmt.Errorf("%w: odd hex length %d", ErrInvalidObjectID, len(s))
+ }
+
+ if len(s) != objectFormat.HexLen() {
+ return id, fmt.Errorf("%w: got %d chars, expected %d", ErrInvalidObjectID, len(s), objectFormat.HexLen())
+ }
+
+ decoded, err := hex.DecodeString(s)
+ if err != nil {
+ return id, fmt.Errorf("%w: decode: %w", ErrInvalidObjectID, err)
+ }
+
+ copy(id.data[:], decoded)
+ id.objectFormat = objectFormat
+
+ return id, nil
+}
+
// String returns the canonical object format name.
func (objectFormat ObjectFormat) String() string {
return objectFormat.details().name
diff --git a/object/id/object_id_parse.go b/object/id/object_id_parse.go
deleted file mode 100644
index 59b6669c..00000000
--- a/object/id/object_id_parse.go
+++ /dev/null
@@ -1,49 +0,0 @@
-package id
-
-import (
- "encoding/hex"
- "fmt"
-)
-
-// FromBytes builds an object ID from raw bytes for the specified object format.
-func FromBytes(objectFormat ObjectFormat, b []byte) (ObjectID, error) {
- var id ObjectID
- if objectFormat.Size() == 0 {
- return id, ErrInvalidObjectFormat
- }
-
- if len(b) != objectFormat.Size() {
- return id, fmt.Errorf("%w: got %d bytes, expected %d", ErrInvalidObjectID, len(b), objectFormat.Size())
- }
-
- copy(id.data[:], b)
- id.objectFormat = objectFormat
-
- return id, nil
-}
-
-// FromHex parses an object ID from hex for the specified object format.
-func FromHex(objectFormat ObjectFormat, s string) (ObjectID, error) {
- var id ObjectID
- if objectFormat.Size() == 0 {
- return id, ErrInvalidObjectFormat
- }
-
- if len(s)%2 != 0 {
- return id, fmt.Errorf("%w: odd hex length %d", ErrInvalidObjectID, len(s))
- }
-
- if len(s) != objectFormat.HexLen() {
- return id, fmt.Errorf("%w: got %d chars, expected %d", ErrInvalidObjectID, len(s), objectFormat.HexLen())
- }
-
- decoded, err := hex.DecodeString(s)
- if err != nil {
- return id, fmt.Errorf("%w: decode: %w", ErrInvalidObjectID, err)
- }
-
- copy(id.data[:], decoded)
- id.objectFormat = objectFormat
-
- return id, nil
-}
diff --git a/object/id/object_id_test.go b/object/id/object_id_test.go
index 5d6130d2..40957692 100644
--- a/object/id/object_id_test.go
+++ b/object/id/object_id_test.go
@@ -11,13 +11,13 @@ import (
func TestFromBytesErrors(t *testing.T) {
t.Parallel()
- _, err := id.FromBytes(id.ObjectFormatUnknown, []byte{1, 2})
+ _, err := id.ObjectFormatUnknown.FromBytes([]byte{1, 2})
if err == nil {
t.Fatalf("expected FromBytes unknown object format error")
}
for _, objectFormat := range id.SupportedObjectFormats() {
- _, err = id.FromBytes(objectFormat, []byte{1, 2})
+ _, err = objectFormat.FromBytes([]byte{1, 2})
if err == nil {
t.Fatalf("expected FromBytes wrong size error")
}
@@ -28,9 +28,9 @@ func TestBytesReturnsCopy(t *testing.T) {
t.Parallel()
for _, objectFormat := range id.SupportedObjectFormats() {
- id, err := id.FromHex(objectFormat, strings.Repeat("01", objectFormat.Size()))
+ id, err := objectFormat.FromString(strings.Repeat("01", objectFormat.Size()))
if err != nil {
- t.Fatalf("ParseHex failed: %v", err)
+ t.Fatalf("FromString failed: %v", err)
}
b1 := id.Bytes()
@@ -47,15 +47,15 @@ func TestBytesReturnsCopy(t *testing.T) {
}
}
-func TestFromHexErrors(t *testing.T) {
+func TestFromStringErrors(t *testing.T) {
t.Parallel()
t.Run("unknown object format", func(t *testing.T) {
t.Parallel()
- _, err := id.FromHex(id.ObjectFormatUnknown, "00")
+ _, err := id.ObjectFormatUnknown.FromString("00")
if err == nil {
- t.Fatalf("expected FromHex error")
+ t.Fatalf("expected FromString error")
}
})
// TODO: This may need to be revisited when hash-function-transition is implemented.
@@ -64,25 +64,25 @@ func TestFromHexErrors(t *testing.T) {
t.Run(objectFormat.String(), func(t *testing.T) {
t.Parallel()
- _, err := id.FromHex(objectFormat, strings.Repeat("0", objectFormat.HexLen()-1))
+ _, err := objectFormat.FromString(strings.Repeat("0", objectFormat.HexLen()-1))
if err == nil {
- t.Fatalf("expected FromHex odd-len error")
+ t.Fatalf("expected FromString odd-len error")
}
- _, err = id.FromHex(objectFormat, strings.Repeat("0", objectFormat.HexLen()-2))
+ _, err = objectFormat.FromString(strings.Repeat("0", objectFormat.HexLen()-2))
if err == nil {
- t.Fatalf("expected FromHex wrong-len error")
+ t.Fatalf("expected FromString wrong-len error")
}
- _, err = id.FromHex(objectFormat, "z"+strings.Repeat("0", objectFormat.HexLen()-1))
+ _, err = objectFormat.FromString("z" + strings.Repeat("0", objectFormat.HexLen()-1))
if err == nil {
- t.Fatalf("expected FromHex invalid-hex error")
+ t.Fatalf("expected FromString invalid-hex error")
}
})
}
}
-func TestFromHexRoundtrip(t *testing.T) {
+func TestFromStringRoundtrip(t *testing.T) {
t.Parallel()
for _, objectFormat := range id.SupportedObjectFormats() {
@@ -91,9 +91,9 @@ func TestFromHexRoundtrip(t *testing.T) {
hex := strings.Repeat("01", objectFormat.Size())
- i, err := id.FromHex(objectFormat, hex)
+ i, err := objectFormat.FromString(hex)
if err != nil {
- t.Fatalf("FromHex failed: %v", err)
+ t.Fatalf("FromString failed: %v", err)
}
if got := i.String(); got != hex {
@@ -109,7 +109,7 @@ func TestFromHexRoundtrip(t *testing.T) {
t.Fatalf("Bytes len = %d, want %d", len(raw), objectFormat.Size())
}
- id2, err := id.FromBytes(objectFormat, raw)
+ id2, err := objectFormat.FromBytes(raw)
if err != nil {
t.Fatalf("FromBytes failed: %v", err)
}