From b197e382ca794d059c2c92c20dc27105fba44003 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 17 Aug 2020 20:26:25 +0300 Subject: [PATCH] v2/object: Implement uni-structures for Object Signed-off-by: Leonard Lyubich --- v2/object/convert.go | 322 ++++++++++++++++++++++++++++ v2/object/grpc/types.go | 35 +++ v2/object/types.go | 457 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 814 insertions(+) create mode 100644 v2/object/convert.go create mode 100644 v2/object/types.go diff --git a/v2/object/convert.go b/v2/object/convert.go new file mode 100644 index 0000000..b742e0d --- /dev/null +++ b/v2/object/convert.go @@ -0,0 +1,322 @@ +package object + +import ( + object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" + "github.com/nspcc-dev/neofs-api-go/v2/refs" + refsGRPC "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc" + "github.com/nspcc-dev/neofs-api-go/v2/service" +) + +func TypeToGRPCField(t Type) object.ObjectType { + return object.ObjectType(t) +} + +func TypeFromGRPCField(t object.ObjectType) Type { + return Type(t) +} + +func MatchTypeToGRPCField(t MatchType) object.MatchType { + return object.MatchType(t) +} + +func MatchTypeFromGRPCField(t object.MatchType) MatchType { + return MatchType(t) +} + +func ShortHeaderToGRPCMessage(h *ShortHeader) *object.ShortHeader { + if h == nil { + return nil + } + + m := new(object.ShortHeader) + + m.SetVersion( + service.VersionToGRPCMessage(h.GetVersion()), + ) + + m.SetCreationEpoch(h.GetCreationEpoch()) + + m.SetOwnerId( + refs.OwnerIDToGRPCMessage(h.GetOwnerID()), + ) + + m.SetObjectType( + TypeToGRPCField(h.GetObjectType()), + ) + + m.SetPayloadLength(h.GeyPayloadLength()) + + return m +} + +func ShortHeaderFromGRPCMessage(m *object.ShortHeader) *ShortHeader { + if m == nil { + return nil + } + + h := new(ShortHeader) + + h.SetVersion( + service.VersionFromGRPCMessage(m.GetVersion()), + ) + + h.SetCreationEpoch(m.GetCreationEpoch()) + + h.SetOwnerID( + refs.OwnerIDFromGRPCMessage(m.GetOwnerId()), + ) + + h.SetObjectType( + TypeFromGRPCField(m.GetObjectType()), + ) + + h.SetPayloadLength(m.GetPayloadLength()) + + return h +} + +func AttributeToGRPCMessage(a *Attribute) *object.Header_Attribute { + if a == nil { + return nil + } + + m := new(object.Header_Attribute) + + m.SetKey(a.GetKey()) + m.SetValue(a.GetValue()) + + return m +} + +func AttributeFromGRPCMessage(m *object.Header_Attribute) *Attribute { + if m == nil { + return nil + } + + h := new(Attribute) + + h.SetKey(m.GetKey()) + h.SetValue(m.GetValue()) + + return h +} + +func SplitHeaderToGRPCMessage(h *SplitHeader) *object.Header_Split { + if h == nil { + return nil + } + + m := new(object.Header_Split) + + m.SetParent( + refs.ObjectIDToGRPCMessage(h.GetParent()), + ) + + m.SetPrevious( + refs.ObjectIDToGRPCMessage(h.GetPrevious()), + ) + + m.SetParentSignature( + service.SignatureToGRPCMessage(h.GetParentSignature()), + ) + + m.SetParentHeader( + HeaderToGRPCMessage(h.GetParentHeader()), + ) + + children := h.GetChildren() + childMsg := make([]*refsGRPC.ObjectID, 0, len(children)) + + for i := range children { + childMsg = append(childMsg, refs.ObjectIDToGRPCMessage(children[i])) + } + + m.SetChildren(childMsg) + + return m +} + +func SplitHeaderFromGRPCMessage(m *object.Header_Split) *SplitHeader { + if m == nil { + return nil + } + + h := new(SplitHeader) + + h.SetParent( + refs.ObjectIDFromGRPCMessage(m.GetParent()), + ) + + h.SetPrevious( + refs.ObjectIDFromGRPCMessage(m.GetPrevious()), + ) + + h.SetParentSignature( + service.SignatureFromGRPCMessage(m.GetParentSignature()), + ) + + h.SetParentHeader( + HeaderFromGRPCMessage(m.GetParentHeader()), + ) + + childMsg := m.GetChildren() + children := make([]*refs.ObjectID, 0, len(childMsg)) + + for i := range childMsg { + children = append(children, refs.ObjectIDFromGRPCMessage(childMsg[i])) + } + + h.SetChildren(children) + + return h +} + +func HeaderToGRPCMessage(h *Header) *object.Header { + if h == nil { + return nil + } + + m := new(object.Header) + + m.SetVersion( + service.VersionToGRPCMessage(h.GetVersion()), + ) + + m.SetContainerId( + refs.ContainerIDToGRPCMessage(h.GetContainerID()), + ) + + m.SetOwnerId( + refs.OwnerIDToGRPCMessage(h.GetOwnerID()), + ) + + m.SetCreationEpoch(h.GetCreationEpoch()) + + m.SetPayloadLength(h.GetPayloadLength()) + + m.SetPayloadHash(h.GetPayloadHash()) + + m.SetHomomorphicHash(h.GetHomomorphicHash()) + + m.SetObjectType( + TypeToGRPCField(h.GetObjectType()), + ) + + m.SetSessionToken( + service.SessionTokenToGRPCMessage(h.GetSessionToken()), + ) + + attr := h.GetAttributes() + attrMsg := make([]*object.Header_Attribute, 0, len(attr)) + + for i := range attr { + attrMsg = append(attrMsg, AttributeToGRPCMessage(attr[i])) + } + + m.SetAttributes(attrMsg) + + m.SetSplit( + SplitHeaderToGRPCMessage(h.GetSplit()), + ) + + return m +} + +func HeaderFromGRPCMessage(m *object.Header) *Header { + if m == nil { + return nil + } + + h := new(Header) + + h.SetVersion( + service.VersionFromGRPCMessage(m.GetVersion()), + ) + + h.SetContainerID( + refs.ContainerIDFromGRPCMessage(m.GetContainerId()), + ) + + h.SetOwnerID( + refs.OwnerIDFromGRPCMessage(m.GetOwnerId()), + ) + + h.SetCreationEpoch(m.GetCreationEpoch()) + + h.SetPayloadLength(m.GetPayloadLength()) + + h.SetPayloadHash(m.GetPayloadHash()) + + h.SetHomomorphicHash(m.GetHomomorphicHash()) + + h.SetObjectType( + TypeFromGRPCField(m.GetObjectType()), + ) + + h.SetSessionToken( + service.SessionTokenFromGRPCMessage(m.GetSessionToken()), + ) + + attrMsg := m.GetAttributes() + attr := make([]*Attribute, 0, len(attrMsg)) + + for i := range attrMsg { + attr = append(attr, AttributeFromGRPCMessage(attrMsg[i])) + } + + h.SetAttributes(attr) + + h.SetSplit( + SplitHeaderFromGRPCMessage(m.GetSplit()), + ) + + return h +} + +func ObjectToGRPCMessage(o *Object) *object.Object { + if o == nil { + return nil + } + + m := new(object.Object) + + m.SetObjectId( + refs.ObjectIDToGRPCMessage(o.GetObjectID()), + ) + + m.SetSignature( + service.SignatureToGRPCMessage(o.GetSignature()), + ) + + m.SetHeader( + HeaderToGRPCMessage(o.GetHeader()), + ) + + m.SetPayload(o.GetPayload()) + + return m +} + +func ObjectFromGRPCMessage(m *object.Object) *Object { + if m == nil { + return nil + } + + o := new(Object) + + o.SetObjectID( + refs.ObjectIDFromGRPCMessage(m.GetObjectId()), + ) + + o.SetSignature( + service.SignatureFromGRPCMessage(m.GetSignature()), + ) + + o.SetHeader( + HeaderFromGRPCMessage(m.GetHeader()), + ) + + o.SetPayload(m.GetPayload()) + + return o +} diff --git a/v2/object/grpc/types.go b/v2/object/grpc/types.go index c857b8a..b5197f6 100644 --- a/v2/object/grpc/types.go +++ b/v2/object/grpc/types.go @@ -158,3 +158,38 @@ func (m *Object) SetPayload(v []byte) { m.Payload = v } } + +// SetVersion sets version of the object. +func (m *ShortHeader) SetVersion(v *service.Version) { + if m != nil { + m.Version = v + } +} + +// SetCreationEpoch sets creation epoch number. +func (m *ShortHeader) SetCreationEpoch(v uint64) { + if m != nil { + m.CreationEpoch = v + } +} + +// SetOwnerId sets identifier of the object owner. +func (m *ShortHeader) SetOwnerId(v *refs.OwnerID) { + if m != nil { + m.OwnerId = v + } +} + +// SetObjectType sets type of the object. +func (m *ShortHeader) SetObjectType(v ObjectType) { + if m != nil { + m.ObjectType = v + } +} + +// SetPayloadLength sets length of the object payload. +func (m *ShortHeader) SetPayloadLength(v uint64) { + if m != nil { + m.PayloadLength = v + } +} diff --git a/v2/object/types.go b/v2/object/types.go new file mode 100644 index 0000000..191457b --- /dev/null +++ b/v2/object/types.go @@ -0,0 +1,457 @@ +package object + +import ( + "github.com/nspcc-dev/neofs-api-go/v2/refs" + "github.com/nspcc-dev/neofs-api-go/v2/service" +) + +type Type uint32 + +type MatchType uint32 + +type ShortHeader struct { + version *service.Version + + creatEpoch uint64 + + ownerID *refs.OwnerID + + typ Type + + payloadLen uint64 +} + +type Attribute struct { + key, val string +} + +type SplitHeader struct { + par, prev *refs.ObjectID + + parSig *service.Signature + + parHdr *Header + + children []*refs.ObjectID +} + +type Header struct { + version *service.Version + + cid *refs.ContainerID + + ownerID *refs.OwnerID + + creatEpoch uint64 + + payloadLen uint64 + + payloadHash, homoHash []byte + + typ Type + + sessionToken *service.SessionToken + + attr []*Attribute + + split *SplitHeader +} + +type Object struct { + objectID *refs.ObjectID + + idSig *service.Signature + + header *Header + + payload []byte +} + +const ( + TypeRegular Type = iota + TypeTombstone + TypeStorageGroup +) + +const ( + MatchUnknown MatchType = iota + MatchStringEqual +) + +func (h *ShortHeader) GetVersion() *service.Version { + if h != nil { + return h.version + } + + return nil +} + +func (h *ShortHeader) SetVersion(v *service.Version) { + if h != nil { + h.SetVersion(v) + } +} + +func (h *ShortHeader) GetCreationEpoch() uint64 { + if h != nil { + return h.creatEpoch + } + + return 0 +} + +func (h *ShortHeader) SetCreationEpoch(v uint64) { + if h != nil { + h.creatEpoch = v + } +} + +func (h *ShortHeader) GetOwnerID() *refs.OwnerID { + if h != nil { + return h.ownerID + } + + return nil +} + +func (h *ShortHeader) SetOwnerID(v *refs.OwnerID) { + if h != nil { + h.ownerID = v + } +} + +func (h *ShortHeader) GetObjectType() Type { + if h != nil { + return h.typ + } + + return TypeRegular +} + +func (h *ShortHeader) SetObjectType(v Type) { + if h != nil { + h.typ = v + } +} + +func (h *ShortHeader) GeyPayloadLength() uint64 { + if h != nil { + return h.payloadLen + } + + return 0 +} + +func (h *ShortHeader) SetPayloadLength(v uint64) { + if h != nil { + h.payloadLen = v + } +} + +func (a *Attribute) GetKey() string { + if a != nil { + return a.key + } + + return "" +} + +func (a *Attribute) SetKey(v string) { + if a != nil { + a.key = v + } +} + +func (a *Attribute) GetValue() string { + if a != nil { + return a.val + } + + return "" +} + +func (a *Attribute) SetValue(v string) { + if a != nil { + a.val = v + } +} + +func (h *SplitHeader) GetParent() *refs.ObjectID { + if h != nil { + return h.par + } + + return nil +} + +func (h *SplitHeader) SetParent(v *refs.ObjectID) { + if h != nil { + h.par = v + } +} + +func (h *SplitHeader) GetPrevious() *refs.ObjectID { + if h != nil { + return h.prev + } + + return nil +} + +func (h *SplitHeader) SetPrevious(v *refs.ObjectID) { + if h != nil { + h.prev = v + } +} + +func (h *SplitHeader) GetParentSignature() *service.Signature { + if h != nil { + return h.parSig + } + + return nil +} + +func (h *SplitHeader) SetParentSignature(v *service.Signature) { + if h != nil { + h.parSig = v + } +} + +func (h *SplitHeader) GetParentHeader() *Header { + if h != nil { + return h.parHdr + } + + return nil +} + +func (h *SplitHeader) SetParentHeader(v *Header) { + if h != nil { + h.parHdr = v + } +} + +func (h *SplitHeader) GetChildren() []*refs.ObjectID { + if h != nil { + return h.children + } + + return nil +} + +func (h *SplitHeader) SetChildren(v []*refs.ObjectID) { + if h != nil { + h.children = v + } +} + +func (h *Header) GetVersion() *service.Version { + if h != nil { + return h.version + } + + return nil +} + +func (h *Header) SetVersion(v *service.Version) { + if h != nil { + h.version = v + } +} + +func (h *Header) GetContainerID() *refs.ContainerID { + if h != nil { + return h.cid + } + + return nil +} + +func (h *Header) SetContainerID(v *refs.ContainerID) { + if h != nil { + h.cid = v + } +} + +func (h *Header) GetOwnerID() *refs.OwnerID { + if h != nil { + return h.ownerID + } + + return nil +} + +func (h *Header) SetOwnerID(v *refs.OwnerID) { + if h != nil { + h.ownerID = v + } +} + +func (h *Header) GetCreationEpoch() uint64 { + if h != nil { + return h.creatEpoch + } + + return 0 +} + +func (h *Header) SetCreationEpoch(v uint64) { + if h != nil { + h.creatEpoch = v + } +} + +func (h *Header) GetPayloadLength() uint64 { + if h != nil { + return h.payloadLen + } + + return 0 +} + +func (h *Header) SetPayloadLength(v uint64) { + if h != nil { + h.payloadLen = v + } +} + +func (h *Header) GetPayloadHash() []byte { + if h != nil { + return h.payloadHash + } + + return nil +} + +func (h *Header) SetPayloadHash(v []byte) { + if h != nil { + h.payloadHash = v + } +} + +func (h *Header) GetObjectType() Type { + if h != nil { + return h.typ + } + + return TypeRegular +} + +func (h *Header) SetObjectType(v Type) { + if h != nil { + h.typ = v + } +} + +func (h *Header) GetHomomorphicHash() []byte { + if h != nil { + return h.homoHash + } + + return nil +} + +func (h *Header) SetHomomorphicHash(v []byte) { + if h != nil { + h.homoHash = v + } +} + +func (h *Header) GetSessionToken() *service.SessionToken { + if h != nil { + return h.sessionToken + } + + return nil +} + +func (h *Header) SetSessionToken(v *service.SessionToken) { + if h != nil { + h.sessionToken = v + } +} + +func (h *Header) GetAttributes() []*Attribute { + if h != nil { + return h.attr + } + + return nil +} + +func (h *Header) SetAttributes(v []*Attribute) { + if h != nil { + h.attr = v + } +} + +func (h *Header) GetSplit() *SplitHeader { + if h != nil { + return h.split + } + + return nil +} + +func (h *Header) SetSplit(v *SplitHeader) { + if h != nil { + h.split = v + } +} + +func (o *Object) GetObjectID() *refs.ObjectID { + if o != nil { + return o.objectID + } + + return nil +} + +func (o *Object) SetObjectID(v *refs.ObjectID) { + if o != nil { + o.objectID = v + } +} + +func (o *Object) GetSignature() *service.Signature { + if o != nil { + return o.idSig + } + + return nil +} + +func (o *Object) SetSignature(v *service.Signature) { + if o != nil { + o.idSig = v + } +} + +func (o *Object) GetHeader() *Header { + if o != nil { + return o.header + } + + return nil +} + +func (o *Object) SetHeader(v *Header) { + if o != nil { + o.header = v + } +} + +func (o *Object) GetPayload() []byte { + if o != nil { + return o.payload + } + + return nil +} + +func (o *Object) SetPayload(v []byte) { + if o != nil { + o.payload = v + } +}