blob: ef46a6d73c9d8efbc6ec77c0f122cb81cbdfd216 [file] [log] [blame]
Herbie Ong800c9902018-12-06 15:28:53 -08001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Damien Neil5c5b5312019-05-14 12:44:37 -07005package prototext
Herbie Ong800c9902018-12-06 15:28:53 -08006
7import (
8 "fmt"
Herbie Ong0dcfb9a2019-01-14 15:32:26 -08009 "strings"
Herbie Ong21a39742019-04-08 17:32:44 -070010 "unicode/utf8"
Herbie Ong800c9902018-12-06 15:28:53 -080011
Joe Tsai5ae10aa2019-07-11 18:23:08 -070012 "google.golang.org/protobuf/internal/encoding/messageset"
Damien Neile89e6242019-05-13 23:55:40 -070013 "google.golang.org/protobuf/internal/encoding/text"
14 "google.golang.org/protobuf/internal/errors"
15 "google.golang.org/protobuf/internal/fieldnum"
Joe Tsai5ae10aa2019-07-11 18:23:08 -070016 "google.golang.org/protobuf/internal/flags"
Damien Neile89e6242019-05-13 23:55:40 -070017 "google.golang.org/protobuf/internal/pragma"
18 "google.golang.org/protobuf/internal/set"
19 "google.golang.org/protobuf/proto"
20 pref "google.golang.org/protobuf/reflect/protoreflect"
21 "google.golang.org/protobuf/reflect/protoregistry"
Herbie Ong800c9902018-12-06 15:28:53 -080022)
23
24// Unmarshal reads the given []byte into the given proto.Message.
Joe Tsaicdb77732019-05-14 16:05:06 -070025func Unmarshal(b []byte, m proto.Message) error {
26 return UnmarshalOptions{}.Unmarshal(b, m)
Herbie Ong800c9902018-12-06 15:28:53 -080027}
28
Herbie Ong42577ea2019-03-26 16:26:22 -070029// UnmarshalOptions is a configurable textproto format unmarshaler.
Herbie Ong800c9902018-12-06 15:28:53 -080030type UnmarshalOptions struct {
31 pragma.NoUnkeyedLiterals
Herbie Ongc525c972018-12-18 18:04:31 -080032
Herbie Ong42577ea2019-03-26 16:26:22 -070033 // AllowPartial accepts input for messages that will result in missing
34 // required fields. If AllowPartial is false (the default), Unmarshal will
35 // return error if there are any missing required fields.
36 AllowPartial bool
37
Joe Tsai8689fa52019-09-15 00:17:39 -070038 // DiscardUnknown specifies whether to ignore unknown fields when parsing.
39 // An unknown field is any field whose field name or field number does not
40 // resolve to any known or extension field in the message.
41 // By default, unmarshal rejects unknown fields as an error.
42 DiscardUnknown bool
43
Joe Tsai1c283042019-05-14 14:28:19 -070044 // Resolver is used for looking up types when unmarshaling
45 // google.protobuf.Any messages or extension fields.
46 // If nil, this defaults to using protoregistry.GlobalTypes.
47 Resolver interface {
48 protoregistry.MessageTypeResolver
49 protoregistry.ExtensionTypeResolver
50 }
Herbie Ong800c9902018-12-06 15:28:53 -080051}
52
53// Unmarshal reads the given []byte and populates the given proto.Message using options in
54// UnmarshalOptions object.
Joe Tsaicdb77732019-05-14 16:05:06 -070055func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -080056 // Clear all fields before populating it.
Damien Neil5c5b5312019-05-14 12:44:37 -070057 // TODO: Determine if this needs to be consistent with protojson and binary unmarshal where
Herbie Ong800c9902018-12-06 15:28:53 -080058 // behavior is to merge values into existing message. If decision is to not clear the fields
59 // ahead, code will need to be updated properly when merging nested messages.
Joe Tsai378c1322019-04-25 23:48:08 -070060 proto.Reset(m)
Herbie Ong800c9902018-12-06 15:28:53 -080061
62 // Parse into text.Value of message type.
63 val, err := text.Unmarshal(b)
Damien Neil8c86fc52019-06-19 09:28:29 -070064 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080065 return err
66 }
67
Herbie Ongc525c972018-12-18 18:04:31 -080068 if o.Resolver == nil {
69 o.Resolver = protoregistry.GlobalTypes
70 }
Joe Tsai378c1322019-04-25 23:48:08 -070071 err = o.unmarshalMessage(val.Message(), m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070072 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080073 return err
74 }
75
Damien Neil8c86fc52019-06-19 09:28:29 -070076 if o.AllowPartial {
77 return nil
Damien Neil4686e232019-04-05 13:31:40 -070078 }
Damien Neil8c86fc52019-06-19 09:28:29 -070079 return proto.IsInitialized(m)
Herbie Ong800c9902018-12-06 15:28:53 -080080}
81
Herbie Ong800c9902018-12-06 15:28:53 -080082// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
83func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070084 messageDesc := m.Descriptor()
Joe Tsai1799d112019-08-08 13:31:59 -070085 if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
Joe Tsai5ae10aa2019-07-11 18:23:08 -070086 return errors.New("no support for proto1 MessageSets")
87 }
Herbie Ong66c365c2019-01-04 14:08:41 -080088
89 // Handle expanded Any message.
Joe Tsai0fc49f82019-05-01 12:29:25 -070090 if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
Joe Tsai378c1322019-04-25 23:48:08 -070091 return o.unmarshalAny(tmsg[0], m)
Herbie Ong66c365c2019-01-04 14:08:41 -080092 }
93
Herbie Ong800c9902018-12-06 15:28:53 -080094 var seenNums set.Ints
Herbie Ong8a1d4602019-04-02 20:19:36 -070095 var seenOneofs set.Ints
Joe Tsai5ae10aa2019-07-11 18:23:08 -070096 fieldDescs := messageDesc.Fields()
Herbie Ong800c9902018-12-06 15:28:53 -080097 for _, tfield := range tmsg {
98 tkey := tfield[0]
99 tval := tfield[1]
100
Joe Tsai8689fa52019-09-15 00:17:39 -0700101 // Resolve the field descriptor.
Herbie Ongc525c972018-12-18 18:04:31 -0800102 var name pref.Name
Joe Tsai8689fa52019-09-15 00:17:39 -0700103 var fd pref.FieldDescriptor
104 var xt pref.ExtensionType
105 var xtErr error
Herbie Ongc525c972018-12-18 18:04:31 -0800106 switch tkey.Type() {
107 case text.Name:
108 name, _ = tkey.Name()
Herbie Ong800c9902018-12-06 15:28:53 -0800109 fd = fieldDescs.ByName(name)
Joe Tsai8689fa52019-09-15 00:17:39 -0700110 if fd == nil {
Joe Tsaid47ea192019-07-09 22:38:15 -0700111 // The proto name of a group field is in all lowercase,
112 // while the textproto field name is the group message name.
Herbie Ong20aefe92019-06-24 19:21:46 -0700113 gd := fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
114 if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == name {
115 fd = gd
116 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700117 } else if fd.Kind() == pref.GroupKind && fd.Message().Name() != name {
Joe Tsaid47ea192019-07-09 22:38:15 -0700118 fd = nil // reset since field name is actually the message name
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800119 }
Herbie Ongc525c972018-12-18 18:04:31 -0800120 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800121 // Handle extensions only. This code path is not for Any.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700122 if messageDesc.FullName() == "google.protobuf.Any" {
Herbie Ong66c365c2019-01-04 14:08:41 -0800123 break
124 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700125 xt, xtErr = o.findExtension(pref.FullName(tkey.String()))
126 case text.Uint:
127 v, _ := tkey.Uint(false)
128 num := pref.FieldNumber(v)
129 if !num.IsValid() {
130 return errors.New("invalid field number: %d", num)
Herbie Ongc525c972018-12-18 18:04:31 -0800131 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700132 fd = fieldDescs.ByNumber(num)
133 if fd == nil {
134 xt, xtErr = o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
Damien Neil92f76182019-08-02 16:58:08 -0700135 }
Herbie Ong800c9902018-12-06 15:28:53 -0800136 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700137 if xt != nil {
138 fd = xt.TypeDescriptor()
139 if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() {
140 return errors.New("message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName())
141 }
142 } else if xtErr != nil && xtErr != protoregistry.NotFound {
143 return errors.New("unable to resolve: %v", xtErr)
144 }
145 if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() {
146 fd = nil // reset since the weak reference is not linked in
147 }
Herbie Ongc525c972018-12-18 18:04:31 -0800148
Joe Tsai8689fa52019-09-15 00:17:39 -0700149 // Handle unknown fields.
Herbie Ong800c9902018-12-06 15:28:53 -0800150 if fd == nil {
Joe Tsai8689fa52019-09-15 00:17:39 -0700151 if o.DiscardUnknown || messageDesc.ReservedNames().Has(name) {
Herbie Ong7c624e22018-12-13 14:41:22 -0800152 continue
153 }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700154 return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
Herbie Ong800c9902018-12-06 15:28:53 -0800155 }
156
Joe Tsai8689fa52019-09-15 00:17:39 -0700157 // Handle fields identified by field number.
158 if tkey.Type() == text.Uint {
159 // TODO: Add an option to permit parsing field numbers.
160 //
161 // This requires careful thought as the MarshalOptions.EmitUnknown
162 // option allows formatting unknown fields as the field number
163 // and the best-effort textual representation of the field value.
164 // In that case, it may not be possible to unmarshal the value from
165 // a parser that does have information about the unknown field.
166 return errors.New("cannot specify field by number: %v", tkey)
167 }
168
Joe Tsaiac31a352019-05-13 14:32:56 -0700169 switch {
170 case fd.IsList():
171 // If input is not a list, turn it into a list.
172 var items []text.Value
173 if tval.Type() != text.List {
174 items = []text.Value{tval}
175 } else {
176 items = tval.List()
177 }
178
Joe Tsai378c1322019-04-25 23:48:08 -0700179 list := m.Mutable(fd).List()
Damien Neil8c86fc52019-06-19 09:28:29 -0700180 if err := o.unmarshalList(items, fd, list); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800181 return err
182 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700183 case fd.IsMap():
184 // If input is not a list, turn it into a list.
185 var items []text.Value
186 if tval.Type() != text.List {
187 items = []text.Value{tval}
188 } else {
189 items = tval.List()
190 }
191
Joe Tsai378c1322019-04-25 23:48:08 -0700192 mmap := m.Mutable(fd).Map()
Damien Neil8c86fc52019-06-19 09:28:29 -0700193 if err := o.unmarshalMap(items, fd, mmap); err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700194 return err
195 }
196 default:
Herbie Ong8a1d4602019-04-02 20:19:36 -0700197 // If field is a oneof, check if it has already been set.
Joe Tsaiac31a352019-05-13 14:32:56 -0700198 if od := fd.ContainingOneof(); od != nil {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700199 idx := uint64(od.Index())
200 if seenOneofs.Has(idx) {
201 return errors.New("oneof %v is already set", od.FullName())
202 }
203 seenOneofs.Set(idx)
204 }
205
Herbie Ong800c9902018-12-06 15:28:53 -0800206 // Required or optional fields.
207 num := uint64(fd.Number())
208 if seenNums.Has(num) {
209 return errors.New("non-repeated field %v is repeated", fd.FullName())
210 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700211 if err := o.unmarshalSingular(tval, fd, m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800212 return err
213 }
Herbie Ong800c9902018-12-06 15:28:53 -0800214 seenNums.Set(num)
215 }
216 }
217
Damien Neil8c86fc52019-06-19 09:28:29 -0700218 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800219}
220
Herbie Ong6470ea62019-01-07 18:56:57 -0800221// findExtension returns protoreflect.ExtensionType from the Resolver if found.
222func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
223 xt, err := o.Resolver.FindExtensionByName(xtName)
224 if err == nil {
225 return xt, nil
226 }
Joe Tsai5ae10aa2019-07-11 18:23:08 -0700227 return messageset.FindMessageSetExtension(o.Resolver, xtName)
Herbie Ong6470ea62019-01-07 18:56:57 -0800228}
229
Herbie Ong800c9902018-12-06 15:28:53 -0800230// unmarshalSingular unmarshals given text.Value into the non-repeated field.
Joe Tsai378c1322019-04-25 23:48:08 -0700231func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800232 var val pref.Value
233 switch fd.Kind() {
234 case pref.MessageKind, pref.GroupKind:
235 if input.Type() != text.Message {
236 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
237 }
Damien Neild91c4222019-09-04 10:46:00 -0700238 val = m.NewField(fd)
239 if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800240 return err
241 }
Herbie Ong800c9902018-12-06 15:28:53 -0800242 default:
243 var err error
244 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700245 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800246 return err
247 }
248 }
Joe Tsai378c1322019-04-25 23:48:08 -0700249 m.Set(fd, val)
Herbie Ong800c9902018-12-06 15:28:53 -0800250
Damien Neil8c86fc52019-06-19 09:28:29 -0700251 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800252}
253
Herbie Ong800c9902018-12-06 15:28:53 -0800254// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
255// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
256func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
257 const b32 = false
258 const b64 = true
259
260 switch kind := fd.Kind(); kind {
261 case pref.BoolKind:
262 if b, ok := input.Bool(); ok {
263 return pref.ValueOf(bool(b)), nil
264 }
265 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
266 if n, ok := input.Int(b32); ok {
267 return pref.ValueOf(int32(n)), nil
268 }
269 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
270 if n, ok := input.Int(b64); ok {
271 return pref.ValueOf(int64(n)), nil
272 }
273 case pref.Uint32Kind, pref.Fixed32Kind:
274 if n, ok := input.Uint(b32); ok {
275 return pref.ValueOf(uint32(n)), nil
276 }
277 case pref.Uint64Kind, pref.Fixed64Kind:
278 if n, ok := input.Uint(b64); ok {
279 return pref.ValueOf(uint64(n)), nil
280 }
281 case pref.FloatKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700282 if n, ok := input.Float(b32); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800283 return pref.ValueOf(float32(n)), nil
284 }
285 case pref.DoubleKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700286 if n, ok := input.Float(b64); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800287 return pref.ValueOf(float64(n)), nil
288 }
289 case pref.StringKind:
290 if input.Type() == text.String {
Herbie Ong21a39742019-04-08 17:32:44 -0700291 s := input.String()
292 if utf8.ValidString(s) {
293 return pref.ValueOf(s), nil
294 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700295 return pref.Value{}, errors.InvalidUTF8(string(fd.FullName()))
Herbie Ong800c9902018-12-06 15:28:53 -0800296 }
297 case pref.BytesKind:
298 if input.Type() == text.String {
299 return pref.ValueOf([]byte(input.String())), nil
300 }
301 case pref.EnumKind:
302 // If input is int32, use directly.
303 if n, ok := input.Int(b32); ok {
304 return pref.ValueOf(pref.EnumNumber(n)), nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800305 }
306 if name, ok := input.Name(); ok {
307 // Lookup EnumNumber based on name.
Joe Tsaid24bc722019-04-15 23:39:09 -0700308 if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800309 return pref.ValueOf(enumVal.Number()), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800310 }
311 }
312 default:
313 panic(fmt.Sprintf("invalid scalar kind %v", kind))
314 }
315
316 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
317}
318
319// unmarshalList unmarshals given []text.Value into given protoreflect.List.
320func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800321 switch fd.Kind() {
322 case pref.MessageKind, pref.GroupKind:
323 for _, input := range inputList {
324 if input.Type() != text.Message {
325 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
326 }
Damien Neild91c4222019-09-04 10:46:00 -0700327 val := list.NewElement()
328 if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800329 return err
330 }
Damien Neild91c4222019-09-04 10:46:00 -0700331 list.Append(val)
Herbie Ong800c9902018-12-06 15:28:53 -0800332 }
333 default:
334 for _, input := range inputList {
335 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700336 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800337 return err
338 }
339 list.Append(val)
340 }
341 }
342
Damien Neil8c86fc52019-06-19 09:28:29 -0700343 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800344}
345
346// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
347func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800348 // Determine ahead whether map entry is a scalar type or a message type in order to call the
349 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800350 unmarshalMapValue := unmarshalMapScalarValue
Joe Tsaiac31a352019-05-13 14:32:56 -0700351 switch fd.MapValue().Kind() {
Herbie Ong800c9902018-12-06 15:28:53 -0800352 case pref.MessageKind, pref.GroupKind:
353 unmarshalMapValue = o.unmarshalMapMessageValue
354 }
355
356 for _, entry := range input {
357 if entry.Type() != text.Message {
358 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
359 }
360 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
Damien Neil8c86fc52019-06-19 09:28:29 -0700361 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800362 return err
363 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700364 pkey, err := unmarshalMapKey(tkey, fd.MapKey())
Damien Neil8c86fc52019-06-19 09:28:29 -0700365 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800366 return err
367 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700368 err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
Damien Neil8c86fc52019-06-19 09:28:29 -0700369 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800370 return err
371 }
372 }
373
Damien Neil8c86fc52019-06-19 09:28:29 -0700374 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800375}
376
377// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
378// field values. If there are duplicate field names, the value for the last field is returned. If
379// the field name does not exist, it will return the zero value of text.Value. It will return an
380// error if there are unknown field names.
381func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
382 for _, field := range mapEntry {
383 keyStr, ok := field[0].Name()
384 if ok {
385 switch keyStr {
386 case "key":
387 if key.Type() != 0 {
388 return key, value, errors.New("%v contains duplicate key field", name)
389 }
390 key = field[1]
391 case "value":
392 if value.Type() != 0 {
393 return key, value, errors.New("%v contains duplicate value field", name)
394 }
395 value = field[1]
396 default:
397 ok = false
398 }
399 }
400 if !ok {
401 // TODO: Do not return error if ignore unknown option is added and enabled.
402 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
403 }
404 }
405 return key, value, nil
406}
407
408// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
409// integral or string type.
410func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
411 // If input is not set, use the zero value.
412 if input.Type() == 0 {
413 return fd.Default().MapKey(), nil
414 }
415
416 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700417 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800418 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
419 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700420 return val.MapKey(), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800421}
422
423// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
424// the given MapKey.
425func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800426 var value [][2]text.Value
427 if input.Type() != 0 {
428 value = input.Message()
429 }
Damien Neild91c4222019-09-04 10:46:00 -0700430 val := mmap.NewValue()
431 if err := o.unmarshalMessage(value, val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800432 return err
433 }
Damien Neild91c4222019-09-04 10:46:00 -0700434 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700435 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800436}
437
438// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
439// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800440func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800441 var val pref.Value
442 if input.Type() == 0 {
443 val = fd.Default()
444 } else {
445 var err error
446 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700447 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800448 return err
449 }
450 }
451 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700452 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800453}
Herbie Ong66c365c2019-01-04 14:08:41 -0800454
455// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
456// field with key type of text.String type and value type of text.Message.
457func isExpandedAny(tmsg [][2]text.Value) bool {
458 if len(tmsg) != 1 {
459 return false
460 }
461
462 field := tmsg[0]
463 return field[0].Type() == text.String && field[1].Type() == text.Message
464}
465
466// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
467// tfield has key type of text.String and value type of text.Message.
Joe Tsai378c1322019-04-25 23:48:08 -0700468func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error {
Herbie Ong66c365c2019-01-04 14:08:41 -0800469 typeURL := tfield[0].String()
470 value := tfield[1].Message()
471
472 mt, err := o.Resolver.FindMessageByURL(typeURL)
Damien Neil8c86fc52019-06-19 09:28:29 -0700473 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800474 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
475 }
476 // Create new message for the embedded message type and unmarshal the
477 // value into it.
Joe Tsai378c1322019-04-25 23:48:08 -0700478 m2 := mt.New()
Damien Neil8c86fc52019-06-19 09:28:29 -0700479 if err := o.unmarshalMessage(value, m2); err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800480 return err
481 }
482 // Serialize the embedded message and assign the resulting bytes to the value field.
Damien Neil96c229a2019-04-03 12:17:24 -0700483 b, err := proto.MarshalOptions{
Damien Neil0c9f0a92019-06-19 10:41:09 -0700484 AllowPartial: true, // never check required fields inside an Any
Damien Neil96c229a2019-04-03 12:17:24 -0700485 Deterministic: true,
Joe Tsai378c1322019-04-25 23:48:08 -0700486 }.Marshal(m2.Interface())
Damien Neil8c86fc52019-06-19 09:28:29 -0700487 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800488 return err
489 }
490
Joe Tsai378c1322019-04-25 23:48:08 -0700491 fds := m.Descriptor().Fields()
492 fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
493 fdValue := fds.ByNumber(fieldnum.Any_Value)
494
495 m.Set(fdType, pref.ValueOf(typeURL))
496 m.Set(fdValue, pref.ValueOf(b))
Herbie Ong66c365c2019-01-04 14:08:41 -0800497
Damien Neil8c86fc52019-06-19 09:28:29 -0700498 return nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800499}