Source File
encode.go
Belonging Package
google.golang.org/protobuf/encoding/prototext
package prototext
import (
pref
)
const defaultIndent = " "
type MarshalOptions struct {
pragma.NoUnkeyedLiterals
Resolver interface {
protoregistry.ExtensionTypeResolver
protoregistry.MessageTypeResolver
}
}
func ( MarshalOptions) ( proto.Message) string {
if == nil || !.ProtoReflect().IsValid() {
return "<nil>" // invalid syntax, but okay since this is for debugging
}
.allowInvalidUTF8 = true
.AllowPartial = true
.EmitUnknown = true
, := .Marshal()
return string()
}
func ( MarshalOptions) ( proto.Message) ([]byte, error) {
var = [2]byte{'{', '}'}
if .Multiline && .Indent == "" {
.Indent = defaultIndent
}
if .Resolver == nil {
.Resolver = protoregistry.GlobalTypes
}
, := text.NewEncoder(.Indent, , .EmitASCII)
if != nil {
return nil,
}
if == nil {
return []byte{}, nil
}
:= encoder{, }
= .marshalMessage(.ProtoReflect(), false)
if != nil {
return nil,
}
:= .Bytes()
if len(.Indent) > 0 && len() > 0 {
= append(, '\n')
}
if .AllowPartial {
return , nil
}
return , proto.CheckInitialized()
}
type encoder struct {
*text.Encoder
opts MarshalOptions
}
func ( encoder) ( pref.Message, bool) error {
:= .Descriptor()
if !flags.ProtoLegacy && messageset.IsMessageSet() {
return errors.New("no support for proto1 MessageSets")
}
if {
.StartMessage()
defer .EndMessage()
}
if .FullName() == genid.Any_message_fullname {
if .marshalAny() {
return nil
}
:= .Fields()
:= .Len()
for := 0; < ; {
:= .Get()
if := .ContainingOneof(); != nil {
= .WhichOneof()
+= .Fields().Len()
} else {
++
}
if == nil || !.Has() {
continue
}
if := .marshalExtensions(); != nil {
return
}
if .opts.EmitUnknown {
.marshalUnknown(.GetUnknown())
}
return nil
}
func ( encoder) ( string, pref.Value, pref.FieldDescriptor) error {
switch {
case .IsList():
return .marshalList(, .List(), )
case .IsMap():
return .marshalMap(, .Map(), )
default:
.WriteName()
return .marshalSingular(, )
}
}
func ( encoder) ( pref.Value, pref.FieldDescriptor) error {
:= .Kind()
switch {
case pref.BoolKind:
.WriteBool(.Bool())
case pref.StringKind:
:= .String()
if !.opts.allowInvalidUTF8 && strs.EnforceUTF8() && !utf8.ValidString() {
return errors.InvalidUTF8(string(.FullName()))
}
.WriteString()
case pref.Int32Kind, pref.Int64Kind,
pref.Sint32Kind, pref.Sint64Kind,
pref.Sfixed32Kind, pref.Sfixed64Kind:
.WriteInt(.Int())
case pref.Uint32Kind, pref.Uint64Kind,
pref.Fixed32Kind, pref.Fixed64Kind:
.WriteUint(.Uint())
.WriteFloat(.Float(), 32)
func ( encoder) ( string, pref.Map, pref.FieldDescriptor) error {
var error
mapsort.Range(, .MapKey().Kind(), func( pref.MapKey, pref.Value) bool {
.WriteName()
.StartMessage()
defer .EndMessage()
.WriteName(string(genid.MapEntry_Key_field_name))
= .marshalSingular(.Value(), .MapKey())
if != nil {
return false
}
.WriteName(string(genid.MapEntry_Value_field_name))
= .marshalSingular(, .MapValue())
if != nil {
return false
}
return true
})
return
}
var []
.Range(func( pref.FieldDescriptor, pref.Value) bool {
if !.IsExtension() {
return true
:= .FullName()
if messageset.IsMessageSetExtension() {
= .Parent()
}
= append(, {
: string(),
: ,
: ,
})
return true
:= "[" + . + "]"
if := .marshalField(, ., .); != nil {
return
}
}
return nil
}
func ( encoder) ( []byte) {
const = 10
const = 16
for len() > 0 {
, , := protowire.ConsumeTag()
= [:]
.WriteName(strconv.FormatInt(int64(), ))
switch {
case protowire.VarintType:
var uint64
, = protowire.ConsumeVarint()
.WriteUint()
case protowire.Fixed32Type:
var uint32
, = protowire.ConsumeFixed32()
.WriteLiteral("0x" + strconv.FormatUint(uint64(), ))
case protowire.Fixed64Type:
var uint64
, = protowire.ConsumeFixed64()
.WriteLiteral("0x" + strconv.FormatUint(, ))
case protowire.BytesType:
var []byte
, = protowire.ConsumeBytes()
.WriteString(string())
case protowire.StartGroupType:
.StartMessage()
var []byte
, = protowire.ConsumeGroup(, )
.()
.EndMessage()
default:
panic(fmt.Sprintf("prototext: error parsing unknown field wire type: %v", ))
}
= [:]
}
}
:= .Descriptor().Fields()
:= .ByNumber(genid.Any_TypeUrl_field_number)
:= .Get().String()
, := .opts.Resolver.FindMessageByURL()
if != nil {
return false
}
:= .New().Interface()
:= .ByNumber(genid.Any_Value_field_number)
:= .Get()
= proto.UnmarshalOptions{
AllowPartial: true,
Resolver: .opts.Resolver,
}.Unmarshal(.Bytes(), )
if != nil {
return false
}
:= .Snapshot()
.WriteName("[" + + "]")
= .marshalMessage(.ProtoReflect(), true)
if != nil {
.Reset()
return false
}
return true
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin 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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |