decode.go 14 KB

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