[#49] util/proto: Use StableSize() to determine if the struct is empty

`reflect` is not necessary here, and checking `StableSize` is what we
_want_ anyway.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-07-26 16:53:20 +03:00
parent 582d94c81c
commit 59c8421597
2 changed files with 8 additions and 6 deletions

View file

@ -69,6 +69,10 @@ func (x *Status) StableMarshal(buf []byte) []byte {
} }
func (x *Status) StableSize() (size int) { func (x *Status) StableSize() (size int) {
if x == nil {
return 0
}
size += protoutil.UInt32Size(statusCodeFNum, CodeToGRPC(x.code)) size += protoutil.UInt32Size(statusCodeFNum, CodeToGRPC(x.code))
size += protoutil.StringSize(statusMsgFNum, x.msg) size += protoutil.StringSize(statusMsgFNum, x.msg)

View file

@ -10,7 +10,6 @@ import (
"encoding/binary" "encoding/binary"
"math" "math"
"math/bits" "math/bits"
"reflect"
) )
type ( type (
@ -303,14 +302,13 @@ func NestedStructurePrefix(field int64) (prefix uint64, ln int) {
} }
func NestedStructureMarshal(field int64, buf []byte, v stableMarshaller) int { func NestedStructureMarshal(field int64, buf []byte, v stableMarshaller) int {
if v == nil || reflect.ValueOf(v).IsNil() { n := v.StableSize()
if n == 0 {
return 0 return 0
} }
prefix, _ := NestedStructurePrefix(field) prefix, _ := NestedStructurePrefix(field)
offset := binary.PutUvarint(buf, prefix) offset := binary.PutUvarint(buf, prefix)
n := v.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n)) offset += binary.PutUvarint(buf[offset:], uint64(n))
v.StableMarshal(buf[offset:]) v.StableMarshal(buf[offset:])
@ -318,12 +316,12 @@ func NestedStructureMarshal(field int64, buf []byte, v stableMarshaller) int {
} }
func NestedStructureSize(field int64, v stableMarshaller) (size int) { func NestedStructureSize(field int64, v stableMarshaller) (size int) {
if v == nil || reflect.ValueOf(v).IsNil() { n := v.StableSize()
if n == 0 {
return 0 return 0
} }
_, ln := NestedStructurePrefix(field) _, ln := NestedStructurePrefix(field)
n := v.StableSize()
size = ln + VarUIntSize(uint64(n)) + n size = ln + VarUIntSize(uint64(n)) + n
return size return size