package proto
import (
)
type MarshalOptions struct {
pragma.NoUnkeyedLiterals
AllowPartial bool
Deterministic bool
UseCachedSize bool
}
func ( Message) ([]byte, error) {
if == nil {
return nil, nil
}
, := MarshalOptions{}.marshal(nil, .ProtoReflect())
if len(.Buf) == 0 && == nil {
.Buf = emptyBytesForMessage()
}
return .Buf,
}
func ( MarshalOptions) ( Message) ([]byte, error) {
if == nil {
return nil, nil
}
, := .marshal(nil, .ProtoReflect())
if len(.Buf) == 0 && == nil {
.Buf = emptyBytesForMessage()
}
return .Buf,
}
func ( Message) []byte {
if == nil || !.ProtoReflect().IsValid() {
return nil
}
return emptyBuf[:]
}
func ( MarshalOptions) ( []byte, Message) ([]byte, error) {
if == nil {
return , nil
}
, := .marshal(, .ProtoReflect())
return .Buf,
}
func ( MarshalOptions) ( protoiface.MarshalInput) (protoiface.MarshalOutput, error) {
return .marshal(.Buf, .Message)
}
func ( MarshalOptions) ( []byte, protoreflect.Message) ( protoiface.MarshalOutput, error) {
:= .AllowPartial
.AllowPartial = true
if := protoMethods(); != nil && .Marshal != nil &&
!(.Deterministic && .Flags&protoiface.SupportMarshalDeterministic == 0) {
:= protoiface.MarshalInput{
Message: ,
Buf: ,
}
if .Deterministic {
.Flags |= protoiface.MarshalDeterministic
}
if .UseCachedSize {
.Flags |= protoiface.MarshalUseCachedSize
}
if .Size != nil {
:= .Size(protoiface.SizeInput{
Message: ,
Flags: .Flags,
})
if cap() < len()+.Size {
.Buf = make([]byte, len(), growcap(cap(), len()+.Size))
copy(.Buf, )
}
.Flags |= protoiface.MarshalUseCachedSize
}
, = .Marshal()
} else {
.Buf, = .marshalMessageSlow(, )
}
if != nil {
return ,
}
if {
return , nil
}
return , checkInitialized()
}
func ( MarshalOptions) ( []byte, protoreflect.Message) ([]byte, error) {
, := .marshal(, )
return .Buf,
}
func (, int) ( int) {
if > *2 {
=
} else if < 1024 {
= * 2
} else {
=
for 0 < && < {
+= / 4
}
if <= 0 {
=
}
}
return
}
func ( MarshalOptions) ( []byte, protoreflect.Message) ([]byte, error) {
if messageset.IsMessageSet(.Descriptor()) {
return .marshalMessageSet(, )
}
:= order.AnyFieldOrder
if .Deterministic {
= order.LegacyFieldOrder
}
var error
order.RangeFields(, , func( protoreflect.FieldDescriptor, protoreflect.Value) bool {
, = .marshalField(, , )
return == nil
})
if != nil {
return ,
}
= append(, .GetUnknown()...)
return , nil
}
func ( MarshalOptions) ( []byte, protoreflect.FieldDescriptor, protoreflect.Value) ([]byte, error) {
switch {
case .IsList():
return .marshalList(, , .List())
case .IsMap():
return .marshalMap(, , .Map())
default:
= protowire.AppendTag(, .Number(), wireTypes[.Kind()])
return .marshalSingular(, , )
}
}
func ( MarshalOptions) ( []byte, protoreflect.FieldDescriptor, protoreflect.List) ([]byte, error) {
if .IsPacked() && .Len() > 0 {
= protowire.AppendTag(, .Number(), protowire.BytesType)
, := appendSpeculativeLength()
for , := 0, .Len(); < ; ++ {
var error
, = .marshalSingular(, , .Get())
if != nil {
return ,
}
}
= finishSpeculativeLength(, )
return , nil
}
:= .Kind()
for , := 0, .Len(); < ; ++ {
var error
= protowire.AppendTag(, .Number(), wireTypes[])
, = .marshalSingular(, , .Get())
if != nil {
return ,
}
}
return , nil
}
func ( MarshalOptions) ( []byte, protoreflect.FieldDescriptor, protoreflect.Map) ([]byte, error) {
:= .MapKey()
:= .MapValue()
:= order.AnyKeyOrder
if .Deterministic {
= order.GenericKeyOrder
}
var error
order.RangeEntries(, , func( protoreflect.MapKey, protoreflect.Value) bool {
= protowire.AppendTag(, .Number(), protowire.BytesType)
var int
, = appendSpeculativeLength()
, = .marshalField(, , .Value())
if != nil {
return false
}
, = .marshalField(, , )
if != nil {
return false
}
= finishSpeculativeLength(, )
return true
})
return ,
}
const speculativeLength = 1
func ( []byte) ([]byte, int) {
:= len()
= append(, "\x00\x00\x00\x00"[:speculativeLength]...)
return ,
}
func ( []byte, int) []byte {
:= len() - - speculativeLength
:= protowire.SizeVarint(uint64())
if != speculativeLength {
for := 0; < -speculativeLength; ++ {
= append(, 0)
}
copy([+:], [+speculativeLength:])
= [:++]
}
protowire.AppendVarint([:], uint64())
return
}