Compare commits

...

2 commits

Author SHA1 Message Date
241a9f1ad0 [#69] util: Rename stableMarshaler
All checks were successful
DCO action / DCO (pull_request) Successful in 49s
Tests and linters / Tests (1.19) (pull_request) Successful in 1m20s
Tests and linters / Lint (pull_request) Successful in 1m36s
Tests and linters / Tests with -race (pull_request) Successful in 1m59s
Tests and linters / Tests (1.20) (pull_request) Successful in 2m40s
It should be with a single `l`, see `json.Marshaler`.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2023-11-21 11:58:47 +03:00
c72590d831 [#59] util: Restore backwards compatibility in NestedStructureMarshal()
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2023-11-21 11:58:30 +03:00
2 changed files with 24 additions and 11 deletions

View file

@ -211,7 +211,7 @@ func (c *ObjectSessionContext) StableMarshal(buf []byte) []byte {
} }
offset := proto.EnumMarshal(objectCtxVerbField, buf, int32(c.verb)) offset := proto.EnumMarshal(objectCtxVerbField, buf, int32(c.verb))
proto.NestedStructureMarshal(objectCtxTargetField, buf[offset:], objectSessionContextTarget{ proto.NestedStructureMarshalUnchecked(objectCtxTargetField, buf[offset:], objectSessionContextTarget{
cnr: c.cnr, cnr: c.cnr,
objs: c.objs, objs: c.objs,
}) })
@ -225,7 +225,7 @@ func (c *ObjectSessionContext) StableSize() (size int) {
} }
size += proto.EnumSize(objectCtxVerbField, int32(c.verb)) size += proto.EnumSize(objectCtxVerbField, int32(c.verb))
size += proto.NestedStructureSize(objectCtxTargetField, objectSessionContextTarget{ size += proto.NestedStructureSizeUnchecked(objectCtxTargetField, objectSessionContextTarget{
cnr: c.cnr, cnr: c.cnr,
objs: c.objs, objs: c.objs,
}) })

View file

@ -15,7 +15,7 @@ import (
) )
type ( type (
stableMarshaller interface { stableMarshaler interface {
StableMarshal([]byte) []byte StableMarshal([]byte) []byte
StableSize() int StableSize() int
} }
@ -249,12 +249,21 @@ func VarUIntSize(x uint64) int {
return (bits.Len64(x|1) + 6) / 7 return (bits.Len64(x|1) + 6) / 7
} }
func NestedStructureMarshal[T stableMarshaller](field int64, buf []byte, v T) int { type ptrStableMarshaler[T any] interface {
n := v.StableSize() stableMarshaler
if n == 0 { ~*T
}
func NestedStructureMarshal[T any, M ptrStableMarshaler[T]](field int64, buf []byte, v M) int {
if v == nil {
return 0 return 0
} }
return NestedStructureMarshalUnchecked(field, buf, v)
}
func NestedStructureMarshalUnchecked[T stableMarshaler](field int64, buf []byte, v T) int {
n := v.StableSize()
prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType) prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType)
offset := binary.PutUvarint(buf, prefix) offset := binary.PutUvarint(buf, prefix)
offset += binary.PutUvarint(buf[offset:], uint64(n)) offset += binary.PutUvarint(buf[offset:], uint64(n))
@ -263,13 +272,17 @@ func NestedStructureMarshal[T stableMarshaller](field int64, buf []byte, v T) in
return offset + n return offset + n
} }
func NestedStructureSize[T stableMarshaller](field int64, v T) (size int) { func NestedStructureSize[T any, M ptrStableMarshaler[T]](field int64, v M) (size int) {
n := v.StableSize() if v == nil {
if n == 0 {
return 0 return 0
} }
size = protowire.SizeGroup(protowire.Number(field), protowire.SizeBytes(n))
return return NestedStructureSizeUnchecked(field, v)
}
func NestedStructureSizeUnchecked[T stableMarshaler](field int64, v T) int {
n := v.StableSize()
return protowire.SizeGroup(protowire.Number(field), protowire.SizeBytes(n))
} }
func Fixed64Marshal(field int, buf []byte, v uint64) int { func Fixed64Marshal(field int, buf []byte, v uint64) int {