decode.go 13 KB

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