decode.go 14 KB

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