forked from TrueCloudLab/frostfs-api-go
[#140] sdk: Refactor reference types
Refactor v2-compatible reference types to be wrappers over corresponding types from v2. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
2026473733
commit
524280a5e8
10 changed files with 73 additions and 215 deletions
|
@ -4,47 +4,29 @@ import (
|
|||
"crypto/sha256"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ID represents object identifier that
|
||||
// supports different type of values.
|
||||
type ID struct {
|
||||
val []byte
|
||||
// ID represents v2-compatible object identifier.
|
||||
type ID refs.ObjectID
|
||||
|
||||
// NewIDFromV2 wraps v2 ObjectID message to ID.
|
||||
func NewIDFromV2(idV2 *refs.ObjectID) *ID {
|
||||
return (*ID)(idV2)
|
||||
}
|
||||
|
||||
// NewID creates and initializes blank ID.
|
||||
//
|
||||
// Works similar as NewIDFromV2(new(ObjectID)).
|
||||
func NewID() *ID {
|
||||
return NewIDFromV2(new(refs.ObjectID))
|
||||
}
|
||||
|
||||
// SetSHA256 sets object identifier value to SHA256 checksum.
|
||||
func (id *ID) SetSHA256(v [sha256.Size]byte) {
|
||||
if id != nil {
|
||||
id.val = v[:]
|
||||
}
|
||||
(*refs.ObjectID)(id).SetValue(v[:])
|
||||
}
|
||||
|
||||
// ToV2 converts ID to v2 ObjectID message.
|
||||
func (id *ID) ToV2() *refs.ObjectID {
|
||||
if id != nil {
|
||||
idV2 := new(refs.ObjectID)
|
||||
idV2.SetValue(id.val)
|
||||
|
||||
return idV2
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IDFromV2 converts v2 ObjectID message to ID.
|
||||
//
|
||||
// Returns an error if the format of the identifier
|
||||
// in the message is broken.
|
||||
func IDFromV2(idV2 *refs.ObjectID) (*ID, error) {
|
||||
val := idV2.GetValue()
|
||||
if ln := len(val); ln != sha256.Size {
|
||||
return nil, errors.Errorf("could not convert %T to %T: invalid length %d",
|
||||
idV2, (*ID)(nil), ln,
|
||||
)
|
||||
}
|
||||
|
||||
return &ID{
|
||||
val: val,
|
||||
}, nil
|
||||
return (*refs.ObjectID)(id)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue