From 2cb57a8835671cf3e87f6e6026352c48ac9f2765 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Fri, 30 Jun 2023 15:00:51 +0300 Subject: [PATCH] [#45] api-go: Add PutSingle request wrappers Signed-off-by: Dmitrii Stepanov --- object/convert.go | 72 +++++++++++++++++++++++++++++++++++++++++ object/grpc/service.go | 25 ++++++++++++++ object/marshal.go | 35 ++++++++++++++++++++ object/message_test.go | 1 + object/test/generate.go | 22 +++++++++++++ signature/body.go | 2 ++ 6 files changed, 157 insertions(+) diff --git a/object/convert.go b/object/convert.go index 7099b4c..63f8def 100644 --- a/object/convert.go +++ b/object/convert.go @@ -2042,3 +2042,75 @@ func (r *GetRangeHashResponse) FromGRPCMessage(m grpc.Message) error { return r.ResponseHeaders.FromMessage(v) } + +func (r *PutSingleRequestBody) ToGRPCMessage() grpc.Message { + var m *object.PutSingleRequest_Body + + if r != nil { + m = new(object.PutSingleRequest_Body) + m.SetObject(r.GetObject().ToGRPCMessage().(*object.Object)) + m.SetCopiesNumber(r.GetCopiesNumber()) + } + + return m +} + +func (r *PutSingleRequestBody) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*object.PutSingleRequest_Body) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + if v.GetObject() == nil { + r.object = nil + } else { + if r.object == nil { + r.object = new(Object) + } + + err := r.object.FromGRPCMessage(v.GetObject()) + if err != nil { + return err + } + } + + r.copyNum = v.GetCopiesNumber() + + return nil +} + +func (r *PutSingleRequest) ToGRPCMessage() grpc.Message { + var m *object.PutSingleRequest + + if r != nil { + m = new(object.PutSingleRequest) + + m.SetBody(r.body.ToGRPCMessage().(*object.PutSingleRequest_Body)) + r.RequestHeaders.ToMessage(m) + } + + return m +} + +func (r *PutSingleRequest) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*object.PutSingleRequest) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + body := v.GetBody() + if body == nil { + r.body = nil + } else { + if r.body == nil { + r.body = new(PutSingleRequestBody) + } + + err := r.body.FromGRPCMessage(body) + if err != nil { + return err + } + } + + return r.RequestHeaders.FromMessage(v) +} diff --git a/object/grpc/service.go b/object/grpc/service.go index 74dc168..7e870a1 100644 --- a/object/grpc/service.go +++ b/object/grpc/service.go @@ -495,3 +495,28 @@ func (m *GetRangeHashResponse) SetMetaHeader(v *session.ResponseMetaHeader) { func (m *GetRangeHashResponse) SetVerifyHeader(v *session.ResponseVerificationHeader) { m.VerifyHeader = v } + +// SetObject set object of the body. +func (m *PutSingleRequest_Body) SetObject(o *Object) { + m.Object = o +} + +// SetCopiesNumber sets copies number of the body. +func (m *PutSingleRequest_Body) SetCopiesNumber(v []uint32) { + m.CopiesNumber = v +} + +// SetBody sets body of the request. +func (m *PutSingleRequest) SetBody(b *PutSingleRequest_Body) { + m.Body = b +} + +// SetMetaHeader sets meta header of the request. +func (m *PutSingleRequest) SetMetaHeader(v *session.RequestMetaHeader) { + m.MetaHeader = v +} + +// SetVerifyHeader sets verification header of the request. +func (m *PutSingleRequest) SetVerifyHeader(v *session.RequestVerificationHeader) { + m.VerifyHeader = v +} diff --git a/object/marshal.go b/object/marshal.go index 7a89b97..f210f44 100644 --- a/object/marshal.go +++ b/object/marshal.go @@ -110,6 +110,9 @@ const ( getRangeHashRespBodyTypeField = 1 getRangeHashRespBodyHashListField = 2 + + putSingleReqObjectField = 1 + putSingleReqCopiesNumberField = 2 ) func (h *ShortHeader) StableMarshal(buf []byte) []byte { @@ -1063,3 +1066,35 @@ func (r *GetRangeHashResponseBody) StableSize() (size int) { func (r *GetRangeHashResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.GetRangeHashResponse_Body)) } + +func (r *PutSingleRequestBody) StableMarshal(buf []byte) []byte { + if r == nil { + return []byte{} + } + if buf == nil { + buf = make([]byte, r.StableSize()) + } + + var offset int + offset += proto.NestedStructureMarshal(putSingleReqObjectField, buf[offset:], r.object) + proto.RepeatedUInt32Marshal(putSingleReqCopiesNumberField, buf[offset:], r.copyNum) + + return buf +} + +func (r *PutSingleRequestBody) StableSize() int { + if r == nil { + return 0 + } + + var size int + size += proto.NestedStructureSize(putSingleReqObjectField, r.object) + arrSize, _ := proto.RepeatedUInt32Size(putSingleReqCopiesNumberField, r.copyNum) + size += arrSize + + return size +} + +func (r *PutSingleRequestBody) Unmarshal(data []byte) error { + return message.Unmarshal(r, data, new(object.PutSingleRequest_Body)) +} diff --git a/object/message_test.go b/object/message_test.go index c690e66..ecf0425 100644 --- a/object/message_test.go +++ b/object/message_test.go @@ -51,5 +51,6 @@ func TestMessageConvert(t *testing.T) { func(empty bool) message.Message { return objecttest.GenerateGetRangeHashResponseBody(empty) }, func(empty bool) message.Message { return objecttest.GenerateGetRangeHashResponse(empty) }, func(empty bool) message.Message { return objecttest.GenerateLock(empty) }, + func(empty bool) message.Message { return objecttest.GeneratePutSingleRequest(empty) }, ) } diff --git a/object/test/generate.go b/object/test/generate.go index 32d7a3c..4f163d2 100644 --- a/object/test/generate.go +++ b/object/test/generate.go @@ -610,3 +610,25 @@ func GenerateLock(empty bool) *object.Lock { return m } + +func GeneratePutSingleRequest(empty bool) *object.PutSingleRequest { + m := new(object.PutSingleRequest) + + if !empty { + m.SetBody(GeneratePutSingleRequestBody(false)) + } + + m.SetMetaHeader(sessiontest.GenerateRequestMetaHeader(empty)) + m.SetVerificationHeader(sessiontest.GenerateRequestVerificationHeader(empty)) + + return m +} + +func GeneratePutSingleRequestBody(empty bool) *object.PutSingleRequestBody { + b := new(object.PutSingleRequestBody) + if !empty { + b.SetObject(GenerateObject(empty)) + b.SetCopiesNumber([]uint32{12345}) + } + return b +} diff --git a/signature/body.go b/signature/body.go index 6d43249..68c361d 100644 --- a/signature/body.go +++ b/signature/body.go @@ -87,6 +87,8 @@ func serviceMessageBody(req any) stableMarshaler { return v.GetBody() case *object.GetRangeHashResponse: return v.GetBody() + case *object.PutSingleRequest: + return v.GetBody() /* Netmap */ case *netmap.LocalNodeInfoRequest: