2019-03-27 16:23:20 +00:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/v2/internal/encoding/wire"
|
|
|
|
"github.com/golang/protobuf/v2/reflect/protoreflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Size returns the size in bytes of the wire-format encoding of m.
|
|
|
|
func Size(m Message) int {
|
2019-04-08 01:18:31 +00:00
|
|
|
return MarshalOptions{}.Size(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size in bytes of the wire-format encoding of m.
|
|
|
|
func (o MarshalOptions) Size(m Message) int {
|
2019-04-01 20:31:55 +00:00
|
|
|
if size, err := sizeMessageFast(m); err == nil {
|
|
|
|
return size
|
|
|
|
}
|
2019-03-27 16:23:20 +00:00
|
|
|
return sizeMessage(m.ProtoReflect())
|
|
|
|
}
|
|
|
|
|
2019-04-01 20:31:55 +00:00
|
|
|
func sizeMessageFast(m Message) (int, error) {
|
|
|
|
// TODO: Pass MarshalOptions to size to permit disabling fast path?
|
|
|
|
methods := protoMethods(m)
|
|
|
|
if methods == nil || methods.Size == nil {
|
|
|
|
return 0, errInternalNoFast
|
|
|
|
}
|
|
|
|
return methods.Size(m), nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:23:20 +00:00
|
|
|
func sizeMessage(m protoreflect.Message) (size int) {
|
2019-05-01 19:29:25 +00:00
|
|
|
fieldDescs := m.Descriptor().Fields()
|
2019-03-27 16:23:20 +00:00
|
|
|
knownFields := m.KnownFields()
|
|
|
|
m.KnownFields().Range(func(num protoreflect.FieldNumber, value protoreflect.Value) bool {
|
2019-05-01 19:29:25 +00:00
|
|
|
field := fieldDescs.ByNumber(num)
|
2019-03-27 16:23:20 +00:00
|
|
|
if field == nil {
|
2019-05-01 19:29:25 +00:00
|
|
|
field = knownFields.ExtensionTypes().ByNumber(num).Descriptor()
|
2019-03-27 16:23:20 +00:00
|
|
|
if field == nil {
|
2019-05-01 19:29:25 +00:00
|
|
|
panic(fmt.Errorf("no descriptor for field %d in %q", num, m.Descriptor().FullName()))
|
2019-03-27 16:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
size += sizeField(field, value)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
m.UnknownFields().Range(func(_ protoreflect.FieldNumber, raw protoreflect.RawFields) bool {
|
|
|
|
size += len(raw)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:32:56 +00:00
|
|
|
func sizeField(fd protoreflect.FieldDescriptor, value protoreflect.Value) (size int) {
|
|
|
|
num := fd.Number()
|
2019-03-27 16:23:20 +00:00
|
|
|
switch {
|
2019-05-13 21:32:56 +00:00
|
|
|
case fd.IsList():
|
|
|
|
return sizeList(num, fd, value.List())
|
|
|
|
case fd.IsMap():
|
|
|
|
return sizeMap(num, fd, value.Map())
|
2019-03-27 16:23:20 +00:00
|
|
|
default:
|
2019-05-13 21:32:56 +00:00
|
|
|
return wire.SizeTag(num) + sizeSingular(num, fd.Kind(), value)
|
2019-03-27 16:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:32:56 +00:00
|
|
|
func sizeList(num wire.Number, fd protoreflect.FieldDescriptor, list protoreflect.List) (size int) {
|
|
|
|
if fd.IsPacked() {
|
|
|
|
content := 0
|
|
|
|
for i, llen := 0, list.Len(); i < llen; i++ {
|
|
|
|
content += sizeSingular(num, fd.Kind(), list.Get(i))
|
|
|
|
}
|
|
|
|
return wire.SizeTag(num) + wire.SizeBytes(content)
|
|
|
|
}
|
2019-03-27 16:23:20 +00:00
|
|
|
|
|
|
|
for i, llen := 0, list.Len(); i < llen; i++ {
|
2019-05-13 21:32:56 +00:00
|
|
|
size += wire.SizeTag(num) + sizeSingular(num, fd.Kind(), list.Get(i))
|
2019-03-27 16:23:20 +00:00
|
|
|
}
|
2019-05-13 21:32:56 +00:00
|
|
|
return size
|
2019-03-27 16:23:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:32:56 +00:00
|
|
|
func sizeMap(num wire.Number, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) (size int) {
|
|
|
|
mapv.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool {
|
|
|
|
size += wire.SizeTag(num)
|
|
|
|
size += wire.SizeBytes(sizeField(fd.MapKey(), key.Value()) + sizeField(fd.MapValue(), value))
|
|
|
|
return true
|
|
|
|
})
|
2019-03-27 16:23:20 +00:00
|
|
|
return size
|
|
|
|
}
|