Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CopyTo method for deep copy #512

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ func (o *Object) ToV2() *object.Object {
return (*object.Object)(o)
}

// CopyTo writes deep copy of the [Object] to dst.
func (o Object) CopyTo(dst *Object) {
id := (*object.Object)(&o).GetObjectID()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id, sig and header have reference fields, why don't we deep copy them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I will update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, Now we have a monster inside SDK.

I tend to think we should implement this CopyTo inside api-go lib, because SDK in many cases uses types from it. Not for all types, but for most frequently used in SDK. For instance, take from this PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create an issue for api-go, but for now we need some way to handle this in SDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(*object.Object)(dst).SetObjectID(copyObjectID(id))

sig := (*object.Object)(&o).GetSignature()
(*object.Object)(dst).SetSignature(copySignature(sig))

header := (*object.Object)(&o).GetHeader()
(*object.Object)(dst).SetHeader(copyHeader(header))

dst.SetPayload(copyByteSlice(o.Payload()))
}

// MarshalHeaderJSON marshals object's header into JSON format.
func (o *Object) MarshalHeaderJSON() ([]byte, error) {
return (*object.Object)(o).GetHeader().MarshalJSON()
Expand Down
180 changes: 180 additions & 0 deletions object/object_copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package object

import (
"github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
)

func copyByteSlice(sl []byte) []byte {
Copy link
Member

@carpawell carpawell Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you copy a slice of something more than once, how about 😎generic😎 copySlice?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave it for 1.21.

Copy link
Member

@carpawell carpawell Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with 1.21 it is even better and (if my suggestion is applied) could be fixed with sed (the current version will require more work). also, that is just more boilerplate code
not critical for me

if sl == nil {
return nil
}

bts := make([]byte, len(sl))
copy(bts, sl)
return bts
}

func copyObjectID(id *refs.ObjectID) *refs.ObjectID {
if id == nil {
return nil
}

var newID refs.ObjectID
newID.SetValue(copyByteSlice(id.GetValue()))

return &newID
}

func copySignature(sig *refs.Signature) *refs.Signature {
if sig == nil {
return nil
}

var newSig refs.Signature
newSig.SetScheme(sig.GetScheme())
newSig.SetKey(copyByteSlice(sig.GetKey()))
newSig.SetSign(copyByteSlice(sig.GetSign()))

return &newSig
}

func copySession(session *v2session.Token) *v2session.Token {
if session == nil {
return nil
}

var newSession v2session.Token
if body := session.GetBody(); body != nil {
var newBody v2session.TokenBody
newBody.SetID(copyByteSlice(body.GetID()))

if ownerID := body.GetOwnerID(); ownerID != nil {
var newOwnerID refs.OwnerID
newOwnerID.SetValue(copyByteSlice(ownerID.GetValue()))

newBody.SetOwnerID(&newOwnerID)
} else {
newBody.SetOwnerID(nil)
}

if lifetime := body.GetLifetime(); lifetime != nil {
newLifetime := *lifetime
newBody.SetLifetime(&newLifetime)
} else {
newBody.SetLifetime(nil)
}

newBody.SetSessionKey(copyByteSlice(body.GetSessionKey()))

// it is an interface. Both implementations do nothing inside implemented functions.
newBody.SetContext(body.GetContext())

newSession.SetBody(&newBody)
} else {
newSession.SetBody(nil)
}

newSession.SetSignature(copySignature(session.GetSignature()))

return &newSession
}

func copySplitHeader(spl *object.SplitHeader) *object.SplitHeader {
if spl == nil {
return nil
}

var newSpl object.SplitHeader

newSpl.SetParent(copyObjectID(spl.GetParent()))
newSpl.SetPrevious(copyObjectID(spl.GetPrevious()))
newSpl.SetParentSignature(copySignature(spl.GetParentSignature()))
newSpl.SetParentHeader(copyHeader(spl.GetParentHeader()))

if children := spl.GetChildren(); children != nil {
newChildren := make([]refs.ObjectID, len(children))
copy(newChildren, children)

newSpl.SetChildren(newChildren)
} else {
newSpl.SetChildren(nil)
}

newSpl.SetSplitID(copyByteSlice(spl.GetSplitID()))

return &newSpl
}

func copyHeader(header *object.Header) *object.Header {
if header == nil {
return nil
}

var newHeader object.Header

newHeader.SetCreationEpoch(header.GetCreationEpoch())
newHeader.SetPayloadLength(header.GetPayloadLength())
newHeader.SetObjectType(header.GetObjectType())

if ver := header.GetVersion(); ver != nil {
newVer := *ver
newHeader.SetVersion(&newVer)
} else {
newHeader.SetVersion(nil)
}

if containerID := header.GetContainerID(); containerID != nil {
var newContainerID refs.ContainerID
newContainerID.SetValue(copyByteSlice(containerID.GetValue()))

newHeader.SetContainerID(&newContainerID)
} else {
newHeader.SetContainerID(nil)
}

if ownerID := header.GetOwnerID(); ownerID != nil {
var newOwnerID refs.OwnerID
newOwnerID.SetValue(copyByteSlice(ownerID.GetValue()))

newHeader.SetOwnerID(&newOwnerID)
} else {
newHeader.SetOwnerID(nil)
}

if payloadHash := header.GetPayloadHash(); payloadHash != nil {
var newPayloadHash refs.Checksum
newPayloadHash.SetType(payloadHash.GetType())
newPayloadHash.SetSum(copyByteSlice(payloadHash.GetSum()))

newHeader.SetPayloadHash(&newPayloadHash)
} else {
newHeader.SetPayloadHash(nil)
}

if homoHash := header.GetHomomorphicHash(); homoHash != nil {
var newHomoHash refs.Checksum
newHomoHash.SetType(homoHash.GetType())
newHomoHash.SetSum(copyByteSlice(homoHash.GetSum()))

newHeader.SetHomomorphicHash(&newHomoHash)
} else {
newHeader.SetHomomorphicHash(nil)
}

newHeader.SetSessionToken(copySession(header.GetSessionToken()))

if attrs := header.GetAttributes(); attrs != nil {
newAttributes := make([]object.Attribute, len(attrs))
copy(newAttributes, attrs)

newHeader.SetAttributes(newAttributes)
} else {
newHeader.SetAttributes(nil)
}

newHeader.SetSplit(copySplitHeader(header.GetSplit()))

return &newHeader
}
Loading