[#94] object: Generate protobufs for Patch method

* Generate protobufs for patch method;
* Create marshalers, unmarshalers, converters for gererated types;
* Add unit-tests.

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2024-07-24 17:40:47 +03:00 committed by Evgenii Stratonikov
parent 3dfa2f4fd6
commit 9b90d139c5
9 changed files with 472 additions and 0 deletions

View file

@ -2345,3 +2345,217 @@ func (r *PutSingleResponse) FromGRPCMessage(m grpc.Message) error {
return r.ResponseHeaders.FromMessage(v)
}
func (r *PatchRequestBodyPatch) ToGRPCMessage() grpc.Message {
var m *object.PatchRequest_Body_Patch
if r != nil {
m = new(object.PatchRequest_Body_Patch)
m.SetSourceRange(r.Range.ToGRPCMessage().(*object.Range))
m.SetChunk(r.Chunk)
}
return m
}
func (r *PatchRequestBodyPatch) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*object.PatchRequest_Body_Patch)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
var err error
srcRange := v.GetSourceRange()
if srcRange == nil {
r.Range = nil
} else {
if r.Range == nil {
r.Range = new(Range)
}
err = r.Range.FromGRPCMessage(srcRange)
if err != nil {
return err
}
}
r.Chunk = v.GetChunk()
return nil
}
func (r *PatchRequestBody) ToGRPCMessage() grpc.Message {
var m *object.PatchRequest_Body
if r != nil {
m = new(object.PatchRequest_Body)
m.SetAddress(r.Address.ToGRPCMessage().(*refsGRPC.Address))
m.SetNewAttributes(AttributesToGRPC(r.NewAttributes))
m.SetReplaceAttributes(r.ReplaceAttributes)
m.SetPatch(r.Patch.ToGRPCMessage().(*object.PatchRequest_Body_Patch))
}
return m
}
func (r *PatchRequestBody) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*object.PatchRequest_Body)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
var err error
addr := v.GetAddress()
if addr == nil {
r.Address = nil
} else {
if r.Address == nil {
r.Address = new(refs.Address)
}
err = r.Address.FromGRPCMessage(addr)
if err != nil {
return err
}
}
r.NewAttributes, err = AttributesFromGRPC(v.GetNewAttributes())
if err != nil {
return err
}
r.ReplaceAttributes = v.GetReplaceAttributes()
patch := v.GetPatch()
if patch == nil {
r.Patch = nil
} else {
if r.Patch == nil {
r.Patch = new(PatchRequestBodyPatch)
}
err = r.Patch.FromGRPCMessage(patch)
if err != nil {
return err
}
}
return nil
}
func (r *PatchRequest) ToGRPCMessage() grpc.Message {
var m *object.PatchRequest
if r != nil {
m = new(object.PatchRequest)
m.SetBody(r.Body.ToGRPCMessage().(*object.PatchRequest_Body))
r.RequestHeaders.ToMessage(m)
}
return m
}
func (r *PatchRequest) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*object.PatchRequest)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
var err error
body := v.GetBody()
if body == nil {
r.Body = nil
} else {
if r.Body == nil {
r.Body = new(PatchRequestBody)
}
err = r.Body.FromGRPCMessage(body)
if err != nil {
return err
}
}
return r.RequestHeaders.FromMessage(v)
}
func (r *PatchResponseBody) ToGRPCMessage() grpc.Message {
var m *object.PatchResponse_Body
if r != nil {
m = new(object.PatchResponse_Body)
m.SetObjectID(r.ObjectID.ToGRPCMessage().(*refsGRPC.ObjectID))
}
return m
}
func (r *PatchResponseBody) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*object.PatchResponse_Body)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
var err error
objID := v.GetObjectId()
if objID == nil {
r.ObjectID = nil
} else {
if r.ObjectID == nil {
r.ObjectID = new(refs.ObjectID)
}
err = r.ObjectID.FromGRPCMessage(objID)
if err != nil {
return err
}
}
return nil
}
func (r *PatchResponse) ToGRPCMessage() grpc.Message {
var m *object.PatchResponse
if r != nil {
m = new(object.PatchResponse)
m.SetBody(r.Body.ToGRPCMessage().(*object.PatchResponse_Body))
r.ResponseHeaders.ToMessage(m)
}
return m
}
func (r *PatchResponse) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*object.PatchResponse)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
var err error
body := v.GetBody()
if body == nil {
r.Body = nil
} else {
if r.Body == nil {
r.Body = new(PatchResponseBody)
}
err = r.Body.FromGRPCMessage(body)
if err != nil {
return err
}
}
return r.ResponseHeaders.FromMessage(v)
}

View file

@ -556,3 +556,55 @@ func (m *PutSingleResponse) SetMetaHeader(v *session.ResponseMetaHeader) {
func (m *PutSingleResponse) SetVerifyHeader(v *session.ResponseVerificationHeader) {
m.VerifyHeader = v
}
func (m *PatchRequest_Body) SetAddress(addr *refs.Address) {
m.Address = addr
}
func (m *PatchRequest_Body) SetNewAttributes(attrs []*Header_Attribute) {
m.NewAttributes = attrs
}
func (m *PatchRequest_Body) SetReplaceAttributes(replaceAttributes bool) {
m.ReplaceAttributes = replaceAttributes
}
func (m *PatchRequest_Body) SetPatch(patch *PatchRequest_Body_Patch) {
m.Patch = patch
}
func (m *PatchRequest_Body_Patch) SetSourceRange(r *Range) {
m.SourceRange = r
}
func (m *PatchRequest_Body_Patch) SetChunk(chunk []byte) {
m.Chunk = chunk
}
func (m *PatchRequest) SetBody(b *PatchRequest_Body) {
m.Body = b
}
func (m *PatchRequest) SetMetaHeader(v *session.RequestMetaHeader) {
m.MetaHeader = v
}
func (m *PatchRequest) SetVerifyHeader(v *session.RequestVerificationHeader) {
m.VerifyHeader = v
}
func (m *PatchResponse_Body) SetObjectID(objectID *refs.ObjectID) {
m.ObjectId = objectID
}
func (m *PatchResponse) SetBody(b *PatchResponse_Body) {
m.Body = b
}
func (m *PatchResponse) SetMetaHeader(v *session.ResponseMetaHeader) {
m.MetaHeader = v
}
func (m *PatchResponse) SetVerifyHeader(v *session.ResponseVerificationHeader) {
m.VerifyHeader = v
}

Binary file not shown.

Binary file not shown.

View file

@ -132,6 +132,16 @@ const (
putSingleReqObjectField = 1
putSingleReqCopiesNumberField = 2
patchRequestBodyPatchRangeField = 1
patchRequestBodyPatchChunkField = 2
patchRequestBodyAddrField = 1
patchRequestBodyNewAttrsField = 2
patchRequestBodyReplaceAttrField = 3
patchRequestBodyPatchField = 4
patchResponseBodyObjectIDField = 1
)
func (h *ShortHeader) StableMarshal(buf []byte) []byte {
@ -1314,3 +1324,105 @@ func (r *PutSingleResponseBody) StableSize() int {
func (r *PutSingleResponseBody) Unmarshal(data []byte) error {
return message.Unmarshal(r, data, new(object.PutSingleResponse_Body))
}
func (r *PatchRequestBodyPatch) StableMarshal(buf []byte) []byte {
if r == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, r.StableSize())
}
var offset int
offset += proto.NestedStructureMarshal(patchRequestBodyPatchRangeField, buf[offset:], r.Range)
offset += proto.BytesMarshal(patchRequestBodyPatchChunkField, buf[offset:], r.Chunk)
return buf
}
func (r *PatchRequestBodyPatch) StableSize() int {
if r == nil {
return 0
}
var size int
size += proto.NestedStructureSize(patchRequestBodyPatchRangeField, r.Range)
size += proto.BytesSize(patchRequestBodyPatchChunkField, r.Chunk)
return size
}
func (r *PatchRequestBodyPatch) Unmarshal(data []byte) error {
return message.Unmarshal(r, data, new(object.PatchRequest_Body_Patch))
}
func (r *PatchRequestBody) StableMarshal(buf []byte) []byte {
if r == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, r.StableSize())
}
var offset int
offset += proto.NestedStructureMarshal(patchRequestBodyAddrField, buf[offset:], r.Address)
for i := range r.NewAttributes {
offset += proto.NestedStructureMarshal(patchRequestBodyNewAttrsField, buf[offset:], &r.NewAttributes[i])
}
offset += proto.BoolMarshal(patchRequestBodyReplaceAttrField, buf[offset:], r.ReplaceAttributes)
offset += proto.NestedStructureMarshal(patchRequestBodyPatchField, buf[offset:], r.Patch)
return buf
}
func (r *PatchRequestBody) StableSize() int {
if r == nil {
return 0
}
var size int
size += proto.NestedStructureSize(patchRequestBodyAddrField, r.Address)
for i := range r.NewAttributes {
size += proto.NestedStructureSize(patchRequestBodyNewAttrsField, &r.NewAttributes[i])
}
size += proto.BoolSize(patchRequestBodyReplaceAttrField, r.ReplaceAttributes)
size += proto.NestedStructureSize(patchRequestBodyPatchField, r.Patch)
return size
}
func (r *PatchRequestBody) Unmarshal(data []byte) error {
return message.Unmarshal(r, data, new(object.PatchRequest_Body))
}
func (r *PatchResponseBody) StableSize() int {
if r == nil {
return 0
}
var size int
size += proto.NestedStructureSize(patchResponseBodyObjectIDField, r.ObjectID)
return size
}
func (r *PatchResponseBody) StableMarshal(buf []byte) []byte {
if r == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, r.StableSize())
}
var offset int
offset += proto.NestedStructureMarshal(patchResponseBodyObjectIDField, buf[offset:], r.ObjectID)
return buf
}
func (r *PatchResponseBody) Unmarshal(data []byte) error {
return message.Unmarshal(r, data, new(object.PatchResponse_Body))
}

View file

@ -56,5 +56,10 @@ func TestMessageConvert(t *testing.T) {
func(empty bool) message.Message { return objecttest.GenerateLock(empty) },
func(empty bool) message.Message { return objecttest.GeneratePutSingleRequest(empty) },
func(empty bool) message.Message { return objecttest.GeneratePutSingleResponse(empty) },
func(empty bool) message.Message { return objecttest.GeneratePatchRequestBodyPatch(empty) },
func(empty bool) message.Message { return objecttest.GeneratePatchRequestBody(empty) },
func(empty bool) message.Message { return objecttest.GeneratePatchRequest(empty) },
func(empty bool) message.Message { return objecttest.GeneratePatchResponseBody(empty) },
func(empty bool) message.Message { return objecttest.GeneratePatchResponse(empty) },
)
}

View file

@ -691,6 +691,63 @@ func GeneratePutSingleResponse(empty bool) *object.PutSingleResponse {
return m
}
func GeneratePatchRequestBodyPatch(empty bool) *object.PatchRequestBodyPatch {
m := new(object.PatchRequestBodyPatch)
if !empty {
m.Range = GenerateRange(false)
m.Chunk = []byte("GeneratePatchRequestBodyPatch")
}
return m
}
func GeneratePatchRequestBody(empty bool) *object.PatchRequestBody {
m := new(object.PatchRequestBody)
if !empty {
m.Address = refstest.GenerateAddress(empty)
m.NewAttributes = GenerateAttributes(empty)
m.ReplaceAttributes = false
m.Patch = GeneratePatchRequestBodyPatch(empty)
}
return m
}
func GeneratePatchRequest(empty bool) *object.PatchRequest {
m := new(object.PatchRequest)
if !empty {
m.Body = GeneratePatchRequestBody(empty)
}
m.SetMetaHeader(sessiontest.GenerateRequestMetaHeader(empty))
m.SetVerificationHeader(sessiontest.GenerateRequestVerificationHeader(empty))
return m
}
func GeneratePatchResponseBody(empty bool) *object.PatchResponseBody {
m := new(object.PatchResponseBody)
if !empty {
m.ObjectID = refstest.GenerateObjectID(empty)
}
return m
}
func GeneratePatchResponse(empty bool) *object.PatchResponse {
m := new(object.PatchResponse)
if !empty {
m.Body = GeneratePatchResponseBody(empty)
}
return m
}
func randomInt(n int) int {
return rand.New(rand.NewSource(time.Now().UnixNano())).Intn(n)
}

View file

@ -349,6 +349,38 @@ type PutSingleResponse struct {
session.ResponseHeaders
}
type PatchRequestBodyPatch struct {
Range *Range
Chunk []byte
}
type PatchRequestBody struct {
Address *refs.Address
NewAttributes []Attribute
ReplaceAttributes bool
Patch *PatchRequestBodyPatch
}
type PatchRequest struct {
Body *PatchRequestBody
session.RequestHeaders
}
type PatchResponseBody struct {
ObjectID *refs.ObjectID
}
type PatchResponse struct {
Body *PatchResponseBody
session.ResponseHeaders
}
const (
TypeRegular Type = iota
TypeTombstone

Binary file not shown.