git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/object/patch.go (about)

     1  package object
     2  
     3  import (
     4  	v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
     5  	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
     6  	oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
     7  )
     8  
     9  // Patch is a patch that's applied for an object.
    10  type Patch struct {
    11  	// The address of the object for which the patch is being applied.
    12  	Address oid.Address
    13  
    14  	// The list of new attributes to set in the object's header.
    15  	NewAttributes []Attribute
    16  
    17  	// If ReplaceAttributes flag is true, then the header's attributes are reset and
    18  	// filled with NewAttributes. Otherwise, the attributes are just merged.
    19  	ReplaceAttributes bool
    20  
    21  	// Payload patch. If this field is not set, then it assumed such Patch patches only
    22  	// header (see NewAttributes, ReplaceAttributes).
    23  	PayloadPatch *PayloadPatch
    24  }
    25  
    26  func (p *Patch) ToV2() *v2object.PatchRequestBody {
    27  	if p == nil {
    28  		return nil
    29  	}
    30  
    31  	v2 := new(v2object.PatchRequestBody)
    32  
    33  	addrV2 := new(refs.Address)
    34  	p.Address.WriteToV2(addrV2)
    35  	v2.SetAddress(addrV2)
    36  
    37  	attrs := make([]v2object.Attribute, len(p.NewAttributes))
    38  	for i := range p.NewAttributes {
    39  		attrs[i] = *p.NewAttributes[i].ToV2()
    40  	}
    41  	v2.SetNewAttributes(attrs)
    42  	v2.SetReplaceAttributes(p.ReplaceAttributes)
    43  
    44  	v2.SetPatch(p.PayloadPatch.ToV2())
    45  
    46  	return v2
    47  }
    48  
    49  func (p *Patch) FromV2(patch *v2object.PatchRequestBody) {
    50  	if patch == nil {
    51  		return
    52  	}
    53  
    54  	if addr := patch.GetAddress(); addr != nil {
    55  		_ = p.Address.ReadFromV2(*addr)
    56  	}
    57  
    58  	newAttrs := patch.GetNewAttributes()
    59  	p.NewAttributes = make([]Attribute, len(newAttrs))
    60  	for i := range newAttrs {
    61  		p.NewAttributes[i] = *NewAttributeFromV2(&newAttrs[i])
    62  	}
    63  
    64  	p.ReplaceAttributes = patch.GetReplaceAttributes()
    65  
    66  	if v2patch := patch.GetPatch(); v2patch != nil {
    67  		p.PayloadPatch = new(PayloadPatch)
    68  		p.PayloadPatch.FromV2(v2patch)
    69  	}
    70  }
    71  
    72  // Patch is a patch that's applied for an object's payload.
    73  type PayloadPatch struct {
    74  	// Range of the patch application.
    75  	Range *Range
    76  
    77  	// Chunk is the payload that replaces (or is appended to) the original object payload.
    78  	Chunk []byte
    79  }
    80  
    81  func (p *PayloadPatch) ToV2() *v2object.PatchRequestBodyPatch {
    82  	if p == nil {
    83  		return nil
    84  	}
    85  
    86  	v2 := new(v2object.PatchRequestBodyPatch)
    87  
    88  	v2.Chunk = p.Chunk
    89  	v2.Range = p.Range.ToV2()
    90  
    91  	return v2
    92  }
    93  
    94  func (p *PayloadPatch) FromV2(patch *v2object.PatchRequestBodyPatch) {
    95  	if patch == nil {
    96  		return
    97  	}
    98  
    99  	p.Chunk = patch.Chunk
   100  	if patch.Range != nil {
   101  		p.Range = NewRangeFromV2(patch.Range)
   102  	}
   103  }