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

     1  package session
     2  
     3  import (
     4  	"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message"
     5  	session "github.com/TrueCloudLab/frostfs-api-go/v2/session/grpc"
     6  	"github.com/TrueCloudLab/frostfs-api-go/v2/util/proto"
     7  	goproto "google.golang.org/protobuf/proto"
     8  )
     9  
    10  const (
    11  	createReqBodyOwnerField      = 1
    12  	createReqBodyExpirationField = 2
    13  
    14  	createRespBodyIDField  = 1
    15  	createRespBodyKeyField = 2
    16  
    17  	xheaderKeyField   = 1
    18  	xheaderValueField = 2
    19  
    20  	lifetimeExpirationField     = 1
    21  	lifetimeNotValidBeforeField = 2
    22  	lifetimeIssuedAtField       = 3
    23  
    24  	objectCtxVerbField   = 1
    25  	objectCtxTargetField = 2
    26  
    27  	sessionTokenBodyIDField        = 1
    28  	sessionTokenBodyOwnerField     = 2
    29  	sessionTokenBodyLifetimeField  = 3
    30  	sessionTokenBodyKeyField       = 4
    31  	sessionTokenBodyObjectCtxField = 5
    32  	sessionTokenBodyCnrCtxField    = 6
    33  
    34  	sessionTokenBodyField      = 1
    35  	sessionTokenSignatureField = 2
    36  
    37  	reqMetaHeaderVersionField      = 1
    38  	reqMetaHeaderEpochField        = 2
    39  	reqMetaHeaderTTLField          = 3
    40  	reqMetaHeaderXHeadersField     = 4
    41  	reqMetaHeaderSessionTokenField = 5
    42  	reqMetaHeaderBearerTokenField  = 6
    43  	reqMetaHeaderOriginField       = 7
    44  	reqMetaHeaderNetMagicField     = 8
    45  
    46  	reqVerifHeaderBodySignatureField   = 1
    47  	reqVerifHeaderMetaSignatureField   = 2
    48  	reqVerifHeaderOriginSignatureField = 3
    49  	reqVerifHeaderOriginField          = 4
    50  
    51  	respMetaHeaderVersionField  = 1
    52  	respMetaHeaderEpochField    = 2
    53  	respMetaHeaderTTLField      = 3
    54  	respMetaHeaderXHeadersField = 4
    55  	respMetaHeaderOriginField   = 5
    56  	respMetaHeaderStatusField   = 6
    57  
    58  	respVerifHeaderBodySignatureField   = 1
    59  	respVerifHeaderMetaSignatureField   = 2
    60  	respVerifHeaderOriginSignatureField = 3
    61  	respVerifHeaderOriginField          = 4
    62  )
    63  
    64  func (c *CreateRequestBody) StableMarshal(buf []byte) []byte {
    65  	if c == nil {
    66  		return []byte{}
    67  	}
    68  
    69  	if buf == nil {
    70  		buf = make([]byte, c.StableSize())
    71  	}
    72  
    73  	var offset int
    74  
    75  	offset += proto.NestedStructureMarshal(createReqBodyOwnerField, buf[offset:], c.ownerID)
    76  	proto.UInt64Marshal(createReqBodyExpirationField, buf[offset:], c.expiration)
    77  
    78  	return buf
    79  }
    80  
    81  func (c *CreateRequestBody) StableSize() (size int) {
    82  	if c == nil {
    83  		return 0
    84  	}
    85  
    86  	size += proto.NestedStructureSize(createReqBodyOwnerField, c.ownerID)
    87  	size += proto.UInt64Size(createReqBodyExpirationField, c.expiration)
    88  
    89  	return size
    90  }
    91  
    92  func (c *CreateRequestBody) Unmarshal(data []byte) error {
    93  	return message.Unmarshal(c, data, new(session.CreateRequest_Body))
    94  }
    95  
    96  func (c *CreateResponseBody) StableMarshal(buf []byte) []byte {
    97  	if c == nil {
    98  		return []byte{}
    99  	}
   100  
   101  	if buf == nil {
   102  		buf = make([]byte, c.StableSize())
   103  	}
   104  
   105  	var offset int
   106  
   107  	offset += proto.BytesMarshal(createRespBodyIDField, buf[offset:], c.id)
   108  	proto.BytesMarshal(createRespBodyKeyField, buf[offset:], c.sessionKey)
   109  
   110  	return buf
   111  }
   112  
   113  func (c *CreateResponseBody) StableSize() (size int) {
   114  	if c == nil {
   115  		return 0
   116  	}
   117  
   118  	size += proto.BytesSize(createRespBodyIDField, c.id)
   119  	size += proto.BytesSize(createRespBodyKeyField, c.sessionKey)
   120  
   121  	return size
   122  }
   123  
   124  func (c *CreateResponseBody) Unmarshal(data []byte) error {
   125  	return message.Unmarshal(c, data, new(session.CreateResponse_Body))
   126  }
   127  
   128  func (x *XHeader) StableMarshal(buf []byte) []byte {
   129  	if x == nil {
   130  		return []byte{}
   131  	}
   132  
   133  	if buf == nil {
   134  		buf = make([]byte, x.StableSize())
   135  	}
   136  
   137  	var offset int
   138  
   139  	offset += proto.StringMarshal(xheaderKeyField, buf[offset:], x.key)
   140  	proto.StringMarshal(xheaderValueField, buf[offset:], x.val)
   141  
   142  	return buf
   143  }
   144  
   145  func (x *XHeader) StableSize() (size int) {
   146  	if x == nil {
   147  		return 0
   148  	}
   149  
   150  	size += proto.StringSize(xheaderKeyField, x.key)
   151  	size += proto.StringSize(xheaderValueField, x.val)
   152  
   153  	return size
   154  }
   155  
   156  func (x *XHeader) Unmarshal(data []byte) error {
   157  	m := new(session.XHeader)
   158  	if err := goproto.Unmarshal(data, m); err != nil {
   159  		return err
   160  	}
   161  
   162  	return x.FromGRPCMessage(m)
   163  }
   164  
   165  func (l *TokenLifetime) StableMarshal(buf []byte) []byte {
   166  	if l == nil {
   167  		return []byte{}
   168  	}
   169  
   170  	if buf == nil {
   171  		buf = make([]byte, l.StableSize())
   172  	}
   173  
   174  	var offset int
   175  
   176  	offset += proto.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp)
   177  	offset += proto.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf)
   178  	proto.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat)
   179  
   180  	return buf
   181  }
   182  
   183  func (l *TokenLifetime) StableSize() (size int) {
   184  	if l == nil {
   185  		return 0
   186  	}
   187  
   188  	size += proto.UInt64Size(lifetimeExpirationField, l.exp)
   189  	size += proto.UInt64Size(lifetimeNotValidBeforeField, l.nbf)
   190  	size += proto.UInt64Size(lifetimeIssuedAtField, l.iat)
   191  
   192  	return size
   193  }
   194  
   195  func (l *TokenLifetime) Unmarshal(data []byte) error {
   196  	m := new(session.SessionToken_Body_TokenLifetime)
   197  	if err := goproto.Unmarshal(data, m); err != nil {
   198  		return err
   199  	}
   200  
   201  	return l.FromGRPCMessage(m)
   202  }
   203  
   204  func (c *ObjectSessionContext) StableMarshal(buf []byte) []byte {
   205  	if c == nil {
   206  		return []byte{}
   207  	}
   208  
   209  	if buf == nil {
   210  		buf = make([]byte, c.StableSize())
   211  	}
   212  
   213  	offset := proto.EnumMarshal(objectCtxVerbField, buf, int32(c.verb))
   214  	proto.NestedStructureMarshal(objectCtxTargetField, buf[offset:], &objectSessionContextTarget{
   215  		cnr:  c.cnr,
   216  		objs: c.objs,
   217  	})
   218  
   219  	return buf
   220  }
   221  
   222  func (c *ObjectSessionContext) StableSize() (size int) {
   223  	if c == nil {
   224  		return 0
   225  	}
   226  
   227  	size += proto.EnumSize(objectCtxVerbField, int32(c.verb))
   228  	size += proto.NestedStructureSize(objectCtxTargetField, &objectSessionContextTarget{
   229  		cnr:  c.cnr,
   230  		objs: c.objs,
   231  	})
   232  
   233  	return size
   234  }
   235  
   236  func (c *ObjectSessionContext) Unmarshal(data []byte) error {
   237  	m := new(session.ObjectSessionContext)
   238  	if err := goproto.Unmarshal(data, m); err != nil {
   239  		return err
   240  	}
   241  
   242  	return c.FromGRPCMessage(m)
   243  }
   244  
   245  const (
   246  	_ = iota
   247  	cnrCtxVerbFNum
   248  	cnrCtxWildcardFNum
   249  	cnrCtxCidFNum
   250  )
   251  
   252  func (x *ContainerSessionContext) StableMarshal(buf []byte) []byte {
   253  	if x == nil {
   254  		return []byte{}
   255  	}
   256  
   257  	if buf == nil {
   258  		buf = make([]byte, x.StableSize())
   259  	}
   260  
   261  	var offset int
   262  
   263  	offset += proto.EnumMarshal(cnrCtxVerbFNum, buf[offset:], int32(ContainerSessionVerbToGRPCField(x.verb)))
   264  	offset += proto.BoolMarshal(cnrCtxWildcardFNum, buf[offset:], x.wildcard)
   265  	proto.NestedStructureMarshal(cnrCtxCidFNum, buf[offset:], x.cid)
   266  
   267  	return buf
   268  }
   269  
   270  func (x *ContainerSessionContext) StableSize() (size int) {
   271  	if x == nil {
   272  		return 0
   273  	}
   274  
   275  	size += proto.EnumSize(cnrCtxVerbFNum, int32(ContainerSessionVerbToGRPCField(x.verb)))
   276  	size += proto.BoolSize(cnrCtxWildcardFNum, x.wildcard)
   277  	size += proto.NestedStructureSize(cnrCtxCidFNum, x.cid)
   278  
   279  	return size
   280  }
   281  
   282  func (x *ContainerSessionContext) Unmarshal(data []byte) error {
   283  	return message.Unmarshal(x, data, new(session.ContainerSessionContext))
   284  }
   285  
   286  func (t *TokenBody) StableMarshal(buf []byte) []byte {
   287  	if t == nil {
   288  		return []byte{}
   289  	}
   290  
   291  	if buf == nil {
   292  		buf = make([]byte, t.StableSize())
   293  	}
   294  
   295  	var offset int
   296  
   297  	offset += proto.BytesMarshal(sessionTokenBodyIDField, buf[offset:], t.id)
   298  	offset += proto.NestedStructureMarshal(sessionTokenBodyOwnerField, buf[offset:], t.ownerID)
   299  	offset += proto.NestedStructureMarshal(sessionTokenBodyLifetimeField, buf[offset:], t.lifetime)
   300  	offset += proto.BytesMarshal(sessionTokenBodyKeyField, buf[offset:], t.sessionKey)
   301  
   302  	if t.ctx != nil {
   303  		switch v := t.ctx.(type) {
   304  		case *ObjectSessionContext:
   305  			proto.NestedStructureMarshal(sessionTokenBodyObjectCtxField, buf[offset:], v)
   306  		case *ContainerSessionContext:
   307  			proto.NestedStructureMarshal(sessionTokenBodyCnrCtxField, buf[offset:], v)
   308  		default:
   309  			panic("cannot marshal unknown session token context")
   310  		}
   311  	}
   312  
   313  	return buf
   314  }
   315  
   316  func (t *TokenBody) StableSize() (size int) {
   317  	if t == nil {
   318  		return 0
   319  	}
   320  
   321  	size += proto.BytesSize(sessionTokenBodyIDField, t.id)
   322  	size += proto.NestedStructureSize(sessionTokenBodyOwnerField, t.ownerID)
   323  	size += proto.NestedStructureSize(sessionTokenBodyLifetimeField, t.lifetime)
   324  	size += proto.BytesSize(sessionTokenBodyKeyField, t.sessionKey)
   325  
   326  	if t.ctx != nil {
   327  		switch v := t.ctx.(type) {
   328  		case *ObjectSessionContext:
   329  			size += proto.NestedStructureSize(sessionTokenBodyObjectCtxField, v)
   330  		case *ContainerSessionContext:
   331  			size += proto.NestedStructureSize(sessionTokenBodyCnrCtxField, v)
   332  		default:
   333  			panic("cannot marshal unknown session token context")
   334  		}
   335  	}
   336  
   337  	return size
   338  }
   339  
   340  func (t *TokenBody) Unmarshal(data []byte) error {
   341  	m := new(session.SessionToken_Body)
   342  	if err := goproto.Unmarshal(data, m); err != nil {
   343  		return err
   344  	}
   345  
   346  	return t.FromGRPCMessage(m)
   347  }
   348  
   349  func (t *Token) StableMarshal(buf []byte) []byte {
   350  	if t == nil {
   351  		return []byte{}
   352  	}
   353  
   354  	if buf == nil {
   355  		buf = make([]byte, t.StableSize())
   356  	}
   357  
   358  	var offset int
   359  
   360  	offset += proto.NestedStructureMarshal(sessionTokenBodyField, buf[offset:], t.body)
   361  	proto.NestedStructureMarshal(sessionTokenSignatureField, buf[offset:], t.sig)
   362  
   363  	return buf
   364  }
   365  
   366  func (t *Token) StableSize() (size int) {
   367  	if t == nil {
   368  		return 0
   369  	}
   370  
   371  	size += proto.NestedStructureSize(sessionTokenBodyField, t.body)
   372  	size += proto.NestedStructureSize(sessionTokenSignatureField, t.sig)
   373  
   374  	return size
   375  }
   376  
   377  func (t *Token) Unmarshal(data []byte) error {
   378  	m := new(session.SessionToken)
   379  	if err := goproto.Unmarshal(data, m); err != nil {
   380  		return err
   381  	}
   382  
   383  	return t.FromGRPCMessage(m)
   384  }
   385  
   386  func (r *RequestMetaHeader) StableMarshal(buf []byte) []byte {
   387  	if r == nil {
   388  		return []byte{}
   389  	}
   390  
   391  	if buf == nil {
   392  		buf = make([]byte, r.StableSize())
   393  	}
   394  
   395  	var offset int
   396  
   397  	offset += proto.NestedStructureMarshal(reqMetaHeaderVersionField, buf[offset:], r.version)
   398  	offset += proto.UInt64Marshal(reqMetaHeaderEpochField, buf[offset:], r.epoch)
   399  	offset += proto.UInt32Marshal(reqMetaHeaderTTLField, buf[offset:], r.ttl)
   400  
   401  	for i := range r.xHeaders {
   402  		offset += proto.NestedStructureMarshal(reqMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i])
   403  	}
   404  
   405  	offset += proto.NestedStructureMarshal(reqMetaHeaderSessionTokenField, buf[offset:], r.sessionToken)
   406  	offset += proto.NestedStructureMarshal(reqMetaHeaderBearerTokenField, buf[offset:], r.bearerToken)
   407  	offset += proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf[offset:], r.origin)
   408  	proto.UInt64Marshal(reqMetaHeaderNetMagicField, buf[offset:], r.netMagic)
   409  
   410  	return buf
   411  }
   412  
   413  func (r *RequestMetaHeader) StableSize() (size int) {
   414  	if r == nil {
   415  		return 0
   416  	}
   417  
   418  	if r.version != nil {
   419  		size += proto.NestedStructureSize(reqMetaHeaderVersionField, r.version)
   420  	}
   421  
   422  	size += proto.UInt64Size(reqMetaHeaderEpochField, r.epoch)
   423  	size += proto.UInt32Size(reqMetaHeaderTTLField, r.ttl)
   424  
   425  	for i := range r.xHeaders {
   426  		size += proto.NestedStructureSize(reqMetaHeaderXHeadersField, &r.xHeaders[i])
   427  	}
   428  
   429  	size += proto.NestedStructureSize(reqMetaHeaderSessionTokenField, r.sessionToken)
   430  	size += proto.NestedStructureSize(reqMetaHeaderBearerTokenField, r.bearerToken)
   431  	size += proto.NestedStructureSize(reqMetaHeaderOriginField, r.origin)
   432  	size += proto.UInt64Size(reqMetaHeaderNetMagicField, r.netMagic)
   433  
   434  	return size
   435  }
   436  
   437  func (r *RequestMetaHeader) Unmarshal(data []byte) error {
   438  	m := new(session.RequestMetaHeader)
   439  	if err := goproto.Unmarshal(data, m); err != nil {
   440  		return err
   441  	}
   442  
   443  	return r.FromGRPCMessage(m)
   444  }
   445  
   446  func (r *RequestVerificationHeader) StableMarshal(buf []byte) []byte {
   447  	if r == nil {
   448  		return []byte{}
   449  	}
   450  
   451  	if buf == nil {
   452  		buf = make([]byte, r.StableSize())
   453  	}
   454  
   455  	var offset int
   456  
   457  	offset += proto.NestedStructureMarshal(reqVerifHeaderBodySignatureField, buf[offset:], r.bodySig)
   458  	offset += proto.NestedStructureMarshal(reqVerifHeaderMetaSignatureField, buf[offset:], r.metaSig)
   459  	offset += proto.NestedStructureMarshal(reqVerifHeaderOriginSignatureField, buf[offset:], r.originSig)
   460  	proto.NestedStructureMarshal(reqVerifHeaderOriginField, buf[offset:], r.origin)
   461  
   462  	return buf
   463  }
   464  
   465  func (r *RequestVerificationHeader) StableSize() (size int) {
   466  	if r == nil {
   467  		return 0
   468  	}
   469  
   470  	size += proto.NestedStructureSize(reqVerifHeaderBodySignatureField, r.bodySig)
   471  	size += proto.NestedStructureSize(reqVerifHeaderMetaSignatureField, r.metaSig)
   472  	size += proto.NestedStructureSize(reqVerifHeaderOriginSignatureField, r.originSig)
   473  	size += proto.NestedStructureSize(reqVerifHeaderOriginField, r.origin)
   474  
   475  	return size
   476  }
   477  
   478  func (r *RequestVerificationHeader) Unmarshal(data []byte) error {
   479  	m := new(session.RequestVerificationHeader)
   480  	if err := goproto.Unmarshal(data, m); err != nil {
   481  		return err
   482  	}
   483  
   484  	return r.FromGRPCMessage(m)
   485  }
   486  
   487  func (r *ResponseMetaHeader) StableMarshal(buf []byte) []byte {
   488  	if r == nil {
   489  		return []byte{}
   490  	}
   491  
   492  	if buf == nil {
   493  		buf = make([]byte, r.StableSize())
   494  	}
   495  
   496  	var offset int
   497  
   498  	offset += proto.NestedStructureMarshal(respMetaHeaderVersionField, buf[offset:], r.version)
   499  	offset += proto.UInt64Marshal(respMetaHeaderEpochField, buf[offset:], r.epoch)
   500  	offset += proto.UInt32Marshal(respMetaHeaderTTLField, buf[offset:], r.ttl)
   501  
   502  	for i := range r.xHeaders {
   503  		offset += proto.NestedStructureMarshal(respMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i])
   504  	}
   505  
   506  	offset += proto.NestedStructureMarshal(respMetaHeaderOriginField, buf[offset:], r.origin)
   507  	proto.NestedStructureMarshal(respMetaHeaderStatusField, buf[offset:], r.status)
   508  
   509  	return buf
   510  }
   511  
   512  func (r *ResponseMetaHeader) StableSize() (size int) {
   513  	if r == nil {
   514  		return 0
   515  	}
   516  
   517  	if r.version != nil {
   518  		size += proto.NestedStructureSize(respMetaHeaderVersionField, r.version)
   519  	}
   520  
   521  	size += proto.UInt64Size(respMetaHeaderEpochField, r.epoch)
   522  	size += proto.UInt32Size(respMetaHeaderTTLField, r.ttl)
   523  
   524  	for i := range r.xHeaders {
   525  		size += proto.NestedStructureSize(respMetaHeaderXHeadersField, &r.xHeaders[i])
   526  	}
   527  
   528  	size += proto.NestedStructureSize(respMetaHeaderOriginField, r.origin)
   529  	size += proto.NestedStructureSize(respMetaHeaderStatusField, r.status)
   530  
   531  	return size
   532  }
   533  
   534  func (r *ResponseMetaHeader) Unmarshal(data []byte) error {
   535  	m := new(session.ResponseMetaHeader)
   536  	if err := goproto.Unmarshal(data, m); err != nil {
   537  		return err
   538  	}
   539  
   540  	return r.FromGRPCMessage(m)
   541  }
   542  
   543  func (r *ResponseVerificationHeader) StableMarshal(buf []byte) []byte {
   544  	if r == nil {
   545  		return []byte{}
   546  	}
   547  
   548  	if buf == nil {
   549  		buf = make([]byte, r.StableSize())
   550  	}
   551  
   552  	var offset int
   553  
   554  	offset += proto.NestedStructureMarshal(respVerifHeaderBodySignatureField, buf[offset:], r.bodySig)
   555  	offset += proto.NestedStructureMarshal(respVerifHeaderMetaSignatureField, buf[offset:], r.metaSig)
   556  	offset += proto.NestedStructureMarshal(respVerifHeaderOriginSignatureField, buf[offset:], r.originSig)
   557  	proto.NestedStructureMarshal(respVerifHeaderOriginField, buf[offset:], r.origin)
   558  
   559  	return buf
   560  }
   561  
   562  func (r *ResponseVerificationHeader) StableSize() (size int) {
   563  	if r == nil {
   564  		return 0
   565  	}
   566  
   567  	size += proto.NestedStructureSize(respVerifHeaderBodySignatureField, r.bodySig)
   568  	size += proto.NestedStructureSize(respVerifHeaderMetaSignatureField, r.metaSig)
   569  	size += proto.NestedStructureSize(respVerifHeaderOriginSignatureField, r.originSig)
   570  	size += proto.NestedStructureSize(respVerifHeaderOriginField, r.origin)
   571  
   572  	return size
   573  }
   574  
   575  func (r *ResponseVerificationHeader) Unmarshal(data []byte) error {
   576  	m := new(session.ResponseVerificationHeader)
   577  	if err := goproto.Unmarshal(data, m); err != nil {
   578  		return err
   579  	}
   580  
   581  	return r.FromGRPCMessage(m)
   582  }