From df5c69eea540e8071513da9485fdba323f6467fa Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Sat, 5 Mar 2022 17:21:16 +0300 Subject: [PATCH] [#139] object: Implement function to init object creation All NeoFS object must have at least container and owner identifiers. Add `InitCreation` function which write all required fields to the object instance. Extend `Object` type docs with all the constructors. Signed-off-by: Leonard Lyubich --- object/object.go | 27 ++++++++++++++++++++++++--- object/object_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 object/object_test.go diff --git a/object/object.go b/object/object.go index 26ff38e3..ce56c4fd 100644 --- a/object/object.go +++ b/object/object.go @@ -12,11 +12,32 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/version" ) -// Object represents v2-compatible NeoFS object that provides -// a convenient interface for working in isolation -// from the internal structure of an object. +// Object represents in-memory structure of the NeoFS object. +// Type is compatible with NeoFS API V2 protocol. +// +// Instance can be created depending on scenario: +// * InitCreation (an object to be placed in container); +// * New (blank instance, usually needed for decoding); +// * NewFromV2 (when working under NeoFS API V2 protocol). type Object object.Object +// RequiredFields contains the minimum set of object data that must be set +// by the NeoFS user at the stage of creation. +type RequiredFields struct { + // Identifier of the NeoFS container associated with the object. + Container cid.ID + + // Object owner ID in the NeoFS system. + Owner owner.ID +} + +// InitCreation initializes the object instance with minimum set of required fields. +// Object is expected (but not required) to be blank. Object must not be nil. +func InitCreation(dst *Object, rf RequiredFields) { + dst.SetContainerID(&rf.Container) + dst.SetOwnerID(&rf.Owner) +} + // NewFromV2 wraps v2 Object message to Object. func NewFromV2(oV2 *object.Object) *Object { return (*Object)(oV2) diff --git a/object/object_test.go b/object/object_test.go new file mode 100644 index 00000000..286ca4ba --- /dev/null +++ b/object/object_test.go @@ -0,0 +1,24 @@ +package object_test + +import ( + "testing" + + cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" + "github.com/nspcc-dev/neofs-sdk-go/object" + ownertest "github.com/nspcc-dev/neofs-sdk-go/owner/test" + "github.com/stretchr/testify/require" +) + +func TestInitCreation(t *testing.T) { + var o object.Object + cnr := *cidtest.ID() + own := *ownertest.ID() + + object.InitCreation(&o, object.RequiredFields{ + Container: cnr, + Owner: own, + }) + + require.Equal(t, &cnr, o.ContainerID()) + require.Equal(t, &own, o.OwnerID()) +}