Source File
decode.go
Belonging Package
google.golang.org/protobuf/encoding/prototext
package prototext
import (
pref
)
type UnmarshalOptions struct {
pragma.NoUnkeyedLiterals
Resolver interface {
protoregistry.MessageTypeResolver
protoregistry.ExtensionTypeResolver
}
}
func ( UnmarshalOptions) ( []byte, proto.Message) error {
proto.Reset()
if .Resolver == nil {
.Resolver = protoregistry.GlobalTypes
}
:= decoder{text.NewDecoder(), }
if := .unmarshalMessage(.ProtoReflect(), false); != nil {
return
}
if .AllowPartial {
return nil
}
return proto.CheckInitialized()
}
type decoder struct {
*text.Decoder
opts UnmarshalOptions
}
func ( decoder) ( pref.Message, bool) error {
:= .Descriptor()
if !flags.ProtoLegacy && messageset.IsMessageSet() {
return errors.New("no support for proto1 MessageSets")
}
if .FullName() == genid.Any_message_fullname {
return .unmarshalAny(, )
}
if {
, := .Read()
if != nil {
return
}
if .Kind() != text.MessageOpen {
return .unexpectedTokenError()
}
}
var set.Ints
var set.Ints
:= .Fields()
case text.EOF:
if {
return text.ErrUnexpectedEOF
}
return nil
default:
if && == text.MessageClose {
return nil
}
return .unexpectedTokenError()
}
, = .findExtension(pref.FullName(.TypeName()))
case text.FieldNumber:
= true
:= pref.FieldNumber(.FieldNumber())
if !.IsValid() {
return .newError(.Pos(), "invalid field number: %d", )
}
= .ByNumber()
if == nil {
, = .opts.Resolver.FindExtensionByNumber(.FullName(), )
}
}
if != nil {
= .TypeDescriptor()
if !.ExtensionRanges().Has(.Number()) || .ContainingMessage().FullName() != .FullName() {
return .newError(.Pos(), "message %v cannot be extended by %v", .FullName(), .FullName())
}
} else if != nil && != protoregistry.NotFound {
return .newError(.Pos(), "unable to resolve [%s]: %v", .RawString(), )
}
if flags.ProtoLegacy {
if != nil && .IsWeak() && .Message().IsPlaceholder() {
= nil // reset since the weak reference is not linked in
}
}
if == nil {
if .opts.DiscardUnknown || .ReservedNames().Has() {
.skipValue()
continue
}
return .newError(.Pos(), "unknown field: %v", .RawString())
}
return .newError(.Pos(), "cannot specify field by number: %v", .RawString())
}
switch {
case .IsList():
:= .Kind()
if != pref.MessageKind && != pref.GroupKind && !.HasSeparator() {
return .syntaxError(.Pos(), "missing field separator :")
}
:= .Mutable().List()
if := .unmarshalList(, ); != nil {
return
}
case .IsMap():
:= .Mutable().Map()
if := .unmarshalMap(, ); != nil {
return
}
default:
:= .Kind()
if != pref.MessageKind && != pref.GroupKind && !.HasSeparator() {
return .syntaxError(.Pos(), "missing field separator :")
}
if := .ContainingOneof(); != nil {
:= uint64(.Index())
if .Has() {
return .newError(.Pos(), "error parsing %q, oneof %v is already set", .RawString(), .FullName())
}
.Set()
}
:= uint64(.Number())
if .Has() {
return .newError(.Pos(), "non-repeated field %q is repeated", .RawString())
}
if := .unmarshalSingular(, ); != nil {
return
}
.Set()
}
}
return nil
}
func ( decoder) ( pref.FullName) (pref.ExtensionType, error) {
, := .opts.Resolver.FindExtensionByName()
if == nil {
return , nil
}
return messageset.FindMessageSetExtension(.opts.Resolver, )
}
func ( decoder) ( pref.FieldDescriptor) (pref.Value, error) {
, := .Read()
if != nil {
return pref.Value{},
}
if .Kind() != text.Scalar {
return pref.Value{}, .unexpectedTokenError()
}
:= .Kind()
switch {
case pref.BoolKind:
if , := .Bool(); {
return pref.ValueOfBool(), nil
}
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
if , := .Int32(); {
return pref.ValueOfInt32(), nil
}
case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
if , := .Int64(); {
return pref.ValueOfInt64(), nil
}
case pref.Uint32Kind, pref.Fixed32Kind:
if , := .Uint32(); {
return pref.ValueOfUint32(), nil
}
case pref.Uint64Kind, pref.Fixed64Kind:
if , := .Uint64(); {
return pref.ValueOfUint64(), nil
}
case pref.FloatKind:
if , := .Float32(); {
return pref.ValueOfFloat32(), nil
}
case pref.DoubleKind:
if , := .Float64(); {
return pref.ValueOfFloat64(), nil
}
case pref.StringKind:
if , := .String(); {
if strs.EnforceUTF8() && !utf8.ValidString() {
return pref.Value{}, .newError(.Pos(), "contains invalid UTF-8")
}
return pref.ValueOfString(), nil
}
case pref.BytesKind:
if , := .String(); {
return pref.ValueOfBytes([]byte()), nil
}
case pref.EnumKind:
if := .Enum().Values().ByName(pref.Name()); != nil {
return pref.ValueOfEnum(.Number()), nil
}
}
if , := .Int32(); {
return pref.ValueOfEnum(pref.EnumNumber()), nil
}
default:
panic(fmt.Sprintf("invalid scalar kind %v", ))
}
return pref.Value{}, .newError(.Pos(), "invalid value for %v type: %v", , .RawString())
}
func ( decoder) ( pref.FieldDescriptor, pref.List) error {
, := .Peek()
if != nil {
return
}
switch .Kind() {
case pref.MessageKind, pref.GroupKind:
switch .Kind() {
case text.ListOpen:
.Read()
for {
, := .Peek()
if != nil {
return
}
switch .Kind() {
case text.ListClose:
.Read()
return nil
case text.MessageOpen:
:= .NewElement()
if := .unmarshalMessage(.Message(), true); != nil {
return
}
.Append()
default:
return .unexpectedTokenError()
}
}
case text.MessageOpen:
:= .NewElement()
if := .unmarshalMessage(.Message(), true); != nil {
return
}
.Append()
return nil
}
default:
switch .Kind() {
case text.ListOpen:
.Read()
for {
, := .Peek()
if != nil {
return
}
switch .Kind() {
case text.ListClose:
.Read()
return nil
case text.Scalar:
, := .unmarshalScalar()
if != nil {
return
}
.Append()
default:
return .unexpectedTokenError()
}
}
case text.Scalar:
, := .unmarshalScalar()
if != nil {
return
}
.Append()
return nil
}
}
return .unexpectedTokenError()
}
var func() (pref.Value, error)
switch .MapValue().Kind() {
case pref.MessageKind, pref.GroupKind:
= func() (pref.Value, error) {
:= .NewValue()
if := .unmarshalMessage(.Message(), true); != nil {
return pref.Value{},
}
return , nil
}
default:
= func() (pref.Value, error) {
return .unmarshalScalar(.MapValue())
}
}
, := .Read()
if != nil {
return
}
switch .Kind() {
case text.MessageOpen:
return .unmarshalMapEntry(, , )
case text.ListOpen:
for {
, := .Read()
if != nil {
return
}
switch .Kind() {
case text.ListClose:
return nil
case text.MessageOpen:
if := .unmarshalMapEntry(, , ); != nil {
return
}
default:
return .unexpectedTokenError()
}
}
default:
return .unexpectedTokenError()
}
}
case text.MessageClose:
break
default:
return .unexpectedTokenError()
}
switch := pref.Name(.IdentName()); {
case genid.MapEntry_Key_field_name:
if !.HasSeparator() {
return .syntaxError(.Pos(), "missing field separator :")
}
if .IsValid() {
return .newError(.Pos(), "map entry %q cannot be repeated", )
}
, := .unmarshalScalar(.MapKey())
if != nil {
return
}
= .MapKey()
case genid.MapEntry_Value_field_name:
if := .MapValue().Kind(); ( != pref.MessageKind) && ( != pref.GroupKind) {
if !.HasSeparator() {
return .syntaxError(.Pos(), "missing field separator :")
}
}
if .IsValid() {
return .newError(.Pos(), "map entry %q cannot be repeated", )
}
, = ()
if != nil {
return
}
default:
if !.opts.DiscardUnknown {
return .newError(.Pos(), "unknown map entry field %q", )
}
.skipValue()
}
}
if !.IsValid() {
= .MapKey().Default().MapKey()
}
if !.IsValid() {
switch .MapValue().Kind() {
if !.HasSeparator() {
return .syntaxError(.Pos(), "missing field separator :")
}
switch := pref.Name(.IdentName()); {
case genid.Any_TypeUrl_field_name:
if {
return .newError(.Pos(), "duplicate %v field", genid.Any_TypeUrl_field_fullname)
}
if {
return .newError(.Pos(), "conflict with [%s] field", )
}
, := .Read()
if != nil {
return
}
var bool
, = .String()
if ! {
return .newError(.Pos(), "invalid %v field value: %v", genid.Any_TypeUrl_field_fullname, .RawString())
}
= true
case genid.Any_Value_field_name:
if {
return .newError(.Pos(), "duplicate %v field", genid.Any_Value_field_fullname)
}
if {
return .newError(.Pos(), "conflict with [%s] field", )
}
, := .Read()
if != nil {
return
}
, := .String()
if ! {
return .newError(.Pos(), "invalid %v field value: %v", genid.Any_Value_field_fullname, .RawString())
}
= []byte()
= true
default:
if !.opts.DiscardUnknown {
return .newError(.Pos(), "invalid field name %q in %v message", .RawString(), genid.Any_message_fullname)
}
}
case text.TypeName:
if {
return .newError(.Pos(), "cannot have more than one type")
}
if {
return .newError(.Pos(), "conflict with type_url field")
}
= .TypeName()
var error
, = .unmarshalExpandedAny(, .Pos())
if != nil {
return
}
= true
default:
if !.opts.DiscardUnknown {
return .newError(.Pos(), "invalid field name %q in %v message", .RawString(), genid.Any_message_fullname)
}
}
}
:= .Descriptor().Fields()
if len() > 0 {
.Set(.ByNumber(genid.Any_TypeUrl_field_number), pref.ValueOfString())
}
if len() > 0 {
.Set(.ByNumber(genid.Any_Value_field_number), pref.ValueOfBytes())
}
return nil
}
func ( decoder) ( string, int) ([]byte, error) {
, := .opts.Resolver.FindMessageByURL()
if != nil {
return nil, .newError(, "unable to resolve message [%v]: %v", , )
:= .New()
if := .unmarshalMessage(, true); != nil {
return nil,
, := proto.MarshalOptions{
AllowPartial: true, // Never check required fields inside an Any.
Deterministic: true,
}.Marshal(.Interface())
if != nil {
return nil, .newError(, "error in marshaling message into Any.value: %v", )
}
return , nil
}
switch .Kind() {
case text.MessageOpen:
return .skipMessageValue()
case text.ListOpen:
for {
, := .Read()
if != nil {
return
}
switch .Kind() {
case text.ListClose:
return nil
case text.MessageOpen:
return .skipMessageValue()
![]() |
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. |