github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/refs/marshal.go (about)

     1  package refs
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	refs "github.com/TrueCloudLab/frostfs-api-go/v2/refs/grpc"
     7  	"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message"
     8  	"github.com/TrueCloudLab/frostfs-api-go/v2/util/proto"
     9  )
    10  
    11  const (
    12  	ownerIDValField = 1
    13  
    14  	containerIDValField = 1
    15  
    16  	objectIDValField = 1
    17  
    18  	addressContainerField = 1
    19  	addressObjectField    = 2
    20  
    21  	checksumTypeField  = 1
    22  	checksumValueField = 2
    23  
    24  	signatureKeyField    = 1
    25  	signatureValueField  = 2
    26  	signatureSchemeField = 3
    27  
    28  	versionMajorField = 1
    29  	versionMinorField = 2
    30  )
    31  
    32  func (o *OwnerID) StableMarshal(buf []byte) []byte {
    33  	if o == nil {
    34  		return []byte{}
    35  	}
    36  
    37  	if buf == nil {
    38  		buf = make([]byte, o.StableSize())
    39  	}
    40  
    41  	proto.BytesMarshal(ownerIDValField, buf, o.val)
    42  
    43  	return buf
    44  }
    45  
    46  func (o *OwnerID) StableSize() int {
    47  	if o == nil {
    48  		return 0
    49  	}
    50  
    51  	return proto.BytesSize(ownerIDValField, o.val)
    52  }
    53  
    54  func (o *OwnerID) Unmarshal(data []byte) error {
    55  	return message.Unmarshal(o, data, new(refs.OwnerID))
    56  }
    57  
    58  func (c *ContainerID) StableMarshal(buf []byte) []byte {
    59  	if c == nil {
    60  		return []byte{}
    61  	}
    62  
    63  	if buf == nil {
    64  		buf = make([]byte, c.StableSize())
    65  	}
    66  
    67  	proto.BytesMarshal(containerIDValField, buf, c.val)
    68  
    69  	return buf
    70  }
    71  
    72  func (c *ContainerID) StableSize() int {
    73  	if c == nil {
    74  		return 0
    75  	}
    76  
    77  	return proto.BytesSize(containerIDValField, c.val)
    78  }
    79  
    80  func (c *ContainerID) Unmarshal(data []byte) error {
    81  	return message.Unmarshal(c, data, new(refs.ContainerID))
    82  }
    83  
    84  func (o *ObjectID) StableMarshal(buf []byte) []byte {
    85  	if o == nil {
    86  		return []byte{}
    87  	}
    88  
    89  	if buf == nil {
    90  		buf = make([]byte, o.StableSize())
    91  	}
    92  
    93  	proto.BytesMarshal(objectIDValField, buf, o.val)
    94  
    95  	return buf
    96  }
    97  
    98  // ObjectIDNestedListSize returns byte length of nested
    99  // repeated ObjectID field with fNum number.
   100  func ObjectIDNestedListSize(fNum int64, ids []ObjectID) (sz int) {
   101  	for i := range ids {
   102  		sz += proto.NestedStructureSize(fNum, &ids[i])
   103  	}
   104  
   105  	return
   106  }
   107  
   108  func (o *ObjectID) StableSize() int {
   109  	if o == nil {
   110  		return 0
   111  	}
   112  
   113  	return proto.BytesSize(objectIDValField, o.val)
   114  }
   115  
   116  // ObjectIDNestedListMarshal writes protobuf repeated ObjectID field
   117  // with fNum number to buf.
   118  func ObjectIDNestedListMarshal(fNum int64, buf []byte, ids []ObjectID) (off int) {
   119  	prefix, _ := proto.NestedStructurePrefix(fNum)
   120  	for i := range ids {
   121  		off += binary.PutUvarint(buf[off:], prefix)
   122  
   123  		n := ids[i].StableSize()
   124  		off += binary.PutUvarint(buf[off:], uint64(n))
   125  		off += proto.BytesMarshal(objectIDValField, buf[off:], ids[i].val)
   126  	}
   127  
   128  	return
   129  }
   130  
   131  func (o *ObjectID) Unmarshal(data []byte) error {
   132  	return message.Unmarshal(o, data, new(refs.ObjectID))
   133  }
   134  
   135  func (a *Address) StableMarshal(buf []byte) []byte {
   136  	if a == nil {
   137  		return []byte{}
   138  	}
   139  
   140  	if buf == nil {
   141  		buf = make([]byte, a.StableSize())
   142  	}
   143  
   144  	var offset int
   145  
   146  	offset += proto.NestedStructureMarshal(addressContainerField, buf[offset:], a.cid)
   147  	proto.NestedStructureMarshal(addressObjectField, buf[offset:], a.oid)
   148  
   149  	return buf
   150  }
   151  
   152  func (a *Address) StableSize() (size int) {
   153  	if a == nil {
   154  		return 0
   155  	}
   156  
   157  	size += proto.NestedStructureSize(addressContainerField, a.cid)
   158  	size += proto.NestedStructureSize(addressObjectField, a.oid)
   159  
   160  	return size
   161  }
   162  
   163  func (a *Address) Unmarshal(data []byte) error {
   164  	return message.Unmarshal(a, data, new(refs.Address))
   165  }
   166  
   167  func (c *Checksum) StableMarshal(buf []byte) []byte {
   168  	if c == nil {
   169  		return []byte{}
   170  	}
   171  
   172  	if buf == nil {
   173  		buf = make([]byte, c.StableSize())
   174  	}
   175  
   176  	var offset int
   177  
   178  	offset += proto.EnumMarshal(checksumTypeField, buf[offset:], int32(c.typ))
   179  	proto.BytesMarshal(checksumValueField, buf[offset:], c.sum)
   180  
   181  	return buf
   182  }
   183  
   184  func (c *Checksum) StableSize() (size int) {
   185  	if c == nil {
   186  		return 0
   187  	}
   188  
   189  	size += proto.EnumSize(checksumTypeField, int32(c.typ))
   190  	size += proto.BytesSize(checksumValueField, c.sum)
   191  
   192  	return size
   193  }
   194  
   195  func (c *Checksum) Unmarshal(data []byte) error {
   196  	return message.Unmarshal(c, data, new(refs.Checksum))
   197  }
   198  
   199  func (s *Signature) StableMarshal(buf []byte) []byte {
   200  	if s == nil {
   201  		return []byte{}
   202  	}
   203  
   204  	if buf == nil {
   205  		buf = make([]byte, s.StableSize())
   206  	}
   207  
   208  	var offset int
   209  
   210  	offset += proto.BytesMarshal(signatureKeyField, buf[offset:], s.key)
   211  	offset += proto.BytesMarshal(signatureValueField, buf[offset:], s.sign)
   212  	proto.EnumMarshal(signatureSchemeField, buf[offset:], int32(s.scheme))
   213  
   214  	return buf
   215  }
   216  
   217  func (s *Signature) StableSize() (size int) {
   218  	if s == nil {
   219  		return 0
   220  	}
   221  
   222  	size += proto.BytesSize(signatureKeyField, s.key)
   223  	size += proto.BytesSize(signatureValueField, s.sign)
   224  	size += proto.EnumSize(signatureSchemeField, int32(s.scheme))
   225  
   226  	return size
   227  }
   228  
   229  func (s *Signature) Unmarshal(data []byte) error {
   230  	return message.Unmarshal(s, data, new(refs.Signature))
   231  }
   232  
   233  func (v *Version) StableMarshal(buf []byte) []byte {
   234  	if v == nil {
   235  		return []byte{}
   236  	}
   237  
   238  	if buf == nil {
   239  		buf = make([]byte, v.StableSize())
   240  	}
   241  
   242  	var offset int
   243  
   244  	offset += proto.UInt32Marshal(versionMajorField, buf[offset:], v.major)
   245  	proto.UInt32Marshal(versionMinorField, buf[offset:], v.minor)
   246  
   247  	return buf
   248  }
   249  
   250  func (v *Version) StableSize() (size int) {
   251  	if v == nil {
   252  		return 0
   253  	}
   254  
   255  	size += proto.UInt32Size(versionMajorField, v.major)
   256  	size += proto.UInt32Size(versionMinorField, v.minor)
   257  
   258  	return size
   259  }
   260  
   261  func (v *Version) Unmarshal(data []byte) error {
   262  	return message.Unmarshal(v, data, new(refs.Version))
   263  }
   264  
   265  // SubnetID message field numbers
   266  const (
   267  	_ = iota
   268  	subnetIDValFNum
   269  )
   270  
   271  // StableMarshal marshals SubnetID to NeoFS API V2 binary format (Protocol Buffers with direct field order).
   272  //
   273  // Returns a slice of recorded data. Data is written to the provided buffer if there is enough space.
   274  func (s *SubnetID) StableMarshal(buf []byte) []byte {
   275  	if s == nil {
   276  		return []byte{}
   277  	}
   278  
   279  	if buf == nil {
   280  		buf = make([]byte, s.StableSize())
   281  	}
   282  
   283  	proto.Fixed32Marshal(subnetIDValFNum, buf, s.value)
   284  
   285  	return buf
   286  }
   287  
   288  // StableSize returns the number of bytes required to write SubnetID in NeoFS API V2 binary format (see StableMarshal).
   289  func (s *SubnetID) StableSize() (size int) {
   290  	if s != nil {
   291  		size += proto.Fixed32Size(subnetIDValFNum, s.value)
   292  	}
   293  
   294  	return
   295  }
   296  
   297  // Unmarshal unmarshals SubnetID from NeoFS API V2 binary format (see StableMarshal).
   298  // Must not be called on nil.
   299  //
   300  // Note: empty data corresponds to zero ID value or nil pointer to it.
   301  func (s *SubnetID) Unmarshal(data []byte) error {
   302  	return message.Unmarshal(s, data, new(refs.SubnetID))
   303  }