package proto
import (
protoV2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/runtime/protoiface"
)
func Size (m Message ) int {
if m == nil {
return 0
}
mi := MessageV2 (m )
return protoV2 .Size (mi )
}
func Marshal (m Message ) ([]byte , error ) {
b , err := marshalAppend (nil , m , false )
if b == nil {
b = zeroBytes
}
return b , err
}
var zeroBytes = make ([]byte , 0 , 0 )
func marshalAppend(buf []byte , m Message , deterministic bool ) ([]byte , error ) {
if m == nil {
return nil , ErrNil
}
mi := MessageV2 (m )
nbuf , err := protoV2 .MarshalOptions {
Deterministic : deterministic ,
AllowPartial : true ,
}.MarshalAppend (buf , mi )
if err != nil {
return buf , err
}
if len (buf ) == len (nbuf ) {
if !mi .ProtoReflect ().IsValid () {
return buf , ErrNil
}
}
return nbuf , checkRequiredNotSet (mi )
}
func Unmarshal (b []byte , m Message ) error {
m .Reset ()
return UnmarshalMerge (b , m )
}
func UnmarshalMerge (b []byte , m Message ) error {
mi := MessageV2 (m )
out , err := protoV2 .UnmarshalOptions {
AllowPartial : true ,
Merge : true ,
}.UnmarshalState (protoiface .UnmarshalInput {
Buf : b ,
Message : mi .ProtoReflect (),
})
if err != nil {
return err
}
if out .Flags &protoiface .UnmarshalInitialized > 0 {
return nil
}
return checkRequiredNotSet (mi )
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .