vendor/google.golang.org/protobuf/encoding/prototext/encode.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    18 	"google.golang.org/protobuf/internal/order"
    18 	"google.golang.org/protobuf/internal/order"
    19 	"google.golang.org/protobuf/internal/pragma"
    19 	"google.golang.org/protobuf/internal/pragma"
    20 	"google.golang.org/protobuf/internal/strs"
    20 	"google.golang.org/protobuf/internal/strs"
    21 	"google.golang.org/protobuf/proto"
    21 	"google.golang.org/protobuf/proto"
    22 	"google.golang.org/protobuf/reflect/protoreflect"
    22 	"google.golang.org/protobuf/reflect/protoreflect"
    23 	pref "google.golang.org/protobuf/reflect/protoreflect"
       
    24 	"google.golang.org/protobuf/reflect/protoregistry"
    23 	"google.golang.org/protobuf/reflect/protoregistry"
    25 )
    24 )
    26 
    25 
    27 const defaultIndent = "  "
    26 const defaultIndent = "  "
    28 
    27 
   148 	*text.Encoder
   147 	*text.Encoder
   149 	opts MarshalOptions
   148 	opts MarshalOptions
   150 }
   149 }
   151 
   150 
   152 // marshalMessage marshals the given protoreflect.Message.
   151 // marshalMessage marshals the given protoreflect.Message.
   153 func (e encoder) marshalMessage(m pref.Message, inclDelims bool) error {
   152 func (e encoder) marshalMessage(m protoreflect.Message, inclDelims bool) error {
   154 	messageDesc := m.Descriptor()
   153 	messageDesc := m.Descriptor()
   155 	if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
   154 	if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
   156 		return errors.New("no support for proto1 MessageSets")
   155 		return errors.New("no support for proto1 MessageSets")
   157 	}
   156 	}
   158 
   157 
   188 
   187 
   189 	return nil
   188 	return nil
   190 }
   189 }
   191 
   190 
   192 // marshalField marshals the given field with protoreflect.Value.
   191 // marshalField marshals the given field with protoreflect.Value.
   193 func (e encoder) marshalField(name string, val pref.Value, fd pref.FieldDescriptor) error {
   192 func (e encoder) marshalField(name string, val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
   194 	switch {
   193 	switch {
   195 	case fd.IsList():
   194 	case fd.IsList():
   196 		return e.marshalList(name, val.List(), fd)
   195 		return e.marshalList(name, val.List(), fd)
   197 	case fd.IsMap():
   196 	case fd.IsMap():
   198 		return e.marshalMap(name, val.Map(), fd)
   197 		return e.marshalMap(name, val.Map(), fd)
   202 	}
   201 	}
   203 }
   202 }
   204 
   203 
   205 // marshalSingular marshals the given non-repeated field value. This includes
   204 // marshalSingular marshals the given non-repeated field value. This includes
   206 // all scalar types, enums, messages, and groups.
   205 // all scalar types, enums, messages, and groups.
   207 func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error {
   206 func (e encoder) marshalSingular(val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
   208 	kind := fd.Kind()
   207 	kind := fd.Kind()
   209 	switch kind {
   208 	switch kind {
   210 	case pref.BoolKind:
   209 	case protoreflect.BoolKind:
   211 		e.WriteBool(val.Bool())
   210 		e.WriteBool(val.Bool())
   212 
   211 
   213 	case pref.StringKind:
   212 	case protoreflect.StringKind:
   214 		s := val.String()
   213 		s := val.String()
   215 		if !e.opts.allowInvalidUTF8 && strs.EnforceUTF8(fd) && !utf8.ValidString(s) {
   214 		if !e.opts.allowInvalidUTF8 && strs.EnforceUTF8(fd) && !utf8.ValidString(s) {
   216 			return errors.InvalidUTF8(string(fd.FullName()))
   215 			return errors.InvalidUTF8(string(fd.FullName()))
   217 		}
   216 		}
   218 		e.WriteString(s)
   217 		e.WriteString(s)
   219 
   218 
   220 	case pref.Int32Kind, pref.Int64Kind,
   219 	case protoreflect.Int32Kind, protoreflect.Int64Kind,
   221 		pref.Sint32Kind, pref.Sint64Kind,
   220 		protoreflect.Sint32Kind, protoreflect.Sint64Kind,
   222 		pref.Sfixed32Kind, pref.Sfixed64Kind:
   221 		protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind:
   223 		e.WriteInt(val.Int())
   222 		e.WriteInt(val.Int())
   224 
   223 
   225 	case pref.Uint32Kind, pref.Uint64Kind,
   224 	case protoreflect.Uint32Kind, protoreflect.Uint64Kind,
   226 		pref.Fixed32Kind, pref.Fixed64Kind:
   225 		protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
   227 		e.WriteUint(val.Uint())
   226 		e.WriteUint(val.Uint())
   228 
   227 
   229 	case pref.FloatKind:
   228 	case protoreflect.FloatKind:
   230 		// Encoder.WriteFloat handles the special numbers NaN and infinites.
   229 		// Encoder.WriteFloat handles the special numbers NaN and infinites.
   231 		e.WriteFloat(val.Float(), 32)
   230 		e.WriteFloat(val.Float(), 32)
   232 
   231 
   233 	case pref.DoubleKind:
   232 	case protoreflect.DoubleKind:
   234 		// Encoder.WriteFloat handles the special numbers NaN and infinites.
   233 		// Encoder.WriteFloat handles the special numbers NaN and infinites.
   235 		e.WriteFloat(val.Float(), 64)
   234 		e.WriteFloat(val.Float(), 64)
   236 
   235 
   237 	case pref.BytesKind:
   236 	case protoreflect.BytesKind:
   238 		e.WriteString(string(val.Bytes()))
   237 		e.WriteString(string(val.Bytes()))
   239 
   238 
   240 	case pref.EnumKind:
   239 	case protoreflect.EnumKind:
   241 		num := val.Enum()
   240 		num := val.Enum()
   242 		if desc := fd.Enum().Values().ByNumber(num); desc != nil {
   241 		if desc := fd.Enum().Values().ByNumber(num); desc != nil {
   243 			e.WriteLiteral(string(desc.Name()))
   242 			e.WriteLiteral(string(desc.Name()))
   244 		} else {
   243 		} else {
   245 			// Use numeric value if there is no enum description.
   244 			// Use numeric value if there is no enum description.
   246 			e.WriteInt(int64(num))
   245 			e.WriteInt(int64(num))
   247 		}
   246 		}
   248 
   247 
   249 	case pref.MessageKind, pref.GroupKind:
   248 	case protoreflect.MessageKind, protoreflect.GroupKind:
   250 		return e.marshalMessage(val.Message(), true)
   249 		return e.marshalMessage(val.Message(), true)
   251 
   250 
   252 	default:
   251 	default:
   253 		panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
   252 		panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
   254 	}
   253 	}
   255 	return nil
   254 	return nil
   256 }
   255 }
   257 
   256 
   258 // marshalList marshals the given protoreflect.List as multiple name-value fields.
   257 // marshalList marshals the given protoreflect.List as multiple name-value fields.
   259 func (e encoder) marshalList(name string, list pref.List, fd pref.FieldDescriptor) error {
   258 func (e encoder) marshalList(name string, list protoreflect.List, fd protoreflect.FieldDescriptor) error {
   260 	size := list.Len()
   259 	size := list.Len()
   261 	for i := 0; i < size; i++ {
   260 	for i := 0; i < size; i++ {
   262 		e.WriteName(name)
   261 		e.WriteName(name)
   263 		if err := e.marshalSingular(list.Get(i), fd); err != nil {
   262 		if err := e.marshalSingular(list.Get(i), fd); err != nil {
   264 			return err
   263 			return err
   266 	}
   265 	}
   267 	return nil
   266 	return nil
   268 }
   267 }
   269 
   268 
   270 // marshalMap marshals the given protoreflect.Map as multiple name-value fields.
   269 // marshalMap marshals the given protoreflect.Map as multiple name-value fields.
   271 func (e encoder) marshalMap(name string, mmap pref.Map, fd pref.FieldDescriptor) error {
   270 func (e encoder) marshalMap(name string, mmap protoreflect.Map, fd protoreflect.FieldDescriptor) error {
   272 	var err error
   271 	var err error
   273 	order.RangeEntries(mmap, order.GenericKeyOrder, func(key pref.MapKey, val pref.Value) bool {
   272 	order.RangeEntries(mmap, order.GenericKeyOrder, func(key protoreflect.MapKey, val protoreflect.Value) bool {
   274 		e.WriteName(name)
   273 		e.WriteName(name)
   275 		e.StartMessage()
   274 		e.StartMessage()
   276 		defer e.EndMessage()
   275 		defer e.EndMessage()
   277 
   276 
   278 		e.WriteName(string(genid.MapEntry_Key_field_name))
   277 		e.WriteName(string(genid.MapEntry_Key_field_name))
   332 	}
   331 	}
   333 }
   332 }
   334 
   333 
   335 // marshalAny marshals the given google.protobuf.Any message in expanded form.
   334 // marshalAny marshals the given google.protobuf.Any message in expanded form.
   336 // It returns true if it was able to marshal, else false.
   335 // It returns true if it was able to marshal, else false.
   337 func (e encoder) marshalAny(any pref.Message) bool {
   336 func (e encoder) marshalAny(any protoreflect.Message) bool {
   338 	// Construct the embedded message.
   337 	// Construct the embedded message.
   339 	fds := any.Descriptor().Fields()
   338 	fds := any.Descriptor().Fields()
   340 	fdType := fds.ByNumber(genid.Any_TypeUrl_field_number)
   339 	fdType := fds.ByNumber(genid.Any_TypeUrl_field_number)
   341 	typeURL := any.Get(fdType).String()
   340 	typeURL := any.Get(fdType).String()
   342 	mt, err := e.opts.Resolver.FindMessageByURL(typeURL)
   341 	mt, err := e.opts.Resolver.FindMessageByURL(typeURL)