diff options
Diffstat (limited to 'object/id/object_format_ops.go')
| -rw-r--r-- | object/id/object_format_ops.go | 49 |
1 files changed, 48 insertions, 1 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 |
