decode.go 18 KB

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