protogen: Do not panic in StableSize() #51

Merged
fyrchik merged 2 commits from fyrchik/frostfs-api-go:fix-stablesize-alloc into master 2024-09-04 19:51:16 +00:00
3 changed files with 21 additions and 0 deletions

View file

@ -65,6 +65,10 @@ func (f *Filter) StableMarshal(buf []byte) []byte {
}
func (f *Filter) StableSize() (size int) {
if f == nil {
return 0
}
size += protoutil.StringSize(nameFilterField, f.name)
size += protoutil.StringSize(keyFilterField, f.key)
size += protoutil.EnumSize(opFilterField, int32(f.op))
@ -101,6 +105,10 @@ func (s *Selector) StableMarshal(buf []byte) []byte {
}
func (s *Selector) StableSize() (size int) {
if s == nil {
return 0
}
size += protoutil.StringSize(nameSelectorField, s.name)
size += protoutil.UInt32Size(countSelectorField, s.count)
size += protoutil.EnumSize(countSelectorField, int32(s.clause))
@ -132,6 +140,10 @@ func (r *Replica) StableMarshal(buf []byte) []byte {
}
func (r *Replica) StableSize() (size int) {
if r == nil {
return 0
}
size += protoutil.UInt32Size(countReplicaField, r.count)
size += protoutil.StringSize(selectorReplicaField, r.selector)
@ -173,6 +185,10 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) []byte {
}
func (p *PlacementPolicy) StableSize() (size int) {
if p == nil {
return 0
}
for i := range p.replicas {
size += protoutil.NestedStructureSize(replicasPolicyField, &p.replicas[i])
}

View file

@ -30,6 +30,10 @@ func (x *Detail) StableMarshal(buf []byte) []byte {
}
func (x *Detail) StableSize() (size int) {
if x == nil {
return 0
}
size += protoutil.UInt32Size(detailIDFNum, x.id)
size += protoutil.BytesSize(detailValueFNum, x.val)

View file

@ -59,6 +59,7 @@ func emitMessage(g *protogen.GeneratedFile, msg *protogen.Message) {
g.P("//")
g.P("// Structures with the same field values have the same binary size.")
g.P("func (x *", msg.GoIdent.GoName, ") StableSize() (size int) {")
g.P("if x == nil { return 0 }")
if len(fs) != 0 {
for _, f := range fs {
if f.Desc.IsList() && marshalers[f.Desc.Kind()].RepeatedDouble {