decode.go 15 KB

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