forked from TrueCloudLab/frostfs-api-go
[#145] sdk/object: Refactor logic of field setters
In previous implementation RawObject constructor initialized internal v2 Object structure recursively so that further you can directly set the values of nested fields. This caused the object created by constructor New to be different from the object created from the empty object v2. Remove recursive initialization of v2 Object from New and NewRaw constructors. Make setters to initialize nested structures on demand. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
052a4a97dc
commit
1ac6f2eeeb
3 changed files with 61 additions and 55 deletions
|
@ -26,7 +26,7 @@ func NewFromV2(oV2 *object.Object) *Object {
|
||||||
//
|
//
|
||||||
// Works similar as NewFromV2(new(Object)).
|
// Works similar as NewFromV2(new(Object)).
|
||||||
func New() *Object {
|
func New() *Object {
|
||||||
return NewFromV2(initObjectRecursive())
|
return NewFromV2(new(object.Object))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToV2 converts Object to v2 Object message.
|
// ToV2 converts Object to v2 Object message.
|
||||||
|
|
|
@ -25,7 +25,7 @@ func NewRawFromV2(oV2 *object.Object) *RawObject {
|
||||||
//
|
//
|
||||||
// Works similar as NewRawFromV2(new(Object)).
|
// Works similar as NewRawFromV2(new(Object)).
|
||||||
func NewRaw() *RawObject {
|
func NewRaw() *RawObject {
|
||||||
return NewRawFromV2(initObjectRecursive())
|
return NewRawFromV2(new(object.Object))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Object returns read-only object instance.
|
// Object returns read-only object instance.
|
||||||
|
|
110
pkg/object/rw.go
110
pkg/object/rw.go
|
@ -12,18 +12,6 @@ import (
|
||||||
// public getter and private setters.
|
// public getter and private setters.
|
||||||
type rwObject object.Object
|
type rwObject object.Object
|
||||||
|
|
||||||
func initObjectRecursive() *object.Object {
|
|
||||||
obj := new(object.Object)
|
|
||||||
|
|
||||||
hdr := new(object.Header)
|
|
||||||
|
|
||||||
hdr.SetSplit(new(object.SplitHeader))
|
|
||||||
|
|
||||||
obj.SetHeader(hdr)
|
|
||||||
|
|
||||||
return obj
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: add session token methods
|
// TODO: add session token methods
|
||||||
|
|
||||||
// ToV2 converts Object to v2 Object message.
|
// ToV2 converts Object to v2 Object message.
|
||||||
|
@ -31,6 +19,30 @@ func (o *rwObject) ToV2() *object.Object {
|
||||||
return (*object.Object)(o)
|
return (*object.Object)(o)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *rwObject) setHeaderField(setter func(*object.Header)) {
|
||||||
|
obj := (*object.Object)(o)
|
||||||
|
h := obj.GetHeader()
|
||||||
|
|
||||||
|
if h == nil {
|
||||||
|
h = new(object.Header)
|
||||||
|
obj.SetHeader(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
setter(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *rwObject) setSplitFields(setter func(*object.SplitHeader)) {
|
||||||
|
o.setHeaderField(func(h *object.Header) {
|
||||||
|
split := h.GetSplit()
|
||||||
|
if split == nil {
|
||||||
|
split = new(object.SplitHeader)
|
||||||
|
h.SetSplit(split)
|
||||||
|
}
|
||||||
|
|
||||||
|
setter(split)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// GetID returns object identifier.
|
// GetID returns object identifier.
|
||||||
func (o *rwObject) GetID() *ID {
|
func (o *rwObject) GetID() *ID {
|
||||||
return NewIDFromV2(
|
return NewIDFromV2(
|
||||||
|
@ -78,9 +90,9 @@ func (o *rwObject) GetVersion() *pkg.Version {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setVersion(v *pkg.Version) {
|
func (o *rwObject) setVersion(v *pkg.Version) {
|
||||||
(*object.Object)(o).
|
o.setHeaderField(func(h *object.Header) {
|
||||||
GetHeader().
|
h.SetVersion(v.ToV2())
|
||||||
SetVersion(v.ToV2())
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPayloadSize returns payload length of the object.
|
// GetPayloadSize returns payload length of the object.
|
||||||
|
@ -91,9 +103,9 @@ func (o *rwObject) GetPayloadSize() uint64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setPayloadSize(v uint64) {
|
func (o *rwObject) setPayloadSize(v uint64) {
|
||||||
(*object.Object)(o).
|
o.setHeaderField(func(h *object.Header) {
|
||||||
GetHeader().
|
h.SetPayloadLength(v)
|
||||||
SetPayloadLength(v)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetContainerID returns identifier of the related container.
|
// GetContainerID returns identifier of the related container.
|
||||||
|
@ -106,9 +118,9 @@ func (o *rwObject) GetContainerID() *container.ID {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setContainerID(v *container.ID) {
|
func (o *rwObject) setContainerID(v *container.ID) {
|
||||||
(*object.Object)(o).
|
o.setHeaderField(func(h *object.Header) {
|
||||||
GetHeader().
|
h.SetContainerID(v.ToV2())
|
||||||
SetContainerID(v.ToV2())
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOwnerID returns identifier of the object owner.
|
// GetOwnerID returns identifier of the object owner.
|
||||||
|
@ -121,9 +133,9 @@ func (o *rwObject) GetOwnerID() *owner.ID {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setOwnerID(v *owner.ID) {
|
func (o *rwObject) setOwnerID(v *owner.ID) {
|
||||||
(*object.Object)(o).
|
o.setHeaderField(func(h *object.Header) {
|
||||||
GetHeader().
|
h.SetOwnerID(v.ToV2())
|
||||||
SetOwnerID(v.ToV2())
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCreationEpoch returns epoch number in which object was created.
|
// GetCreationEpoch returns epoch number in which object was created.
|
||||||
|
@ -134,9 +146,9 @@ func (o *rwObject) GetCreationEpoch() uint64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setCreationEpoch(v uint64) {
|
func (o *rwObject) setCreationEpoch(v uint64) {
|
||||||
(*object.Object)(o).
|
o.setHeaderField(func(h *object.Header) {
|
||||||
GetHeader().
|
h.SetCreationEpoch(v)
|
||||||
SetCreationEpoch(v)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPayloadChecksum returns checksum of the object payload.
|
// GetPayloadChecksum returns checksum of the object payload.
|
||||||
|
@ -149,9 +161,9 @@ func (o *rwObject) GetPayloadChecksum() *pkg.Checksum {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setPayloadChecksum(v *pkg.Checksum) {
|
func (o *rwObject) setPayloadChecksum(v *pkg.Checksum) {
|
||||||
(*object.Object)(o).
|
o.setHeaderField(func(h *object.Header) {
|
||||||
GetHeader().
|
h.SetPayloadHash(v.ToV2())
|
||||||
SetPayloadHash(v.ToV2())
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPayloadHomomorphicHash returns homomorphic hash of the object payload.
|
// GetPayloadHomomorphicHash returns homomorphic hash of the object payload.
|
||||||
|
@ -164,9 +176,9 @@ func (o *rwObject) GetPayloadHomomorphicHash() *pkg.Checksum {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setPayloadHomomorphicHash(v *pkg.Checksum) {
|
func (o *rwObject) setPayloadHomomorphicHash(v *pkg.Checksum) {
|
||||||
(*object.Object)(o).
|
o.setHeaderField(func(h *object.Header) {
|
||||||
GetHeader().
|
h.SetHomomorphicHash(v.ToV2())
|
||||||
SetHomomorphicHash(v.ToV2())
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAttributes returns object attributes.
|
// GetAttributes returns object attributes.
|
||||||
|
@ -185,16 +197,15 @@ func (o *rwObject) GetAttributes() []*Attribute {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setAttributes(v ...*Attribute) {
|
func (o *rwObject) setAttributes(v ...*Attribute) {
|
||||||
h := (*object.Object)(o).
|
|
||||||
GetHeader()
|
|
||||||
|
|
||||||
attrs := make([]*object.Attribute, 0, len(v))
|
attrs := make([]*object.Attribute, 0, len(v))
|
||||||
|
|
||||||
for i := range v {
|
for i := range v {
|
||||||
attrs = append(attrs, v[i].ToV2())
|
attrs = append(attrs, v[i].ToV2())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
o.setHeaderField(func(h *object.Header) {
|
||||||
h.SetAttributes(attrs)
|
h.SetAttributes(attrs)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPreviousID returns identifier of the previous sibling object.
|
// GetPreviousID returns identifier of the previous sibling object.
|
||||||
|
@ -208,10 +219,9 @@ func (o *rwObject) GetPreviousID() *ID {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setPreviousID(v *ID) {
|
func (o *rwObject) setPreviousID(v *ID) {
|
||||||
(*object.Object)(o).
|
o.setSplitFields(func(split *object.SplitHeader) {
|
||||||
GetHeader().
|
split.SetPrevious(v.ToV2())
|
||||||
GetSplit().
|
})
|
||||||
SetPrevious(v.ToV2())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChildren return list of the identifiers of the child objects.
|
// GetChildren return list of the identifiers of the child objects.
|
||||||
|
@ -231,17 +241,15 @@ func (o *rwObject) GetChildren() []*ID {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setChildren(v ...*ID) {
|
func (o *rwObject) setChildren(v ...*ID) {
|
||||||
h := (*object.Object)(o).
|
|
||||||
GetHeader().
|
|
||||||
GetSplit()
|
|
||||||
|
|
||||||
ids := make([]*refs.ObjectID, 0, len(v))
|
ids := make([]*refs.ObjectID, 0, len(v))
|
||||||
|
|
||||||
for i := range v {
|
for i := range v {
|
||||||
ids = append(ids, v[i].ToV2())
|
ids = append(ids, v[i].ToV2())
|
||||||
}
|
}
|
||||||
|
|
||||||
h.SetChildren(ids)
|
o.setSplitFields(func(split *object.SplitHeader) {
|
||||||
|
split.SetChildren(ids)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetParent returns parent object w/o payload.
|
// GetParent returns parent object w/o payload.
|
||||||
|
@ -259,11 +267,9 @@ func (o *rwObject) GetParent() *Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rwObject) setParent(v *Object) {
|
func (o *rwObject) setParent(v *Object) {
|
||||||
h := (*object.Object)(o).
|
o.setSplitFields(func(split *object.SplitHeader) {
|
||||||
GetHeader().
|
split.SetParent((*object.Object)(v.rwObject).GetObjectID())
|
||||||
GetSplit()
|
split.SetParentSignature((*object.Object)(v.rwObject).GetSignature())
|
||||||
|
split.SetParentHeader((*object.Object)(v.rwObject).GetHeader())
|
||||||
h.SetParent((*object.Object)(v.rwObject).GetObjectID())
|
})
|
||||||
h.SetParentSignature((*object.Object)(v.rwObject).GetSignature())
|
|
||||||
h.SetParentHeader((*object.Object)(v.rwObject).GetHeader())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue