decode.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. // 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. package prototext
  5. import (
  6. "fmt"
  7. "unicode/utf8"
  8. "google.golang.org/protobuf/internal/encoding/messageset"
  9. "google.golang.org/protobuf/internal/encoding/text"
  10. "google.golang.org/protobuf/internal/errors"
  11. "google.golang.org/protobuf/internal/flags"
  12. "google.golang.org/protobuf/internal/genid"
  13. "google.golang.org/protobuf/internal/pragma"
  14. "google.golang.org/protobuf/internal/set"
  15. "google.golang.org/protobuf/internal/strs"
  16. "google.golang.org/protobuf/proto"
  17. "google.golang.org/protobuf/reflect/protoreflect"
  18. "google.golang.org/protobuf/reflect/protoregistry"
  19. )
  20. // Unmarshal reads the given []byte into the given proto.Message.
  21. // The provided message must be mutable (e.g., a non-nil pointer to a message).
  22. func Unmarshal(b []byte, m proto.Message) error {
  23. return UnmarshalOptions{}.Unmarshal(b, m)
  24. }
  25. // UnmarshalOptions is a configurable textproto format unmarshaler.
  26. type UnmarshalOptions struct {
  27. pragma.NoUnkeyedLiterals
  28. // AllowPartial accepts input for messages that will result in missing
  29. // required fields. If AllowPartial is false (the default), Unmarshal will
  30. // return error if there are any missing required fields.
  31. AllowPartial bool
  32. // DiscardUnknown specifies whether to ignore unknown fields when parsing.
  33. // An unknown field is any field whose field name or field number does not
  34. // resolve to any known or extension field in the message.
  35. // By default, unmarshal rejects unknown fields as an error.
  36. DiscardUnknown bool
  37. // Resolver is used for looking up types when unmarshaling
  38. // google.protobuf.Any messages or extension fields.
  39. // If nil, this defaults to using protoregistry.GlobalTypes.
  40. Resolver interface {
  41. protoregistry.MessageTypeResolver
  42. protoregistry.ExtensionTypeResolver
  43. }
  44. }
  45. // Unmarshal reads the given []byte and populates the given proto.Message
  46. // using options in the UnmarshalOptions object.
  47. // The provided message must be mutable (e.g., a non-nil pointer to a message).
  48. func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
  49. return o.unmarshal(b, m)
  50. }
  51. // unmarshal is a centralized function that all unmarshal operations go through.
  52. // For profiling purposes, avoid changing the name of this function or
  53. // introducing other code paths for unmarshal that do not go through this.
  54. func (o UnmarshalOptions) unmarshal(b []byte, m proto.Message) error {
  55. proto.Reset(m)
  56. if o.Resolver == nil {
  57. o.Resolver = protoregistry.GlobalTypes
  58. }
  59. dec := decoder{text.NewDecoder(b), o}
  60. if err := dec.unmarshalMessage(m.ProtoReflect(), false); err != nil {
  61. return err
  62. }
  63. if o.AllowPartial {
  64. return nil
  65. }
  66. return proto.CheckInitialized(m)
  67. }
  68. type decoder struct {
  69. *text.Decoder
  70. opts UnmarshalOptions
  71. }
  72. // newError returns an error object with position info.
  73. func (d decoder) newError(pos int, f string, x ...interface{}) error {
  74. line, column := d.Position(pos)
  75. head := fmt.Sprintf("(line %d:%d): ", line, column)
  76. return errors.New(head+f, x...)
  77. }
  78. // unexpectedTokenError returns a syntax error for the given unexpected token.
  79. func (d decoder) unexpectedTokenError(tok text.Token) error {
  80. return d.syntaxError(tok.Pos(), "unexpected token: %s", tok.RawString())
  81. }
  82. // syntaxError returns a syntax error for given position.
  83. func (d decoder) syntaxError(pos int, f string, x ...interface{}) error {
  84. line, column := d.Position(pos)
  85. head := fmt.Sprintf("syntax error (line %d:%d): ", line, column)
  86. return errors.New(head+f, x...)
  87. }
  88. // unmarshalMessage unmarshals into the given protoreflect.Message.
  89. func (d decoder) unmarshalMessage(m protoreflect.Message, checkDelims bool) error {
  90. messageDesc := m.Descriptor()
  91. if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
  92. return errors.New("no support for proto1 MessageSets")
  93. }
  94. if messageDesc.FullName() == genid.Any_message_fullname {
  95. return d.unmarshalAny(m, checkDelims)
  96. }
  97. if checkDelims {
  98. tok, err := d.Read()
  99. if err != nil {
  100. return err
  101. }
  102. if tok.Kind() != text.MessageOpen {
  103. return d.unexpectedTokenError(tok)
  104. }
  105. }
  106. var seenNums set.Ints
  107. var seenOneofs set.Ints
  108. fieldDescs := messageDesc.Fields()
  109. for {
  110. // Read field name.
  111. tok, err := d.Read()
  112. if err != nil {
  113. return err
  114. }
  115. switch typ := tok.Kind(); typ {
  116. case text.Name:
  117. // Continue below.
  118. case text.EOF:
  119. if checkDelims {
  120. return text.ErrUnexpectedEOF
  121. }
  122. return nil
  123. default:
  124. if checkDelims && typ == text.MessageClose {
  125. return nil
  126. }
  127. return d.unexpectedTokenError(tok)
  128. }
  129. // Resolve the field descriptor.
  130. var name protoreflect.Name
  131. var fd protoreflect.FieldDescriptor
  132. var xt protoreflect.ExtensionType
  133. var xtErr error
  134. var isFieldNumberName bool
  135. switch tok.NameKind() {
  136. case text.IdentName:
  137. name = protoreflect.Name(tok.IdentName())
  138. fd = fieldDescs.ByTextName(string(name))
  139. case text.TypeName:
  140. // Handle extensions only. This code path is not for Any.
  141. xt, xtErr = d.opts.Resolver.FindExtensionByName(protoreflect.FullName(tok.TypeName()))
  142. case text.FieldNumber:
  143. isFieldNumberName = true
  144. num := protoreflect.FieldNumber(tok.FieldNumber())
  145. if !num.IsValid() {
  146. return d.newError(tok.Pos(), "invalid field number: %d", num)
  147. }
  148. fd = fieldDescs.ByNumber(num)
  149. if fd == nil {
  150. xt, xtErr = d.opts.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
  151. }
  152. }
  153. if xt != nil {
  154. fd = xt.TypeDescriptor()
  155. if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() {
  156. return d.newError(tok.Pos(), "message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName())
  157. }
  158. } else if xtErr != nil && xtErr != protoregistry.NotFound {
  159. return d.newError(tok.Pos(), "unable to resolve [%s]: %v", tok.RawString(), xtErr)
  160. }
  161. if flags.ProtoLegacy {
  162. if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() {
  163. fd = nil // reset since the weak reference is not linked in
  164. }
  165. }
  166. // Handle unknown fields.
  167. if fd == nil {
  168. if d.opts.DiscardUnknown || messageDesc.ReservedNames().Has(name) {
  169. d.skipValue()
  170. continue
  171. }
  172. return d.newError(tok.Pos(), "unknown field: %v", tok.RawString())
  173. }
  174. // Handle fields identified by field number.
  175. if isFieldNumberName {
  176. // TODO: Add an option to permit parsing field numbers.
  177. //
  178. // This requires careful thought as the MarshalOptions.EmitUnknown
  179. // option allows formatting unknown fields as the field number and the
  180. // best-effort textual representation of the field value. In that case,
  181. // it may not be possible to unmarshal the value from a parser that does
  182. // have information about the unknown field.
  183. return d.newError(tok.Pos(), "cannot specify field by number: %v", tok.RawString())
  184. }
  185. switch {
  186. case fd.IsList():
  187. kind := fd.Kind()
  188. if kind != protoreflect.MessageKind && kind != protoreflect.GroupKind && !tok.HasSeparator() {
  189. return d.syntaxError(tok.Pos(), "missing field separator :")
  190. }
  191. list := m.Mutable(fd).List()
  192. if err := d.unmarshalList(fd, list); err != nil {
  193. return err
  194. }
  195. case fd.IsMap():
  196. mmap := m.Mutable(fd).Map()
  197. if err := d.unmarshalMap(fd, mmap); err != nil {
  198. return err
  199. }
  200. default:
  201. kind := fd.Kind()
  202. if kind != protoreflect.MessageKind && kind != protoreflect.GroupKind && !tok.HasSeparator() {
  203. return d.syntaxError(tok.Pos(), "missing field separator :")
  204. }
  205. // If field is a oneof, check if it has already been set.
  206. if od := fd.ContainingOneof(); od != nil {
  207. idx := uint64(od.Index())
  208. if seenOneofs.Has(idx) {
  209. return d.newError(tok.Pos(), "error parsing %q, oneof %v is already set", tok.RawString(), od.FullName())
  210. }
  211. seenOneofs.Set(idx)
  212. }
  213. num := uint64(fd.Number())
  214. if seenNums.Has(num) {
  215. return d.newError(tok.Pos(), "non-repeated field %q is repeated", tok.RawString())
  216. }
  217. if err := d.unmarshalSingular(fd, m); err != nil {
  218. return err
  219. }
  220. seenNums.Set(num)
  221. }
  222. }
  223. return nil
  224. }
  225. // unmarshalSingular unmarshals a non-repeated field value specified by the
  226. // given FieldDescriptor.
  227. func (d decoder) unmarshalSingular(fd protoreflect.FieldDescriptor, m protoreflect.Message) error {
  228. var val protoreflect.Value
  229. var err error
  230. switch fd.Kind() {
  231. case protoreflect.MessageKind, protoreflect.GroupKind:
  232. val = m.NewField(fd)
  233. err = d.unmarshalMessage(val.Message(), true)
  234. default:
  235. val, err = d.unmarshalScalar(fd)
  236. }
  237. if err == nil {
  238. m.Set(fd, val)
  239. }
  240. return err
  241. }
  242. // unmarshalScalar unmarshals a scalar/enum protoreflect.Value specified by the
  243. // given FieldDescriptor.
  244. func (d decoder) unmarshalScalar(fd protoreflect.FieldDescriptor) (protoreflect.Value, error) {
  245. tok, err := d.Read()
  246. if err != nil {
  247. return protoreflect.Value{}, err
  248. }
  249. if tok.Kind() != text.Scalar {
  250. return protoreflect.Value{}, d.unexpectedTokenError(tok)
  251. }
  252. kind := fd.Kind()
  253. switch kind {
  254. case protoreflect.BoolKind:
  255. if b, ok := tok.Bool(); ok {
  256. return protoreflect.ValueOfBool(b), nil
  257. }
  258. case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
  259. if n, ok := tok.Int32(); ok {
  260. return protoreflect.ValueOfInt32(n), nil
  261. }
  262. case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
  263. if n, ok := tok.Int64(); ok {
  264. return protoreflect.ValueOfInt64(n), nil
  265. }
  266. case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
  267. if n, ok := tok.Uint32(); ok {
  268. return protoreflect.ValueOfUint32(n), nil
  269. }
  270. case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
  271. if n, ok := tok.Uint64(); ok {
  272. return protoreflect.ValueOfUint64(n), nil
  273. }
  274. case protoreflect.FloatKind:
  275. if n, ok := tok.Float32(); ok {
  276. return protoreflect.ValueOfFloat32(n), nil
  277. }
  278. case protoreflect.DoubleKind:
  279. if n, ok := tok.Float64(); ok {
  280. return protoreflect.ValueOfFloat64(n), nil
  281. }
  282. case protoreflect.StringKind:
  283. if s, ok := tok.String(); ok {
  284. if strs.EnforceUTF8(fd) && !utf8.ValidString(s) {
  285. return protoreflect.Value{}, d.newError(tok.Pos(), "contains invalid UTF-8")
  286. }
  287. return protoreflect.ValueOfString(s), nil
  288. }
  289. case protoreflect.BytesKind:
  290. if b, ok := tok.String(); ok {
  291. return protoreflect.ValueOfBytes([]byte(b)), nil
  292. }
  293. case protoreflect.EnumKind:
  294. if lit, ok := tok.Enum(); ok {
  295. // Lookup EnumNumber based on name.
  296. if enumVal := fd.Enum().Values().ByName(protoreflect.Name(lit)); enumVal != nil {
  297. return protoreflect.ValueOfEnum(enumVal.Number()), nil
  298. }
  299. }
  300. if num, ok := tok.Int32(); ok {
  301. return protoreflect.ValueOfEnum(protoreflect.EnumNumber(num)), nil
  302. }
  303. default:
  304. panic(fmt.Sprintf("invalid scalar kind %v", kind))
  305. }
  306. return protoreflect.Value{}, d.newError(tok.Pos(), "invalid value for %v type: %v", kind, tok.RawString())
  307. }
  308. // unmarshalList unmarshals into given protoreflect.List. A list value can
  309. // either be in [] syntax or simply just a single scalar/message value.
  310. func (d decoder) unmarshalList(fd protoreflect.FieldDescriptor, list protoreflect.List) error {
  311. tok, err := d.Peek()
  312. if err != nil {
  313. return err
  314. }
  315. switch fd.Kind() {
  316. case protoreflect.MessageKind, protoreflect.GroupKind:
  317. switch tok.Kind() {
  318. case text.ListOpen:
  319. d.Read()
  320. for {
  321. tok, err := d.Peek()
  322. if err != nil {
  323. return err
  324. }
  325. switch tok.Kind() {
  326. case text.ListClose:
  327. d.Read()
  328. return nil
  329. case text.MessageOpen:
  330. pval := list.NewElement()
  331. if err := d.unmarshalMessage(pval.Message(), true); err != nil {
  332. return err
  333. }
  334. list.Append(pval)
  335. default:
  336. return d.unexpectedTokenError(tok)
  337. }
  338. }
  339. case text.MessageOpen:
  340. pval := list.NewElement()
  341. if err := d.unmarshalMessage(pval.Message(), true); err != nil {
  342. return err
  343. }
  344. list.Append(pval)
  345. return nil
  346. }
  347. default:
  348. switch tok.Kind() {
  349. case text.ListOpen:
  350. d.Read()
  351. for {
  352. tok, err := d.Peek()
  353. if err != nil {
  354. return err
  355. }
  356. switch tok.Kind() {
  357. case text.ListClose:
  358. d.Read()
  359. return nil
  360. case text.Scalar:
  361. pval, err := d.unmarshalScalar(fd)
  362. if err != nil {
  363. return err
  364. }
  365. list.Append(pval)
  366. default:
  367. return d.unexpectedTokenError(tok)
  368. }
  369. }
  370. case text.Scalar:
  371. pval, err := d.unmarshalScalar(fd)
  372. if err != nil {
  373. return err
  374. }
  375. list.Append(pval)
  376. return nil
  377. }
  378. }
  379. return d.unexpectedTokenError(tok)
  380. }
  381. // unmarshalMap unmarshals into given protoreflect.Map. A map value is a
  382. // textproto message containing {key: <kvalue>, value: <mvalue>}.
  383. func (d decoder) unmarshalMap(fd protoreflect.FieldDescriptor, mmap protoreflect.Map) error {
  384. // Determine ahead whether map entry is a scalar type or a message type in
  385. // order to call the appropriate unmarshalMapValue func inside
  386. // unmarshalMapEntry.
  387. var unmarshalMapValue func() (protoreflect.Value, error)
  388. switch fd.MapValue().Kind() {
  389. case protoreflect.MessageKind, protoreflect.GroupKind:
  390. unmarshalMapValue = func() (protoreflect.Value, error) {
  391. pval := mmap.NewValue()
  392. if err := d.unmarshalMessage(pval.Message(), true); err != nil {
  393. return protoreflect.Value{}, err
  394. }
  395. return pval, nil
  396. }
  397. default:
  398. unmarshalMapValue = func() (protoreflect.Value, error) {
  399. return d.unmarshalScalar(fd.MapValue())
  400. }
  401. }
  402. tok, err := d.Read()
  403. if err != nil {
  404. return err
  405. }
  406. switch tok.Kind() {
  407. case text.MessageOpen:
  408. return d.unmarshalMapEntry(fd, mmap, unmarshalMapValue)
  409. case text.ListOpen:
  410. for {
  411. tok, err := d.Read()
  412. if err != nil {
  413. return err
  414. }
  415. switch tok.Kind() {
  416. case text.ListClose:
  417. return nil
  418. case text.MessageOpen:
  419. if err := d.unmarshalMapEntry(fd, mmap, unmarshalMapValue); err != nil {
  420. return err
  421. }
  422. default:
  423. return d.unexpectedTokenError(tok)
  424. }
  425. }
  426. default:
  427. return d.unexpectedTokenError(tok)
  428. }
  429. }
  430. // unmarshalMap unmarshals into given protoreflect.Map. A map value is a
  431. // textproto message containing {key: <kvalue>, value: <mvalue>}.
  432. func (d decoder) unmarshalMapEntry(fd protoreflect.FieldDescriptor, mmap protoreflect.Map, unmarshalMapValue func() (protoreflect.Value, error)) error {
  433. var key protoreflect.MapKey
  434. var pval protoreflect.Value
  435. Loop:
  436. for {
  437. // Read field name.
  438. tok, err := d.Read()
  439. if err != nil {
  440. return err
  441. }
  442. switch tok.Kind() {
  443. case text.Name:
  444. if tok.NameKind() != text.IdentName {
  445. if !d.opts.DiscardUnknown {
  446. return d.newError(tok.Pos(), "unknown map entry field %q", tok.RawString())
  447. }
  448. d.skipValue()
  449. continue Loop
  450. }
  451. // Continue below.
  452. case text.MessageClose:
  453. break Loop
  454. default:
  455. return d.unexpectedTokenError(tok)
  456. }
  457. switch name := protoreflect.Name(tok.IdentName()); name {
  458. case genid.MapEntry_Key_field_name:
  459. if !tok.HasSeparator() {
  460. return d.syntaxError(tok.Pos(), "missing field separator :")
  461. }
  462. if key.IsValid() {
  463. return d.newError(tok.Pos(), "map entry %q cannot be repeated", name)
  464. }
  465. val, err := d.unmarshalScalar(fd.MapKey())
  466. if err != nil {
  467. return err
  468. }
  469. key = val.MapKey()
  470. case genid.MapEntry_Value_field_name:
  471. if kind := fd.MapValue().Kind(); (kind != protoreflect.MessageKind) && (kind != protoreflect.GroupKind) {
  472. if !tok.HasSeparator() {
  473. return d.syntaxError(tok.Pos(), "missing field separator :")
  474. }
  475. }
  476. if pval.IsValid() {
  477. return d.newError(tok.Pos(), "map entry %q cannot be repeated", name)
  478. }
  479. pval, err = unmarshalMapValue()
  480. if err != nil {
  481. return err
  482. }
  483. default:
  484. if !d.opts.DiscardUnknown {
  485. return d.newError(tok.Pos(), "unknown map entry field %q", name)
  486. }
  487. d.skipValue()
  488. }
  489. }
  490. if !key.IsValid() {
  491. key = fd.MapKey().Default().MapKey()
  492. }
  493. if !pval.IsValid() {
  494. switch fd.MapValue().Kind() {
  495. case protoreflect.MessageKind, protoreflect.GroupKind:
  496. // If value field is not set for message/group types, construct an
  497. // empty one as default.
  498. pval = mmap.NewValue()
  499. default:
  500. pval = fd.MapValue().Default()
  501. }
  502. }
  503. mmap.Set(key, pval)
  504. return nil
  505. }
  506. // unmarshalAny unmarshals an Any textproto. It can either be in expanded form
  507. // or non-expanded form.
  508. func (d decoder) unmarshalAny(m protoreflect.Message, checkDelims bool) error {
  509. var typeURL string
  510. var bValue []byte
  511. var seenTypeUrl bool
  512. var seenValue bool
  513. var isExpanded bool
  514. if checkDelims {
  515. tok, err := d.Read()
  516. if err != nil {
  517. return err
  518. }
  519. if tok.Kind() != text.MessageOpen {
  520. return d.unexpectedTokenError(tok)
  521. }
  522. }
  523. Loop:
  524. for {
  525. // Read field name. Can only have 3 possible field names, i.e. type_url,
  526. // value and type URL name inside [].
  527. tok, err := d.Read()
  528. if err != nil {
  529. return err
  530. }
  531. if typ := tok.Kind(); typ != text.Name {
  532. if checkDelims {
  533. if typ == text.MessageClose {
  534. break Loop
  535. }
  536. } else if typ == text.EOF {
  537. break Loop
  538. }
  539. return d.unexpectedTokenError(tok)
  540. }
  541. switch tok.NameKind() {
  542. case text.IdentName:
  543. // Both type_url and value fields require field separator :.
  544. if !tok.HasSeparator() {
  545. return d.syntaxError(tok.Pos(), "missing field separator :")
  546. }
  547. switch name := protoreflect.Name(tok.IdentName()); name {
  548. case genid.Any_TypeUrl_field_name:
  549. if seenTypeUrl {
  550. return d.newError(tok.Pos(), "duplicate %v field", genid.Any_TypeUrl_field_fullname)
  551. }
  552. if isExpanded {
  553. return d.newError(tok.Pos(), "conflict with [%s] field", typeURL)
  554. }
  555. tok, err := d.Read()
  556. if err != nil {
  557. return err
  558. }
  559. var ok bool
  560. typeURL, ok = tok.String()
  561. if !ok {
  562. return d.newError(tok.Pos(), "invalid %v field value: %v", genid.Any_TypeUrl_field_fullname, tok.RawString())
  563. }
  564. seenTypeUrl = true
  565. case genid.Any_Value_field_name:
  566. if seenValue {
  567. return d.newError(tok.Pos(), "duplicate %v field", genid.Any_Value_field_fullname)
  568. }
  569. if isExpanded {
  570. return d.newError(tok.Pos(), "conflict with [%s] field", typeURL)
  571. }
  572. tok, err := d.Read()
  573. if err != nil {
  574. return err
  575. }
  576. s, ok := tok.String()
  577. if !ok {
  578. return d.newError(tok.Pos(), "invalid %v field value: %v", genid.Any_Value_field_fullname, tok.RawString())
  579. }
  580. bValue = []byte(s)
  581. seenValue = true
  582. default:
  583. if !d.opts.DiscardUnknown {
  584. return d.newError(tok.Pos(), "invalid field name %q in %v message", tok.RawString(), genid.Any_message_fullname)
  585. }
  586. }
  587. case text.TypeName:
  588. if isExpanded {
  589. return d.newError(tok.Pos(), "cannot have more than one type")
  590. }
  591. if seenTypeUrl {
  592. return d.newError(tok.Pos(), "conflict with type_url field")
  593. }
  594. typeURL = tok.TypeName()
  595. var err error
  596. bValue, err = d.unmarshalExpandedAny(typeURL, tok.Pos())
  597. if err != nil {
  598. return err
  599. }
  600. isExpanded = true
  601. default:
  602. if !d.opts.DiscardUnknown {
  603. return d.newError(tok.Pos(), "invalid field name %q in %v message", tok.RawString(), genid.Any_message_fullname)
  604. }
  605. }
  606. }
  607. fds := m.Descriptor().Fields()
  608. if len(typeURL) > 0 {
  609. m.Set(fds.ByNumber(genid.Any_TypeUrl_field_number), protoreflect.ValueOfString(typeURL))
  610. }
  611. if len(bValue) > 0 {
  612. m.Set(fds.ByNumber(genid.Any_Value_field_number), protoreflect.ValueOfBytes(bValue))
  613. }
  614. return nil
  615. }
  616. func (d decoder) unmarshalExpandedAny(typeURL string, pos int) ([]byte, error) {
  617. mt, err := d.opts.Resolver.FindMessageByURL(typeURL)
  618. if err != nil {
  619. return nil, d.newError(pos, "unable to resolve message [%v]: %v", typeURL, err)
  620. }
  621. // Create new message for the embedded message type and unmarshal the value
  622. // field into it.
  623. m := mt.New()
  624. if err := d.unmarshalMessage(m, true); err != nil {
  625. return nil, err
  626. }
  627. // Serialize the embedded message and return the resulting bytes.
  628. b, err := proto.MarshalOptions{
  629. AllowPartial: true, // Never check required fields inside an Any.
  630. Deterministic: true,
  631. }.Marshal(m.Interface())
  632. if err != nil {
  633. return nil, d.newError(pos, "error in marshaling message into Any.value: %v", err)
  634. }
  635. return b, nil
  636. }
  637. // skipValue makes the decoder parse a field value in order to advance the read
  638. // to the next field. It relies on Read returning an error if the types are not
  639. // in valid sequence.
  640. func (d decoder) skipValue() error {
  641. tok, err := d.Read()
  642. if err != nil {
  643. return err
  644. }
  645. // Only need to continue reading for messages and lists.
  646. switch tok.Kind() {
  647. case text.MessageOpen:
  648. return d.skipMessageValue()
  649. case text.ListOpen:
  650. for {
  651. tok, err := d.Read()
  652. if err != nil {
  653. return err
  654. }
  655. switch tok.Kind() {
  656. case text.ListClose:
  657. return nil
  658. case text.MessageOpen:
  659. return d.skipMessageValue()
  660. default:
  661. // Skip items. This will not validate whether skipped values are
  662. // of the same type or not, same behavior as C++
  663. // TextFormat::Parser::AllowUnknownField(true) version 3.8.0.
  664. }
  665. }
  666. }
  667. return nil
  668. }
  669. // skipMessageValue makes the decoder parse and skip over all fields in a
  670. // message. It assumes that the previous read type is MessageOpen.
  671. func (d decoder) skipMessageValue() error {
  672. for {
  673. tok, err := d.Read()
  674. if err != nil {
  675. return err
  676. }
  677. switch tok.Kind() {
  678. case text.MessageClose:
  679. return nil
  680. case text.Name:
  681. if err := d.skipValue(); err != nil {
  682. return err
  683. }
  684. }
  685. }
  686. }