From 2d70391e315fdacd7c907d1df81b23097b3861dc Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Sat, 6 Nov 2021 13:40:15 +0300 Subject: [PATCH] [#350] v2/session: Support `status` field in `ResponseMetaHeader` Signed-off-by: Leonard Lyubich --- v2/session/convert.go | 17 +++++++++++++++++ v2/session/grpc/types.go | 8 ++++++++ v2/session/marshal.go | 11 ++++++++++- v2/session/test/generate.go | 2 ++ v2/session/types.go | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/v2/session/convert.go b/v2/session/convert.go index 42101d30..80158320 100644 --- a/v2/session/convert.go +++ b/v2/session/convert.go @@ -10,6 +10,8 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" refsGRPC "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc" session "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" + "github.com/nspcc-dev/neofs-api-go/v2/status" + statusGRPC "github.com/nspcc-dev/neofs-api-go/v2/status/grpc" ) func (c *CreateRequestBody) ToGRPCMessage() grpc.Message { @@ -560,6 +562,7 @@ func (r *ResponseMetaHeader) ToGRPCMessage() grpc.Message { m.SetEpoch(r.epoch) m.SetTtl(r.ttl) m.SetOrigin(r.origin.ToGRPCMessage().(*session.ResponseMetaHeader)) + m.SetStatus(r.status.ToGRPCMessage().(*statusGRPC.Status)) } return m @@ -601,6 +604,20 @@ func (r *ResponseMetaHeader) FromGRPCMessage(m grpc.Message) error { } } + st := v.GetStatus() + if st == nil { + r.status = nil + } else { + if r.status == nil { + r.status = new(status.Status) + } + + err = r.status.FromGRPCMessage(st) + if err != nil { + return err + } + } + r.xHeaders, err = XHeadersFromGRPC(v.GetXHeaders()) if err != nil { return err diff --git a/v2/session/grpc/types.go b/v2/session/grpc/types.go index cee46384..07d5d13f 100644 --- a/v2/session/grpc/types.go +++ b/v2/session/grpc/types.go @@ -3,6 +3,7 @@ package session import ( acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc" refs "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc" + status "github.com/nspcc-dev/neofs-api-go/v2/status/grpc" ) // SetKey sets key to the X-Header. @@ -219,6 +220,13 @@ func (m *ResponseMetaHeader) SetOrigin(v *ResponseMetaHeader) { } } +// SetStatus sets response status. +func (m *ResponseMetaHeader) SetStatus(v *status.Status) { + if m != nil { + m.Status = v + } +} + // SetBodySignature sets signature of the request body. func (m *RequestVerificationHeader) SetBodySignature(v *refs.Signature) { if m != nil { diff --git a/v2/session/marshal.go b/v2/session/marshal.go index 05085d7f..7ffe5e07 100644 --- a/v2/session/marshal.go +++ b/v2/session/marshal.go @@ -52,6 +52,7 @@ const ( respMetaHeaderTTLField = 3 respMetaHeaderXHeadersField = 4 respMetaHeaderOriginField = 5 + respMetaHeaderStatusField = 6 respVerifHeaderBodySignatureField = 1 respVerifHeaderMetaSignatureField = 2 @@ -712,7 +713,14 @@ func (r *ResponseMetaHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n } - _, err = proto.NestedStructureMarshal(respMetaHeaderOriginField, buf[offset:], r.origin) + n, err = proto.NestedStructureMarshal(respMetaHeaderOriginField, buf[offset:], r.origin) + if err != nil { + return nil, err + } + + offset += n + + _, err = proto.NestedStructureMarshal(respMetaHeaderStatusField, buf[offset:], r.status) if err != nil { return nil, err } @@ -737,6 +745,7 @@ func (r *ResponseMetaHeader) StableSize() (size int) { } size += proto.NestedStructureSize(respMetaHeaderOriginField, r.origin) + size += proto.NestedStructureSize(respMetaHeaderStatusField, r.status) return size } diff --git a/v2/session/test/generate.go b/v2/session/test/generate.go index 4aec4a4d..8473d806 100644 --- a/v2/session/test/generate.go +++ b/v2/session/test/generate.go @@ -4,6 +4,7 @@ import ( acltest "github.com/nspcc-dev/neofs-api-go/v2/acl/test" refstest "github.com/nspcc-dev/neofs-api-go/v2/refs/test" "github.com/nspcc-dev/neofs-api-go/v2/session" + statustest "github.com/nspcc-dev/neofs-api-go/v2/status/test" ) func GenerateCreateRequestBody(empty bool) *session.CreateRequestBody { @@ -89,6 +90,7 @@ func generateResponseMetaHeader(empty, withOrigin bool) *session.ResponseMetaHea m.SetXHeaders(GenerateXHeaders(empty)) m.SetVersion(refstest.GenerateVersion(empty)) + m.SetStatus(statustest.Status(empty)) if withOrigin { m.SetOrigin(generateResponseMetaHeader(empty, false)) diff --git a/v2/session/types.go b/v2/session/types.go index 8d092a2c..8a978176 100644 --- a/v2/session/types.go +++ b/v2/session/types.go @@ -3,6 +3,7 @@ package session import ( "github.com/nspcc-dev/neofs-api-go/v2/acl" "github.com/nspcc-dev/neofs-api-go/v2/refs" + "github.com/nspcc-dev/neofs-api-go/v2/status" ) type CreateRequestBody struct { @@ -105,6 +106,8 @@ type ResponseMetaHeader struct { xHeaders []*XHeader origin *ResponseMetaHeader + + status *status.Status } const ( @@ -608,6 +611,39 @@ func (r *ResponseMetaHeader) SetOrigin(v *ResponseMetaHeader) { } } +// GetStatus returns response status. +func (r *ResponseMetaHeader) GetStatus() *status.Status { + if r != nil { + return r.status + } + + return nil +} + +// SetStatus sets response status. +func (r *ResponseMetaHeader) SetStatus(v *status.Status) { + if r != nil { + r.status = v + } +} + +// SetStatus sets status of the message which can carry ResponseMetaHeader. +// +// Sets status field on the "highest" level of meta headers. +// If meta header is missing in message, it is allocated. +func SetStatus(msg interface { + GetMetaHeader() *ResponseMetaHeader + SetMetaHeader(*ResponseMetaHeader) +}, st *status.Status) { + meta := msg.GetMetaHeader() + if meta == nil { + meta = new(ResponseMetaHeader) + msg.SetMetaHeader(meta) + } + + meta.SetStatus(st) +} + func (c *ObjectSessionContext) sessionTokenContext() {} func (c *ObjectSessionContext) GetVerb() ObjectSessionVerb {