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 (
|
2019-06-19 22:22:13 +00:00
|
|
|
"google.golang.org/protobuf/internal/encoding/messageset"
|
2019-05-14 06:55:40 +00:00
|
|
|
"google.golang.org/protobuf/internal/encoding/wire"
|
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2019-07-12 20:37:59 +00:00
|
|
|
"google.golang.org/protobuf/runtime/protoiface"
|
2019-03-27 16:23:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-03-27 16:23:20 +00:00
|
|
|
return sizeMessage(m.ProtoReflect())
|
|
|
|
}
|
|
|
|
|
2019-07-11 06:14:31 +00:00
|
|
|
func sizeMessage(m protoreflect.Message) (size int) {
|
2020-01-06 19:17:07 +00:00
|
|
|
methods := protoMethods(m)
|
|
|
|
if methods != nil && methods.Size != nil {
|
2020-02-14 22:49:35 +00:00
|
|
|
out := methods.Size(protoiface.SizeInput{
|
|
|
|
Message: m,
|
|
|
|
})
|
|
|
|
return out.Size
|
2019-04-01 20:31:55 +00:00
|
|
|
}
|
2020-01-21 21:29:51 +00:00
|
|
|
if methods != nil && methods.Marshal != nil {
|
2020-01-06 19:17:07 +00:00
|
|
|
// This is not efficient, but we don't have any choice.
|
|
|
|
// This case is mainly used for legacy types with a Marshal method.
|
2020-02-14 22:49:35 +00:00
|
|
|
out, _ := methods.Marshal(protoiface.MarshalInput{
|
|
|
|
Message: m,
|
|
|
|
})
|
2020-01-21 21:29:51 +00:00
|
|
|
return len(out.Buf)
|
2020-01-06 19:17:07 +00:00
|
|
|
}
|
2019-07-11 06:14:31 +00:00
|
|
|
return sizeMessageSlow(m)
|
2019-04-01 20:31:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 06:14:31 +00:00
|
|
|
func sizeMessageSlow(m protoreflect.Message) (size int) {
|
2019-06-19 22:22:13 +00:00
|
|
|
if messageset.IsMessageSet(m.Descriptor()) {
|
|
|
|
return sizeMessageSet(m)
|
|
|
|
}
|
2019-04-26 06:48:08 +00:00
|
|
|
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
|
|
|
|
size += sizeField(fd, v)
|
2019-03-27 16:23:20 +00:00
|
|
|
return true
|
|
|
|
})
|
2019-04-26 06:48:08 +00:00
|
|
|
size += len(m.GetUnknown())
|
2019-03-27 16:23:20 +00:00
|
|
|
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) {
|
2019-04-26 06:48:08 +00:00
|
|
|
if fd.IsPacked() && list.Len() > 0 {
|
2019-05-13 21:32:56 +00:00
|
|
|
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
|
|
|
|
}
|