decode.go 15 KB

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