From edcaef847817ab13284749284e9ca0594adc86fb Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 2 Sep 2020 15:45:46 +0300 Subject: [PATCH] [#17] core/object: Implement Object and Address types Signed-off-by: Leonard Lyubich --- go.mod | 16 +++++----- pkg/core/object/address.go | 35 +++++++++++++++++++++ pkg/core/object/object.go | 63 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 pkg/core/object/address.go create mode 100644 pkg/core/object/object.go diff --git a/go.mod b/go.mod index 062e41f3d..7d5f0ff3e 100644 --- a/go.mod +++ b/go.mod @@ -4,21 +4,20 @@ go 1.14 require ( bou.ke/monkey v1.0.2 - github.com/fasthttp/router v1.0.2 + github.com/fasthttp/router v1.0.2 // indirect github.com/gogo/protobuf v1.3.1 github.com/golang/protobuf v1.4.2 github.com/google/uuid v1.1.1 - github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 - github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 + github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect github.com/mr-tron/base58 v1.1.3 github.com/multiformats/go-multiaddr v0.2.0 github.com/multiformats/go-multiaddr-net v0.1.2 // v0.1.1 => v0.1.2 - github.com/multiformats/go-multihash v0.0.13 - github.com/nspcc-dev/hrw v1.0.9 + github.com/multiformats/go-multihash v0.0.13 // indirect + github.com/nspcc-dev/hrw v1.0.9 // indirect github.com/nspcc-dev/neo-go v0.91.1-pre.0.20200827184617-7560aa345a78 - github.com/nspcc-dev/neofs-api-go v1.3.1-0.20200901143416-cf66f44adb32 + github.com/nspcc-dev/neofs-api-go v1.3.1-0.20200902121740-5a6dff8c83ba github.com/nspcc-dev/neofs-crypto v0.3.0 - github.com/nspcc-dev/netmap v1.7.0 + github.com/nspcc-dev/netmap v1.7.0 // indirect github.com/nspcc-dev/tzhash v1.4.0 // indirect github.com/panjf2000/ants/v2 v2.3.0 github.com/pkg/errors v0.9.1 @@ -31,13 +30,12 @@ require ( github.com/valyala/fasthttp v1.9.0 go.etcd.io/bbolt v1.3.4 go.uber.org/atomic v1.5.1 - go.uber.org/dig v1.8.0 + go.uber.org/dig v1.8.0 // indirect go.uber.org/multierr v1.4.0 // indirect go.uber.org/zap v1.13.0 golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect golang.org/x/tools v0.0.0-20200123022218-593de606220b // indirect - google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a google.golang.org/grpc v1.29.1 ) diff --git a/pkg/core/object/address.go b/pkg/core/object/address.go new file mode 100644 index 000000000..328e50ed8 --- /dev/null +++ b/pkg/core/object/address.go @@ -0,0 +1,35 @@ +package object + +import ( + "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-api-go/v2/refs" +) + +type Address struct { + *object.Address +} + +// MarshalStableV2 marshals Address to v2 binary format. +func (a *Address) MarshalStableV2() ([]byte, error) { + if a != nil { + return a.ToV2().StableMarshal(nil) + } + + return nil, nil +} + +// AddressFromV2 converts v2 Address message to Address. +func AddressFromV2(aV2 *refs.Address) (*Address, error) { + if aV2 == nil { + return nil, nil + } + + a, err := object.AddressFromV2(aV2) + if err != nil { + return nil, err + } + + return &Address{ + Address: a, + }, nil +} diff --git a/pkg/core/object/object.go b/pkg/core/object/object.go new file mode 100644 index 000000000..6b05c92ad --- /dev/null +++ b/pkg/core/object/object.go @@ -0,0 +1,63 @@ +package object + +import ( + "github.com/nspcc-dev/neofs-api-go/pkg/object" + objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" +) + +// Object represents the NeoFS object. +// +// Object inherits object type from NeoFS SDK. +// It is used to implement some useful methods and functions +// for convenient processing of an object by a node. +type Object struct { + *object.Object +} + +// MarshalStableV2 marshals Object to v2 binary format. +func (o *Object) MarshalStableV2() ([]byte, error) { + if o != nil { + return o.ToV2().StableMarshal(nil) + } + + return nil, nil +} + +// Address returns address of the object. +func (o *Object) Address() *Address { + if o != nil { + return &Address{ + Address: o.Object.Address(), + } + } + + return nil +} + +// FromV2 converts v2 Object message to Object. +func FromV2(oV2 *objectV2.Object) (*Object, error) { + if oV2 == nil { + return nil, nil + } + + o, err := object.FromV2(oV2) + if err != nil { + return nil, err + } + + return &Object{ + Object: o, + }, nil +} + +// FromBytes restores Object from binary format. +func FromBytes(data []byte) (*Object, error) { + o, err := object.FromBytes(data) + if err != nil { + return nil, err + } + + return &Object{ + Object: o, + }, nil +}