go-bot
97 строк · 3.0 Кб
1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package proto6
7import (8"google.golang.org/protobuf/encoding/protowire"9"google.golang.org/protobuf/internal/encoding/messageset"10"google.golang.org/protobuf/reflect/protoreflect"11"google.golang.org/protobuf/runtime/protoiface"12)
13
14// Size returns the size in bytes of the wire-format encoding of m.
15func Size(m Message) int {16return MarshalOptions{}.Size(m)17}
18
19// Size returns the size in bytes of the wire-format encoding of m.
20func (o MarshalOptions) Size(m Message) int {21// Treat a nil message interface as an empty message; nothing to output.22if m == nil {23return 024}25
26return o.size(m.ProtoReflect())27}
28
29// size is a centralized function that all size operations go through.
30// For profiling purposes, avoid changing the name of this function or
31// introducing other code paths for size that do not go through this.
32func (o MarshalOptions) size(m protoreflect.Message) (size int) {33methods := protoMethods(m)34if methods != nil && methods.Size != nil {35out := methods.Size(protoiface.SizeInput{36Message: m,37})38return out.Size39}40if methods != nil && methods.Marshal != nil {41// This is not efficient, but we don't have any choice.42// This case is mainly used for legacy types with a Marshal method.43out, _ := methods.Marshal(protoiface.MarshalInput{44Message: m,45})46return len(out.Buf)47}48return o.sizeMessageSlow(m)49}
50
51func (o MarshalOptions) sizeMessageSlow(m protoreflect.Message) (size int) {52if messageset.IsMessageSet(m.Descriptor()) {53return o.sizeMessageSet(m)54}55m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {56size += o.sizeField(fd, v)57return true58})59size += len(m.GetUnknown())60return size61}
62
63func (o MarshalOptions) sizeField(fd protoreflect.FieldDescriptor, value protoreflect.Value) (size int) {64num := fd.Number()65switch {66case fd.IsList():67return o.sizeList(num, fd, value.List())68case fd.IsMap():69return o.sizeMap(num, fd, value.Map())70default:71return protowire.SizeTag(num) + o.sizeSingular(num, fd.Kind(), value)72}73}
74
75func (o MarshalOptions) sizeList(num protowire.Number, fd protoreflect.FieldDescriptor, list protoreflect.List) (size int) {76if fd.IsPacked() && list.Len() > 0 {77content := 078for i, llen := 0, list.Len(); i < llen; i++ {79content += o.sizeSingular(num, fd.Kind(), list.Get(i))80}81return protowire.SizeTag(num) + protowire.SizeBytes(content)82}83
84for i, llen := 0, list.Len(); i < llen; i++ {85size += protowire.SizeTag(num) + o.sizeSingular(num, fd.Kind(), list.Get(i))86}87return size88}
89
90func (o MarshalOptions) sizeMap(num protowire.Number, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) (size int) {91mapv.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool {92size += protowire.SizeTag(num)93size += protowire.SizeBytes(o.sizeField(fd.MapKey(), key.Value()) + o.sizeField(fd.MapValue(), value))94return true95})96return size97}
98