decode.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. "strings"
  8. "unicode/utf8"
  9. "google.golang.org/protobuf/internal/encoding/messageset"
  10. "google.golang.org/protobuf/internal/encoding/text"
  11. "google.golang.org/protobuf/internal/errors"
  12. "google.golang.org/protobuf/internal/fieldnum"
  13. "google.golang.org/protobuf/internal/flags"
  14. "google.golang.org/protobuf/internal/pragma"
  15. "google.golang.org/protobuf/internal/set"
  16. "google.golang.org/protobuf/proto"
  17. pref "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. func Unmarshal(b []byte, m proto.Message) error {
  22. return UnmarshalOptions{}.Unmarshal(b, m)
  23. }
  24. // UnmarshalOptions is a configurable textproto format unmarshaler.
  25. type UnmarshalOptions struct {
  26. pragma.NoUnkeyedLiterals
  27. // AllowPartial accepts input for messages that will result in missing
  28. // required fields. If AllowPartial is false (the default), Unmarshal will
  29. // return error if there are any missing required fields.
  30. AllowPartial bool
  31. // DiscardUnknown specifies whether to ignore unknown fields when parsing.
  32. // An unknown field is any field whose field name or field number does not
  33. // resolve to any known or extension field in the message.
  34. // By default, unmarshal rejects unknown fields as an error.
  35. DiscardUnknown bool
  36. // Resolver is used for looking up types when unmarshaling
  37. // google.protobuf.Any messages or extension fields.
  38. // If nil, this defaults to using protoregistry.GlobalTypes.
  39. Resolver interface {
  40. protoregistry.MessageTypeResolver
  41. protoregistry.ExtensionTypeResolver
  42. }
  43. }
  44. // Unmarshal reads the given []byte and populates the given proto.Message using options in
  45. // UnmarshalOptions object.
  46. func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
  47. proto.Reset(m)
  48. // Parse into text.Value of message type.
  49. val, err := text.Unmarshal(b)
  50. if err != nil {
  51. return err
  52. }
  53. if o.Resolver == nil {
  54. o.Resolver = protoregistry.GlobalTypes
  55. }
  56. err = o.unmarshalMessage(val.Message(), m.ProtoReflect())
  57. if err != nil {
  58. return err
  59. }
  60. if o.AllowPartial {
  61. return nil
  62. }
  63. return proto.IsInitialized(m)
  64. }
  65. // unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
  66. func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
  67. messageDesc := m.Descriptor()
  68. if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
  69. return errors.New("no support for proto1 MessageSets")
  70. }
  71. // Handle expanded Any message.
  72. if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
  73. return o.unmarshalAny(tmsg[0], m)
  74. }
  75. var seenNums set.Ints
  76. var seenOneofs set.Ints
  77. fieldDescs := messageDesc.Fields()
  78. for _, tfield := range tmsg {
  79. tkey := tfield[0]
  80. tval := tfield[1]
  81. // Resolve the field descriptor.
  82. var name pref.Name
  83. var fd pref.FieldDescriptor
  84. var xt pref.ExtensionType
  85. var xtErr error
  86. switch tkey.Type() {
  87. case text.Name:
  88. name, _ = tkey.Name()
  89. fd = fieldDescs.ByName(name)
  90. if fd == nil {
  91. // The proto name of a group field is in all lowercase,
  92. // while the textproto field name is the group message name.
  93. gd := fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
  94. if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == name {
  95. fd = gd
  96. }
  97. } else if fd.Kind() == pref.GroupKind && fd.Message().Name() != name {
  98. fd = nil // reset since field name is actually the message name
  99. }
  100. case text.String:
  101. // Handle extensions only. This code path is not for Any.
  102. if messageDesc.FullName() == "google.protobuf.Any" {
  103. break
  104. }
  105. xt, xtErr = o.findExtension(pref.FullName(tkey.String()))
  106. case text.Uint:
  107. v, _ := tkey.Uint(false)
  108. num := pref.FieldNumber(v)
  109. if !num.IsValid() {
  110. return errors.New("invalid field number: %d", num)
  111. }
  112. fd = fieldDescs.ByNumber(num)
  113. if fd == nil {
  114. xt, xtErr = o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
  115. }
  116. }
  117. if xt != nil {
  118. fd = xt.TypeDescriptor()
  119. if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() {
  120. return errors.New("message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName())
  121. }
  122. } else if xtErr != nil && xtErr != protoregistry.NotFound {
  123. return errors.New("unable to resolve: %v", xtErr)
  124. }
  125. if flags.ProtoLegacy {
  126. if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() {
  127. fd = nil // reset since the weak reference is not linked in
  128. }
  129. }
  130. // Handle unknown fields.
  131. if fd == nil {
  132. if o.DiscardUnknown || messageDesc.ReservedNames().Has(name) {
  133. continue
  134. }
  135. return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
  136. }
  137. // Handle fields identified by field number.
  138. if tkey.Type() == text.Uint {
  139. // TODO: Add an option to permit parsing field numbers.
  140. //
  141. // This requires careful thought as the MarshalOptions.EmitUnknown
  142. // option allows formatting unknown fields as the field number
  143. // and the best-effort textual representation of the field value.
  144. // In that case, it may not be possible to unmarshal the value from
  145. // a parser that does have information about the unknown field.
  146. return errors.New("cannot specify field by number: %v", tkey)
  147. }
  148. switch {
  149. case fd.IsList():
  150. // If input is not a list, turn it into a list.
  151. var items []text.Value
  152. if tval.Type() != text.List {
  153. items = []text.Value{tval}
  154. } else {
  155. items = tval.List()
  156. }
  157. list := m.Mutable(fd).List()
  158. if err := o.unmarshalList(items, fd, list); err != nil {
  159. return err
  160. }
  161. case fd.IsMap():
  162. // If input is not a list, turn it into a list.
  163. var items []text.Value
  164. if tval.Type() != text.List {
  165. items = []text.Value{tval}
  166. } else {
  167. items = tval.List()
  168. }
  169. mmap := m.Mutable(fd).Map()
  170. if err := o.unmarshalMap(items, fd, mmap); err != nil {
  171. return err
  172. }
  173. default:
  174. // If field is a oneof, check if it has already been set.
  175. if od := fd.ContainingOneof(); od != nil {
  176. idx := uint64(od.Index())
  177. if seenOneofs.Has(idx) {
  178. return errors.New("oneof %v is already set", od.FullName())
  179. }
  180. seenOneofs.Set(idx)
  181. }
  182. // Required or optional fields.
  183. num := uint64(fd.Number())
  184. if seenNums.Has(num) {
  185. return errors.New("non-repeated field %v is repeated", fd.FullName())
  186. }
  187. if err := o.unmarshalSingular(tval, fd, m); err != nil {
  188. return err
  189. }
  190. seenNums.Set(num)
  191. }
  192. }
  193. return nil
  194. }
  195. // findExtension returns protoreflect.ExtensionType from the Resolver if found.
  196. func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
  197. xt, err := o.Resolver.FindExtensionByName(xtName)
  198. if err == nil {
  199. return xt, nil
  200. }
  201. return messageset.FindMessageSetExtension(o.Resolver, xtName)
  202. }
  203. // unmarshalSingular unmarshals given text.Value into the non-repeated field.
  204. func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error {
  205. var val pref.Value
  206. switch fd.Kind() {
  207. case pref.MessageKind, pref.GroupKind:
  208. if input.Type() != text.Message {
  209. return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
  210. }
  211. val = m.NewField(fd)
  212. if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
  213. return err
  214. }
  215. default:
  216. var err error
  217. val, err = unmarshalScalar(input, fd)
  218. if err != nil {
  219. return err
  220. }
  221. }
  222. m.Set(fd, val)
  223. return nil
  224. }
  225. // unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
  226. // the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
  227. func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
  228. const b32 = false
  229. const b64 = true
  230. switch kind := fd.Kind(); kind {
  231. case pref.BoolKind:
  232. if b, ok := input.Bool(); ok {
  233. return pref.ValueOfBool(bool(b)), nil
  234. }
  235. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  236. if n, ok := input.Int(b32); ok {
  237. return pref.ValueOfInt32(int32(n)), nil
  238. }
  239. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  240. if n, ok := input.Int(b64); ok {
  241. return pref.ValueOfInt64(int64(n)), nil
  242. }
  243. case pref.Uint32Kind, pref.Fixed32Kind:
  244. if n, ok := input.Uint(b32); ok {
  245. return pref.ValueOfUint32(uint32(n)), nil
  246. }
  247. case pref.Uint64Kind, pref.Fixed64Kind:
  248. if n, ok := input.Uint(b64); ok {
  249. return pref.ValueOfUint64(uint64(n)), nil
  250. }
  251. case pref.FloatKind:
  252. if n, ok := input.Float(b32); ok {
  253. return pref.ValueOfFloat32(float32(n)), nil
  254. }
  255. case pref.DoubleKind:
  256. if n, ok := input.Float(b64); ok {
  257. return pref.ValueOfFloat64(float64(n)), nil
  258. }
  259. case pref.StringKind:
  260. if input.Type() == text.String {
  261. s := input.String()
  262. if utf8.ValidString(s) {
  263. return pref.ValueOfString(s), nil
  264. }
  265. return pref.Value{}, errors.InvalidUTF8(string(fd.FullName()))
  266. }
  267. case pref.BytesKind:
  268. if input.Type() == text.String {
  269. return pref.ValueOfBytes([]byte(input.String())), nil
  270. }
  271. case pref.EnumKind:
  272. // If input is int32, use directly.
  273. if n, ok := input.Int(b32); ok {
  274. return pref.ValueOfEnum(pref.EnumNumber(n)), nil
  275. }
  276. if name, ok := input.Name(); ok {
  277. // Lookup EnumNumber based on name.
  278. if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
  279. return pref.ValueOfEnum(enumVal.Number()), nil
  280. }
  281. }
  282. default:
  283. panic(fmt.Sprintf("invalid scalar kind %v", kind))
  284. }
  285. return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
  286. }
  287. // unmarshalList unmarshals given []text.Value into given protoreflect.List.
  288. func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
  289. switch fd.Kind() {
  290. case pref.MessageKind, pref.GroupKind:
  291. for _, input := range inputList {
  292. if input.Type() != text.Message {
  293. return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
  294. }
  295. val := list.NewElement()
  296. if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
  297. return err
  298. }
  299. list.Append(val)
  300. }
  301. default:
  302. for _, input := range inputList {
  303. val, err := unmarshalScalar(input, fd)
  304. if err != nil {
  305. return err
  306. }
  307. list.Append(val)
  308. }
  309. }
  310. return nil
  311. }
  312. // unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
  313. func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
  314. // Determine ahead whether map entry is a scalar type or a message type in order to call the
  315. // appropriate unmarshalMapValue func inside the for loop below.
  316. unmarshalMapValue := unmarshalMapScalarValue
  317. switch fd.MapValue().Kind() {
  318. case pref.MessageKind, pref.GroupKind:
  319. unmarshalMapValue = o.unmarshalMapMessageValue
  320. }
  321. for _, entry := range input {
  322. if entry.Type() != text.Message {
  323. return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
  324. }
  325. tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
  326. if err != nil {
  327. return err
  328. }
  329. pkey, err := unmarshalMapKey(tkey, fd.MapKey())
  330. if err != nil {
  331. return err
  332. }
  333. err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
  334. if err != nil {
  335. return err
  336. }
  337. }
  338. return nil
  339. }
  340. // parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
  341. // field values. If there are duplicate field names, the value for the last field is returned. If
  342. // the field name does not exist, it will return the zero value of text.Value. It will return an
  343. // error if there are unknown field names.
  344. func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
  345. for _, field := range mapEntry {
  346. keyStr, ok := field[0].Name()
  347. if ok {
  348. switch keyStr {
  349. case "key":
  350. if key.Type() != 0 {
  351. return key, value, errors.New("%v contains duplicate key field", name)
  352. }
  353. key = field[1]
  354. case "value":
  355. if value.Type() != 0 {
  356. return key, value, errors.New("%v contains duplicate value field", name)
  357. }
  358. value = field[1]
  359. default:
  360. ok = false
  361. }
  362. }
  363. if !ok {
  364. // TODO: Do not return error if ignore unknown option is added and enabled.
  365. return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
  366. }
  367. }
  368. return key, value, nil
  369. }
  370. // unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
  371. // integral or string type.
  372. func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
  373. // If input is not set, use the zero value.
  374. if input.Type() == 0 {
  375. return fd.Default().MapKey(), nil
  376. }
  377. val, err := unmarshalScalar(input, fd)
  378. if err != nil {
  379. return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
  380. }
  381. return val.MapKey(), nil
  382. }
  383. // unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
  384. // the given MapKey.
  385. func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
  386. var value [][2]text.Value
  387. if input.Type() != 0 {
  388. value = input.Message()
  389. }
  390. val := mmap.NewValue()
  391. if err := o.unmarshalMessage(value, val.Message()); err != nil {
  392. return err
  393. }
  394. mmap.Set(pkey, val)
  395. return nil
  396. }
  397. // unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
  398. // for the given MapKey.
  399. func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
  400. var val pref.Value
  401. if input.Type() == 0 {
  402. val = fd.Default()
  403. } else {
  404. var err error
  405. val, err = unmarshalScalar(input, fd)
  406. if err != nil {
  407. return err
  408. }
  409. }
  410. mmap.Set(pkey, val)
  411. return nil
  412. }
  413. // isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
  414. // field with key type of text.String type and value type of text.Message.
  415. func isExpandedAny(tmsg [][2]text.Value) bool {
  416. if len(tmsg) != 1 {
  417. return false
  418. }
  419. field := tmsg[0]
  420. return field[0].Type() == text.String && field[1].Type() == text.Message
  421. }
  422. // unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
  423. // tfield has key type of text.String and value type of text.Message.
  424. func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error {
  425. typeURL := tfield[0].String()
  426. value := tfield[1].Message()
  427. mt, err := o.Resolver.FindMessageByURL(typeURL)
  428. if err != nil {
  429. return errors.New("unable to resolve message [%v]: %v", typeURL, err)
  430. }
  431. // Create new message for the embedded message type and unmarshal the
  432. // value into it.
  433. m2 := mt.New()
  434. if err := o.unmarshalMessage(value, m2); err != nil {
  435. return err
  436. }
  437. // Serialize the embedded message and assign the resulting bytes to the value field.
  438. b, err := proto.MarshalOptions{
  439. AllowPartial: true, // never check required fields inside an Any
  440. Deterministic: true,
  441. }.Marshal(m2.Interface())
  442. if err != nil {
  443. return err
  444. }
  445. fds := m.Descriptor().Fields()
  446. fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
  447. fdValue := fds.ByNumber(fieldnum.Any_Value)
  448. m.Set(fdType, pref.ValueOfString(typeURL))
  449. m.Set(fdValue, pref.ValueOfBytes(b))
  450. return nil
  451. }