decode.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // Copyright 2019 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 protojson
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "google.golang.org/protobuf/encoding/protowire"
  12. "google.golang.org/protobuf/internal/encoding/json"
  13. "google.golang.org/protobuf/internal/encoding/messageset"
  14. "google.golang.org/protobuf/internal/errors"
  15. "google.golang.org/protobuf/internal/flags"
  16. "google.golang.org/protobuf/internal/genid"
  17. "google.golang.org/protobuf/internal/pragma"
  18. "google.golang.org/protobuf/internal/set"
  19. "google.golang.org/protobuf/proto"
  20. "google.golang.org/protobuf/reflect/protoreflect"
  21. "google.golang.org/protobuf/reflect/protoregistry"
  22. )
  23. // Unmarshal reads the given []byte into the given [proto.Message].
  24. // The provided message must be mutable (e.g., a non-nil pointer to a message).
  25. func Unmarshal(b []byte, m proto.Message) error {
  26. return UnmarshalOptions{}.Unmarshal(b, m)
  27. }
  28. // UnmarshalOptions is a configurable JSON format parser.
  29. type UnmarshalOptions struct {
  30. pragma.NoUnkeyedLiterals
  31. // If AllowPartial is set, input for messages that will result in missing
  32. // required fields will not return an error.
  33. AllowPartial bool
  34. // If DiscardUnknown is set, unknown fields and enum name values are ignored.
  35. DiscardUnknown bool
  36. // Resolver is used for looking up types when unmarshaling
  37. // google.protobuf.Any messages or extension fields.
  38. // If nil, this defaults to using protoregistry.GlobalTypes.
  39. Resolver interface {
  40. protoregistry.MessageTypeResolver
  41. protoregistry.ExtensionTypeResolver
  42. }
  43. // RecursionLimit limits how deeply messages may be nested.
  44. // If zero, a default limit is applied.
  45. RecursionLimit int
  46. }
  47. // Unmarshal reads the given []byte and populates the given [proto.Message]
  48. // using options in the UnmarshalOptions object.
  49. // It will clear the message first before setting the fields.
  50. // If it returns an error, the given message may be partially set.
  51. // The provided message must be mutable (e.g., a non-nil pointer to a message).
  52. func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
  53. return o.unmarshal(b, m)
  54. }
  55. // unmarshal is a centralized function that all unmarshal operations go through.
  56. // For profiling purposes, avoid changing the name of this function or
  57. // introducing other code paths for unmarshal that do not go through this.
  58. func (o UnmarshalOptions) unmarshal(b []byte, m proto.Message) error {
  59. proto.Reset(m)
  60. if o.Resolver == nil {
  61. o.Resolver = protoregistry.GlobalTypes
  62. }
  63. if o.RecursionLimit == 0 {
  64. o.RecursionLimit = protowire.DefaultRecursionLimit
  65. }
  66. dec := decoder{json.NewDecoder(b), o}
  67. if err := dec.unmarshalMessage(m.ProtoReflect(), false); err != nil {
  68. return err
  69. }
  70. // Check for EOF.
  71. tok, err := dec.Read()
  72. if err != nil {
  73. return err
  74. }
  75. if tok.Kind() != json.EOF {
  76. return dec.unexpectedTokenError(tok)
  77. }
  78. if o.AllowPartial {
  79. return nil
  80. }
  81. return proto.CheckInitialized(m)
  82. }
  83. type decoder struct {
  84. *json.Decoder
  85. opts UnmarshalOptions
  86. }
  87. // newError returns an error object with position info.
  88. func (d decoder) newError(pos int, f string, x ...interface{}) error {
  89. line, column := d.Position(pos)
  90. head := fmt.Sprintf("(line %d:%d): ", line, column)
  91. return errors.New(head+f, x...)
  92. }
  93. // unexpectedTokenError returns a syntax error for the given unexpected token.
  94. func (d decoder) unexpectedTokenError(tok json.Token) error {
  95. return d.syntaxError(tok.Pos(), "unexpected token %s", tok.RawString())
  96. }
  97. // syntaxError returns a syntax error for given position.
  98. func (d decoder) syntaxError(pos int, f string, x ...interface{}) error {
  99. line, column := d.Position(pos)
  100. head := fmt.Sprintf("syntax error (line %d:%d): ", line, column)
  101. return errors.New(head+f, x...)
  102. }
  103. // unmarshalMessage unmarshals a message into the given protoreflect.Message.
  104. func (d decoder) unmarshalMessage(m protoreflect.Message, skipTypeURL bool) error {
  105. d.opts.RecursionLimit--
  106. if d.opts.RecursionLimit < 0 {
  107. return errors.New("exceeded max recursion depth")
  108. }
  109. if unmarshal := wellKnownTypeUnmarshaler(m.Descriptor().FullName()); unmarshal != nil {
  110. return unmarshal(d, m)
  111. }
  112. tok, err := d.Read()
  113. if err != nil {
  114. return err
  115. }
  116. if tok.Kind() != json.ObjectOpen {
  117. return d.unexpectedTokenError(tok)
  118. }
  119. messageDesc := m.Descriptor()
  120. if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
  121. return errors.New("no support for proto1 MessageSets")
  122. }
  123. var seenNums set.Ints
  124. var seenOneofs set.Ints
  125. fieldDescs := messageDesc.Fields()
  126. for {
  127. // Read field name.
  128. tok, err := d.Read()
  129. if err != nil {
  130. return err
  131. }
  132. switch tok.Kind() {
  133. default:
  134. return d.unexpectedTokenError(tok)
  135. case json.ObjectClose:
  136. return nil
  137. case json.Name:
  138. // Continue below.
  139. }
  140. name := tok.Name()
  141. // Unmarshaling a non-custom embedded message in Any will contain the
  142. // JSON field "@type" which should be skipped because it is not a field
  143. // of the embedded message, but simply an artifact of the Any format.
  144. if skipTypeURL && name == "@type" {
  145. d.Read()
  146. continue
  147. }
  148. // Get the FieldDescriptor.
  149. var fd protoreflect.FieldDescriptor
  150. if strings.HasPrefix(name, "[") && strings.HasSuffix(name, "]") {
  151. // Only extension names are in [name] format.
  152. extName := protoreflect.FullName(name[1 : len(name)-1])
  153. extType, err := d.opts.Resolver.FindExtensionByName(extName)
  154. if err != nil && err != protoregistry.NotFound {
  155. return d.newError(tok.Pos(), "unable to resolve %s: %v", tok.RawString(), err)
  156. }
  157. if extType != nil {
  158. fd = extType.TypeDescriptor()
  159. if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() {
  160. return d.newError(tok.Pos(), "message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName())
  161. }
  162. }
  163. } else {
  164. // The name can either be the JSON name or the proto field name.
  165. fd = fieldDescs.ByJSONName(name)
  166. if fd == nil {
  167. fd = fieldDescs.ByTextName(name)
  168. }
  169. }
  170. if flags.ProtoLegacy {
  171. if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() {
  172. fd = nil // reset since the weak reference is not linked in
  173. }
  174. }
  175. if fd == nil {
  176. // Field is unknown.
  177. if d.opts.DiscardUnknown {
  178. if err := d.skipJSONValue(); err != nil {
  179. return err
  180. }
  181. continue
  182. }
  183. return d.newError(tok.Pos(), "unknown field %v", tok.RawString())
  184. }
  185. // Do not allow duplicate fields.
  186. num := uint64(fd.Number())
  187. if seenNums.Has(num) {
  188. return d.newError(tok.Pos(), "duplicate field %v", tok.RawString())
  189. }
  190. seenNums.Set(num)
  191. // No need to set values for JSON null unless the field type is
  192. // google.protobuf.Value or google.protobuf.NullValue.
  193. if tok, _ := d.Peek(); tok.Kind() == json.Null && !isKnownValue(fd) && !isNullValue(fd) {
  194. d.Read()
  195. continue
  196. }
  197. switch {
  198. case fd.IsList():
  199. list := m.Mutable(fd).List()
  200. if err := d.unmarshalList(list, fd); err != nil {
  201. return err
  202. }
  203. case fd.IsMap():
  204. mmap := m.Mutable(fd).Map()
  205. if err := d.unmarshalMap(mmap, fd); err != nil {
  206. return err
  207. }
  208. default:
  209. // If field is a oneof, check if it has already been set.
  210. if od := fd.ContainingOneof(); od != nil {
  211. idx := uint64(od.Index())
  212. if seenOneofs.Has(idx) {
  213. return d.newError(tok.Pos(), "error parsing %s, oneof %v is already set", tok.RawString(), od.FullName())
  214. }
  215. seenOneofs.Set(idx)
  216. }
  217. // Required or optional fields.
  218. if err := d.unmarshalSingular(m, fd); err != nil {
  219. return err
  220. }
  221. }
  222. }
  223. }
  224. func isKnownValue(fd protoreflect.FieldDescriptor) bool {
  225. md := fd.Message()
  226. return md != nil && md.FullName() == genid.Value_message_fullname
  227. }
  228. func isNullValue(fd protoreflect.FieldDescriptor) bool {
  229. ed := fd.Enum()
  230. return ed != nil && ed.FullName() == genid.NullValue_enum_fullname
  231. }
  232. // unmarshalSingular unmarshals to the non-repeated field specified
  233. // by the given FieldDescriptor.
  234. func (d decoder) unmarshalSingular(m protoreflect.Message, fd protoreflect.FieldDescriptor) error {
  235. var val protoreflect.Value
  236. var err error
  237. switch fd.Kind() {
  238. case protoreflect.MessageKind, protoreflect.GroupKind:
  239. val = m.NewField(fd)
  240. err = d.unmarshalMessage(val.Message(), false)
  241. default:
  242. val, err = d.unmarshalScalar(fd)
  243. }
  244. if err != nil {
  245. return err
  246. }
  247. if val.IsValid() {
  248. m.Set(fd, val)
  249. }
  250. return nil
  251. }
  252. // unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by
  253. // the given FieldDescriptor.
  254. func (d decoder) unmarshalScalar(fd protoreflect.FieldDescriptor) (protoreflect.Value, error) {
  255. const b32 int = 32
  256. const b64 int = 64
  257. tok, err := d.Read()
  258. if err != nil {
  259. return protoreflect.Value{}, err
  260. }
  261. kind := fd.Kind()
  262. switch kind {
  263. case protoreflect.BoolKind:
  264. if tok.Kind() == json.Bool {
  265. return protoreflect.ValueOfBool(tok.Bool()), nil
  266. }
  267. case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
  268. if v, ok := unmarshalInt(tok, b32); ok {
  269. return v, nil
  270. }
  271. case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
  272. if v, ok := unmarshalInt(tok, b64); ok {
  273. return v, nil
  274. }
  275. case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
  276. if v, ok := unmarshalUint(tok, b32); ok {
  277. return v, nil
  278. }
  279. case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
  280. if v, ok := unmarshalUint(tok, b64); ok {
  281. return v, nil
  282. }
  283. case protoreflect.FloatKind:
  284. if v, ok := unmarshalFloat(tok, b32); ok {
  285. return v, nil
  286. }
  287. case protoreflect.DoubleKind:
  288. if v, ok := unmarshalFloat(tok, b64); ok {
  289. return v, nil
  290. }
  291. case protoreflect.StringKind:
  292. if tok.Kind() == json.String {
  293. return protoreflect.ValueOfString(tok.ParsedString()), nil
  294. }
  295. case protoreflect.BytesKind:
  296. if v, ok := unmarshalBytes(tok); ok {
  297. return v, nil
  298. }
  299. case protoreflect.EnumKind:
  300. if v, ok := unmarshalEnum(tok, fd, d.opts.DiscardUnknown); ok {
  301. return v, nil
  302. }
  303. default:
  304. panic(fmt.Sprintf("unmarshalScalar: invalid scalar kind %v", kind))
  305. }
  306. return protoreflect.Value{}, d.newError(tok.Pos(), "invalid value for %v type: %v", kind, tok.RawString())
  307. }
  308. func unmarshalInt(tok json.Token, bitSize int) (protoreflect.Value, bool) {
  309. switch tok.Kind() {
  310. case json.Number:
  311. return getInt(tok, bitSize)
  312. case json.String:
  313. // Decode number from string.
  314. s := strings.TrimSpace(tok.ParsedString())
  315. if len(s) != len(tok.ParsedString()) {
  316. return protoreflect.Value{}, false
  317. }
  318. dec := json.NewDecoder([]byte(s))
  319. tok, err := dec.Read()
  320. if err != nil {
  321. return protoreflect.Value{}, false
  322. }
  323. return getInt(tok, bitSize)
  324. }
  325. return protoreflect.Value{}, false
  326. }
  327. func getInt(tok json.Token, bitSize int) (protoreflect.Value, bool) {
  328. n, ok := tok.Int(bitSize)
  329. if !ok {
  330. return protoreflect.Value{}, false
  331. }
  332. if bitSize == 32 {
  333. return protoreflect.ValueOfInt32(int32(n)), true
  334. }
  335. return protoreflect.ValueOfInt64(n), true
  336. }
  337. func unmarshalUint(tok json.Token, bitSize int) (protoreflect.Value, bool) {
  338. switch tok.Kind() {
  339. case json.Number:
  340. return getUint(tok, bitSize)
  341. case json.String:
  342. // Decode number from string.
  343. s := strings.TrimSpace(tok.ParsedString())
  344. if len(s) != len(tok.ParsedString()) {
  345. return protoreflect.Value{}, false
  346. }
  347. dec := json.NewDecoder([]byte(s))
  348. tok, err := dec.Read()
  349. if err != nil {
  350. return protoreflect.Value{}, false
  351. }
  352. return getUint(tok, bitSize)
  353. }
  354. return protoreflect.Value{}, false
  355. }
  356. func getUint(tok json.Token, bitSize int) (protoreflect.Value, bool) {
  357. n, ok := tok.Uint(bitSize)
  358. if !ok {
  359. return protoreflect.Value{}, false
  360. }
  361. if bitSize == 32 {
  362. return protoreflect.ValueOfUint32(uint32(n)), true
  363. }
  364. return protoreflect.ValueOfUint64(n), true
  365. }
  366. func unmarshalFloat(tok json.Token, bitSize int) (protoreflect.Value, bool) {
  367. switch tok.Kind() {
  368. case json.Number:
  369. return getFloat(tok, bitSize)
  370. case json.String:
  371. s := tok.ParsedString()
  372. switch s {
  373. case "NaN":
  374. if bitSize == 32 {
  375. return protoreflect.ValueOfFloat32(float32(math.NaN())), true
  376. }
  377. return protoreflect.ValueOfFloat64(math.NaN()), true
  378. case "Infinity":
  379. if bitSize == 32 {
  380. return protoreflect.ValueOfFloat32(float32(math.Inf(+1))), true
  381. }
  382. return protoreflect.ValueOfFloat64(math.Inf(+1)), true
  383. case "-Infinity":
  384. if bitSize == 32 {
  385. return protoreflect.ValueOfFloat32(float32(math.Inf(-1))), true
  386. }
  387. return protoreflect.ValueOfFloat64(math.Inf(-1)), true
  388. }
  389. // Decode number from string.
  390. if len(s) != len(strings.TrimSpace(s)) {
  391. return protoreflect.Value{}, false
  392. }
  393. dec := json.NewDecoder([]byte(s))
  394. tok, err := dec.Read()
  395. if err != nil {
  396. return protoreflect.Value{}, false
  397. }
  398. return getFloat(tok, bitSize)
  399. }
  400. return protoreflect.Value{}, false
  401. }
  402. func getFloat(tok json.Token, bitSize int) (protoreflect.Value, bool) {
  403. n, ok := tok.Float(bitSize)
  404. if !ok {
  405. return protoreflect.Value{}, false
  406. }
  407. if bitSize == 32 {
  408. return protoreflect.ValueOfFloat32(float32(n)), true
  409. }
  410. return protoreflect.ValueOfFloat64(n), true
  411. }
  412. func unmarshalBytes(tok json.Token) (protoreflect.Value, bool) {
  413. if tok.Kind() != json.String {
  414. return protoreflect.Value{}, false
  415. }
  416. s := tok.ParsedString()
  417. enc := base64.StdEncoding
  418. if strings.ContainsAny(s, "-_") {
  419. enc = base64.URLEncoding
  420. }
  421. if len(s)%4 != 0 {
  422. enc = enc.WithPadding(base64.NoPadding)
  423. }
  424. b, err := enc.DecodeString(s)
  425. if err != nil {
  426. return protoreflect.Value{}, false
  427. }
  428. return protoreflect.ValueOfBytes(b), true
  429. }
  430. func unmarshalEnum(tok json.Token, fd protoreflect.FieldDescriptor, discardUnknown bool) (protoreflect.Value, bool) {
  431. switch tok.Kind() {
  432. case json.String:
  433. // Lookup EnumNumber based on name.
  434. s := tok.ParsedString()
  435. if enumVal := fd.Enum().Values().ByName(protoreflect.Name(s)); enumVal != nil {
  436. return protoreflect.ValueOfEnum(enumVal.Number()), true
  437. }
  438. if discardUnknown {
  439. return protoreflect.Value{}, true
  440. }
  441. case json.Number:
  442. if n, ok := tok.Int(32); ok {
  443. return protoreflect.ValueOfEnum(protoreflect.EnumNumber(n)), true
  444. }
  445. case json.Null:
  446. // This is only valid for google.protobuf.NullValue.
  447. if isNullValue(fd) {
  448. return protoreflect.ValueOfEnum(0), true
  449. }
  450. }
  451. return protoreflect.Value{}, false
  452. }
  453. func (d decoder) unmarshalList(list protoreflect.List, fd protoreflect.FieldDescriptor) error {
  454. tok, err := d.Read()
  455. if err != nil {
  456. return err
  457. }
  458. if tok.Kind() != json.ArrayOpen {
  459. return d.unexpectedTokenError(tok)
  460. }
  461. switch fd.Kind() {
  462. case protoreflect.MessageKind, protoreflect.GroupKind:
  463. for {
  464. tok, err := d.Peek()
  465. if err != nil {
  466. return err
  467. }
  468. if tok.Kind() == json.ArrayClose {
  469. d.Read()
  470. return nil
  471. }
  472. val := list.NewElement()
  473. if err := d.unmarshalMessage(val.Message(), false); err != nil {
  474. return err
  475. }
  476. list.Append(val)
  477. }
  478. default:
  479. for {
  480. tok, err := d.Peek()
  481. if err != nil {
  482. return err
  483. }
  484. if tok.Kind() == json.ArrayClose {
  485. d.Read()
  486. return nil
  487. }
  488. val, err := d.unmarshalScalar(fd)
  489. if err != nil {
  490. return err
  491. }
  492. if val.IsValid() {
  493. list.Append(val)
  494. }
  495. }
  496. }
  497. return nil
  498. }
  499. func (d decoder) unmarshalMap(mmap protoreflect.Map, fd protoreflect.FieldDescriptor) error {
  500. tok, err := d.Read()
  501. if err != nil {
  502. return err
  503. }
  504. if tok.Kind() != json.ObjectOpen {
  505. return d.unexpectedTokenError(tok)
  506. }
  507. // Determine ahead whether map entry is a scalar type or a message type in
  508. // order to call the appropriate unmarshalMapValue func inside the for loop
  509. // below.
  510. var unmarshalMapValue func() (protoreflect.Value, error)
  511. switch fd.MapValue().Kind() {
  512. case protoreflect.MessageKind, protoreflect.GroupKind:
  513. unmarshalMapValue = func() (protoreflect.Value, error) {
  514. val := mmap.NewValue()
  515. if err := d.unmarshalMessage(val.Message(), false); err != nil {
  516. return protoreflect.Value{}, err
  517. }
  518. return val, nil
  519. }
  520. default:
  521. unmarshalMapValue = func() (protoreflect.Value, error) {
  522. return d.unmarshalScalar(fd.MapValue())
  523. }
  524. }
  525. Loop:
  526. for {
  527. // Read field name.
  528. tok, err := d.Read()
  529. if err != nil {
  530. return err
  531. }
  532. switch tok.Kind() {
  533. default:
  534. return d.unexpectedTokenError(tok)
  535. case json.ObjectClose:
  536. break Loop
  537. case json.Name:
  538. // Continue.
  539. }
  540. // Unmarshal field name.
  541. pkey, err := d.unmarshalMapKey(tok, fd.MapKey())
  542. if err != nil {
  543. return err
  544. }
  545. // Check for duplicate field name.
  546. if mmap.Has(pkey) {
  547. return d.newError(tok.Pos(), "duplicate map key %v", tok.RawString())
  548. }
  549. // Read and unmarshal field value.
  550. pval, err := unmarshalMapValue()
  551. if err != nil {
  552. return err
  553. }
  554. if pval.IsValid() {
  555. mmap.Set(pkey, pval)
  556. }
  557. }
  558. return nil
  559. }
  560. // unmarshalMapKey converts given token of Name kind into a protoreflect.MapKey.
  561. // A map key type is any integral or string type.
  562. func (d decoder) unmarshalMapKey(tok json.Token, fd protoreflect.FieldDescriptor) (protoreflect.MapKey, error) {
  563. const b32 = 32
  564. const b64 = 64
  565. const base10 = 10
  566. name := tok.Name()
  567. kind := fd.Kind()
  568. switch kind {
  569. case protoreflect.StringKind:
  570. return protoreflect.ValueOfString(name).MapKey(), nil
  571. case protoreflect.BoolKind:
  572. switch name {
  573. case "true":
  574. return protoreflect.ValueOfBool(true).MapKey(), nil
  575. case "false":
  576. return protoreflect.ValueOfBool(false).MapKey(), nil
  577. }
  578. case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
  579. if n, err := strconv.ParseInt(name, base10, b32); err == nil {
  580. return protoreflect.ValueOfInt32(int32(n)).MapKey(), nil
  581. }
  582. case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
  583. if n, err := strconv.ParseInt(name, base10, b64); err == nil {
  584. return protoreflect.ValueOfInt64(int64(n)).MapKey(), nil
  585. }
  586. case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
  587. if n, err := strconv.ParseUint(name, base10, b32); err == nil {
  588. return protoreflect.ValueOfUint32(uint32(n)).MapKey(), nil
  589. }
  590. case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
  591. if n, err := strconv.ParseUint(name, base10, b64); err == nil {
  592. return protoreflect.ValueOfUint64(uint64(n)).MapKey(), nil
  593. }
  594. default:
  595. panic(fmt.Sprintf("invalid kind for map key: %v", kind))
  596. }
  597. return protoreflect.MapKey{}, d.newError(tok.Pos(), "invalid value for %v key: %s", kind, tok.RawString())
  598. }