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