From 84d981e88e53ee40381ca4d3a05bec59a5af88b5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 12 Mar 2022 15:20:19 +0300 Subject: [PATCH] util/proto: do not return error from primitive marshalers Signed-off-by: Evgenii Stratonikov --- accounting/marshal.go | 18 +--- acl/marshal.go | 93 +++-------------- audit/marshal.go | 75 ++------------ container/marshal.go | 45 ++------ netmap/marshal.go | 190 ++++++---------------------------- object/marshal.go | 204 ++++++------------------------------- refs/marshal.go | 77 +++----------- reputation/marshal.go | 36 ++----- session/marshal.go | 160 +++++------------------------ status/marshal.go | 26 +---- storagegroup/marshal.go | 14 +-- tombstone/marshal.go | 15 +-- util/proto/marshal.go | 80 +++++++-------- util/proto/marshal_test.go | 101 ++++-------------- 14 files changed, 202 insertions(+), 932 deletions(-) diff --git a/accounting/marshal.go b/accounting/marshal.go index 8c2e06f5..0d908c01 100644 --- a/accounting/marshal.go +++ b/accounting/marshal.go @@ -24,22 +24,10 @@ func (d *Decimal) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, d.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.Int64Marshal(decimalValueField, buf[offset:], d.val) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.UInt32Marshal(decimalPrecisionField, buf[offset:], d.prec) - if err != nil { - return nil, err - } + offset += protoutil.Int64Marshal(decimalValueField, buf[offset:], d.val) + protoutil.UInt32Marshal(decimalPrecisionField, buf[offset:], d.prec) return buf, nil } diff --git a/acl/marshal.go b/acl/marshal.go index 5ef3676f..11c6174d 100644 --- a/acl/marshal.go +++ b/acl/marshal.go @@ -114,19 +114,8 @@ func (r *Record) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = protoutil.EnumMarshal(recordOperationField, buf[offset:], int32(r.op)) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.EnumMarshal(recordActionField, buf[offset:], int32(r.action)) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.EnumMarshal(recordOperationField, buf[offset:], int32(r.op)) + offset += protoutil.EnumMarshal(recordActionField, buf[offset:], int32(r.action)) for i := range r.filters { n, err = protoutil.NestedStructureMarshal(recordFiltersField, buf[offset:], &r.filters[i]) @@ -184,36 +173,12 @@ func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, f.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.EnumMarshal(filterHeaderTypeField, buf[offset:], int32(f.hdrType)) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.EnumMarshal(filterMatchTypeField, buf[offset:], int32(f.matchType)) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.StringMarshal(filterNameField, buf[offset:], f.key) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.StringMarshal(filterValueField, buf[offset:], f.value) - if err != nil { - return nil, err - } + offset += protoutil.EnumMarshal(filterHeaderTypeField, buf[offset:], int32(f.hdrType)) + offset += protoutil.EnumMarshal(filterMatchTypeField, buf[offset:], int32(f.matchType)) + offset += protoutil.StringMarshal(filterNameField, buf[offset:], f.key) + protoutil.StringMarshal(filterValueField, buf[offset:], f.value) return buf, nil } @@ -247,22 +212,10 @@ func (t *Target) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, t.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.EnumMarshal(targetTypeField, buf[offset:], int32(t.role)) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.RepeatedBytesMarshal(targetKeysField, buf[offset:], t.keys) - if err != nil { - return nil, err - } + offset += protoutil.EnumMarshal(targetTypeField, buf[offset:], int32(t.role)) + protoutil.RepeatedBytesMarshal(targetKeysField, buf[offset:], t.keys) return buf, nil } @@ -292,29 +245,11 @@ func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, l.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) - if err != nil { - return nil, err - } + offset += protoutil.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp) + offset += protoutil.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) + protoutil.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) return buf, nil } diff --git a/audit/marshal.go b/audit/marshal.go index dc46a21f..02633a42 100644 --- a/audit/marshal.go +++ b/audit/marshal.go @@ -48,12 +48,7 @@ func (a *DataAuditResult) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.Fixed64Marshal(auditEpochFNum, buf[offset:], a.auditEpoch) - if err != nil { - return nil, err - } - - offset += n + offset += proto.Fixed64Marshal(auditEpochFNum, buf[offset:], a.auditEpoch) n, err = proto.NestedStructureMarshal(cidFNum, buf[offset:], a.cid) if err != nil { @@ -62,33 +57,10 @@ func (a *DataAuditResult) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.BytesMarshal(pubKeyFNum, buf[offset:], a.pubKey) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.BoolMarshal(completeFNum, buf[offset:], a.complete) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt32Marshal(requestsFNum, buf[offset:], a.requests) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt32Marshal(retriesFNum, buf[offset:], a.retries) - if err != nil { - return nil, err - } - - offset += n + offset += proto.BytesMarshal(pubKeyFNum, buf[offset:], a.pubKey) + offset += proto.BoolMarshal(completeFNum, buf[offset:], a.complete) + offset += proto.UInt32Marshal(requestsFNum, buf[offset:], a.requests) + offset += proto.UInt32Marshal(retriesFNum, buf[offset:], a.retries) n, err = refs.ObjectIDNestedListMarshal(passSGFNum, buf[offset:], a.passSG) if err != nil { @@ -104,38 +76,11 @@ func (a *DataAuditResult) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.UInt32Marshal(hitFNum, buf[offset:], a.hit) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt32Marshal(missFNum, buf[offset:], a.miss) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt32Marshal(failFNum, buf[offset:], a.fail) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.RepeatedBytesMarshal(passNodesFNum, buf[offset:], a.passNodes) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.RepeatedBytesMarshal(failNodesFNum, buf[offset:], a.failNodes) - if err != nil { - return nil, err - } + offset += proto.UInt32Marshal(hitFNum, buf[offset:], a.hit) + offset += proto.UInt32Marshal(missFNum, buf[offset:], a.miss) + offset += proto.UInt32Marshal(failFNum, buf[offset:], a.fail) + offset += proto.RepeatedBytesMarshal(passNodesFNum, buf[offset:], a.passNodes) + proto.RepeatedBytesMarshal(failNodesFNum, buf[offset:], a.failNodes) return buf, nil } diff --git a/container/marshal.go b/container/marshal.go index f5f7c11f..f8a940d3 100644 --- a/container/marshal.go +++ b/container/marshal.go @@ -60,22 +60,10 @@ func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, a.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.StringMarshal(attributeKeyField, buf[offset:], a.key) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.StringMarshal(attributeValueField, buf[offset:], a.val) - if err != nil { - return nil, err - } + offset += protoutil.StringMarshal(attributeKeyField, buf[offset:], a.key) + protoutil.StringMarshal(attributeValueField, buf[offset:], a.val) return buf, nil } @@ -123,19 +111,8 @@ func (c *Container) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = protoutil.BytesMarshal(containerNonceField, buf[offset:], c.nonce) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.UInt32Marshal(containerBasicACLField, buf[offset:], c.basicACL) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.BytesMarshal(containerNonceField, buf[offset:], c.nonce) + offset += protoutil.UInt32Marshal(containerBasicACLField, buf[offset:], c.basicACL) for i := range c.attr { n, err = protoutil.NestedStructureMarshal(containerAttributesField, buf[offset:], &c.attr[i]) @@ -612,12 +589,7 @@ func (a *UsedSpaceAnnouncement) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = protoutil.UInt64Marshal(usedSpaceAnnounceEpochField, buf[offset:], a.epoch) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.UInt64Marshal(usedSpaceAnnounceEpochField, buf[offset:], a.epoch) n, err = protoutil.NestedStructureMarshal(usedSpaceAnnounceCIDField, buf[offset:], a.cid) if err != nil { @@ -626,10 +598,7 @@ func (a *UsedSpaceAnnouncement) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = protoutil.UInt64Marshal(usedSpaceAnnounceUsedSpaceField, buf[offset:], a.usedSpace) - if err != nil { - return nil, err - } + protoutil.UInt64Marshal(usedSpaceAnnounceUsedSpaceField, buf[offset:], a.usedSpace) return buf, nil } diff --git a/netmap/marshal.go b/netmap/marshal.go index 70a8b4cb..72a974e5 100644 --- a/netmap/marshal.go +++ b/netmap/marshal.go @@ -55,33 +55,10 @@ func (f *Filter) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = protoutil.StringMarshal(nameFilterField, buf[offset:], f.name) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.StringMarshal(keyFilterField, buf[offset:], f.key) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.EnumMarshal(opFilterField, buf[offset:], int32(f.op)) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.StringMarshal(valueFilterField, buf[offset:], f.value) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.StringMarshal(nameFilterField, buf[offset:], f.name) + offset += protoutil.StringMarshal(keyFilterField, buf[offset:], f.key) + offset += protoutil.EnumMarshal(opFilterField, buf[offset:], int32(f.op)) + offset += protoutil.StringMarshal(valueFilterField, buf[offset:], f.value) for i := range f.filters { n, err = protoutil.NestedStructureMarshal(filtersFilterField, buf[offset:], &f.filters[i]) @@ -120,43 +97,13 @@ func (s *Selector) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, s.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.StringMarshal(nameSelectorField, buf[offset:], s.name) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.UInt32Marshal(countSelectorField, buf[offset:], s.count) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.EnumMarshal(clauseSelectorField, buf[offset:], int32(s.clause)) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.StringMarshal(attributeSelectorField, buf[offset:], s.attribute) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.StringMarshal(filterSelectorField, buf[offset:], s.filter) - if err != nil { - return nil, err - } + offset += protoutil.StringMarshal(nameSelectorField, buf[offset:], s.name) + offset += protoutil.UInt32Marshal(countSelectorField, buf[offset:], s.count) + offset += protoutil.EnumMarshal(clauseSelectorField, buf[offset:], int32(s.clause)) + offset += protoutil.StringMarshal(attributeSelectorField, buf[offset:], s.attribute) + protoutil.StringMarshal(filterSelectorField, buf[offset:], s.filter) return buf, nil } @@ -184,22 +131,10 @@ func (r *Replica) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.UInt32Marshal(countReplicaField, buf[offset:], r.count) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.StringMarshal(selectorReplicaField, buf[offset:], r.selector) - if err != nil { - return nil, err - } + offset += protoutil.UInt32Marshal(countReplicaField, buf[offset:], r.count) + protoutil.StringMarshal(selectorReplicaField, buf[offset:], r.selector) return buf, nil } @@ -238,12 +173,7 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) ([]byte, error) { offset += n } - n, err = protoutil.UInt32Marshal(backupPolicyField, buf[offset:], p.backupFactor) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.UInt32Marshal(backupPolicyField, buf[offset:], p.backupFactor) for i := range p.selectors { n, err = protoutil.NestedStructureMarshal(selectorsPolicyField, buf[offset:], &p.selectors[i]) @@ -304,32 +234,13 @@ func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, a.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.StringMarshal(keyAttributeField, buf[offset:], a.key) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.StringMarshal(valueAttributeField, buf[offset:], a.value) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.StringMarshal(keyAttributeField, buf[offset:], a.key) + offset += protoutil.StringMarshal(valueAttributeField, buf[offset:], a.value) for i := range a.parents { - n, err = protoutil.StringMarshal(parentsAttributeField, buf[offset:], a.parents[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.StringMarshal(parentsAttributeField, buf[offset:], a.parents[i]) } return buf, nil @@ -368,19 +279,8 @@ func (ni *NodeInfo) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = protoutil.BytesMarshal(keyNodeInfoField, buf[offset:], ni.publicKey) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.RepeatedStringMarshal(addressNodeInfoField, buf[offset:], ni.addresses) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.BytesMarshal(keyNodeInfoField, buf[offset:], ni.publicKey) + offset += protoutil.RepeatedStringMarshal(addressNodeInfoField, buf[offset:], ni.addresses) for i := range ni.attributes { n, err = protoutil.NestedStructureMarshal(attributesNodeInfoField, buf[offset:], &ni.attributes[i]) @@ -391,10 +291,7 @@ func (ni *NodeInfo) StableMarshal(buf []byte) ([]byte, error) { offset += n } - _, err = protoutil.EnumMarshal(stateNodeInfoField, buf[offset:], int32(ni.state)) - if err != nil { - return nil, err - } + protoutil.EnumMarshal(stateNodeInfoField, buf[offset:], int32(ni.state)) return buf, nil } @@ -491,22 +388,10 @@ func (x *NetworkParameter) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.BytesMarshal(netPrmKeyFNum, buf[offset:], x.k) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.BytesMarshal(netPrmValFNum, buf[offset:], x.v) - if err != nil { - return nil, err - } + offset += protoutil.BytesMarshal(netPrmKeyFNum, buf[offset:], x.k) + protoutil.BytesMarshal(netPrmValFNum, buf[offset:], x.v) return buf, nil } @@ -583,30 +468,13 @@ func (i *NetworkInfo) StableMarshal(buf []byte) ([]byte, error) { } var ( - offset, n int - err error + offset int + err error ) - n, err = protoutil.UInt64Marshal(netInfoCurEpochFNum, buf[offset:], i.curEpoch) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.UInt64Marshal(netInfoMagicNumFNum, buf[offset:], i.magicNum) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.Int64Marshal(netInfoMSPerBlockFNum, buf[offset:], i.msPerBlock) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.UInt64Marshal(netInfoCurEpochFNum, buf[offset:], i.curEpoch) + offset += protoutil.UInt64Marshal(netInfoMagicNumFNum, buf[offset:], i.magicNum) + offset += protoutil.Int64Marshal(netInfoMSPerBlockFNum, buf[offset:], i.msPerBlock) _, err = protoutil.NestedStructureMarshal(netInfoCfgFNum, buf[offset:], i.netCfg) if err != nil { diff --git a/object/marshal.go b/object/marshal.go index a3e69b3b..79b767d0 100644 --- a/object/marshal.go +++ b/object/marshal.go @@ -133,12 +133,7 @@ func (h *ShortHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.UInt64Marshal(shortHdrEpochField, buf[offset:], h.creatEpoch) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt64Marshal(shortHdrEpochField, buf[offset:], h.creatEpoch) n, err = proto.NestedStructureMarshal(shortHdrOwnerField, buf[offset:], h.ownerID) if err != nil { @@ -147,19 +142,8 @@ func (h *ShortHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.EnumMarshal(shortHdrObjectTypeField, buf[offset:], int32(h.typ)) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt64Marshal(shortHdrPayloadLength, buf[offset:], h.payloadLen) - if err != nil { - return nil, err - } - - offset += n + offset += proto.EnumMarshal(shortHdrObjectTypeField, buf[offset:], int32(h.typ)) + offset += proto.UInt64Marshal(shortHdrPayloadLength, buf[offset:], h.payloadLen) n, err = proto.NestedStructureMarshal(shortHdrHashField, buf[offset:], h.payloadHash) if err != nil { @@ -205,22 +189,10 @@ func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, a.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.StringMarshal(attributeKeyField, buf[offset:], a.key) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.StringMarshal(attributeValueField, buf[offset:], a.val) - if err != nil { - return nil, err - } + offset += proto.StringMarshal(attributeKeyField, buf[offset:], a.key) + proto.StringMarshal(attributeValueField, buf[offset:], a.val) return buf, nil } @@ -289,10 +261,7 @@ func (h *SplitHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.BytesMarshal(splitHdrSplitIDField, buf[offset:], h.splitID) - if err != nil { - return nil, err - } + proto.BytesMarshal(splitHdrSplitIDField, buf[offset:], h.splitID) return buf, nil } @@ -351,19 +320,8 @@ func (h *Header) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.UInt64Marshal(hdrEpochField, buf[offset:], h.creatEpoch) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt64Marshal(hdrPayloadLengthField, buf[offset:], h.payloadLen) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt64Marshal(hdrEpochField, buf[offset:], h.creatEpoch) + offset += proto.UInt64Marshal(hdrPayloadLengthField, buf[offset:], h.payloadLen) n, err = proto.NestedStructureMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash) if err != nil { @@ -372,12 +330,7 @@ func (h *Header) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.EnumMarshal(hdrObjectTypeField, buf[offset:], int32(h.typ)) - if err != nil { - return nil, err - } - - offset += n + offset += proto.EnumMarshal(hdrObjectTypeField, buf[offset:], int32(h.typ)) n, err = proto.NestedStructureMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash) if err != nil { @@ -515,10 +468,7 @@ func (o *Object) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.BytesMarshal(objPayloadField, buf[offset:], o.payload) - if err != nil { - return nil, err - } + proto.BytesMarshal(objPayloadField, buf[offset:], o.payload) return buf, nil } @@ -554,12 +504,7 @@ func (s *SplitInfo) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = proto.BytesMarshal(splitInfoSplitIDField, buf[offset:], s.splitID) - if err != nil { - return nil, err - } - - offset += n + offset += proto.BytesMarshal(splitInfoSplitIDField, buf[offset:], s.splitID) n, err = proto.NestedStructureMarshal(splitInfoLastPartField, buf[offset:], s.lastPart) if err != nil { @@ -613,10 +558,7 @@ func (r *GetRequestBody) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.BoolMarshal(getReqBodyRawFlagField, buf[offset:], r.raw) - if err != nil { - return nil, err - } + proto.BoolMarshal(getReqBodyRawFlagField, buf[offset:], r.raw) return buf, nil } @@ -706,10 +648,7 @@ func (r *GetResponseBody) StableMarshal(buf []byte) ([]byte, error) { } case *GetObjectPartChunk: if v != nil { - _, err := proto.BytesMarshal(getRespBodyChunkField, buf, v.chunk) - if err != nil { - return nil, err - } + proto.BytesMarshal(getRespBodyChunkField, buf, v.chunk) } case *SplitInfo: _, err := proto.NestedStructureMarshal(getRespBodySplitInfoField, buf, v) @@ -784,10 +723,7 @@ func (r *PutObjectPartInit) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.UInt32Marshal(putReqInitCopiesNumField, buf[offset:], r.copyNum) - if err != nil { - return nil, err - } + proto.UInt32Marshal(putReqInitCopiesNumField, buf[offset:], r.copyNum) return buf, nil } @@ -827,10 +763,7 @@ func (r *PutRequestBody) StableMarshal(buf []byte) ([]byte, error) { } case *PutObjectPartChunk: if v != nil { - _, err := proto.BytesMarshal(putReqBodyChunkField, buf, v.chunk) - if err != nil { - return nil, err - } + proto.BytesMarshal(putReqBodyChunkField, buf, v.chunk) } default: panic("unknown one of object put request body type") @@ -977,17 +910,8 @@ func (r *HeadRequestBody) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.BoolMarshal(headReqBodyMainFlagField, buf[offset:], r.mainOnly) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.BoolMarshal(headReqBodyRawFlagField, buf[offset:], r.raw) - if err != nil { - return nil, err - } + offset += proto.BoolMarshal(headReqBodyMainFlagField, buf[offset:], r.mainOnly) + proto.BoolMarshal(headReqBodyRawFlagField, buf[offset:], r.raw) return buf, nil } @@ -1086,29 +1010,11 @@ func (f *SearchFilter) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, f.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.EnumMarshal(searchFilterMatchField, buf[offset:], int32(f.matchType)) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.StringMarshal(searchFilterNameField, buf[offset:], f.key) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.StringMarshal(searchFilterValueField, buf[offset:], f.val) - if err != nil { - return nil, err - } + offset += proto.EnumMarshal(searchFilterMatchField, buf[offset:], int32(f.matchType)) + offset += proto.StringMarshal(searchFilterNameField, buf[offset:], f.key) + proto.StringMarshal(searchFilterValueField, buf[offset:], f.val) return buf, nil } @@ -1150,12 +1056,7 @@ func (r *SearchRequestBody) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.UInt32Marshal(searchReqBodyVersionField, buf[offset:], r.version) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt32Marshal(searchReqBodyVersionField, buf[offset:], r.version) for i := range r.filters { n, err = proto.NestedStructureMarshal(searchReqBodyFiltersField, buf[offset:], &r.filters[i]) @@ -1233,22 +1134,10 @@ func (r *Range) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.UInt64Marshal(rangeOffsetField, buf[offset:], r.off) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.UInt64Marshal(rangeLengthField, buf[offset:], r.len) - if err != nil { - return nil, err - } + offset += proto.UInt64Marshal(rangeOffsetField, buf[offset:], r.off) + proto.UInt64Marshal(rangeLengthField, buf[offset:], r.len) return buf, nil } @@ -1296,10 +1185,7 @@ func (r *GetRangeRequestBody) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.BoolMarshal(getRangeReqBodyRawField, buf[offset:], r.raw) - if err != nil { - return nil, err - } + proto.BoolMarshal(getRangeReqBodyRawField, buf[offset:], r.raw) return buf, nil } @@ -1333,10 +1219,7 @@ func (r *GetRangeResponseBody) StableMarshal(buf []byte) ([]byte, error) { case nil: case *GetRangePartChunk: if v != nil { - _, err := proto.BytesMarshal(getRangeRespChunkField, buf, v.chunk) - if err != nil { - return nil, err - } + proto.BytesMarshal(getRangeRespChunkField, buf, v.chunk) } case *SplitInfo: if v != nil { @@ -1408,17 +1291,8 @@ func (r *GetRangeHashRequestBody) StableMarshal(buf []byte) ([]byte, error) { offset += n } - n, err = proto.BytesMarshal(getRangeHashReqBodySaltField, buf[offset:], r.salt) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.EnumMarshal(getRangeHashReqBodyTypeField, buf[offset:], int32(r.typ)) - if err != nil { - return nil, err - } + offset += proto.BytesMarshal(getRangeHashReqBodySaltField, buf[offset:], r.salt) + proto.EnumMarshal(getRangeHashReqBodyTypeField, buf[offset:], int32(r.typ)) return buf, nil } @@ -1453,22 +1327,10 @@ func (r *GetRangeHashResponseBody) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.EnumMarshal(getRangeHashRespBodyTypeField, buf, int32(r.typ)) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.RepeatedBytesMarshal(getRangeHashRespBodyHashListField, buf[offset:], r.hashList) - if err != nil { - return nil, err - } + offset += proto.EnumMarshal(getRangeHashRespBodyTypeField, buf, int32(r.typ)) + proto.RepeatedBytesMarshal(getRangeHashRespBodyHashListField, buf[offset:], r.hashList) return buf, nil } diff --git a/refs/marshal.go b/refs/marshal.go index aa04b63e..ffb1ba50 100644 --- a/refs/marshal.go +++ b/refs/marshal.go @@ -36,10 +36,7 @@ func (o *OwnerID) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, o.StableSize()) } - _, err := proto.BytesMarshal(ownerIDValField, buf, o.val) - if err != nil { - return nil, err - } + proto.BytesMarshal(ownerIDValField, buf, o.val) return buf, nil } @@ -65,10 +62,7 @@ func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, c.StableSize()) } - _, err := proto.BytesMarshal(containerIDValField, buf, c.val) - if err != nil { - return nil, err - } + proto.BytesMarshal(containerIDValField, buf, c.val) return buf, nil } @@ -94,10 +88,7 @@ func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, o.StableSize()) } - _, err := proto.BytesMarshal(objectIDValField, buf, o.val) - if err != nil { - return nil, err - } + proto.BytesMarshal(objectIDValField, buf, o.val) return buf, nil } @@ -195,22 +186,10 @@ func (c *Checksum) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, c.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.EnumMarshal(checksumTypeField, buf[offset:], int32(c.typ)) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.BytesMarshal(checksumValueField, buf[offset:], c.sum) - if err != nil { - return nil, err - } + offset += proto.EnumMarshal(checksumTypeField, buf[offset:], int32(c.typ)) + proto.BytesMarshal(checksumValueField, buf[offset:], c.sum) return buf, nil } @@ -239,29 +218,11 @@ func (s *Signature) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, s.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.BytesMarshal(signatureKeyField, buf[offset:], s.key) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.BytesMarshal(signatureValueField, buf[offset:], s.sign) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.EnumMarshal(signatureSchemeField, buf[offset:], int32(s.scheme)) - if err != nil { - return nil, err - } + offset += proto.BytesMarshal(signatureKeyField, buf[offset:], s.key) + offset += proto.BytesMarshal(signatureValueField, buf[offset:], s.sign) + proto.EnumMarshal(signatureSchemeField, buf[offset:], int32(s.scheme)) return buf, nil } @@ -291,22 +252,10 @@ func (v *Version) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, v.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.UInt32Marshal(versionMajorField, buf[offset:], v.major) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.UInt32Marshal(versionMinorField, buf[offset:], v.minor) - if err != nil { - return nil, err - } + offset += proto.UInt32Marshal(versionMajorField, buf[offset:], v.major) + proto.UInt32Marshal(versionMinorField, buf[offset:], v.minor) return buf, nil } diff --git a/reputation/marshal.go b/reputation/marshal.go index 20d1fd77..789cb14e 100644 --- a/reputation/marshal.go +++ b/reputation/marshal.go @@ -20,10 +20,7 @@ func (x *PeerID) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, x.StableSize()) } - _, err := protoutil.BytesMarshal(peerIDPubKeyFNum, buf, x.publicKey) - if err != nil { - return nil, err - } + protoutil.BytesMarshal(peerIDPubKeyFNum, buf, x.publicKey) return buf, nil } @@ -65,10 +62,7 @@ func (x *Trust) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = protoutil.Float64Marshal(trustValueFNum, buf[offset:], x.val) - if err != nil { - return nil, err - } + protoutil.Float64Marshal(trustValueFNum, buf[offset:], x.val) return buf, nil } @@ -230,12 +224,7 @@ func (x *AnnounceLocalTrustRequestBody) StableMarshal(buf []byte) ([]byte, error err error ) - n, err = protoutil.UInt64Marshal(announceLocalTrustBodyEpochFNum, buf[offset:], x.epoch) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.UInt64Marshal(announceLocalTrustBodyEpochFNum, buf[offset:], x.epoch) for i := range x.trusts { n, err = protoutil.NestedStructureMarshal(announceLocalTrustBodyTrustsFNum, buf[offset:], &x.trusts[i]) @@ -292,23 +281,12 @@ func (x *AnnounceIntermediateResultRequestBody) StableMarshal(buf []byte) ([]byt } var ( - offset, n int - err error + offset int + err error ) - n, err = protoutil.UInt64Marshal(announceInterResBodyEpochFNum, buf, x.epoch) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.UInt32Marshal(announceInterResBodyIterFNum, buf[offset:], x.iter) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.UInt64Marshal(announceInterResBodyEpochFNum, buf, x.epoch) + offset += protoutil.UInt32Marshal(announceInterResBodyIterFNum, buf[offset:], x.iter) _, err = protoutil.NestedStructureMarshal(announceInterResBodyTrustFNum, buf[offset:], x.trust) if err != nil { diff --git a/session/marshal.go b/session/marshal.go index a57d1a5c..062076d3 100644 --- a/session/marshal.go +++ b/session/marshal.go @@ -82,10 +82,7 @@ func (c *CreateRequestBody) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.UInt64Marshal(createReqBodyExpirationField, buf[offset:], c.expiration) - if err != nil { - return nil, err - } + proto.UInt64Marshal(createReqBodyExpirationField, buf[offset:], c.expiration) return buf, nil } @@ -114,22 +111,10 @@ func (c *CreateResponseBody) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, c.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.BytesMarshal(createRespBodyIDField, buf[offset:], c.id) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.BytesMarshal(createRespBodyKeyField, buf[offset:], c.sessionKey) - if err != nil { - return nil, err - } + offset += proto.BytesMarshal(createRespBodyIDField, buf[offset:], c.id) + proto.BytesMarshal(createRespBodyKeyField, buf[offset:], c.sessionKey) return buf, nil } @@ -158,22 +143,10 @@ func (x *XHeader) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.StringMarshal(xheaderKeyField, buf[offset:], x.key) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.StringMarshal(xheaderValueField, buf[offset:], x.val) - if err != nil { - return nil, err - } + offset += proto.StringMarshal(xheaderKeyField, buf[offset:], x.key) + proto.StringMarshal(xheaderValueField, buf[offset:], x.val) return buf, nil } @@ -207,29 +180,11 @@ func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, l.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) - if err != nil { - return nil, err - } + offset += proto.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp) + offset += proto.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) + proto.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) return buf, nil } @@ -264,22 +219,10 @@ func (c *ObjectSessionContext) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, c.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.EnumMarshal(objectCtxVerbField, buf[offset:], int32(c.verb)) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.NestedStructureMarshal(objectCtxAddressField, buf[offset:], c.addr) - if err != nil { - return nil, err - } + offset += proto.EnumMarshal(objectCtxVerbField, buf[offset:], int32(c.verb)) + proto.NestedStructureMarshal(objectCtxAddressField, buf[offset:], c.addr) return buf, nil } @@ -320,29 +263,11 @@ func (x *ContainerSessionContext) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.EnumMarshal(cnrCtxVerbFNum, buf[offset:], int32(ContainerSessionVerbToGRPCField(x.verb))) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.BoolMarshal(cnrCtxWildcardFNum, buf[offset:], x.wildcard) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.NestedStructureMarshal(cnrCtxCidFNum, buf[offset:], x.cid) - if err != nil { - return nil, err - } + offset += proto.EnumMarshal(cnrCtxVerbFNum, buf[offset:], int32(ContainerSessionVerbToGRPCField(x.verb))) + offset += proto.BoolMarshal(cnrCtxWildcardFNum, buf[offset:], x.wildcard) + proto.NestedStructureMarshal(cnrCtxCidFNum, buf[offset:], x.cid) return buf, nil } @@ -377,12 +302,7 @@ func (t *TokenBody) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = proto.BytesMarshal(sessionTokenBodyIDField, buf[offset:], t.id) - if err != nil { - return nil, err - } - - offset += n + offset += proto.BytesMarshal(sessionTokenBodyIDField, buf[offset:], t.id) n, err = proto.NestedStructureMarshal(sessionTokenBodyOwnerField, buf[offset:], t.ownerID) if err != nil { @@ -398,12 +318,7 @@ func (t *TokenBody) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.BytesMarshal(sessionTokenBodyKeyField, buf[offset:], t.sessionKey) - if err != nil { - return nil, err - } - - offset += n + offset += proto.BytesMarshal(sessionTokenBodyKeyField, buf[offset:], t.sessionKey) if t.ctx != nil { switch v := t.ctx.(type) { @@ -528,19 +443,8 @@ func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.UInt64Marshal(reqMetaHeaderEpochField, buf[offset:], r.epoch) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt32Marshal(reqMetaHeaderTTLField, buf[offset:], r.ttl) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt64Marshal(reqMetaHeaderEpochField, buf[offset:], r.epoch) + offset += proto.UInt32Marshal(reqMetaHeaderTTLField, buf[offset:], r.ttl) for i := range r.xHeaders { n, err = proto.NestedStructureMarshal(reqMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) @@ -572,10 +476,7 @@ func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.UInt64Marshal(reqMetaHeaderNetMagicField, buf[offset:], r.netMagic) - if err != nil { - return nil, err - } + proto.UInt64Marshal(reqMetaHeaderNetMagicField, buf[offset:], r.netMagic) return buf, nil } @@ -699,19 +600,8 @@ func (r *ResponseMetaHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.UInt64Marshal(respMetaHeaderEpochField, buf[offset:], r.epoch) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.UInt32Marshal(respMetaHeaderTTLField, buf[offset:], r.ttl) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt64Marshal(respMetaHeaderEpochField, buf[offset:], r.epoch) + offset += proto.UInt32Marshal(respMetaHeaderTTLField, buf[offset:], r.ttl) for i := range r.xHeaders { n, err = proto.NestedStructureMarshal(respMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) diff --git a/status/marshal.go b/status/marshal.go index 5e04f24f..15001984 100644 --- a/status/marshal.go +++ b/status/marshal.go @@ -21,22 +21,10 @@ func (x *Detail) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.UInt32Marshal(detailIDFNum, buf[offset:], x.id) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.BytesMarshal(detailValueFNum, buf[offset:], x.val) - if err != nil { - return nil, err - } + offset += protoutil.UInt32Marshal(detailIDFNum, buf[offset:], x.id) + offset += protoutil.BytesMarshal(detailValueFNum, buf[offset:], x.val) return buf, nil } @@ -73,20 +61,16 @@ func (x *Status) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = protoutil.UInt32Marshal(statusCodeFNum, buf[offset:], CodeToGRPC(x.code)) + offset += protoutil.UInt32Marshal(statusCodeFNum, buf[offset:], CodeToGRPC(x.code)) if err != nil { return nil, err } - offset += n - - n, err = protoutil.StringMarshal(statusMsgFNum, buf[offset:], x.msg) + offset += protoutil.StringMarshal(statusMsgFNum, buf[offset:], x.msg) if err != nil { return nil, err } - offset += n - for i := range x.details { n, err = protoutil.NestedStructureMarshal(statusDetailsFNum, buf[offset:], &x.details[i]) if err != nil { diff --git a/storagegroup/marshal.go b/storagegroup/marshal.go index 534f6f7e..d5e60c1c 100644 --- a/storagegroup/marshal.go +++ b/storagegroup/marshal.go @@ -30,12 +30,7 @@ func (s *StorageGroup) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = proto.UInt64Marshal(sizeField, buf[offset:], s.size) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt64Marshal(sizeField, buf[offset:], s.size) n, err = proto.NestedStructureMarshal(hashField, buf[offset:], s.hash) if err != nil { @@ -44,12 +39,7 @@ func (s *StorageGroup) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.UInt64Marshal(expirationField, buf[offset:], s.exp) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt64Marshal(expirationField, buf[offset:], s.exp) _, err = refs.ObjectIDNestedListMarshal(objectIDsField, buf[offset:], s.members) if err != nil { diff --git a/tombstone/marshal.go b/tombstone/marshal.go index cd778b93..f2f896a3 100644 --- a/tombstone/marshal.go +++ b/tombstone/marshal.go @@ -28,19 +28,8 @@ func (s *Tombstone) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = proto.UInt64Marshal(expFNum, buf[offset:], s.exp) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID) - if err != nil { - return nil, err - } - - offset += n + offset += proto.UInt64Marshal(expFNum, buf[offset:], s.exp) + offset += proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID) for i := range s.members { n, err = proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i]) diff --git a/util/proto/marshal.go b/util/proto/marshal.go index 21e80133..a1bb1e96 100644 --- a/util/proto/marshal.go +++ b/util/proto/marshal.go @@ -20,9 +20,9 @@ type ( } ) -func BytesMarshal(field int, buf, v []byte) (int, error) { +func BytesMarshal(field int, buf, v []byte) int { if len(v) == 0 { - return 0, nil + return 0 } prefix := field<<3 | 0x2 @@ -33,7 +33,7 @@ func BytesMarshal(field int, buf, v []byte) (int, error) { i += binary.PutUvarint(buf[i:], uint64(len(v))) i += copy(buf[i:], v) - return i, nil + return i } func BytesSize(field int, v []byte) int { @@ -47,7 +47,7 @@ func BytesSize(field int, v []byte) int { return VarUIntSize(uint64(prefix)) + VarUIntSize(uint64(ln)) + ln } -func StringMarshal(field int, buf []byte, v string) (int, error) { +func StringMarshal(field int, buf []byte, v string) int { return BytesMarshal(field, buf, []byte(v)) } @@ -55,9 +55,9 @@ func StringSize(field int, v string) int { return BytesSize(field, []byte(v)) } -func BoolMarshal(field int, buf []byte, v bool) (int, error) { +func BoolMarshal(field int, buf []byte, v bool) int { if !v { - return 0, nil + return 0 } prefix := field << 3 @@ -67,7 +67,7 @@ func BoolMarshal(field int, buf []byte, v bool) (int, error) { i := binary.PutUvarint(buf, uint64(prefix)) buf[i] = 0x1 - return i + 1, nil + return i + 1 } func BoolSize(field int, v bool) int { @@ -80,9 +80,9 @@ func BoolSize(field int, v bool) int { return VarUIntSize(uint64(prefix)) + 1 // bool is always 1 byte long } -func UInt64Marshal(field int, buf []byte, v uint64) (int, error) { +func UInt64Marshal(field int, buf []byte, v uint64) int { if v == 0 { - return 0, nil + return 0 } prefix := field << 3 @@ -92,7 +92,7 @@ func UInt64Marshal(field int, buf []byte, v uint64) (int, error) { i := binary.PutUvarint(buf, uint64(prefix)) i += binary.PutUvarint(buf[i:], v) - return i, nil + return i } func UInt64Size(field int, v uint64) int { @@ -105,7 +105,7 @@ func UInt64Size(field int, v uint64) int { return VarUIntSize(uint64(prefix)) + VarUIntSize(v) } -func Int64Marshal(field int, buf []byte, v int64) (int, error) { +func Int64Marshal(field int, buf []byte, v int64) int { return UInt64Marshal(field, buf, uint64(v)) } @@ -113,7 +113,7 @@ func Int64Size(field int, v int64) int { return UInt64Size(field, uint64(v)) } -func UInt32Marshal(field int, buf []byte, v uint32) (int, error) { +func UInt32Marshal(field int, buf []byte, v uint32) int { return UInt64Marshal(field, buf, uint64(v)) } @@ -121,7 +121,7 @@ func UInt32Size(field int, v uint32) int { return UInt64Size(field, uint64(v)) } -func Int32Marshal(field int, buf []byte, v int32) (int, error) { +func Int32Marshal(field int, buf []byte, v int32) int { return UInt64Marshal(field, buf, uint64(v)) } @@ -129,7 +129,7 @@ func Int32Size(field int, v int32) int { return UInt64Size(field, uint64(v)) } -func EnumMarshal(field int, buf []byte, v int32) (int, error) { +func EnumMarshal(field int, buf []byte, v int32) int { return UInt64Marshal(field, buf, uint64(v)) } @@ -137,19 +137,14 @@ func EnumSize(field int, v int32) int { return UInt64Size(field, uint64(v)) } -func RepeatedBytesMarshal(field int, buf []byte, v [][]byte) (int, error) { +func RepeatedBytesMarshal(field int, buf []byte, v [][]byte) int { var offset int for i := range v { - off, err := BytesMarshal(field, buf[offset:], v[i]) - if err != nil { - return 0, err - } - - offset += off + offset += BytesMarshal(field, buf[offset:], v[i]) } - return offset, nil + return offset } func RepeatedBytesSize(field int, v [][]byte) (size int) { @@ -160,19 +155,14 @@ func RepeatedBytesSize(field int, v [][]byte) (size int) { return size } -func RepeatedStringMarshal(field int, buf []byte, v []string) (int, error) { +func RepeatedStringMarshal(field int, buf []byte, v []string) int { var offset int for i := range v { - off, err := StringMarshal(field, buf[offset:], v[i]) - if err != nil { - return 0, err - } - - offset += off + offset += StringMarshal(field, buf[offset:], v[i]) } - return offset, nil + return offset } func RepeatedStringSize(field int, v []string) (size int) { @@ -183,9 +173,9 @@ func RepeatedStringSize(field int, v []string) (size int) { return size } -func RepeatedUInt64Marshal(field int, buf []byte, v []uint64) (int, error) { +func RepeatedUInt64Marshal(field int, buf []byte, v []uint64) int { if len(v) == 0 { - return 0, nil + return 0 } prefix := field<<3 | 0x02 @@ -197,7 +187,7 @@ func RepeatedUInt64Marshal(field int, buf []byte, v []uint64) (int, error) { offset += binary.PutUvarint(buf[offset:], v[i]) } - return offset, nil + return offset } func RepeatedUInt64Size(field int, v []uint64) (size, arraySize int) { @@ -218,9 +208,9 @@ func RepeatedUInt64Size(field int, v []uint64) (size, arraySize int) { return size, arraySize } -func RepeatedInt64Marshal(field int, buf []byte, v []int64) (int, error) { +func RepeatedInt64Marshal(field int, buf []byte, v []int64) int { if len(v) == 0 { - return 0, nil + return 0 } convert := make([]uint64, len(v)) @@ -244,9 +234,9 @@ func RepeatedInt64Size(field int, v []int64) (size, arraySize int) { return RepeatedUInt64Size(field, convert) } -func RepeatedUInt32Marshal(field int, buf []byte, v []uint32) (int, error) { +func RepeatedUInt32Marshal(field int, buf []byte, v []uint32) int { if len(v) == 0 { - return 0, nil + return 0 } convert := make([]uint64, len(v)) @@ -270,9 +260,9 @@ func RepeatedUInt32Size(field int, v []uint32) (size, arraySize int) { return RepeatedUInt64Size(field, convert) } -func RepeatedInt32Marshal(field int, buf []byte, v []int32) (int, error) { +func RepeatedInt32Marshal(field int, buf []byte, v []int32) int { if len(v) == 0 { - return 0, nil + return 0 } convert := make([]uint64, len(v)) @@ -337,9 +327,9 @@ func NestedStructureSize(field int64, v stableMarshaller) (size int) { return size } -func Fixed64Marshal(field int, buf []byte, v uint64) (int, error) { +func Fixed64Marshal(field int, buf []byte, v uint64) int { if v == 0 { - return 0, nil + return 0 } prefix := field<<3 | 1 @@ -349,7 +339,7 @@ func Fixed64Marshal(field int, buf []byte, v uint64) (int, error) { i := binary.PutUvarint(buf, uint64(prefix)) binary.LittleEndian.PutUint64(buf[i:], v) - return i + 8, nil + return i + 8 } func Fixed64Size(fNum int, v uint64) int { @@ -362,9 +352,9 @@ func Fixed64Size(fNum int, v uint64) int { return VarUIntSize(uint64(prefix)) + 8 } -func Float64Marshal(field int, buf []byte, v float64) (int, error) { +func Float64Marshal(field int, buf []byte, v float64) int { if v == 0 { - return 0, nil + return 0 } prefix := field<<3 | 1 @@ -372,7 +362,7 @@ func Float64Marshal(field int, buf []byte, v float64) (int, error) { i := binary.PutUvarint(buf, uint64(prefix)) binary.LittleEndian.PutUint64(buf[i:], math.Float64bits(v)) - return i + 8, nil + return i + 8 } func Float64Size(fNum int, v float64) int { diff --git a/util/proto/marshal_test.go b/util/proto/marshal_test.go index a3f6882f..e19f4ee4 100644 --- a/util/proto/marshal_test.go +++ b/util/proto/marshal_test.go @@ -1,7 +1,6 @@ package proto_test import ( - "fmt" "math" "testing" @@ -59,91 +58,55 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e if wrongField { fieldNum++ } - offset, err := proto.BytesMarshal(fieldNum, buf, s.FieldA) - if err != nil { - return nil, fmt.Errorf("can't marshal field a: %w", err) - } - i += offset + i += proto.BytesMarshal(fieldNum, buf, s.FieldA) fieldNum = 2 if wrongField { fieldNum++ } - offset, err = proto.StringMarshal(fieldNum, buf, s.FieldB) - if err != nil { - return nil, fmt.Errorf("can't marshal field b: %w", err) - } - i += offset + i += proto.StringMarshal(fieldNum, buf, s.FieldB) fieldNum = 200 if wrongField { fieldNum++ } - offset, err = proto.BoolMarshal(fieldNum, buf, s.FieldC) - if err != nil { - return nil, fmt.Errorf("can't marshal field c: %w", err) - } - i += offset + i += proto.BoolMarshal(fieldNum, buf, s.FieldC) fieldNum = 201 if wrongField { fieldNum++ } - offset, err = proto.Int32Marshal(fieldNum, buf, s.FieldD) - if err != nil { - return nil, fmt.Errorf("can't marshal field d: %w", err) - } - i += offset + i += proto.Int32Marshal(fieldNum, buf, s.FieldD) fieldNum = 202 if wrongField { fieldNum++ } - offset, err = proto.UInt32Marshal(fieldNum, buf, s.FieldE) - if err != nil { - return nil, fmt.Errorf("can't marshal field e: %w", err) - } - i += offset + i += proto.UInt32Marshal(fieldNum, buf, s.FieldE) fieldNum = 203 if wrongField { fieldNum++ } - offset, err = proto.Int64Marshal(fieldNum, buf, s.FieldF) - if err != nil { - return nil, fmt.Errorf("can't marshal field f: %w", err) - } - i += offset + i += proto.Int64Marshal(fieldNum, buf, s.FieldF) fieldNum = 204 if wrongField { fieldNum++ } - offset, err = proto.UInt64Marshal(fieldNum, buf, s.FieldG) - if err != nil { - return nil, fmt.Errorf("can't marshal field g: %w", err) - } - i += offset + i += proto.UInt64Marshal(fieldNum, buf, s.FieldG) fieldNum = 205 if wrongField { fieldNum++ } - offset, err = proto.Fixed64Marshal(fieldNum, buf, s.FieldI) - if err != nil { - return nil, fmt.Errorf("can't marshal field I: %w", err) - } - i += offset + i += proto.Fixed64Marshal(fieldNum, buf, s.FieldI) fieldNum = 206 if wrongField { fieldNum++ } - offset, err = proto.Float64Marshal(fieldNum, buf, s.FieldJ) - if err != nil { - return nil, fmt.Errorf("can't marshal field J: %w", err) - } - i += offset + i += proto.Float64Marshal(fieldNum, buf, s.FieldJ) fieldNum = 207 if wrongField { @@ -158,11 +121,7 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e if wrongField { fieldNum++ } - offset, err = proto.EnumMarshal(fieldNum, buf, int32(s.FieldH)) - if err != nil { - return nil, fmt.Errorf("can't marshal field h: %w", err) - } - i += offset + i += proto.EnumMarshal(fieldNum, buf, int32(s.FieldH)) return buf, nil } @@ -190,69 +149,43 @@ func (s *stableRepPrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte buf = make([]byte, s.stableSize()) } - var ( - i, offset, fieldNum int - ) + var i, fieldNum int fieldNum = 1 if wrongField { fieldNum++ } - offset, err := proto.RepeatedBytesMarshal(fieldNum, buf, s.FieldA) - if err != nil { - return nil, fmt.Errorf("can't marshal field a: %w", err) - } - i += offset + i += proto.RepeatedBytesMarshal(fieldNum, buf, s.FieldA) fieldNum = 2 if wrongField { fieldNum++ } - offset, err = proto.RepeatedStringMarshal(fieldNum, buf, s.FieldB) - if err != nil { - return nil, fmt.Errorf("can't marshal field b: %w", err) - } - i += offset + i += proto.RepeatedStringMarshal(fieldNum, buf, s.FieldB) fieldNum = 3 if wrongField { fieldNum++ } - offset, err = proto.RepeatedInt32Marshal(fieldNum, buf, s.FieldC) - if err != nil { - return nil, fmt.Errorf("can't marshal field c: %w", err) - } - i += offset + i += proto.RepeatedInt32Marshal(fieldNum, buf, s.FieldC) fieldNum = 4 if wrongField { fieldNum++ } - offset, err = proto.RepeatedUInt32Marshal(fieldNum, buf, s.FieldD) - if err != nil { - return nil, fmt.Errorf("can't marshal field d: %w", err) - } - i += offset + i += proto.RepeatedUInt32Marshal(fieldNum, buf, s.FieldD) fieldNum = 5 if wrongField { fieldNum++ } - offset, err = proto.RepeatedInt64Marshal(fieldNum, buf, s.FieldE) - if err != nil { - return nil, fmt.Errorf("can't marshal field e: %w", err) - } - i += offset + i += proto.RepeatedInt64Marshal(fieldNum, buf, s.FieldE) fieldNum = 6 if wrongField { fieldNum++ } - offset, err = proto.RepeatedUInt64Marshal(fieldNum, buf, s.FieldF) - if err != nil { - return nil, fmt.Errorf("can't marshal field f: %w", err) - } - i += offset + i += proto.RepeatedUInt64Marshal(fieldNum, buf, s.FieldF) return buf, nil }